--- title: Cross Site Request Forgery --- ## Cross Site Request Forgery Cross Site Request Forgery is a vulnerability in the application caused by the programmer not checking where a request was sent from - this attack is sent to a high privilege level user to gain higher level access to the application. ### Example Cross Site Request Forgery Attack An online blog allows users to submit comments and include an image in the comment, the blog's admin panel allows the blog's author to delete a comment by loading the URL `/admin/deletecomment.php?id=123`. A malicious user could make an image tag that loads the delete comment url for example `` so next time an admin views the comment, the admin's computer will load the url and delete comment number 123. ### Defending your website from cross site request forgery attacks in PHP To defend against a cross site request forgery attack, you should check against a regularly changed token. The url `/admin/deletecomment.php?id=123` would change to `/admin/deletecomment.php?id=123&csrf-token=random-per-user-unique-string-here`. ```PHP Delete Comment'; // Only the logged in user has access to the CSRF Token - the token isn't accessible to the attacker preventing their attack from being successful. ``` #### More Information: * OWASP Wiki - Cross Site Request Forgery * php.net bin2hex() manual * php.net openssl_random_pseudo_bytes() manual