Reflected xss on https://█████████
The redirect parameter was not properly validated, allowing an attacker to inject a javascript: URI that executes arbitrary script in the victim's browser. An attacker could steal cookies, take over accounts, or redirect users to malicious sites.
Originally disclosed on HackerOne
- Triaged in 14 hours
- Fixed in 15 days
- Disclosed after 18 days
Description:
The domain ███ is vulnerable to reflective xss.
By clicking the following link you will get an alert message: https://█████/sec.html?redirect=javascript:alert(document.cookie);//://██████/
The error occurs due to a flaw in the check that verifies the validity of the redirect URL.
The function takes the value of the redirect parameter and checks if the portion after the first :// begins with any of the values in the array.
This check does not include protocol checking, allowing us to prepend the value with javascript:.
Then we can append a commented-out section at the end to ensure that the isSafeHost function returns True.
This is the function that checks if it is valid:
function isSafeHost(uri) {
var safeHosts = ['█████/', '███/', '████/', '██████████'];
// Only consider localhost for local testing
if (window.location.host.includes('localhost')) {
safeHosts.push('localhost');
}
return safeHosts.find((host) => uri.slice(uri.indexOf('://') + 3).startsWith(host));
}
At the end of the check the redirect parameter is used as follows:
window.location.href = rawRedirect;
References
Impact
- Take over a user's account
- Phish users
- Show malicious content
- Redirect users
System Host(s)
██████████
Affected Product(s) and Version(s)
CVE Numbers
Steps to Reproduce
Click on the following link: https://█████████/sec.html?redirect=javascript:alert(1);//://████/
You can change the alert(1) for your own payload
Suggested Mitigation/Remediation Actions
Do a check for protocol and make sure the host comes directly after this protocol.
Context
The target is a web property in the U.S. Department of Defence bug bounty programme, reported through HackerOne and disclosed on 2 June 2023. The report is titled "Reflected xss on https://█████████" and is classified as a medium severity reflected cross-site scripting issue. The affected page is sec.html, which takes a redirect parameter and, after validating it, assigns it to window.location.href. The page takes a redirect parameter and, after validating it, assigns it to window.location.href.
Setup
No accounts, roles, tools or versions are described in the report. The only precondition is that a victim can be induced to open a crafted link to the sec.html page with a redirect parameter, which is the usual condition for reflected XSS.
Finding it
The researcher found that the domain is vulnerable to reflected XSS. Opening the following link produces an alert box:
https://█████/sec.html?redirect=javascript:alert(document.cookie);//://██████/
The error, as the report puts it, comes from a flaw in the check that verifies the validity of the redirect URL. The validation function reads the redirect parameter and checks whether the portion of the string after the first :// begins with one of the values in an allow-list array. That check never looks at the scheme, so the value can be prefixed with javascript:. A commented-out section can then be appended at the end so that the isSafeHost function returns true.
Exploitation
Step 1: Read the validation function
The report gives the host check as it appears in the page:
function isSafeHost(uri) {
var safeHosts = ['█████/', '███/', '████/', '██████████'];
// Only consider localhost for local testing
if (window.location.host.includes('localhost')) {
safeHosts.push('localhost');
}
return safeHosts.find((host) => uri.slice(uri.indexOf('://') + 3).startsWith(host));
}
The function builds a list of trusted host strings. When the page is served from a host containing localhost, it adds localhost to the list for local testing. It then finds the first :// in the URI, slices everything after it, and checks whether that remainder starts with one of the trusted hosts. Nothing in this logic inspects the scheme that precedes ://.
Step 2: Understand where the value goes
At the end of the check the redirect parameter is used directly:
window.location.href = rawRedirect;
Because the validated string is assigned to window.location.href, a javascript: URI runs script in the origin of the page. A javascript: URI runs script in the origin of the page that performs the navigation.
Step 3: Build a payload that passes the check
The payload has three parts:
javascript:alert(document.cookie)is the script to run. The scheme sits before the://that the validator looks for.//://██████/supplies the://the validator searches for, followed by a trusted host string from the array, so the slice after://starts with an allowed value andisSafeHostreturns true.- The
//before the trusted host turns the remainder into a comment in the JavaScript payload, so the browser does not try to execute the trailing text.
Step 4: Deliver the link
The reproduction steps give the link to click:
https://█████████/sec.html?redirect=javascript:alert(1);//://████/
The report notes that alert(1) can be replaced with any payload of the attacker's choosing. When a victim opens the link, the page validates the parameter, accepts it, assigns it to window.location.href, and the browser executes the JavaScript in the context of the target origin.
Root cause
The validator checks only the host portion of the string, taken as everything after the first ://. It does not check the scheme, so a javascript: prefix is accepted as long as the text after :// begins with a trusted host. The trailing // comment lets the attacker satisfy the host check without breaking the JavaScript. The assignment to window.location.href then treats the whole string as a navigation target, and the browser honours the javascript: scheme. The host check does not stop the attack because it validates the wrong part of the URI.
Impact
The report lists the following consequences:
- Take over a user's account
- Phish users
- Show malicious content
- Redirect users
These depend on a victim opening the crafted link, since the payload is reflected through the redirect parameter.
Fix
The suggested remediation is to check the protocol and make sure the host comes directly after that protocol. In practice that means checking the protocol and making sure the host comes directly after it.
Variants to test
- A positive result is the page navigating to or executing the payload instead of rejecting it.
- A positive result is the same execution with a shorter payload.
- A positive result is execution despite the host not being the navigation target.
- A positive result is execution after the browser decodes or normalises the value.
Hunting notes
This bug lives in redirect and navigation helpers: pages that take a URL parameter and assign it to window.location, location.href or similar sinks. A validator that slices after :// and compares only the host, without checking the scheme, is the signal.
What happened, in plain words
A security researcher found a bug on a website belonging to the U.S. Department of Defence. The bug is called reflected cross-site scripting, usually shortened to reflected XSS. In plain terms, the site had a page that took a redirect value, checked it, and then sent the browser to it, but the check could be bypassed.
The page is called sec.html. It accepts a value in the address bar through something called a redirect parameter, checks that value, and then sends the browser to it. The check was meant to make sure the destination was a trusted site. It looked at the wrong part of the address, so an attacker could slip a javascript: instruction past it. When a victim opened a crafted link, the browser ran the attacker's JavaScript as if the page itself had written it.
The report was submitted through HackerOne, a platform where researchers report bugs to companies and government programmes, and was disclosed on 2 June 2023. It is rated medium severity. The researcher is credited as rektile404.
Background you need first
Before walking through the report, here are the ideas it relies on. Each one is explained from scratch.
What a URL is
A URL is the address you type into a browser, like https://example.com/page. It has parts:
- The scheme (also called the protocol) is the bit before
://, such ashttpsorhttp. It tells the browser how to fetch the thing. - The host is the name of the server, such as
example.com. - The path is the part after the host, such as
/page. - The query string starts with
?and carries extra values, such as?redirect=something.
A useful everyday comparison: a URL is like a postal address. The scheme is the kind of delivery (ordinary post, courier, hand delivery), the host is the building, and the path is the flat number inside.
What a parameter is
A parameter is a named value passed in the URL after the ?. In ?redirect=https://example.com, the parameter name is redirect and its value is https://example.com. Websites read these values and act on them. A page that takes a redirect parameter is normally saying: "tell me where to send the visitor next, and I will send them there."
What a cookie is
A cookie is a small piece of data a website stores in your browser. It is how a site remembers who you are between page loads. The most sensitive cookie is usually the session cookie: it acts like a temporary pass that proves you are logged in. If someone else gets hold of it, they can often act as you. That is why the researcher's example payload reads document.cookie: it is a way of showing that a script running on the page can see the visitor's cookies.
What JavaScript is
JavaScript is the programming language that runs inside web pages in your browser. It can read the page, change it, and interact with the browser. When a page runs JavaScript, that code runs with the same permissions as the page itself. That is the heart of XSS: if an attacker can get their JavaScript to run as part of a trusted page, the browser treats it as trusted too.
What a javascript: link is
A normal link starts with https://. A link that starts with javascript: is not a web address at all: it tells the browser "run the code that follows as JavaScript". For example, javascript:alert(1) makes the browser pop up a box saying 1. This is a legitimate browser feature, but it becomes dangerous when a site lets an attacker put a javascript: value somewhere the browser will follow it.
What reflected XSS is
Cross-site scripting (XSS) is a class of bug where an attacker gets their own code to run inside someone else's website. There are a few kinds. Reflected XSS means the malicious value is sent in the request (usually in the URL) and the site bounces it straight back into the page. The attacker does not need to store anything on the server; they just need the victim to click a link. The victim's browser then runs the attacker's code as if the site had written it.
An everyday comparison: imagine a receptionist who reads out whatever name a visitor gives and announces it over the office speaker. If a visitor says a name that is actually an instruction ("announce that the safe is open"), the receptionist repeats it and the office acts on it. The receptionist meant no harm; the problem is that they repeated an untrusted instruction as if it were trusted.
What an allow-list is
An allow-list (sometimes called a whitelist) is a list of things that are permitted, and everything else is rejected. It is the opposite of a block-list, which lists things that are forbidden. Allow-lists are generally safer because it is easier to say exactly what is allowed than to guess every bad thing. The bug here is that the allow-list was applied to the wrong part of the URL.
The page and what it is for
The affected page is sec.html. The affected page is sec.html. It takes a redirect parameter, checks it, and then assigns it to window.location.href. Think of it as a doorman who is supposed to check that a delivery is going to a building on the approved list before letting the courier through.
Finding it
The researcher found that the domain is vulnerable to reflected XSS. Opening the following link produces an alert box:
https://█████/sec.html?redirect=javascript:alert(document.cookie);//://██████/
What this link does, piece by piece:
https://█████/sec.htmlis the page on the target site.?redirect=starts the query string and names the parameter the page reads.javascript:alert(document.cookie)is the value being passed. It is ajavascript:link that, if the browser follows it, pops up a box showing the visitor's cookies.;//://██████/is trailing text designed to satisfy the page's host check. The//at the start of that trailing part turns the rest into a comment in JavaScript, so the browser ignores it when running the payload.
The report says the error comes from a flaw in the check that verifies the validity of the redirect URL. The validation function reads the redirect parameter and checks whether the portion of the string after the first :// begins with one of the values in an allow-list array. That check never looks at the scheme, so the value can be prefixed with javascript:. A commented-out section can then be appended at the end so that the isSafeHost function returns true.
Exploitation
Step 1: Read the validation function
The report gives the host check as it appears in the page:
function isSafeHost(uri) {
var safeHosts = ['█████/', '███/', '████/', '██████████'];
// Only consider localhost for local testing
if (window.location.host.includes('localhost')) {
safeHosts.push('localhost');
}
return safeHosts.find((host) => uri.slice(uri.indexOf('://') + 3).startsWith(host));
}
Line by line, in plain words:
function isSafeHost(uri) {defines a function calledisSafeHostthat takes one input,uri, which is the URL string to check. A function is a named block of code you can call whenever you need it.var safeHosts = ['█████/', '███/', '████/', '██████████'];creates a list (an array) of trusted host strings. These are the hosts the page is willing to redirect to. The actual values are redacted in the report.// Only consider localhost for local testingis a comment. The browser ignores it; it is a note for humans.if (window.location.host.includes('localhost')) {checks whether the page is currently being served from a host whose name containslocalhost.localhostis the standard name a computer uses for itself, so this is a way of detecting local development.safeHosts.push('localhost');addslocalhostto the trusted list, but only when the previous condition is true. This is a convenience for developers testing on their own machine.}closes theifblock.return safeHosts.find((host) => uri.slice(uri.indexOf('://') + 3).startsWith(host));is the actual check. It finds the first://in the URI, takes everything after it, and asks whether that remainder starts with one of the trusted hosts. If it does, the matching host is returned; if not, the result is empty.}closes the function.
The key point: nothing in this logic inspects the scheme that comes before ://. It only looks at what follows the first ://. That is the gap the researcher exploited.
Step 2: Understand where the value goes
At the end of the check the redirect parameter is used directly:
window.location.href = rawRedirect;
Line by line:
window.location.hrefis the browser's way of saying "the current address of this page". Assigning to it tells the browser to navigate somewhere.rawRedirectis the value that came in through theredirectparameter, after the check has run.=assigns the value. So the browser is told: go to whatever the visitor put inredirect.
Because the validated string is assigned to window.location.href, a javascript: URI runs script in the origin of the page. A javascript: URI runs script in the origin of the page that performs the navigation. "Origin" here means the combination of scheme, host and port that the browser uses to decide which page is allowed to touch which data. The report lists account takeover among the possible consequences.
Step 3: Build a payload that passes the check
The payload has three parts:
javascript:alert(document.cookie)is the script to run. The scheme sits before the://that the validator looks for.//://██████/supplies the://the validator searches for, followed by a trusted host string from the array, so the slice after://starts with an allowed value andisSafeHostreturns true.- The
//before the trusted host turns the remainder into a comment in the JavaScript payload, so the browser does not try to execute the trailing text.
Why each part is needed:
- Without the
javascript:prefix, the browser would treat the value as an ordinary web address and just navigate, which is not an attack. - Without the
://and the trusted host, the validator would reject the value because the part after://would not start with an allowed host. - Without the
//comment, the browser would try to run the trailing text as JavaScript and the payload would break.
An everyday comparison: the doorman checks that the delivery label ends with an approved building name. The attacker writes a label that starts with an instruction ("open the safe") and ends with an approved building name, separated by a marker the doorman looks for. The doorman sees the approved name and waves it through, but the courier reads the whole label and follows the instruction.
Step 4: Deliver the link
The reproduction steps give the link to click:
https://█████████/sec.html?redirect=javascript:alert(1);//://████/
This is the same shape as the earlier link, but with a simpler payload, alert(1), which just pops up a box saying 1. The report notes that alert(1) can be replaced with any payload of the attacker's choosing. When a victim opens the link, the page validates the parameter, accepts it, assigns it to window.location.href, and the browser executes the JavaScript in the context of the target origin.
Why a simple alert(1) is used in a report: it proves the code ran without doing anything harmful. It is the security equivalent of a smoke test. Once the researcher can show that their code runs, they can describe what a real attacker would do with the same gap.
Root cause
The validator checks only the host portion of the string, taken as everything after the first ://. It does not check the scheme, so a javascript: prefix is accepted as long as the text after :// begins with a trusted host. The trailing // comment lets the attacker satisfy the host check without breaking the JavaScript. The assignment to window.location.href then treats the whole string as a navigation target, and the browser honours the javascript: scheme. The host check does not stop the attack because it validates the wrong part of the URI.
In plain terms: the guard was checking the destination building but not the kind of delivery. A javascript: delivery is not a normal web address at all, so checking only the host is like checking the envelope but not the contents.
Impact
The report lists the following consequences:
- Take over a user's account
- Phish users
- Show malicious content
- Redirect users
What each one means for someone new to this:
- Take over a user's account: if the attacker's script runs in the target site's origin, it can read the victim's cookies and act as them. That is why the example payload reads
document.cookie. - Phish users: the attacker can make the trusted site show a fake login form or a message that looks official, because the script runs as part of that site.
- Show malicious content: the attacker can change what the page displays, for example to spread misinformation or scare the visitor.
- Redirect users: the attacker can send the visitor somewhere else, such as a lookalike site.
These depend on a victim opening the crafted link, since the payload is reflected through the redirect parameter. The attacker cannot trigger it on their own; they need the victim to click.
Fix
The suggested remediation is to check the protocol and make sure the host comes directly after that protocol. In practice that means checking the protocol and making sure the host comes directly after it.
An everyday comparison: instead of reading the label from the middle outwards, read it from the start. Confirm the first word is one of the approved delivery types, then confirm the next word is an approved building. If either is wrong, refuse the delivery.
Variants to test
- A positive result is the page navigating to or executing the payload instead of rejecting it.
- A positive result is the same execution with a shorter payload.
- A positive result is execution despite the host not being the navigation target.
- A positive result is execution after the browser decodes or normalises the value.
Why these are worth trying: the original bug exists because the check is textual and looks at the wrong part of the string. Any variation that still satisfies the text check while changing what the browser actually does is a candidate for the same class of bug.
Hunting notes
This bug lives in redirect and navigation helpers: pages that take a URL parameter and assign it to window.location, location.href or similar sinks. A "sink" is a place in code where a value is used in a way that has an effect, such as navigating the browser. If an untrusted value reaches a sink without proper checking, that is where trouble starts.
These names are common because sites often need to send users back to where they came from after logging in or out.
A validator that slices after :// and compares only the host, without checking the scheme, is the signal.
Glossary
- Allow-list: a list of values that are permitted, with everything else rejected. Also called a whitelist.
- Array: an ordered list of values in programming, written in square brackets in JavaScript.
- Comment (in code): text that the browser or interpreter ignores, used for notes. In JavaScript,
//starts a comment that runs to the end of the line. - Cookie: a small piece of data a website stores in the browser, often used to remember who is logged in.
- Cross-site scripting (XSS): a bug class where an attacker gets their own code to run inside someone else's website.
data:scheme: a URL scheme that carries the content inline rather than pointing at a server.- Function: a named block of code that can be called to perform a task.
- HackerOne: a platform where security researchers report bugs to companies and government programmes.
- Host: the server name part of a URL, such as
example.com. javascript:scheme: a URL scheme that tells the browser to run the text that follows as JavaScript.localhost: the standard name a computer uses to refer to itself, often used for local development.- Origin: the combination of scheme, host and port that the browser uses to decide which page may access which data.
- Parameter: a named value passed in a URL after the
?. - Phishing: tricking someone into trusting a fake page or message so they hand over information or click something harmful.
- Protocol: another word for scheme, the part of a URL before
://. - Query string: the part of a URL after
?, carrying parameters. - Recon: short for reconnaissance, the phase of mapping out a target before testing it.
- Redirect: sending the browser from one address to another.
- Reflected XSS: XSS where the malicious value is sent in the request and bounced straight back into the page, so the victim must click a crafted link.
- Scheme: the part of a URL before
://, such ashttpsorjavascript. - Session cookie: the cookie that proves a visitor is logged in; stealing it can let an attacker act as that user.
- Sink: a place in code where a value is used in a way that has an effect, such as navigating the browser.
startsWith: a JavaScript method that checks whether a string begins with a given piece of text.slice: a JavaScript method that takes a portion of a string.indexOf: a JavaScript method that finds the position of a piece of text inside a string.vbscript:scheme: an older URL scheme that some browsers once interpreted as VBScript code.window.location.href: the browser property that holds the current page address; assigning to it navigates the browser.