I am trying to prevent CSRF in php in the following way:
A $_SESSION['token']
is generated at the start of each page. I already know that using $_COOKIES
is completely wrong since they are send automatically for each request.
In each <form>
, the following input: <input type="hidden" name="t" value="<?php echo '$_SESSION['token']; ?>">
is appended.
The $_SESSION['token'];
is validated with the $_POST['t']
Now I have several small questions:
$_SESSION
variable, the previous (still open) page becomes invalid, how to prevent this?Thank you very much in advance.
The most effective method of protecting against CSRF is by using anti-CSRF tokens. The developer should add such tokens to all forms that allow users to perform any state-changing operations. When an operation is submitted, the web application should then check for the presence of the correct token.
The most robust way to defend against CSRF attacks is to include a CSRF token within relevant requests. The token should be: Unpredictable with high entropy, as for session tokens in general. Tied to the user's session.
Some web sites defend against CSRF attacks using SameSite cookies. The SameSite attribute can be used to control whether and how cookies are submitted in cross-site requests.
CSRF stands for cross-site request forgery. It's a kind of attack in which a hacker forces you to execute an action against a website where you're currently logged in. For example, you visit the malicious-site.com that has a hidden form. And that form submits on page load to yourbank.com/transfer-fund form.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With