Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_http_req.c

n_http_req regression: valid requests parse, and the smuggling / malformed / oversized cases are rejected.

n_http_req regression: valid requests parse, and the smuggling / malformed / oversized cases are rejected.

Author
Castagnier Mickael
Version
1.0
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "nilorea/n_log.h"
#include <string.h>
static int failures = 0;
/* Parse a NUL-terminated request literal (its byte length is the wire length). */
static n_http_req_status parse(const char* s, n_http_request* out) {
return n_http_request_parse(s, strlen(s), out);
}
#define WANT(expr, want, msg) \
do { \
if ((expr) != (want)) { \
n_log(LOG_ERR, "FAIL: %s", (msg)); \
failures++; \
} \
} while (0)
int main(void) {
/* a valid GET */
WANT(parse("GET /v1/me?x=1 HTTP/1.1\r\nHost: e\r\n\r\n", &r), N_HTTP_REQ_OK,
"valid GET should parse");
WANT(r.method, N_HTTP_REQ_GET, "method GET");
if (strcmp(r.path, "/v1/me?x=1") != 0) {
n_log(LOG_ERR, "FAIL: GET path");
}
/* a valid POST with a body + an Authorization header */
WANT(parse("POST /v1/send HTTP/1.1\r\nContent-Length: 5\r\n"
"Authorization: Bearer tok\r\n\r\nhello",
&r),
N_HTTP_REQ_OK, "valid POST should parse");
WANT(r.method, N_HTTP_REQ_POST, "method POST");
if (r.content_length != 5) {
n_log(LOG_ERR, "FAIL: content_length");
}
if (strcmp(r.authorization, "Bearer tok") != 0) {
n_log(LOG_ERR, "FAIL: authorization capture");
}
/* rejections */
WANT(parse("PUT / HTTP/1.1\r\n\r\n", &r), N_HTTP_REQ_ERROR,
"unsupported method rejected");
WANT(parse("GET http://e/x HTTP/1.1\r\n\r\n", &r), N_HTTP_REQ_ERROR,
"non-origin-form target rejected");
WANT(parse("GET /a#frag HTTP/1.1\r\n\r\n", &r), N_HTTP_REQ_ERROR,
"fragment in target rejected");
WANT(parse("GET / HTTP/2.0\r\n\r\n", &r), N_HTTP_REQ_ERROR,
"unsupported version rejected");
WANT(parse("POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n", &r),
N_HTTP_REQ_ERROR, "Transfer-Encoding rejected (smuggling)");
WANT(parse("POST / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 2\r\n"
"\r\n",
&r),
N_HTTP_REQ_ERROR, "duplicate Content-Length rejected (smuggling)");
WANT(parse("POST / HTTP/1.1\r\nContent-Length: abc\r\n\r\n", &r),
N_HTTP_REQ_ERROR, "non-numeric Content-Length rejected");
/* incomplete: no CRLFCRLF yet, still under the cap -> read more */
WANT(parse("GET / HTTP/1.1\r\nHost: e", &r), N_HTTP_REQ_INCOMPLETE,
"partial head is INCOMPLETE");
/* shorter than the CRLFCRLF terminator: still just "read more" */
"empty buffer is INCOMPLETE");
"buffer shorter than CRLFCRLF is INCOMPLETE");
/* NULL args */
WANT(n_http_request_parse(NULL, 0, &r), N_HTTP_REQ_ERROR, "NULL buf");
/* header values and names: control bytes and non-token names refused */
WANT(parse("GET / HTTP/1.1\r\nX-A: a\nb\r\n\r\n", &r), N_HTTP_REQ_ERROR,
"bare LF inside a header value rejected");
WANT(parse("GET / HTTP/1.1\r\nX-A: a\rb\r\n\r\n", &r), N_HTTP_REQ_ERROR,
"bare CR inside a header value rejected");
/* 28 bytes: the literal holds a NUL, so strlen() cannot measure it */
WANT(n_http_request_parse("GET / HTTP/1.1\r\nX-A: a\0b\r\n\r\n", 28, &r),
N_HTTP_REQ_ERROR, "NUL inside a header value rejected");
WANT(parse("GET / HTTP/1.1\r\nX(A): a\r\n\r\n", &r), N_HTTP_REQ_ERROR,
"separator in a header name rejected");
WANT(parse("GET / HTTP/1.1\r\n folded: a\r\n\r\n", &r), N_HTTP_REQ_ERROR,
"obs-fold continuation line rejected");
WANT(parse("GET / HTTP/1.1\r\nX-A: caf\xc3\xa9\t ok\r\n\r\n", &r),
N_HTTP_REQ_OK, "obs-text and tabs inside a value accepted");
/* the default entry point keeps refusing HEAD (GET and POST only) */
WANT(parse("HEAD / HTTP/1.1\r\n\r\n", &r), N_HTTP_REQ_ERROR,
"HEAD refused by n_http_request_parse");
/* header table and lookup */
{
const char* req =
"GET /x HTTP/1.0\r\nHost: www.example\r\ncookie: a=1\r\n"
"X-Empty:\r\nCOOKIE: b=2 \r\n\r\n";
const char* v;
size_t vlen = 99;
WANT(parse(req, &r), N_HTTP_REQ_OK, "header table request parses");
WANT(r.version_minor, 0, "HTTP/1.0 version_minor");
WANT(r.nb_headers, (size_t)4, "four headers recorded");
v = n_http_request_header(&r, req, "HOST", &vlen);
if (!v || vlen != 11 || memcmp(v, "www.example", 11) != 0) {
n_log(LOG_ERR, "FAIL: Host lookup");
}
v = n_http_request_header(&r, req, "Cookie", &vlen);
if (!v || vlen != 3 || memcmp(v, "a=1", 3) != 0) {
n_log(LOG_ERR, "FAIL: first Cookie lookup");
}
WANT(n_http_request_header_count(&r, req, "cookie"), (size_t)2,
"two Cookie lines counted");
v = n_http_request_header(&r, req, "x-empty", &vlen);
WANT(v != NULL && vlen == 0, 1, "empty header value found, length 0");
WANT(n_http_request_header(&r, req, "Referer", &vlen) == NULL, 1,
"absent header is NULL");
WANT(vlen, (size_t)0, "absent header length is 0");
WANT(n_http_request_header(&r, req, "Hos", NULL) == NULL, 1,
"a name prefix does not match");
WANT(n_http_request_header(NULL, req, "Host", NULL) == NULL, 1,
"NULL request");
WANT(n_http_request_header_count(&r, NULL, "Host"), (size_t)0,
"NULL buffer counts 0");
}
/* n_http_request_parse_ex: methods, body cap, Host rule */
{
lim.body_max = 22LL * 1024 * 1024;
lim.require_host = 1;
#define PARSE_EX(s) n_http_request_parse_ex((s), strlen(s), &lim, &r)
WANT(PARSE_EX("HEAD /a HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_OK,
"HEAD accepted by parse_ex when allowed");
WANT(r.method, N_HTTP_REQ_HEAD, "method HEAD");
WANT(r.version_minor, 1, "HTTP/1.1 version_minor");
WANT(PARSE_EX("POST /u HTTP/1.1\r\nHost: e\r\nContent-Length: 23068672\r\n\r\n"),
N_HTTP_REQ_OK, "22 MiB body accepted under a 22 MiB cap");
WANT(r.content_length, 23068672LL, "22 MiB content_length");
WANT(PARSE_EX("POST /u HTTP/1.1\r\nHost: e\r\nContent-Length: 23068673\r\n\r\n"),
N_HTTP_REQ_ERROR, "body one byte over the cap rejected");
WANT(PARSE_EX("GET / HTTP/1.1\r\n\r\n"), N_HTTP_REQ_ERROR,
"HTTP/1.1 without Host rejected when required");
WANT(PARSE_EX("GET / HTTP/1.1\r\nHost: a\r\nHost: b\r\n\r\n"),
N_HTTP_REQ_ERROR, "two Host headers rejected when required");
WANT(PARSE_EX("GET / HTTP/1.0\r\n\r\n"), N_HTTP_REQ_OK,
"HTTP/1.0 without Host accepted");
WANT(PARSE_EX("PUT / HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_ERROR,
"PUT still rejected by parse_ex");
WANT(PARSE_EX("POST / HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_ERROR,
"method outside the allowed set rejected");
lim.methods = 0;
WANT(PARSE_EX("GET / HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_ERROR,
"empty method set is an error");
WANT(PARSE_EX("GET / HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_ERROR,
"body cap above the limit is an error");
lim.body_max = -1;
WANT(PARSE_EX("GET / HTTP/1.1\r\nHost: e\r\n\r\n"), N_HTTP_REQ_ERROR,
"negative body cap is an error");
#undef PARSE_EX
WANT(n_http_request_parse_ex("GET / HTTP/1.1\r\n\r\n", 18, NULL, &r),
N_HTTP_REQ_ERROR, "NULL limits");
}
if (failures) {
n_log(LOG_ERR, "ex_http_req: %d failure(s)", failures);
return 1;
}
n_log(LOG_NOTICE, "ex_http_req: all checks passed");
return 0;
}
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
#define PARSE_EX(s)
static n_http_req_status parse(const char *s, n_http_request *out)
Definition ex_http_req.c:36
#define WANT(expr, want, msg)
Definition ex_http_req.c:40
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
int require_host
non-zero: an HTTP/1.1 request must carry exactly one Host header and an HTTP/1.0 one at most one (RFC...
Definition n_http_req.h:105
long long body_max
largest accepted Content-Length, 0 to N_HTTP_REQ_BODY_LIMIT_MAX
Definition n_http_req.h:102
char authorization[256u+1]
Definition n_http_req.h:122
unsigned methods
accepted methods, an OR of N_HTTP_REQ_METHOD_BIT(); any other method is an error
Definition n_http_req.h:100
char path[2048u+1]
Definition n_http_req.h:119
size_t nb_headers
Definition n_http_req.h:124
long long content_length
Definition n_http_req.h:120
n_http_req_method method
Definition n_http_req.h:118
#define N_HTTP_REQ_METHOD_BIT(m)
Bit of a method in n_http_req_limits.methods.
Definition n_http_req.h:84
n_http_req_status n_http_request_parse_ex(const char *buf, size_t len, const n_http_req_limits *limits, n_http_request *out)
parse an HTTP/1.1 request head with caller-chosen limits
Definition n_http_req.c:110
n_http_req_status
Result of n_http_request_parse.
Definition n_http_req.h:130
const char * n_http_request_header(const n_http_request *req, const char *buf, const char *name, size_t *value_len)
find a header of a parsed request
Definition n_http_req.c:253
n_http_req_status n_http_request_parse(const char *buf, size_t len, n_http_request *out)
parse an HTTP/1.1 request head from [buf, buf+len)
Definition n_http_req.c:245
size_t n_http_request_header_count(const n_http_request *req, const char *buf, const char *name)
count the headers of a parsed request that carry a name
Definition n_http_req.c:271
#define N_HTTP_REQ_BODY_LIMIT_MAX
Highest body cap n_http_request_parse_ex() accepts (1 GiB).
Definition n_http_req.h:72
@ N_HTTP_REQ_INCOMPLETE
no CRLFCRLF yet, still under the cap
Definition n_http_req.h:132
@ N_HTTP_REQ_ERROR
malformed / oversized / unsupported
Definition n_http_req.h:133
@ N_HTTP_REQ_OK
a complete, valid head was parsed
Definition n_http_req.h:131
@ N_HTTP_REQ_POST
Definition n_http_req.h:79
@ N_HTTP_REQ_GET
Definition n_http_req.h:78
@ N_HTTP_REQ_HEAD
Definition n_http_req.h:80
Limits for n_http_request_parse_ex().
Definition n_http_req.h:97
A parsed request head.
Definition n_http_req.h:117
Pure, strict, bounded parser for an HTTP/1.1 request head.
Generic log system.