XSRF and session.referer_check
I was giving Zend’s on-line “security for developers” course to some customers last week, and as usual Cross-Site Request Forgeries (XSRF) was one of the hardest topics for me to explain and for other people to understand. This made me spend some time thinking about XSRF again, coming up with code demos and such to make it more clear.
Incidentally, I came across a php.ini directive today, which I’ve never noticed before: “session.referer_check”. According to the docs, you can set the value of this directive to a substring of the domain name you’re using. PHP will then check every HTTP_REFERER, if set, to contain this substring. If the referring address exists but does not contain the value of session.referer_check, the embedded session ID will be marked as invalid.
I didn’t test this - but if it works as it’s supposed to, it might be a good quick-and-dirty defense against XSRF - because it will not allow you to continue your session if the requested URL was referred from another domain (through an IMG tag, link, form submission and such).
There are several reasons why I think it’s quick-and-dirty, to name a few:
- It will not help you if the XSRF was launched from your own domain - for example through exploiting an XSS vulnerability in your site
- Not all requests referred to your site from other domains are malicious - for example, I might want to have a “Digg This” or “Add to Facebook Profile” button on my site - if you click those, you’ll be referred to Digg / Facebook from my site. In most cases you’d want your sessions to stay active on those sites, handling the request properly, and not treating it as XSRF
- As the experience of magic_quotes_gpc taught us, trying to solve code-level problems in the engine level will probably end up in a half-baked solution, providing mostly the false sense of security
On the other hand, it’s nice to know PHP has that option (and that it’s turned off by default) - if you’ve just found out your site is full of XSRF vulnerabilities, and you know it will take you a while to fix them, you might just want to turn in on as a temporary solution.
Be careful though - it might break some of your functionalities. Also, there’s a popular Hebrew saying that “nothing is more permanent than temporary solutions” (my own translation here..) - so FIX YOUR CODE if you can.














