WSTG - Latest
Cross-Site Leaks
| ID |
|---|
| WSTG-CLNT-18 |
Summary
Cross-site leaks (XS-Leaks) are a class of client-side issues where a page on another site learns something about a user’s state on the target application without being able to read the target’s responses. The same-origin policy stops the attacker’s page from reading a cross-origin response, but the browser still exposes side effects of loading it: whether a request succeeded or failed, how many frames the page contains, how long it took, whether it redirected, and so on.
When a response differs depending on the user’s data, those side effects turn into a yes/no oracle. Typical examples:
- A search endpoint that returns results or an empty page, so an attacker can test whether the victim’s mailbox contains a given word, one guess per request.
- A profile or resource URL that returns
200for one user and403or404for another, revealing who the victim is or which groups they belong to. - A page that embeds an extra frame, or redirects elsewhere, only when the user is logged in.
The victim only has to visit the attacker’s page while logged in to the target. The browser sends the victim’s cookies with the cross-site requests where cookie settings allow it, so the leak is about the victim’s data, not the attacker’s.
This page covers the leak techniques and the defenses to check. For reading cross-origin data directly, see Cross Origin Resource Sharing and Cross Site Script Inclusion. For framing, see Clickjacking.
Test Objectives
- Identify responses that change with the authenticated user’s state or data.
- Determine whether a cross-site page can observe those differences through browser side channels.
- Assess whether the application uses the browser defenses that stop cross-site requests or remove the side channels.
How to Test
Find State-Dependent Responses
Leaks need a response that differs between two states the attacker wants to tell apart. Good candidates are:
- search, filter and autocomplete endpoints that take the query in the URL;
- pages and API endpoints for a specific object, user, group or tenant;
- pages that only exist, or look different, for logged-in users or for users with a role;
- endpoints that redirect depending on the session, such as a login page that sends an authenticated user straight to a dashboard.
For each candidate, request it in both states, for example with a matching and a non-matching search term, and compare the status code, the Content-Type, the number of <iframe> elements, the redirects and the response size and time. Any difference is a possible oracle. A candidate only matters if the attacker can choose the URL, so focus on parameters in the path or the query string of a GET request.
Check Whether Cookies Are Sent Cross-Site
Most leaks rely on the browser sending the victim’s session cookie with the cross-site request. Check the SameSite attribute of the session cookies (see Cookies Attributes):
SameSite=Strict: the cookie is not sent on any cross-site request, which stops most of the techniques below.SameSite=Lax, or no attribute in browsers that default toLax: the cookie is sent on top-levelGETnavigations, such aswindow.open()or a link, but not on cross-site subresource requests like<script>or<img>. Window-based techniques like frame counting still work.SameSite=None: the cookie is sent on cross-site subresource requests too, where the browser still allows third-party cookies.
Browsers that block or partition third-party cookies remove many subresource-based leaks, so test in more than one browser and note the browser and settings used.
Test the Leak Techniques
Host a test page on a different site from the target, log in to the target in the same browser, and open the test page. Run each check with the state that should match and the one that should not, and see whether the results differ.
Load and Error Events
Elements like <script>, <img> and <link> fire load or error depending on the response, which often reveals the status code class:
<script>
const s = document.createElement('script');
s.src = 'https://target.example/api/search?q=invoice';
s.onload = () => console.log('load');
s.onerror = () => console.log('error');
document.head.appendChild(s);
</script>
Which event fires depends on the resource type, the Content-Type, X-Content-Type-Options: nosniff, the browser’s cross-origin read blocking and the browser itself. The events do not reliably map to HTTP status classes: an <img> can fire load for an error response that contains a valid image, and a <script> can fire error for a 200 response that isn’t valid script. The HTTP status can’t be inferred from them, so compare the two states instead of assuming which event means what. This technique needs the session cookie to be sent on subresource requests.
Frame Counting
A cross-origin window exposes the number of frames it contains through window.length. A page that renders an extra <iframe> for one state leaks that state to the page that opened it:
const w = window.open('https://target.example/search?q=invoice');
if (!w) {
console.log('popup blocked, allow popups for the test page');
} else {
const deadline = Date.now() + 10000;
let last = -1, stable = 0;
const poll = setInterval(() => {
// Wait until the frame count stops changing, or give up after 10 seconds
stable = w.length === last ? stable + 1 : 0;
last = w.length;
if (stable >= 5 || Date.now() > deadline) {
clearInterval(poll);
console.log(w.length, stable >= 5 ? 'stable' : 'timed out, count may be incomplete');
w.close();
}
}, 200);
}
window.open() returns null when the browser blocks the popup, so check for that first. A fixed delay doesn’t show that the page has finished loading, so poll until the count is stable, with a bounded timeout. A stable count is still only a heuristic, because frames that are added late can be missed, so the example reports whether the count was stable or the poll timed out. Treat a timed-out count as incomplete and don’t compare it with a stable one.
Because this is a top-level navigation, SameSite=Lax cookies are sent. The same check works on an <iframe> with frames[0].length when the target can be framed and its session cookie is SameSite=None, since a cross-site frame isn’t a top-level navigation.
Redirects
If the target redirects to another origin in one state only, a test page can detect it with its own Content Security Policy. Serve the test page with a policy that only allows images from the target origin, such as Content-Security-Policy: img-src https://target.example, and load the URL as an image:
<script>
document.addEventListener('securitypolicyviolation', () => console.log('redirected off-origin'));
</script>
<img src="https://target.example/account">
When the request is redirected to an origin the policy does not allow, the browser blocks it and fires a securitypolicyviolation event on the test page. Like the error events above, this needs the cookie on subresource requests. The XS-Leaks wiki describes more navigation-based variants. Many of them depend on the browser, so treat a negative result as inconclusive rather than as proof of safety.
Timing
Where nothing else differs, the response time may. Measure a credentialed request from the test page several times in each state:
async function time(url) {
const start = performance.now();
await fetch(url, { mode: 'no-cors', credentials: 'include' });
return performance.now() - start;
}
A consistent difference, such as a search that does more work when it finds results, can be enough. This measures when the opaque no-cors promise resolves, not when the server finished processing the request. Browser scheduling, connection reuse, HTTP caching and service workers all affect it. Timing leaks are noisy, so alternate requests for the two states many times, discard the first request of each run (connection setup), use a cache-busting parameter or cache: 'no-store', compare medians rather than single values, and only report a difference that is consistent across runs.
Check the Defenses
Look at the response headers of the state-dependent endpoints found above.
-
Fetch Metadata: browsers send
Sec-Fetch-Site,Sec-Fetch-ModeandSec-Fetch-Destwith each request. A resource isolation policy rejects cross-site requests that are not top-level navigations. Replay a request with the headers a cross-site<script>would send and see whether the server still answers normally:curl -i 'https://target.example/api/search?q=invoice' \ -H 'Cookie: session=<session cookie>' \ -H 'Sec-Fetch-Site: cross-site' \ -H 'Sec-Fetch-Mode: no-cors' \ -H 'Sec-Fetch-Dest: script'This only checks how the server handles headers you supply. It doesn’t show that browsers send these headers on real cross-site requests, or that proxies, CDNs and other intermediaries pass them through unchanged. Confirm with a real cross-site request from the test page above, and check the headers in the browser’s developer tools or the server logs.
The server itself must reject the disallowed cross-site request, for example with a
403.Varydoesn’t enforce anything. If the response differs based on any of these headers, check thatVarylists those headers, and only the ones that influence the response. Without it a cache can serve the response that was meant for a same-origin request to a cross-site one. - Cross-Origin-Opener-Policy: with
Cross-Origin-Opener-Policy: same-origin, a page opened from another site gets its own browsing context group, so the opener loses access to it and frame counting throughwindow.open()stops working. - Cross-Origin-Resource-Policy:
Cross-Origin-Resource-Policy: same-originstops every other origin from loading the response as a subresource, whilesame-sitestill allows other origins within the same site, such as a sibling subdomain. The right setting depends on the trust boundary, so check that it isn’t looser than the application needs. - Framing controls:
Content-Security-Policy: frame-ancestorsorX-Frame-Optionsstop the page from being framed, which removes the frame-based variants. - Cache: modern browsers partition the HTTP cache by the top-level site, which stops most cache-probing leaks. Responses with sensitive, user-specific content should still use
Cache-Control: no-store.
Remediation
- Set session cookies with
SameSite=Laxat least, orStrictwhere the application does not need cross-site navigations to stay logged in. - Deploy a Fetch Metadata resource isolation policy that rejects cross-site requests other than top-level navigations, for endpoints that return user-specific data, and add the
Sec-Fetch-*headers the response depends on toVary. - Send
Cross-Origin-Opener-Policy: same-originandCross-Origin-Resource-Policy: same-originon pages and resources that depend on the user’s state. - Prevent framing with
frame-ancestorswhere framing is not needed. - Where possible, avoid observable differences: return the same status code and page structure whether or not a search matches, and protect sensitive queries with an anti-CSRF token or a Fetch Metadata policy. Requiring
POSTalone doesn’t help, since other sites can send cross-sitePOSTrequests from a form.
Tools
- XSinator, a browser test suite that shows which XS-Leak techniques a browser is vulnerable to
- Browser developer tools, to inspect cookies, frames and response headers