Skip to content

fix(security): cap the pre-auth body buffer in ServiceSignature - #894

Merged
fclairamb merged 2 commits into
mainfrom
fix/security-signed-body-cap
Nov 16, 2026
Merged

fclairamb merged 2 commits into
mainfrom
fix/security-signed-body-cap

Conversation

@fclairamb

Copy link
Copy Markdown
Owner

What

Fixes finding 3 of the security review #892 (unbounded pre-auth body buffering).

ServiceSignature runs before authentication on the public /api/v1/orgs/:org/entitlements group. readAndRestoreBody did an uncapped io.ReadAll(req.Body) whenever X-SP-Signature was present (HasSignature only checks the header is non-empty). Any anonymous caller could send that header plus an arbitrarily large body and make the server buffer it fully in memory before the signature check rejected the request — a memory-exhaustion vector on directly-exposed instances. There is no global body cap (limits are per-handler) and ReadHeaderTimeout does not bound the body.

Fix

  • Wrap the read in http.MaxBytesReader with a new maxServiceSignedBodyBytes constant (4 MiB — generous for entitlements JSON writes).
  • An oversized body fails the read and collapses into the same generic 401 as every other signature rejection (reason only in the log), keeping the middleware's single-answer contract.
  • Doc comments updated to state the cap and why it is load-bearing.

No wire-contract change: servicesig canonical string, headers and verification order are untouched.

Tests

New internal/middleware/servicesig_test.go (internal package, reaches the unexported service marker and the cap constant):

  • valid signed request → trusted service, body handed back byte-for-byte
  • no signature header → pass-through, not service-authorized
  • tampered body → 401, handler never runs
  • validly signed oversized body (cap+1) → 401 (would pass without the cap — the regression test)
  • body exactly at the cap → accepted (inclusive boundary, no off-by-one)
  • bodyless signed request → accepted

go test ./internal/middleware/ ./internal/servicesig/ green, golangci-lint run ./internal/middleware/... 0 issues, go build ./... OK.

Refs #892

readAndRestoreBody buffered the whole request body with io.ReadAll before
the signature was verified, on the public /orgs/:org/entitlements routes.
HasSignature only checks the header is present, so any anonymous caller
could force an arbitrarily large allocation (memory exhaustion).

Wrap the read in http.MaxBytesReader (4 MiB, generous for entitlements
JSON). An oversized body fails the read and collapses into the same
generic 401 as every other signature rejection.

Refs #892 (finding 3)
@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.71%. Comparing base (733ce1e) to head (405392d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #894      +/-   ##
==========================================
- Coverage   90.71%   90.71%   -0.01%     
==========================================
  Files        1370     1370              
  Lines      140857   140857              
==========================================
- Hits       127778   127775       -3     
- Misses      13070    13073       +3     
  Partials        9        9              
Files with missing lines Coverage Δ
server/internal/middleware/auth.go 90.50% <100.00%> (+1.35%) ⬆️

... and 26 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@fclairamb
fclairamb merged commit c4da4c5 into main Nov 16, 2026
38 checks passed
@fclairamb
fclairamb deleted the fix/security-signed-body-cap branch November 16, 2026 00:34
fclairamb added a commit that referenced this pull request Nov 16, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.51.0](v0.50.2...v0.51.0)
(2026-11-16)


### Features

* Pingdom check importer, insights period selector, job retry log levels
([#904](#904))
([7914164](7914164))


### Bug Fixes

* **security:** bound status page historyDays, mask config tokens in
replay, JS websocket redirect policy, docs JWT placeholders
([#903](#903))
([6b432e0](6b432e0))
* **security:** cap the pre-auth body buffer in ServiceSignature
([#894](#894))
([c4da4c5](c4da4c5)),
closes [#892](#892)
* **security:** cross-check SAML NameID namespace (GO-2026-6909)
([#896](#896))
([df0d7b6](df0d7b6))
* **security:** treat known dev JWT secrets as placeholders
([#895](#895))
([1cad98a](1cad98a)),
closes [#892](#892)

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant