CSRF double-submit cookie validation
Your task
Validate a CSRF double-submit-cookie defense. Given the token value from the cookie, the token value submitted in the request (header or form field), and a set of HTTP methods considered safe (e.g. ['GET','HEAD','OPTIONS']), return True if the request should be allowed. Safe methods are always allowed regardless of tokens. For unsafe methods, both tokens must be present (non-empty) and equal under a constant-time comparison. Return False otherwise.
Examples
csrf_ok(method: str, cookie_token: str, request_token: str, safe_methods: list[str]) -> boolExample 1
Input
csrf_ok("POST", "abc123", "abc123", ["GET", "HEAD", "OPTIONS"])Output
True