Referer Buys You Nothing
I am very surprised at how often I see Referer checking being mentioned as a safeguard against form spoofing. I can't properly express how completely useless this is. I've even had people try to argue with me, convinced that this is a sound technique.
Consider a hypothetical form located at http://example.org/form.html:
<form action="/process.php" method="POST">
<input type="text" name="foo" />
<input type="submit" />
</form>
To spoof this form, an attacker sends an HTTP POST request to http://example.org/process.php. Assuming the developer who wrote process.php is relying on Referer checking to prevent form spoofing, guess what the expected value is? Does this really seem like a big secret? An attacker will get this right every single time.
If you want to do something useful, at least use some bit of information that isn't obvious. One example is to generate a secret token and include it in the form:
<?php
$token = md5(uniqid(rand(), true));
$_SESSION['token'] = $token;
?>
<form action="/process.php" method="POST">
<input type="hidden" name="token" value="<?php echo $token; ?>" />
<input type="text" name="foo" />
<input type="submit" />
</form>
You can check this value in process.php, and it's not very easy to guess. In fact, the only person with a reasonable chance of knowing this value is the person you send it to.





23 Comments
1.
Mathieu said:
2.
Chris Shiflett said:
3.
HarryFuecks said:
4.
ryan king said:
5.
Mathieu said:
6.
Alan Knowles said:
7.
Vincent said:
8.
Ren said:
9.
Ben Ramsey said:
10.
Leendert Brouwer said:
11.
Chris Shiflett said:
12.
Chris said:
13.
Ren said:
14.
Louis-Philippe Huberdeau said:
15.
Thomas Bley said:
16.
Chris Shiflett said:
17.
Chris said:
18.
Chris Shiflett said:
19.
Chris said:
20.
Tim Traver said:
21.
Chris Shiflett said:
22.
diyaudio guy said:
23.
Nikolas Coukouma said: