CSRF + Drupal (The absolute basics)

April 1, 2014

After all these years developing user interface focussed sites in Drupal, I have sadly not been involved in the technical planning ofthe more transactional elements of a largescale Drupal site - this is about to change. Yesterday, after chatting with another industry professional, I realised I should look into the why of some of these bits as we often just entrust them to Drupal. I will then be able to ensure that, if I need to reproduce the methodology behind these security elements, it is just a simple process of planning the how as I already have a strong understanding of the why.

Therefore this post is focussed on CSRF (or XSRF), what it is, why we should know it, and how Drupal can handle it. Hopefully this will help remind me in the future, and also anyone else that happens to stumble across this post.

What is CSRF?

CSRF (or XSRF or 'Sea Surf') stands for 'Cross-site Request Forgery' and occurs when 'unauthorized commands are transmitted from a user that the website trusts'. This may not make sense as a sentence, so to over-simplify it somewhat, imagine you visit a website and login to do some shopping. Then, unbeknownst to you, a third-party has gotten their greasy javascript hands into that page somehow, and start submitting forms left right and centre through the fact you are already logged in and are authenticated. If said site is unprotected and stores your card details, suddenly a lot of purchases could occur without you knowing.

How can naughty people use CSRF?

Now, some of you with a little dev experience will be thinking 'well we will just stop people entering javascript onto our pages by parsing their inputs, and sanitizing it all' and to a degree this will help. But, one good example I found online is with an image upload form, what if the third-party was able to upload a file that wasn't a .jpg but actually a url to one of their own scripts?

<img src="http://foo.com/logout">

The script simply runs in the background, doing submissions to pages, without the user even knowing. The example above was just a logout link, but of course, could do a lot more if it is actually a page on another site, with some js. If you can do this with images, now think all those times people have said 'dont load the email images unless you trust the source', if you have left your machine logged into a site with a CSRF hole, and that emailer knows it, you will start having some serious issues.

So how is this different from XSS?

Well, this is not so easy to research online. So far, the best explanation I have is around the idea that xss would be a malicious user getting code onto a website to acquire information on another user or present false content to the user. Whereas with CSRF a malicious user runs code either on a site or elsewhere which users a pre-authorised login on a site to pretend to be that user and cheat the site.

How can we/Drupal prevent CSRF dattacks?

Well, there appear to be a few possible options to resolve this, and also a few that will reduce the risk but not actually solve it (eg. using POSTs). From reading though, the most obvious solutions are all around the same concept of ensuring the site is 'confirming the requestor is who it thinks it should be'.

So how do you achieve this, and how does Drupal?

The basic concept is that the website collates a lot of variables that are as individual as possible to that particular request (eg. users session, drupal private key, a unique value for that form/page), then it adds a salt, creating a hash of the results. This hash is then used in 2 places, it is used in the actual form request (this is why you are not advised to cache forms unless you are sure you understand this), and it is used on the server when receiving the request. This means the server is able to confirm the user that sent the request, that it came from the site, and that all is will with the world.

How is this more secure though?

Well only that unique hash is able to submit the form for that user, so long as that hash is given a limited ttl then it is very hard to guess, and get a hold of it (although apparently not impossible if you have a certain XSS flaw too).

But is there an amusing yet interesting story about CSRF?

Why yes, there is actually. This particular attack used a mixture of xss and csrf to become one of the most popular person on mySpace).

http://namb.la/popular/

References and furthe reading