cybersecurity · beginner · ~10 min
Tiny allow-list check, an easy primitive but extremely defensive.
Refuse unexpected HTTP methods with a tiny allowlist — a one-line defence against a class of cache-pollution and request-smuggling tricks.
Implement int is_valid_http_method(const char *m) that returns 1 if m is exactly one of the allowed methods, else 0.
Allowed methods (case-sensitive, uppercase): GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH.
m: a NUL-terminated method string, or NULL. The grader passes a fixed set of strings.Returns int: 1 if m exactly matches an allowed method, else 0.
is_valid_http_method("GET") -> 1
is_valid_http_method("PATCH") -> 1
is_valid_http_method("TRACE") -> 0 (not allowed)
is_valid_http_method("get") -> 0 (case-sensitive)
is_valid_http_method("CONNECT") -> 0 (intentionally excluded)
is_valid_http_method("") -> 0
is_valid_http_method(NULL) -> 0
NULL and the empty string return 0.get, Get) return 0.GETT return 0.TRACE and CONNECT are deliberately excluded.The first byte of every HTTP request is attacker-controlled. Refusing weird methods (TRACE, CONNECT, the legendary GETT) is a one-line defence that blocks an entire class of cache-pollution and request-smuggling tricks.
A NUL-terminated method string m, or NULL.
An int: 1 if m exactly matches an allowed method, else 0.
Exact case-sensitive comparison; no allocations; TRACE/CONNECT excluded.
int is_valid_http_method(const char *m) { /* TODO */ return 0; }
Including TRACE (information-leak prone) or CONNECT (proxy tunneling). Doing a case-insensitive compare.
NULL; empty; lowercase; near-matches like GETT.
O(1) — constant work over a fixed table.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.