← PHP / Laravel · Contents

PHP-038 / Advanced

Comparing tokens or hashes using === (timing attacks)

A02:2021 Cryptographic Failures · CWE-208 Observable Timing Discrepancy

Why is this a problem?

Response time varies with mismatch position, allowing tokens to be guessed one character at a time.

How to fix it

php
if (hash_equals($session->csrf_token, (string) $request->token)) { ... }

Further reading