Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, with the stored-token approach you'd have to keep all generated tokens just in case they came back in at any point. A single stored-token fails not just for multiple browser tabs/windows but also for back/forward navigation. You generally want to manage the potential storage explosion by expiring old tokens (by age and/or number of tokens issued since).</p> <p>Another approach that avoids token storage altogether is to issue a signed token generated using a server-side secret. Then when you get the token back you can check the signature and if it matches you know you signed it. For example:</p> <pre><code>// Only the server knows this string. Make it up randomly and keep it in deployment-specific // settings, in an include file safely outside the webroot // $secret= 'qw9pDr$wEyq%^ynrUi2cNi3'; ... // Issue a signed token // $token= dechex(mt_rand()); $hash= hash_hmac('sha1', $token, $secret); $signed= $token.'-'.$hash; &lt;input type="hidden" name="formkey" value="&lt;?php echo htmlspecialchars($signed); ?&gt;"&gt; ... // Check a token was signed by us, on the way back in // $isok= FALSE; $parts= explode('-', $_POST['formkey']); if (count($parts)===2) { list($token, $hash)= $parts; if ($hash===hash_hmac('sha1', $token, $secret)) $isok= TRUE; } </code></pre> <p>With this, if you get a token with a matching signature you know you generated it. That's not much help in itself, but then you can put extra things in the token other than the randomness, for example user id:</p> <pre><code>$token= dechex($user-&gt;id).'.'.dechex(mt_rand()) ... if ($hash===hash_hmac('sha1', $token, $secret)) { $userid= hexdec(explode('.', $token)[0]); if ($userid===$user-&gt;id) $isok= TRUE </code></pre> <p>Now each form submission has to be authorised by the same user who picked up the form, which pretty much defeats CSRF.</p> <p>Another thing it's a good idea to put in a token is an expiry time, so that a momentary client compromise or MitM attack doesn't leak a token that'll work for that user forever, and a value that is changes on password resets, so that changing password invalidates existing tokens.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload