cybersecurity · intermediate · ~15 min · safe pentest lab
Bounded parsing of a text protocol with allow-list character validation.
Parse an HTTP request line into method, path, and version with strict bounds and allow-list checks — the request line is where most HTTP attacks begin.
Implement int parse_request_line(const char *buf, http_req_t *out) where:
typedef struct {
char method[8]; /* e.g. "GET", "POST" */
char path[256];
char version[16]; /* e.g. "HTTP/1.1" */
} http_req_t;
Return 0 on success (filling out), -1 on any failure.
buf: a NUL-terminated buffer holding the first line of an HTTP request, ending in \r\n. The grader passes fixed strings.out: the struct to fill. Written only on success.Returns int: 0 on success, -1 on any malformed input.
"GET /index.html HTTP/1.1\r\n" -> 0, method="GET", path="/index.html", version="HTTP/1.1"
"GET /a\n" -> -1 (no CRLF)
"GET /a;b HTTP/1.1\r\n" -> -1 (disallowed char in path)
"<260-char path>" -> -1 (path overflow)
buf or NULL out returns -1.\r\n returns -1.A-Za-z0-9/?&=._-.memcpy does not NUL-terminate — terminate each field after copying.The request line is where every HTTP attack starts. A bounded, allow-list parser stops the easy ones at the door.
A NUL-terminated buffer containing the first line of an HTTP request.
0 on success, -1 on any malformed input. Output struct fields are written only on success.
No strcpy without length. CRLF required. Allow-list characters only.
#include <stddef.h>
typedef struct {
char method[8];
char path[256];
char version[16];
} http_req_t;
int parse_request_line(const char *buf, http_req_t *out) {
/* TODO */
(void)buf; (void)out;
return -1;
}
Using sscanf without length specifiers. Forgetting NUL terminator. Allowing double-space (potential request smuggling vector).
Method that's exactly 7 chars (fits, since NUL needs slot 8). Path with ? and &. Trailing space before CRLF.
O(n) in the line length.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.