Lab 5: DOM XSS in jQuery Anchor `href` Attribute Sink Using `location.search` Source

Vulnerability Type: DOM-based Cross-Site Scripting (XSS) 

Attack Vector

The vulnerable application dynamically sets the `href` attribute of an anchor (`<a>`) tag using untrusted input from `location.search` (URL parameters) without proper sanitization. By injecting a `javascript:`pseudo-protocol payload, an attacker can execute arbitrary JavaScript when the link is clicked. 

Exploitation Steps

  1. Identify the Sink and Source
  2. The `returnPath` URL parameter (e.g., `?returnPath=/test`) is unsafely used to set an `<a>` tag’s `href` attribute via jQuery.
  1. Inspect the “Back” button/link and observe:  <a href=”/test”>Back</a>  <!– User-supplied value injected here –>
  1. Click the “Back” button/link.
  2. The `href` attribute executes the JavaScript, leaking the user’s session cookies via `alert(document.cookie)`. 

Mitigation Strategies

  1. Sanitize Dynamic `href` Attributes:
  2. Strip `javascript:` and other dangerous protocols using allowlists (e.g., `http://`, `https://`).
  3. Use `encodeURIComponent()` for untrusted URLs.
  4. Avoid jQuery’s Unsafe Setters: Replace `.attr(“href”, userInput)` with `.attr(“href”, sanitizedPath)` or safer methods like `.text()`.
  5. Content Security Policy (CSP): Restrict `javascript:` in URLs via CSP with `unsafe-hashes` or `strict-dynamic`.
  6. Framework Protections: Modern frameworks (React, Angular) auto-sanitize dynamic attributes by default. 

Key Takeaway

This lab illustrates how client-side DOM manipulation with untrusted URL parameters can lead to XSS via unsafe `href` attributes. Unlike traditional XSS, this attack leverages the `javascript:` protocol, bypassing server-side mitigations. 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x