XSS Attack - Why strip_tags is not enough
Tags: php, xss, security, programming
Published: 2023-09-13 20:05:20

In PHP, it is common to use the `strip_tags()` function as a way to prevent XSS intrusion. However, this function does not even work to mitigate this type of attack, giving a false sense of security. But why?

## What is XSS?
XSS (Cross-Site Scripting) is a form of attack that occurs when an attacker exploits a vulnerability in a web application to insert malicious scripts into its pages. These scripts are executed in the browsers of the application's users and can compromise sensitive information, allow session theft, redirect to other sites, etc.

![how xss attack works](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/feo4fipn297d3w3yq0yj.png)

## Why strip_tags don't work?
The `strip_tags()` function is commonly used to remove HTML and PHP tags from a string. However, it is not designed to handle all forms of malicious input that can lead to XSS (Cross-Site Scripting) attacks. 

Here are some reasons why `strip_tags()` falls short in mitigating XSS attacks:

1. **Attribute-based attacks:** XSS attacks can occur through attributes such as onmouseover or onclick, which can execute JavaScript code when triggered. `strip_tags()` does not remove or sanitize these attributes, allowing potential XSS vulnerabilities to remain.
2. **Tag obfuscation:** Attackers can obfuscate the HTML tags and their attributes to bypass `strip_tags()`. They can use techniques such as mixing case variations, HTML entity encoding, or JavaScript-based obfuscation. `strip_tags()` alone cannot effectively handle these obfuscated tags.
3. **Context-awareness:** XSS vulnerabilities can vary depending on the context in which the user input is displayed. `strip_tags()` does not have knowledge of the specific context and may allow certain tags or attributes that can still lead to XSS attacks.

An example of malicious string that can be used in an XSS attack is as follows:

```
this is a XSS attack <script>alert(“hello world”)<script>
```

If we apply the `strip_tags()` function, we obtain the following result:

```
this is a XSS attack alert(“hello world”)
```

Okay, in this case, it was indeed possible to clean the malicious code from the string. However, the attacker can use the following code:

```
this is a XSS attack &lt;script&gt; alert('oi') &lt;/script&gt;
```

The `strip_tags()` function will not sanitize the string in a way that prevents the injection of code into the page.

## How to prevent it?
The good way to deal with untrusted data is:
> Filter on input, escape on output

This means that you handle the received data (filter), but only transform it (escape or encode) when you send it as output to another system that requires encoding.

There is no way around it. In the data sanitization phase, the only way to effectively prevent XSS attacks is by using a specific library, such as:

- [AntiXSS](https://github.com/voku/anti-xss)
- [HTML Purifier](http://htmlpurifier.org/)

These libraries provide robust mechanisms for preventing XSS attacks by sanitizing and properly handling user input and output.

Here, we are going to use the AntiXSS library.
Now we can sanitize our strings in a much safer way:

```php
<?php

use voku\helper\AntiXSS;

require_once __DIR__ . '/vendor/autoload.php';

$antiXss = new AntiXSS();
$xssString = "this is a XSS attack &lt;script&gt; alert('oi') &lt;/script&gt;";
$clearString = $antiXss->xss_clean($xssString);

//this is a XSS attack
echo $clearString;
```

In the phase of outputting data, you can use template engines like [Twig](https://twig.symfony.com) or [Blade](https://laravel.com/docs/10.x/blade) or [htmlspecialchars](https://www.php.net/manual/en/function.htmlspecialchars.php) function.

Great! Now we have a good way to sanitize XSS.

It's worth mentioning that sanitization is just one of the steps in preventing XSS. But that is a topic for another text...

---

### Do you like data sanitization? Then take a look at my PHP data sanitization library!
{% embed https://github.com/rodrigojavornik/PHPCleanup %}

---

[![Did you like this text? You can buy me a coffee.](https://cdn.buymeacoffee.com/buttons/default-black.png)](https://www.buymeacoffee.com/rodrigojavornik)