Merge pull request #4646 from jaeminbird/http-truncated-body

http: fail a framed response whose connection is lost before its end
This commit is contained in:
Karl Seguin
2026-11-16 15:51:28 +08:00
committed by GitHub
4 changed files with 54 additions and 0 deletions
+20
View File
@@ -758,3 +758,23 @@
});
}
</script>
<script id=fetch_bad_chunk type=module>
{
// Headers arrive, then the chunked body turns out to be malformed:
// reading it must fail rather than resolve with what was decoded.
const state = await testing.async();
let result;
try {
const response = await fetch('http://127.0.0.1:9582/raw/bad_chunk');
result = 'ok:' + await response.text();
} catch (e) {
result = e.name;
}
state.resolve();
await state.done(() => {
testing.expectEqual('TypeError', result);
});
}
</script>
+21
View File
@@ -1310,3 +1310,24 @@ handler runs, abort() cancels the re-send and no load is delivered.
});
}
</script>
<script id=xhr_bad_chunk type=module>
{
// A malformed chunked body is a network error, not a load.
const state = await testing.async();
const result = await new Promise((resolve) => {
const req = new XMLHttpRequest();
const events = [];
req.onload = () => events.push('load');
req.onerror = () => events.push('error');
req.onloadend = () => resolve(events.join(',') + '|' + req.status + '|' + req.responseText);
req.open('GET', 'http://127.0.0.1:9582/raw/bad_chunk');
req.send();
});
state.resolve();
await state.done(() => {
testing.expectEqual('error|0|', result);
});
}
</script>
+6
View File
@@ -1967,9 +1967,15 @@ fn processOneMessage(self: *Client, msg: http.Handles.MultiMessage, transfer: *T
// this is a normal end-of-body (the connection closure signals the end
// of the response per HTTP/1.1 when there is no Content-Length).
// We must check this before endTransfer, which may reset the easy handle.
// A body framed by Content-Length or chunked encoding has its own end,
// so a connection lost before that end is a truncated response.
const is_conn_close_recv = blk: {
const err = effective_err orelse break :blk false;
if (err != error.RecvError) break :blk false;
if (msg.conn.getResponseHeader("content-length", 0) != null) break :blk false;
if (msg.conn.getResponseHeader("transfer-encoding", 0)) |te| {
if (std.ascii.findIgnoreCase(te.value, "chunked") != null) break :blk false;
}
const hdr = msg.conn.getResponseHeader("connection", 0) orelse break :blk true;
break :blk std.ascii.eqlIgnoreCase(hdr.value, "close");
};
+7
View File
@@ -82,6 +82,11 @@ pub const raw_responses = struct {
.head = "HTTP/1.1 200 OK\r\nContent-Length: 262144\r\n\r\n",
.body = &@as([256 * 1024]u8, @splat('a')),
};
// A chunk size that isn't hex.
pub const bad_chunk: RawResponse = .{
.head = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n",
.body = "ZZ\r\nhello\r\n0\r\n\r\n",
};
// A three-digit status outside 100..599, like LinkedIn's 999.
pub const status_999: RawResponse = .{
.head = "HTTP/1.1 999 Request denied\r\nContent-Type: text/plain\r\nContent-Length: 6\r\n\r\n",
@@ -104,6 +109,8 @@ fn serveRaw(req: *std.http.Server.Request, name: []const u8) !void {
raw_responses.large
else if (std.mem.eql(u8, name, "status_999"))
raw_responses.status_999
else if (std.mem.eql(u8, name, "bad_chunk"))
raw_responses.bad_chunk
else
return req.respond("unknown raw response", .{ .status = .not_found });
try req.server.out.writeAll(r.head);