How to prevent Cross-site request forgery (CSRF) effectively in PHP

后端 未结 1 1728
甜味超标
甜味超标 2021-02-09 11:58

I am trying to prevent CSRF in php in the following way:

  1. A $_SESSION[\'token\'] is generated at the start of each page. I already know that using <

1条回答
  •  感情败类
    2021-02-09 12:46

    Is this a good way to prevent CSRF?

    Yes. What this does is to force the client to do a GET on the form before it can do a POST to your form handler. This prevents CSRF in modern browsers since browsers will prevent client-side Javascript to do an XHR GET request to a foreign domain, so a 3rd party cannot imitate your form on their site and successfully get a valid token for the submission.

    When another page is opened as well that sets the same $_SESSION variable, the previous (still open) page becomes invalid, how to prevent this?

    Allow several tokens to be valid at a time, keeping an array of valid tokens in the session. Alternatively, store no tokens at all and use a token signing scheme instead. I've dabbled in and explained that here. Alternative 2: just use a single token for the whole session, without invalidating tokens. (tip o' the hat to @SilverlightFox in the comments)

    For forms this method is clear, but how to handle normal links? Is it necessary to append the token to each link as well?

    No. You only need to protect POST requests since presumably only POST requests can alter sensitive data (wink wink nudge nudge, you're sticking to REST conventions, right?!) and XHR GET requests are already blocked browser-side.

    0 讨论(0)
提交回复
热议问题