Skip to content

Commit 1311275

Browse files
committed
Be more strict when iterating over parsed headers
(This should be moved down a layer so this kind of validation happens at the parsing time, not where we consume this, but I'm too lazy to look at that now.) Found by fuzzing with http-garden.
1 parent 56e2b1d commit 1311275

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/lib/lwan-lua.c

+12-8
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,21 @@ LWAN_LUA_METHOD(http_headers)
6666
lua_newtable(L);
6767

6868
for (size_t i = 0; i < helper->n_header_start; i++) {
69-
const char *key = helper->header_start[i];
70-
const char *key_end = strchr(key, ':');
69+
const char *header = helper->header_start[i];
70+
const char *next_header = helper->header_start[i + 1];
71+
const char *colon = memchr(header, ':', (size_t)(next_header - header));
7172

72-
if (!key_end)
73-
break;
73+
if (!colon)
74+
continue;
7475

75-
const char *value = key_end + 2;
76-
const char *value_end = helper->header_start[i + 1];
76+
const ptrdiff_t header_len = colon - header;
77+
const ptrdiff_t value_len = next_header - colon - 4;
78+
79+
if (header_len < 0 || value_len < 0)
80+
continue;
7781

78-
lua_pushlstring(L, key, (size_t)(key_end - key));
79-
lua_pushlstring(L, value, (size_t)(value_end - value - 2));
82+
lua_pushlstring(L, header, (size_t)header_len);
83+
lua_pushlstring(L, colon + 2, (size_t)value_len);
8084
lua_rawset(L, -3);
8185
}
8286

src/lib/lwan-request.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -2100,8 +2100,11 @@ void lwan_request_foreach_header_for_cgi(struct lwan_request *request,
21002100
if (!colon)
21012101
continue;
21022102

2103-
const size_t header_len = (size_t)(colon - header);
2104-
const size_t value_len = (size_t)(next_header - colon - 4);
2103+
const ptrdiff_t header_len = colon - header;
2104+
const ptrdiff_t value_len = next_header - colon - 4;
2105+
2106+
if (header_len < 0 || value_len < 0)
2107+
continue;
21052108

21062109
r = snprintf(header_name, sizeof(header_name), "HTTP_%.*s",
21072110
(int)header_len, header);
@@ -2120,7 +2123,7 @@ void lwan_request_foreach_header_for_cgi(struct lwan_request *request,
21202123
continue;
21212124
}
21222125

2123-
cb(header_name, header_len + sizeof("HTTP_") - 1, colon + 2, value_len,
2124-
user_data);
2126+
cb(header_name, (size_t)header_len + sizeof("HTTP_") - 1, colon + 2,
2127+
(size_t)value_len, user_data);
21252128
}
21262129
}

0 commit comments

Comments
 (0)