'signed-cookie-session-test-key-0001', 'enable_login_bind_ip' => 'true', ]; public static function get($name, $default = null) { return self::$values[$name] ?? $default; } } } namespace { require dirname(__DIR__) . '/vendor/autoload.php'; use App\Services\Auth\SignedCookieSession; use App\Services\Config; $assert = static function ($condition, string $message): void { if (!$condition) { throw new RuntimeException($message); } }; $encode = static function (string $value): string { return rtrim(strtr(base64_encode($value), '+/', '-_'), '='); }; $signed = static function (array $claims) use ($encode): string { $payload = $encode(json_encode($claims, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)); $key = hash_hkdf('sha256', Config::$values['key'], 32, 'virt-cloud/auth-cookie/v1'); $signature = $encode(hash_hmac('sha256', 'v1.' . $payload, $key, true)); return 'v1.' . $payload . '.' . $signature; }; $now = 1800000000; $token = SignedCookieSession::issue(17, 4, $now + 3600, '2001:db8::17', $now); $claims = SignedCookieSession::verify($token, '2001:0db8:0:0:0:0:0:17', $now + 1); $assert(is_array($claims), 'A valid signed cookie session was rejected.'); $assert($claims['user_id'] === 17 && $claims['session_version'] === 4 && $claims['issued_at'] === $now && $claims['expires_at'] === $now + 3600, 'Verified signed cookie claims changed.'); $assert(preg_match('/^[a-f0-9]{64}$/D', $claims['session_id']) === 1, 'Signed cookie session id is not a 256-bit random identifier.'); $assert(SignedCookieSession::verify($token, '2001:db8::18', $now + 1) === null, 'An IP-bound cookie was accepted from another address.'); $assert(SignedCookieSession::verify($token, null, $now + 1) === null, 'An IP-bound cookie was accepted without a remote address.'); [$prefix, $payload, $signature] = explode('.', $token); $decoded = json_decode(base64_decode(strtr($payload, '-_', '+/')), true, 16, JSON_THROW_ON_ERROR); foreach (['version', 'user_id', 'session_version', 'issued_at', 'expires_at', 'session_id', 'ip_digest'] as $field) { $tampered = $decoded; $tampered[$field] = is_int($tampered[$field]) ? $tampered[$field] + 1 : str_repeat('a', 64); $tamperedPayload = $encode(json_encode($tampered, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)); $assert(SignedCookieSession::verify($prefix . '.' . $tamperedPayload . '.' . $signature, '2001:db8::17', $now + 1) === null, 'Tampering ' . $field . ' did not invalidate the signature.'); } Config::$values['enable_login_bind_ip'] = 'false'; $unbound = SignedCookieSession::issue(23, 0, $now + 30, null, $now); $assert(SignedCookieSession::verify($unbound, '203.0.113.9', $now + 1)['user_id'] === 23, 'An unbound signed cookie unexpectedly required an address match.'); $assert(SignedCookieSession::verify($unbound, null, $now + 30) === null, 'An expired signed cookie session was accepted.'); Config::$values['enable_login_bind_ip'] = 'true'; $assert(SignedCookieSession::verify($unbound, '203.0.113.9', $now + 1) === null, 'An unbound cookie was accepted after IP binding became mandatory.'); Config::$values['enable_login_bind_ip'] = 'false'; $invalid = $claims; $invalid['issued_at'] = $now + 61; $assert(SignedCookieSession::verify($signed($invalid), '2001:db8::17', $now) === null, 'A cookie issued beyond the clock-skew allowance was accepted.'); $invalid = $claims; $invalid['expires_at'] = $invalid['issued_at'] + SignedCookieSession::MAX_TTL + 1; $assert(SignedCookieSession::verify($signed($invalid), '2001:db8::17', $now) === null, 'A cookie beyond the maximum lifetime was accepted.'); $invalid = $claims; $invalid['user_id'] = '17'; $assert(SignedCookieSession::verify($signed($invalid), '2001:db8::17', $now) === null, 'A cookie with non-canonical claim types was accepted.'); $invalid = $claims; $invalid['extra'] = true; $assert(SignedCookieSession::verify($signed($invalid), '2001:db8::17', $now) === null, 'A cookie with an unexpected claim was accepted.'); $assert(SignedCookieSession::verify('v1.invalid.invalid', null, $now) === null && SignedCookieSession::verify(str_repeat('a', 1025), null, $now) === null, 'Malformed or oversized cookie input was accepted.'); Config::$values['key'] = 'short'; try { SignedCookieSession::issue(1, 0, $now + 60, null, $now); throw new RuntimeException('A weak site key was accepted for cookie signing.'); } catch (RuntimeException $exception) { $assert(str_contains($exception->getMessage(), 'at least 32 bytes'), 'Weak-key failure did not identify the configuration requirement.'); } echo "Signed cookie session integrity, lifetime and optional IP binding passed.\n"; }