cybersecurity · intermediate · ~25 min
Strict URL host extraction with explicit reject rules.
Extract just the host from a URL with a strict, conservative parser — URL parsing is a notorious CVE source, and writing one teaches what vetted libraries reject and why.
Implement int extract_host(const char *url, char *out_host, size_t cap) that copies the host into out_host, NUL-terminated and capped to cap-1 bytes.
url: a NUL-terminated URL of the form scheme://host[:port][/path][?query][#fragment]. The grader passes fixed strings.out_host, cap: the output buffer and its capacity.Returns int: 1 on success (writing the host), or 0 on any failure.
extract_host("http://example.com/foo") -> 1, out_host = "example.com"
extract_host("https://api.example.com:8443/v1") -> 1, out_host = "api.example.com"
extract_host("http://127.0.0.1") -> 1, out_host = "127.0.0.1"
extract_host("file:///etc/hosts") -> 0 (empty host)
extract_host("not-a-url") -> 0 (no ://)
extract_host("http://user:pw@evil/foo") -> 0 (userinfo refused)
:// returns 0.out_host returns 0.:// and ends at the first /, :, ?, #, or end of string.user:pass@host) — a known XSS/SSRF vector — rather than parsing it.URL parsing is a notorious source of security bugs (CVEs in nginx, curl, browsers). Real production code uses a vetted library. But understanding the structure — and writing a strict, conservative parser — teaches you what those libraries are doing and why they reject so many edge cases.
A NUL-terminated url, an output buffer out_host, and its capacity cap.
An int: 1 with the host in out_host, or 0 on any failure.
Refuse no-scheme, empty-host, userinfo, and over-capacity hosts. Pure C; cap-aware copy.
#include <stddef.h>
int extract_host(const char *url, char *out_host, size_t cap) { /* TODO */ return 0; }
Accepting userinfo and using it as the host. Forgetting to handle the optional :port. Not capping the copy at cap-1.
Empty URL; URL with no path; IPv4 literal; userinfo (must reject).
O(strlen(url)).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.