CSRF where Referer validation depends on header being present

2 min read Easy PortSwigger
CSRF
Contents

On this page

The lab

here is how portswigger describes it

This lab's email change functionality is vulnerable to CSRF. It attempts to block cross domain requests but has an insecure fallback.

To solve the lab, use your exploit server to host an HTML page that uses a CSRF attack to change the viewer's email address.

What the referer header is?#

whenever your browser sends a request that was triggered by another page, it usually tacks on a Referer header that says which page you came from, the full url of the page that linked to or fired the request. servers can read it to see where their traffic originates. one quirk worth knowing, the header is famously misspelled, it is Referer with one r, baked into the http standard decades ago, while the html attribute we will use to control it is spelled referrer with two, so do not let that trip you up

some sites lean on this header as a cheap csrf defence, they check that the Referer points at their own domain, reasoning that a request forged from an attacker's site would carry the attacker's domain in the Referer and can be rejected. it sounds reasonable, but it only holds if the header is actually there and actually trustworthy, and that is exactly where this lab falls down.

Step 1 - Confirm the referer is being checked#

logging in and sending an email change to repeater, i first changed the domain in the Referer header to something that is not the site and replayed it

111

it gets rejected. so the server really is inspecting the Referer and refusing requests that do not come from its own domain. so far the defence looks like it is doing its job

Step 2 - Delete the referer entirely#

here is the insecure fallback and it is the same trick as deleting a csrf token instead of giving the server a wrong Referer i removed the Referer header from the request completely and sent it again.

222

it is accepted. so the check only runs when a Referer is present, and when there is none at all the server just waves the request through rather than rejecting it. that means we do not need to fake a valid referer, we only need to make sure our forged request carries none

Step 3 - Build the poc that suppresses the referer#

browsers normally add the Referer on their own, so the job is to tell the browser not to we do that with a referrer policy on our exploit page the <meta name="referrer" content="no-referrer"> tag, which instructs the browser to leave the Referer off every request this page makes here is the poc.

html
<html>
<head>
<meta name="referrer" content="no-referrer">
</head>
<body>
<form action="https://your-lab-id.web-security-academy.net/my-account/change-email" method="POST">
<input type="hidden" name="email" value="[email protected]" />
<input type="submit" value="Submit request" />
</form>
<script>
document.forms[0].submit()
</script>
</body>
</html>

the meta tag at the top sets the no referrer policy, so when the auto submitting form fires its post, the browser sends it with no Referer header at all. the request reaches the server, the server looks for a Referer to validate, finds none, and falls back to accepting it, and since there is no token either, the victim's session cookie is all that is needed and the email is changed.

333

with this, the lab is solved!