Table of Contents
Non-persistent XSS
Permanence XSS
Data Verification
Data Cleaning
Output escape
What is the impact of cross-site scripting (XSS) attacks on PHP applications?
How to identify potential XSS vulnerabilities in my PHP application?
What are some common methods used in XSS attacks?
How to prevent XSS attacks in my PHP application?
What role does content security policies play in preventing XSS attacks?
What is the difference between a storage XSS attack and a reflective XSS attack?
How to use PHP's built-in functions to prevent XSS attacks?
What role does HTTPOnly cookies play in preventing XSS attacks?
Can XSS attacks be used to bypass CSRF protection?
What PHP frameworks provide built-in protection against XSS attacks?
Home Backend Development PHP Tutorial Cross-Site Scripting Attacks (XSS)

Cross-Site Scripting Attacks (XSS)

Feb 27, 2025 am 09:12 AM

Cross-Site Scripting Attacks (XSS)

Key Points

  • Cross-site scripting attack (XSS) is a common code injection attack that occurs due to improper user data verification (usually inserting or manipulating hyperlinks via web forms). This may allow harmful client code to be saved on the server or executed in the user's browser.
  • XSS attacks can be divided into two types: non-persistent XSS (malicious code is passed through the server and presented to the victim) and persistent XSS (harmful code bypasses verification and is stored in the website's data store and is executed when information is displayed on the website).
  • Preventing XSS attacks requires never trusting data from users or third-party sources, verifying all data at input, and escaping it when output. This includes implementing data verification, data cleaning and output escaping measures.
  • While many PHP frameworks provide built-in security measures, it is critical to continuously test validation code with the latest XSS test vectors to ensure that the code remains unaffected by XSS attacks.

Cross-site scripting attacks are one of the five major security attacks that occur every day on the Internet, and your PHP scripts may not be spared. This attack, also known as XSS, is basically a code injection attack that is possible due to incorrect verification of user data, which is usually inserted into a page via a web form or using a changed hyperlink. The injected code can be any malicious client code, such as JavaScript, VBScript, HTML, CSS, Flash, etc. This code is used to save harmful data on the server or perform malicious actions in the user's browser. Unfortunately, cross-site scripting attacks occur mainly because developers fail to provide secure code. It is a responsibility for every PHP programmer to understand how to attack their PHP scripts to exploit possible security vulnerabilities. Read this article and you will learn more about cross-site scripting attacks and how to prevent them in your code.

Learn through examples

Let's look at the following code snippet.

<form action="post.php" method="post">
  <input type="text" name="comment" value="">
  <input type="submit" name="submit" value="Submit">
</form>
Copy after login
Copy after login
Copy after login

Here we have a simple form with a text box for data entry and a submit button. After submitting the form, it submits the data to post.php for processing. Assume post.php only outputs data, as shown below:

<?php echo $_POST["comment"]; ?>
Copy after login
Copy after login
Copy after login

Without any filtering, the hacker can submit the following through the form, which will generate a popup in the browser with the message "hacked".

alert("hacked")
Copy after login
Copy after login
Copy after login

This example, although malicious in nature, does not seem to cause much harm. But think about what happens if JavaScript code is written to steal the user's cookies and extract sensitive information from it? There are worse XSS attacks than simple alert() calls.

Cross-site scripting attacks can be divided into two categories based on how they deliver malicious payloads: non-persistent XSS and persistent XSS. Please allow me to discuss each type in detail.

Non-persistent XSS

Also known as a reflective XSS attack, it means that malicious code is not actually stored on the server, but is passed through and presented to the victim, which is the more popular XSS strategy among the two delivery methods. Attacks are initiated from external sources, such as emails or third-party websites. Here is a part of a simple search result script:

<form action="post.php" method="post">
  <input type="text" name="comment" value="">
  <input type="submit" name="submit" value="Submit">
</form>
Copy after login
Copy after login
Copy after login

This example may be a very unsafe outcome page where the search query will be displayed back to the user. The problem here is that the $_GET["query"] variable is not verified or escaped, so the attacker can send the following link to the victim:

<?php echo $_POST["comment"]; ?>
Copy after login
Copy after login
Copy after login

No verification is required, the page will contain:

alert("hacked")
Copy after login
Copy after login
Copy after login

Permanence XSS

This type of attack occurs when the malicious code has passed the verification process and is stored in the data store. This could be a comment, log file, notification message, or any other part of the website that requires user input. Later, when this specific information is displayed on the website, the malicious code executes. Let's use the following example to create a basic file-based comment system. Assuming the same form I introduced earlier, assuming the receiving script just appends the comments to the data file.

<?php // 根据查询获取搜索结果
echo "You searched for: " . $_GET["query"];

// 列出搜索结果
...
Copy after login
Copy after login

Other places, the content of comments.txt is displayed to the visitor:

http://example.com/search.php?query=alert(&quot;hacked&quot;)
Copy after login
Copy after login

When a user submits a comment, it is saved to the data file. The entire file (and therefore the entire comment series) will then be displayed to the reader. If malicious code is submitted, it will be saved and displayed as is without any validation or escape.

Prevent cross-site scripting attacks

Luckily, it's as easy as XSS attacks on unprotected sites, and it's just as easy to prevent them. However, prevention must always be kept in mind even before writing a line of code. The first rule that any web environment (whether development, staging or production) needs to be "enforced" is to never trust data from users or any other third-party sources. This cannot be emphasized too much. Each bit of data input must be verified and escaped at output. This is the golden rule to prevent XSS. To implement reliable security measures to prevent XSS attacks, we should pay attention to data verification, data cleaning, and output escaping.

Data Verification

Data verification is the process of ensuring that the application runs with the correct data. If your PHP script expects the user to enter an integer, any other type of data will be discarded. Each piece of user data received must be verified to ensure its type is correct and discarded if it fails the verification process. For example, if you want to verify a phone number, you will discard any string containing the letters, because the phone number should contain only numbers. You should also consider the length of the string. If you want to be a little looser, you can allow a limited set of special characters, such as plus signs, brackets, and dashes, which are often used to format phone numbers specific to your target locale.

<form action="post.php" method="post">
  <input type="text" name="comment" value="">
  <input type="submit" name="submit" value="Submit">
</form>
Copy after login
Copy after login
Copy after login

Data Cleaning

Data cleaning focuses on manipulating data to ensure it is safe by removing any unwanted bits from the data and normalizing it into the correct form. For example, if you expect plain text strings as user input, you may want to remove any HTML tags from it.

<?php echo $_POST["comment"]; ?>
Copy after login
Copy after login
Copy after login

Sometimes, data verification and cleaning/normalization can be performed simultaneously.

alert("hacked")
Copy after login
Copy after login
Copy after login

Output escape

To protect the integrity of the display/output data, you should escape the data when it is presented to the user. This prevents the browser from applying any unexpected meaning to any special character sequence that may be found.

<?php // 根据查询获取搜索结果
echo "You searched for: " . $_GET["query"];

// 列出搜索结果
...
Copy after login
Copy after login

Together now!

To better understand the three aspects of data processing, let's look at the previous file-based comment system again and modify it to ensure its security. The potential vulnerability in the code stems from the fact that $_POST["comment"] is blindly attached to the comments.txt file and then displayed directly to the user. To ensure it is safe, the $_POST["comment"] value should be verified and cleaned before adding it to the file, and it should be escaped when the file content is displayed to the user.

http://example.com/search.php?query=alert(&quot;hacked&quot;)
Copy after login
Copy after login

The script first verifies incoming comments to ensure that the user has provided a non-zero-length string. After all, blank comments are not very interesting. Data validation needs to be done in a well-defined context, which means that if I expect to get an integer from the user, then I validate it accordingly by converting the data into an integer and processing it as an integer. If this results in invalid data, just discard it and let the user know. The script then cleans up the comments by removing any HTML tags that may be included. Finally, retrieve, filter, and display comments. Typically, the htmlspecialchars() function is sufficient to filter the output intended to be viewed in the browser. However, if the character encoding you are using in a web page is not ISO-8859-1 or UTF-8, you may need to use htmlentities(). For more information about these two functions, read the respective descriptions in the official PHP documentation. Remember that there is no single solution that is 100% secure on an evolving medium like the Web. Thoroughly test your verification code with the latest XSS test vectors. Using the test data from the following sources should reveal whether your code is still vulnerable to XSS attacks.

  • RSnake XSS cheatsheet (a fairly comprehensive list of XSS vectors that you can use to test your code)
  • Zend Framework's XSS test data
  • XSS cheatsheet (using HTML5 features)

Summary

Hope this article explains you very well what cross-site scripting attacks are and how to prevent them from happening in your code. Never trust data from users or any other third-party sources. You can protect yourself by validating incoming values ​​in a well-defined context, cleaning up data to protect your code, and escaping output to protect your users. After writing the code, make sure you work properly by testing the code as thoroughly as possible.

(Picture from Inge Schepers / Shutterstock)

If you like this post, you will like Learnable; a place to learn new skills and skills from masters. Members can instantly access all SitePoint's e-books and interactive online courses, such as Jump Start PHP.

Comments in this article have been closed. Have questions about PHP? Why not ask questions on our forum?

FAQs (FAQ) about PHP security and cross-site scripting attacks (XSS)

What is the impact of cross-site scripting (XSS) attacks on PHP applications?

Cross-site scripting (XSS) attacks can have a significant impact on PHP applications. They can lead to data theft, session hijacking, website corruption, and even distribution of malicious code to users. XSS attacks exploit vulnerabilities in web applications to inject malicious scripts and then execute them by the user's browser. This can jeopardize user interaction with the application and may disclose sensitive information.

How to identify potential XSS vulnerabilities in my PHP application?

Identifying potential XSS vulnerabilities in PHP applications requires a combination of manual code review and automated testing. Find areas in the code where user input is directly included in the output without proper cleaning or verification. Automation tools like XSS Scanners can also help identify potential vulnerabilities by testing various XSS attack vectors.

What are some common methods used in XSS attacks?

XSS attacks usually involve injecting malicious scripts into web pages viewed by other users. This can be done in a variety of ways, such as embedding scripts into URL parameters, form inputs, and even cookies. The malicious script can then perform actions on behalf of the user, such as stealing their session cookies or manipulating web page content.

How to prevent XSS attacks in my PHP application?

Preventing XSS attacks in PHP applications involves validating and cleaning user input, encoded output, and using appropriate HTTP headers. Always treat user input as untrusted and validate it against the allowable list of acceptable values. Clean the input to remove any characters or code that may be harmful. The output is encoded to ensure that any characters that may be harmful become harmless. Use HTTP headers such as content security policies to limit the source of scripts and other resources.

What role does content security policies play in preventing XSS attacks?

Content Security Policy (CSP) HTTP headers play a crucial role in preventing XSS attacks. It allows you to specify a domain that the browser should consider to be a valid source of executable scripts. This means that even if an attacker can inject a script into your web page, the browser won't run it unless the source of the script is whitelisted in your CSP.

What is the difference between a storage XSS attack and a reflective XSS attack?

Storage XSS attacks involve injecting malicious scripts that are permanently stored on the target server. Then, when the user views certain pages, the script is provided to the user. Reflective XSS attacks, on the other hand, involve injecting a script through a URL or form input, which the server immediately returns in the response and executes by the user's browser.

How to use PHP's built-in functions to prevent XSS attacks?

PHP provides some built-in functions that can help prevent XSS attacks. For example, the htmlspecialchars() function can be used to encode special characters in user input, making potential scripts harmless. filter_input() Functions can be used to clean up user input, delete or encode harmful characters.

What role does HTTPOnly cookies play in preventing XSS attacks?

HTTPOnly Cookie is a cookie that cannot be accessed through client scripts. This means that even if an attacker can inject a script into your web page, they cannot use the script to read or modify HTTPOnly cookies. This can help protect sensitive information (such as session identifiers) from being stolen by XSS attacks.

Can XSS attacks be used to bypass CSRF protection?

Yes, XSS attacks may be used to bypass cross-site request forgery (CSRF) protection. If an attacker can inject a script into your web page, they can use it to perform actions on behalf of the user, potentially bypassing any CSRF protections you have implemented. This is why it is important to protect against both XSS and CSRF attacks.

What PHP frameworks provide built-in protection against XSS attacks?

Yes, many PHP frameworks provide built-in protection against XSS attacks. For example, Laravel automatically encodes the output to prevent XSS attacks. Other frameworks such as Symfony and CodeIgniter also provide functionality for cleaning user input and encoded output. However, it must be remembered that no framework can provide complete protection and you should still follow best practices for preventing XSS attacks.

The above is the detailed content of Cross-Site Scripting Attacks (XSS). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles