Core\Csrf::verify
Checks whether a CSRF token belongs to one session. It returns true for a token your own program created for that session under that key, and false for everything else.
Core\Csrf::verify takes three things: the token the request sent, the same value the token was created against, and the same 32 byte key. A token that was changed returns false. So does a token for another session, a token created under an older key, and text that is not a token at all. Only one mistake throws an error, and it is a key that is not 32 bytes long. That is a bug in the program, not a bad request, so an ordinary check never needs a try.
Good to know: this is the only way to check a token. No method gives you the token that was expected. The check always takes the same amount of time, so an attacker who is guessing learns nothing from how long an answer took.
Parameters
$tokenstringThe token from the request. Ataintedvalue is allowed here, because a token always comes from a request.$sessionstringThe value you gaveissue. A token made for a different session returnsfalse.$keysecret bytesThe key you gaveissue. When you change the key, every token made with the old key returnsfalse.
Return value
Returns bool. true for a token made with this key for this session. false for any other text: a changed token, a token made with an old key, a token for another session, or text that is not base64. The comparison takes the same time in every case. All four cases return the same false, so an attacker cannot learn which check failed.
Errors
LogicError$keyis not 32 bytes long. A forged token returnsfalseand never throws. Only a bug in the program throws here.
Examples
Check a token from a form
<?nvs// Shows the check a program does when a form comes back: the token from the// request, the session it should belong to, and the key.
secret bytes $key = Core\Crypto::generateKey();string $session = 'sid-ada';string $token = Core\Csrf::issue($session, $key);
// The form sends back the token the page was rendered with. This prints `yes`.echo (Core\Csrf::verify($token, $session, $key) ? 'yes' : 'no'), "\n";
// The last character of the token is changed. This prints `no`.string $changed = Core\Str::replaceRange($token, -1, 1, '!');echo (Core\Csrf::verify($changed, $session, $key) ? 'yes' : 'no'), "\n";
// The token belongs to another session. This prints `no`.echo (Core\Csrf::verify($token, 'sid-grace', $key) ? 'yes' : 'no'), "\n";yesnonoEvery wrong token is refused
<?nvs// Shows the answer for five tokens that do not belong to this session. Every// one of them is `false`, and none of them throws an error.
secret bytes $key = Core\Crypto::generateKey();secret bytes $olderKey = Core\Crypto::generateKey();string $token = Core\Csrf::issue('sid-ada', $key);
array<string> $wrong = [ '', 'not a token at all', Core\Str::replaceRange($token, -1, 1, '!'), Core\Csrf::issue('sid-grace', $key), Core\Csrf::issue('sid-ada', $olderKey),];
// This prints `false` five times.foreach ($wrong as string $candidate) { echo (Core\Csrf::verify($candidate, 'sid-ada', $key) ? 'true' : 'false'), "\n";}
// The token the page was rendered with is the only one accepted. This prints// `true`.echo (Core\Csrf::verify($token, 'sid-ada', $key) ? 'true' : 'false'), "\n";falsefalsefalsefalsefalsetrueRefuse a post from another site
<?nvs// Shows how a request handler refuses a form whose token does not belong to// the session. The rest of the handler runs only after the token is accepted.
class Handler{ public static function post( string $token, string $session, secret bytes $key, string $email, ): string { if (!Core\Csrf::verify($token, $session, $key)) { return '403 this form did not come from us'; } return '200 the address is now ' . $email; }}
secret bytes $key = Core\Crypto::generateKey();string $session = 'sid-ada';
// The form the program rendered comes back with its token. This prints the// `200` line.string $mine = Core\Csrf::issue($session, $key);echo Handler::post($mine, $session, $key, 'ada@example.org'), "\n";
// Another site sends the same form from the reader's browser. It cannot read// the token, so it sends none. This prints the `403` line.echo Handler::post('', $session, $key, 'attacker@example.org'), "\n";200 the address is now ada@example.org403 this form did not come from us