Skip to content

[pull] master from koajs:master#150

Merged
pull[bot] merged 3 commits into
backup999:masterfrom
koajs:master
May 21, 2026
Merged

[pull] master from koajs:master#150
pull[bot] merged 3 commits into
backup999:masterfrom
koajs:master

Conversation

@pull
Copy link
Copy Markdown

@pull pull Bot commented May 21, 2026

See Commits and Changes for more details.


Created by pull[bot] (v2.0.0-alpha.4)

Can you help keep this open source service alive? 💖 Please sponsor : )

tejgokani and others added 3 commits May 21, 2026 15:18
## Problem

`ctx.request.length` uses `~~len` (double bitwise NOT) to parse the
Content-Length header, which truncates values to a signed 32-bit
integer. For Content-Length values exceeding 2,147,483,647 bytes (~2GB),
the result silently wraps to incorrect values:

```js
~~'2147483648'  → -2147483648  (sign flip)
~~'4294967296'  → 0
~~'10000000000' → 1410065408   (garbage)
```

This affects any middleware using `ctx.request.length` for upload size
enforcement, rate limiting, or request body pre-allocation. In the era
of large file uploads and video processing APIs, this is a realistic
scenario.

## Solution

Replace `~~len` with `Number.parseInt(len, 10)`:

- Replace ~~len (32-bit truncation) with Number.parseInt(len, 10)
- Handles arbitrarily large Content-Length values up to
Number.MAX_SAFE_INTEGER
- Preserves existing behavior for values under 2GB, including zero
- Add comprehensive test cases for overflow boundaries and edge cases

Fixes integer overflow where ~~'2147483648' returned -2147483648 instead
of the correct value. Now correctly parses large file uploads and
returns undefined for unparseable values, consistent with existing
patterns.

## Test Coverage

Added 6 comprehensive test cases covering:
- Zero value (edge case)
- Content-Length > 2GB (2147483648, 10000000000) — new capability
- Floating-point strings ('10.5') — truncation behavior preserved
- Non-numeric strings ('invalid') — returns undefined
- Empty string — already guarded by if check

All existing tests pass (439/439). No breaking changes.

## Checklist

- [x] I have ensured my pull request is not behind the main or master
branch of the original repository.
- [x] I have rebased all commits where necessary so that reviewing this
pull request can be done without having to merge it first.
- [x] I have written a commit message that passes commitlint linting.
- [x] I have ensured that my code changes pass linting tests.
- [x] I have ensured that my code changes pass unit tests.
- [x] I have described my pull request and the reasons for code changes
along with context if necessary.
Node.js 24 ships with a recent npm, removing the need for the
"Install npm@latest" step which was causing CI failures.
@pull pull Bot locked and limited conversation to collaborators May 21, 2026
@pull pull Bot added the ⤵️ pull label May 21, 2026
@pull pull Bot merged commit 0885f13 into backup999:master May 21, 2026
3 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants