Skip to content

fix(aver-scope-shim): stream proxy bodies, harden scope injection#5

Open
5queezer wants to merge 1 commit into
masterfrom
fix/shim-streaming
Open

fix(aver-scope-shim): stream proxy bodies, harden scope injection#5
5queezer wants to merge 1 commit into
masterfrom
fix/shim-streaming

Conversation

@5queezer

Copy link
Copy Markdown
Owner

Summary

Fixes all findings from the aver-scope-shim code review. The proxy moves from main.rs into a testable aver_scope_shim::proxy library module; main.rs is now CLI parsing + startup wiring only.

Findings → fixes

  1. MAJOR — store-and-forward broke MCP streaming / OOM vector (main.rs buffered request via to_bytes(body, usize::MAX) and response via .bytes().await): bodies now stream in both directions (reqwest::Body::wrap_stream out, axum::body::Body::from_stream in). SSE /text/event-stream responses reach the client incrementally; shim memory stays bounded. Request bodies are hard-capped at 16 MiB → 413 (up-front via Content-Length when known, mid-stream tripwire for chunked). Response bodies are deliberately not capped — SSE streams are long-lived. Added reqwest connect_timeout (10 s) plus a per-request deadline (30 s, connect→response headers → 504 on expiry); no total timeout, so it can't kill open SSE channels.
  2. MINOR — empty --scope "" injected an empty header: derive_scope now filters empty/whitespace CLI overrides exactly like the AVER_DEFAULT_SCOPE path (falls through to git/env/global).
  3. MINOR — scope validated per-request with 502: the scope is validated as a legal HeaderValue once at startup; a misconfigured scope now fails fast with a clear error.
  4. MINOR — /mcp/mcp double path: join_upstream collapses a duplicated /mcp prefix (incl. /mcp/ and query-string variants), so the shim URL works with or without a trailing /mcp. Contract also documented in --upstream help text.
  5. TESTS — zero HTTP-path coverage: new tests/proxy_integration.rs spins up real in-process upstream + shim servers on ephemeral ports (offline, deterministic).

Test evidence

cargo test -p aver-scope-shim21 passed, 0 failed:

  • proxy_integration (9): scope header injection; no-scope passthrough (and client-supplied X-Aver-Scope is stripped, never trusted); configured scope replaces forged client header; SSE streams incrementally (upstream sends chunk 1, parks on a Notify; test asserts the client reads chunk 1 before releasing chunk 2 — would deadlock under store-and-forward); 413 for over-cap body with known Content-Length; 413 for over-cap chunked upload (mid-stream cap); 1 MiB body passes through intact; upstream hang → 504; /mcp, /mcp/, and / all reach upstream /mcp.
  • scope_derivation (8, incl. 1 new): empty/whitespace --scope falls through like empty env.
  • lib unit tests (4, new): join_upstream path normalization, scope header-value validation.

cargo fmt clean; cargo clippy -p aver-scope-shim --all-targets -- -D warnings clean.

Notes

  • Hard rules honored: tests deterministic/offline, no #[ignore], no doc/adr/*.md edits, no autoresearch.jsonl changes.
  • New direct dep: futures-util 0.3 (already in the tree transitively); tokio gains time/sync features.
  • Known edge (out of scope): an upstream that answers before reading an over-cap chunked upload can surface 502 instead of 413 — the early response wins the race. Not a regression; previously everything was buffered.

- Stream request/response bodies end-to-end instead of store-and-forward:
  SSE (MCP streamable HTTP) responses now reach clients incrementally and
  shim memory stays bounded. Request bodies are capped at 16 MiB (413);
  response bodies are never capped.
- Add upstream deadline (30 s to response headers) plus 10 s connect
  timeout so an upstream hang no longer hangs the shim.
- Filter empty/whitespace --scope values like the env-var path, so an
  empty X-Aver-Scope header is never injected.
- Validate the scope as a legal HTTP header value once at startup instead
  of failing per-request with 502.
- Collapse a duplicated /mcp path prefix so a shim URL + /mcp is not
  forwarded to /mcp/mcp.

The proxy moves from main.rs into a testable aver_scope_shim::proxy
module; main.rs is CLI/startup wiring only. Adds HTTP integration tests
against an in-process upstream: header injection, no-scope passthrough
(client header stripped), incremental SSE streaming, 413 on known-length
and chunked uploads over the cap, upstream-hang 504, and /mcp collapsing.
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@5queezer, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 36 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 043f19e1-d573-4793-8de9-69e626a91cdb

📥 Commits

Reviewing files that changed from the base of the PR and between f84fd7e and 76d8c97.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • crates/aver-scope-shim/Cargo.toml
  • crates/aver-scope-shim/src/lib.rs
  • crates/aver-scope-shim/src/main.rs
  • crates/aver-scope-shim/src/proxy.rs
  • crates/aver-scope-shim/tests/proxy_integration.rs
  • crates/aver-scope-shim/tests/scope_derivation.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/shim-streaming

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

Comment @coderabbitai help to get the list of available commands.

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