validate($guard->token()))->toBeTrue(); }); test('a mutated token does not validate', function () { $guard = csrf_test_guard(); $token = $guard->token(); expect($guard->validate($token . 'x'))->toBeFalse(); expect($guard->validate(strrev($token)))->toBeFalse(); }); /* * Symfony randomises the encoding on every getToken() call to mitigate BREACH. * The hidden form field and the JS global therefore hold different strings on * the same page render, and both must validate. This looks like a bug and is * not one. */ test('two issued tokens differ and both validate', function () { $guard = csrf_test_guard(); $first = $guard->token(); $second = $guard->token(); expect($first)->not->toBe($second); expect($guard->validate($first))->toBeTrue(); expect($guard->validate($second))->toBeTrue(); }); test('empty and malformed values are rejected', function () { $guard = csrf_test_guard(); $guard->token(); expect($guard->validate(''))->toBeFalse(); expect($guard->validate('not-a-token'))->toBeFalse(); }); test('a disabled guard issues nothing and validates nothing', function () { $guard = new CactiCsrfGuard(null, false); expect($guard->isEnabled())->toBeFalse(); expect($guard->token())->toBe(''); expect($guard->validate('anything'))->toBeFalse(); });