cybersecurity · beginner · ~10 min
Strict allow-list validation for an HTTP header value.
Implement int valid_host_header(const char *h) returning 1
if h is a valid Host-header value:
A-Z, a-z, 0-9, ., -, : (for port), and [ / ]
for IPv6 literals.Else return 0.
A Host header with a smuggled \r\n lets an attacker forge additional headers — the foundation of CRLF injection. Strict validation refuses everything that looks weird.
The header value string.
0/1.
Per-byte allow-list scan.
int valid_host_header(const char *h) { /* TODO */ (void)h; return 0; }
Allowing space (some clients send it; refuse anyway).
IPv6 literal [::1]. Port number. Empty. Length 256.
O(strlen).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.