Compare commits

...

3 Commits

Author SHA1 Message Date
Xavier Roche
95c7d3c044 Route the too-large chunk through the invalid-chunk teardown
Review found the first cut hand-rolled the abort (statuscode/status/finished)
and then fell through into the shared chunk dispatch, which re-ran finalize and
stomped the message; it was only safe by accident. Set chunk_size = -1 instead
and let the existing chunk_size < 0 path tear the transfer down (deletehttp +
deleteaddr + STATUSCODE_INVALID), matching the sibling error handling. Verified
by hand with a lowered cap: the resource is rejected as "Invalid chunk", the
socket is closed, and no partial file is written.

Also fix the test's decoded-vs-raw discriminator: '^40$' never matched the
CRLF-framed size line, and '</body></html>' is split by HTTrack's mirror
comment. Assert no CR (framing decoded) and '</body>' (no truncation).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
2026-07-15 21:43:06 +02:00
Xavier Roche
755481e33b tests: mirror a chunked response (regression for the 2GB cap)
Add a Transfer-Encoding: chunked route to the test server and a crawl test that
mirrors it, asserting the chunk automaton joins the bodies (no leftover chunk
framing) and the new 2GB in-RAM cap stays quiet on ordinary traffic. The cap's
overflow threshold isn't reachable at test scale (it needs gigabytes on the
wire), so this pins the non-regression side; the abort path was verified by
hand with a lowered cap.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
2026-07-15 21:31:02 +02:00
Xavier Roche
83a3af7fc5 Cap the chunked-transfer buffer at 2GB before realloc
In the chunked-transfer automaton, back[i].r.totalsize is a 64-bit signed value
that accumulates the attacker-declared size of each chunk, and the in-memory
branch reallocs to (size_t) totalsize + 1 before the chunk body arrives. With no
upper bound the sum can exceed 2GB across chunks; cast to a 32-bit size_t that
truncates and under-allocates the buffer, so the following chunk data overflows
the heap. The sibling in-memory Content-Length path already caps at INT32_MAX;
apply the same bound here and abort the transfer (refetchable) when it trips.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
2026-07-15 21:15:34 +02:00
4 changed files with 64 additions and 11 deletions

View File

@@ -3310,16 +3310,26 @@ void back_wait(struct_back * sback, httrackp * opt, cache_back * cache,
back[i].chunk_blocksize = -1; /* ending */
back[i].r.totalsize += chunk_size; // noter taille
if (back[i].r.adr != NULL || !back[i].r.is_write) { // Not to disk
back[i].r.adr =
(char *) realloct(back[i].r.adr,
(size_t) back[i].r.totalsize + 1);
if (!back[i].r.adr) {
if (cache->log != NULL) {
hts_log_print(opt, LOG_ERROR,
"not enough memory (" LLintP
") for %s%s",
(LLint) back[i].r.totalsize,
back[i].url_adr, back[i].url_fil);
// totalsize sums attacker-declared chunk sizes; past
// 2GB the (size_t) cast below truncates on 32-bit and
// under-allocates. Mark the chunk invalid so the shared
// error path tears the transfer down.
if (back[i].r.totalsize > INT32_MAX) {
hts_log_print(opt, LOG_WARNING,
"Chunked resource too large for %s%s",
back[i].url_adr, back[i].url_fil);
chunk_size = -1;
} else {
back[i].r.adr = (char *) realloct(
back[i].r.adr, (size_t) back[i].r.totalsize + 1);
if (!back[i].r.adr) {
if (cache->log != NULL) {
hts_log_print(opt, LOG_ERROR,
"not enough memory (" LLintP
") for %s%s",
(LLint) back[i].r.totalsize,
back[i].url_adr, back[i].url_fil);
}
}
}
}

18
tests/55_local-chunked.test Executable file
View File

@@ -0,0 +1,18 @@
#!/bin/bash
# A well-formed Transfer-Encoding: chunked response mirrors intact: the chunk
# automaton (htsback.c) joins the bodies and the 2GB in-RAM cap stays quiet.
# The 76-char run spans a 64-byte chunk boundary (only contiguous once joined);
# a CR anywhere would be leftover framing; '</body>' proves no truncation.
set -euo pipefail
: "${top_srcdir:=..}"
bash "$top_srcdir/tests/local-crawl.sh" --errors 0 \
--found 'chunked/page.html' \
--file-matches 'chunked/page.html' \
'chunk-body chunk-body chunk-body chunk-body chunk-body chunk-body chunk-body' \
--file-matches 'chunked/page.html' '</body>' \
--file-not-matches 'chunked/page.html' $'\r' \
--log-not-found 'too large' \
httrack 'BASEURL/chunked/index.html'

View File

@@ -131,6 +131,7 @@ TESTS = \
51_local-update-codec.test \
52_local-socks5.test \
53_local-proxytrack-cache-corrupt.test \
54_local-update-truncate-purge.test
54_local-update-truncate-purge.test \
55_local-chunked.test
CLEANFILES = check-network_sh.cache

View File

@@ -1215,6 +1215,28 @@ class Handler(SimpleHTTPRequestHandler):
if self.command != "HEAD":
self.wfile.write(body)
def route_chunked_index(self):
self.send_html('\t<a href="page.html">chunked</a>\n')
def route_chunked_page(self):
# Transfer-Encoding: chunked over many small chunks: drives the engine's
# chunk automaton (htsback.c). The mirrored file must equal the joined
# chunk bodies, so the 2GB in-RAM cap doesn't fire on ordinary traffic.
blob = big_html("chunked", "<p>" + "chunk-body " * 300 + "</p>")
self.protocol_version = "HTTP/1.1"
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Transfer-Encoding", "chunked")
self.send_header("Connection", "close")
self.end_headers()
if self.command == "HEAD":
return
step = 64
for off in range(0, len(blob), step):
piece = blob[off : off + step]
self.wfile.write(b"%X\r\n" % len(piece) + piece + b"\r\n")
self.wfile.write(b"0\r\n\r\n")
# Content-Disposition naming: the attachment filename replaces the
# URL-derived name; path components in it are stripped (RFC 2616).
CDISPO_NAMES = {
@@ -1554,6 +1576,8 @@ class Handler(SimpleHTTPRequestHandler):
"/crange206mem/blob.bin": route_crange206mem,
"/size/index.html": route_size_index,
"/size/oversize.bin": route_size_oversize,
"/chunked/index.html": route_chunked_index,
"/chunked/page.html": route_chunked_page,
"/errpage/index.html": route_errpage_index,
"/errpage/good.html": route_errpage_good,
"/errpage/missing.html": route_errpage_missing,