Skip to content

fix(http): parse Content-Length as i64 and cap it before any arithmetic - #3737

Merged
BaseMax merged 2 commits into
mainfrom
fix/http-content-length
Nov 21, 2026
Merged

BaseMax merged 2 commits into
mainfrom
fix/http-content-length

Conversation

@BaseMax

@BaseMax BaseMax commented Nov 21, 2026

Copy link
Copy Markdown
Member

The server parsed Content-Length with str ToInt, which saturates at 2147483647, then added it to the header length in a 32-bit int. Values from about 2^31 up to 15 digits wrapped negative and the first-read path sliced the buffer at a negative offset before the body cap was checked, so one request could crash the server.

Content-Length is now parsed into an i64 with no saturation, and anything over SetMaxBodyBytes (or that would not fit with the header) gets 413 before any length arithmetic, on both read paths. Duplicate Content-Length comparison uses the exact value too. The client chunk size parsers in raw.salam get the same widening, so a hostile chunk size can no longer go negative.

Tested: tests/en/general/net_http_limits now covers 10 to 15 digit values plus a cap at the int maximum (all 413; the server crashed on these before), and net_http_client_safety still passes.

A huge Content-Length overflowed the 32-bit request length and crashed the server. The client chunk size parser gets the same widening.
@BaseMax
BaseMax requested a review from a team as a code owner November 21, 2026 05:32
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 8 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3da0b7dc-e9fb-4d86-b50a-485916031c01
Commits

Reviewing files that changed from the base of the PR and between 3d0dd9e and 7be49b3.

Files ignored due to path filters: 1

  • tests/en/general/net_http_limits.out is excluded by !**/*.out
Files selected for processing: 3

  • std/net/http/ctx_internal.salam
  • std/net/http/raw.salam
  • tests/en/general/net_http_limits.salam
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@BaseMax
BaseMax merged commit 621685b into main Nov 21, 2026
14 checks passed
@BaseMax
BaseMax deleted the fix/http-content-length branch November 21, 2026 05:33
@github-actions

Copy link
Copy Markdown
Contributor
sha=7be49b350c3a9ed17ac8cd27056d4b33b9d2db1a

🤖 Salam AI review

Verdict: ✅ Good to merge

commit 7be49b3 · 4 files · +67 −41 · model salam

Summary

The pull request fixes a denial-of-service in the HTTP server by parsing Content-Length into an i64 instead of a saturating 32-bit int, and by checking the body cap and integer overflow before any length arithmetic on both read paths. The client chunk-size parsers in raw.salam are widened the same way. The change is focused and the new test cases match the described expected statuses.

What changed

std/net/http/ctx_internal.salam gains a _cl_number helper that parses a Content-Length value into an i64 without saturation, and _bad_cl_value, _content_length and _parse_http are widened to use it. A new _body_too_large predicate rejects anything above SetMaxBodyBytes or that would not fit together with the header into a 32-bit int, and both _read_request paths now call it before computing need. std/net/http/raw.salam parses chunk sizes into an i64 (wide) and caps them against the accumulated length during digit parsing. The test file adds 10-15 digit Content-Length cases plus an int-max cap case, all expecting 413.

Review

  • ctx_internal.salam: _cl_number correctly rejects empty, non-digit and over-15-digit inputs by returning -1, and 15 digits of 9s still fits in an i64, so there is no overflow in the parser itself.
  • ctx_internal.salam: _body_too_large checks both cl > _max_body_bytes and head_len + cl >= 2147483647, which prevents the negative-offset slice that caused the crash; the subsequent cl as int is only reached when the sum is known to fit.
  • ctx_internal.salam: the first read path now returns 413 instead of silently ignoring an oversized body, matching the second path.
  • ctx_internal.salam: duplicate Content-Length headers are compared on the exact i64 value, so values that previously saturated to the same int no longer pass.
  • raw.salam: chunk sizes are accumulated in i64 and rejected when they exceed a.len while digits are still being read, preventing a hostile chunk size from going negative before the later size > a.len - src check.
  • The declaration order and visibility of the new private helpers (_cl_number, _body_too_large) are consistent with the rest of the file, and no unused bindings appear to be introduced.

Tests

The added cases exercise 10 through 15 digit values and a cap at the int maximum, all expecting 413, which directly covers the previously crashing inputs. The existing net_http_client_safety path is unaffected because the widening only changes parsing, not the accepted range for well-formed requests.

Risks

The first read path still does not free one (the scratch buffer) on the new 413 early return, but this matches the pre-existing behaviour for that path and one is not owned, so there is no leak. I could not run the compiler or tests in this environment, so the syntax of the patched files was checked only against the documented Salam rules and small isolated snippets; if the full file fails to compile, the first error should be fixed before merging.


⚠️ This review was written automatically by an AI and may contain mistakes. It is a helper for maintainers, not a replacement for a human review: check the code yourself before merging.

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.

1 participant