Skip to content

Core\Csrf::issue

Creates a CSRF token for one session. You put the token in a form. The next request sends it back, and your program can then tell that the request came from your own page.

Core\Csrf::issue takes two things. The first is a value that stays the same for as long as the token should be accepted, such as a session identifier. The second is a 32 byte key, as Core\Crypto::generateKey creates one. The result is a short text of letters, digits, - and _. A hidden field, a header and a URL all carry it unchanged.

Nothing is stored anywhere. The session is sealed inside the token, so a form opened in a second browser tab does not break the form in the first one. Every call creates a different token, and all of them work.

Good to know: Core\Csrf::verify is the only way to check a token. No method gives you the token that was expected, so there is nothing to compare with ==.

Parameters

  • $sessionstringThe value the token belongs to. This is a session id, or any other value that does not change while the token should be valid. A token made for one value never passes verify for another value.
  • $keysecret bytesA 32-byte key, such as the one Core\Crypto::generateKey() returns. The key is not part of the token, so you can use the same key for every session.

Return value

Returns string. A URL-safe base64 string without padding. It uses only A-Za-z0-9-_, so a hidden field, a header and a query string can contain it without escaping. Each call returns a different string for the same inputs. Two tokens for one session are never equal, so == cannot check a token.

Errors

  • LogicError$key is not 32 bytes long. Such a value is not a key.
  • RuntimeErrorThe process does not have enough memory for the token.

Examples

A token for one form

<?nvs
// Shows how a program creates a token for a form, and checks the token when
// the form comes back. The token belongs to one session.
secret bytes $key = Core\Crypto::generateKey();
string $session = 'sid-ada';
// The page is rendered with this token in a hidden field. The token is
// different on every call, so its length is printed here. This prints 79.
string $token = Core\Csrf::issue($session, $key);
echo Core\Str::length($token), "\n";
// The form comes back and carries the token. This prints `yes`.
echo (Core\Csrf::verify($token, $session, $key) ? 'yes' : 'no'), "\n";
// The same token is not accepted for another session. This prints `no`.
echo (Core\Csrf::verify($token, 'sid-grace', $key) ? 'yes' : 'no'), "\n";
Output
79
yes
no

A new token on every call

<?nvs
// Shows that two tokens for one session are two different texts, and that both
// of them work. This is why a program never compares tokens itself.
secret bytes $key = Core\Crypto::generateKey();
string $first = Core\Csrf::issue('sid-ada', $key);
string $second = Core\Csrf::issue('sid-ada', $key);
// The two tokens are different. This prints `different`.
echo ($first == $second ? 'the same' : 'different'), "\n";
// Both are accepted for the session they were created for. This prints `yes`
// twice.
echo (Core\Csrf::verify($first, 'sid-ada', $key) ? 'yes' : 'no'), "\n";
echo (Core\Csrf::verify($second, 'sid-ada', $key) ? 'yes' : 'no'), "\n";
Output
different
yes
yes

One token per action

<?nvs
// Shows how to tie a token to one action as well as to one session. Write the
// name of the action into the value you create the token against.
secret bytes $key = Core\Crypto::generateKey();
string $session = 'sid-ada';
array<string> $forms = ['change-email', 'transfer-money', 'delete-account'];
array<string> $tokens = [];
// Every form on the page gets its own token.
foreach ($forms as string $form) {
$tokens[$form] = Core\Csrf::issue($session . '|' . $form, $key);
}
// The request says which form it comes from, and the program checks the token
// against that form. This prints `yes`.
string $sent = $tokens['transfer-money'];
echo (Core\Csrf::verify($sent, $session . '|transfer-money', $key) ? 'yes' : 'no'), "\n";
// The same token is not accepted for the form that deletes the account. An
// attacker who reads one token cannot use it there. This prints `no`.
echo (Core\Csrf::verify($sent, $session . '|delete-account', $key) ? 'yes' : 'no'), "\n";
Output
yes
no