Skip to content

[#1786] Fix chunked HTTP framing across prefetch/socket boundaries - #1787

Open
kunaalxoxo wants to merge 1 commit into
pgmoneta:mainfrom
kunaalxoxo:fix-http-chunked-framing-1786
Open

kunaalxoxo wants to merge 1 commit into
pgmoneta:mainfrom
kunaalxoxo:fix-http-chunked-framing-1786

Conversation

@kunaalxoxo

@kunaalxoxo kunaalxoxo commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1786

Summary

  • Replaced duplicated prefetch/socket checks in http_read_chunked_body() (src/libpgmoneta/http.c) with unified http_chunk_stream helpers (http_chunk_read_some, http_chunk_read_exact, http_chunk_read_crlf, http_chunk_read_line).
  • Fixes silent framing failure when a trailing \r\n straddles the 1-byte prefetched tail and socket reads, or when read()/SSL_read() splits \r and \n.
  • Added strict RFC 9112 1*HEXDIG parsing in http_parse_chunk_size() (direct hex digit accumulation with SIZE_MAX overflow guarding, BWS + ; chunk extensions, and rejection of empty input, +/- signs, 0x prefixes, and trailing garbage).
  • Added RFC 9112 trailer draining until the terminating empty \r\n line after chunk 0.
  • Added regression tests in test/testcases/test_http.c:
    • test_pgmoneta_http_chunked_split_crlf
    • test_pgmoneta_http_chunked_extensions_and_trailers
    • test_pgmoneta_http_chunked_truncated_trailer_header_rejected (catches trailer truncation regression vs main)
    • Plus negative parser/bounds coverage in test_pgmoneta_http_chunked_invalid_size_rejected (not_hex, 5x, +5, 0x5, empty line, ;ext=1, size_t overflow), test_pgmoneta_http_chunked_oversized_size_line_rejected (4096-byte size line), and test_pgmoneta_http_chunked_truncated_trailer_rejected.
  • Hardened echo_server_thread() against SIGPIPE (SO_NOSIGPIPE / MSG_NOSIGNAL) and early client closes during segmented sends.
  • Added contributor acknowledgement to AUTHORS and doc/manual/en/97-acknowledgement.md.

Testing

Verified in pgmoneta-dev:ubuntu24 (Clang Debug with ASan):

  • clang-format --dry-run --Werror src/libpgmoneta/http.c test/testcases/test_http.c: passed
  • cppcheck --enable=warning --inline-suppr --quiet --template='{id}:{file}:{line}' -I src/include src/libpgmoneta/http.c: 0 warnings
  • cmake --build build -j4: passed
  • ./build/test/pgmoneta-test -m http: 36/36 passed
  • ./build/test/pgmoneta-test -m walbridge: 64/64 passed

Copilot AI balanced review requested due to automatic review settings December 1, 2026 10:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Pranav-error Pranav-error left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Head d1720bc. Verified locally (macOS, ASan Debug): split_crlf and extensions_and_trailers fail on the parent and pass here; a 1-2 byte split at every body offset decodes correctly here (parent fails 17/138); 34/34 in -m http.

  1. The new parser guards aren't covered by the tests. Removing the trailing-garbage check, the isxdigit/endptr checks, or the - 1 in the line bound still passes all 4 new tests. The - 1 one is a 1-byte stack overflow (ASan, http.c:984, on a 4096-byte size line). Can you add cases for 5x, +5 and a 4096-byte chunk-size line?
  2. truncated_trailer_rejected passes on the parent as well, so it doesn't catch this regression. Fine to keep, but please don't count it as one.
  3. In the segmented echo server, send() raises SIGPIPE if the client has closed. On the parent, split_crlf kills the runner (rc 141) instead of reporting FAIL. Can you ignore SIGPIPE or stop at the first failed send?
  4. Nit: strtoul(..., 16) also accepts 0x5, but RFC 9112 chunk-size is HEXDIG only.
  5. Please add yourself to AUTHORS and doc/manual/en/97-acknowledgement.md.

@ecleon26
ecleon26 self-requested a review December 2, 2026 20:42

@ecleon26 ecleon26 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm but it overlaps with #1726 so rebase after it goes in

@kunaalxoxo
kunaalxoxo force-pushed the fix-http-chunked-framing-1786 branch from d1720bc to 3aa5cc1 Compare December 3, 2026 04:22
@kunaalxoxo

Copy link
Copy Markdown
Contributor Author

@Pranav-error Thanks for the thorough review and mutation checks! I've updated the PR (now at 3aa5cc12):

  1. Parser and line-bound test coverage:
    • Expanded test_pgmoneta_http_chunked_invalid_size_rejected to cover 5x, +5, 0x5, an empty chunk-size line (\r\n\r\n), ;ext=1\r\n\r\n, and size_t overflow.
    • Added test_pgmoneta_http_chunked_oversized_size_line_rejected with a 4096-byte chunk-size line, and confirmed under ASan that removing - 1 triggers the 1-byte stack-buffer-overflow on line, and removing the trailing-garbage, empty-hex, or overflow checks fails test_pgmoneta_http_chunked_invalid_size_rejected.
  2. Trailer regression test: Kept test_pgmoneta_http_chunked_truncated_trailer_rejected, removed it from the main-regression list in the PR description, and added test_pgmoneta_http_chunked_truncated_trailer_header_rejected (0\r\nX-Checksum: abc\r\n missing the terminating \r\n), which catches the main trailer bug.
  3. Echo server SIGPIPE: Added echo_send_all() with SO_NOSIGPIPE (when defined) / MSG_NOSIGNAL (when defined) and SIG_IGN fallback, and stopped the segmented loop on the first failed send().
  4. 0x5 nit: Replaced strtoul(..., 16) in http_parse_chunk_size() with direct RFC 9112 1*HEXDIG parsing and SIZE_MAX overflow checking so 0x5, +5, and leading whitespace are rejected uniformly.
  5. Acknowledgements: Added myself to AUTHORS and doc/manual/en/97-acknowledgement.md.

@ecleon26 I'll rebase onto main as soon as #1726 lands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix chunked HTTP framing across prefetch/socket boundaries and validate chunk sizes

4 participants