Skip to content

fix(net): bound connection ingress buffer and enforce upfront parser caps - #6

Merged
vubon merged 1 commit into
mainfrom
fix/resp-ingress-limits
Nov 21, 2026
Merged

vubon merged 1 commit into
mainfrom
fix/resp-ingress-limits

Conversation

@vubon

@vubon vubon commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Summary

This PR resolves Finding H (Step 6) of Architecture Decision Record 6 (improvements/improvement-6-design-decision.md), defending KacheDB against ingress and parser denial-of-service vectors.

Key Deliverables

  1. 64 MiB Unread Ingress Buffer Limit:
    • Enforced across Connection::read_from (epoll), Connection::feed_bytes (io_uring), and decrypted TLS plaintext streams (TlsState::read_and_decrypt).
    • If an incomplete request requires more than 64 MiB of buffer space before parsing completes, the connection is closed immediately with NetError::BufferOverflow.
  2. Two-Pass Incremental CommandFrameDecoder:
    • Introduced in crates/kachedb-proto-resp/src/command_frame.rs.
    • Pass 1 scans framing boundaries and verifies protocol limits with checked arithmetic before allocating memory.
    • Pass 2 materializes argument slices into a SmallVec only after the entire command frame is verified complete.
  3. Upfront Rejection (Pre-Allocation Denial-of-Service Defense):
    • Command Limit: 64 MiB aggregate encoded request size (MAX_COMMAND_BYTES).
    • Argument Size Limit: 2 MiB per bulk argument string (MAX_ARGUMENT_BYTES), preventing memory amplification.
    • Argument Count Limit: 65,536 maximum arguments (MAX_ARGUMENTS).
    • All conversions use checked integer arithmetic (checked_add, checked_mul) to eliminate integer overflow attacks.
  4. Pipelining & Frame Reset:
    • The decoder resets cleanly on frame boundaries, giving each pipelined request an independent 64 MiB budget.
  5. Historical Replay Isolation:
    • parse_command_frame_for_replay allows historical AOF frames to restore without tripping the 2 MiB client argument cap, since AOF frames are already validated and bounded by the journal's 64 MiB record budget.

Verification

  • cargo fmt --all -- --check: PASS (0 diffs)
  • cargo clippy --workspace --all-targets --all-features -- -D warnings: PASS (0 warnings across all 11 crates)
  • cargo test --workspace: PASS (100% test pass rate)
  • prv Staff Review: APPROVE (11/11 dimensions passed)
  • AI-DLC 5-Layer Governance Review: PASS & COMPLIANT

…caps

- Enforce strict 64 MiB unread buffer cap across epoll (read_from), io_uring (feed_bytes), and decrypted TLS streams, returning NetError::BufferOverflow on overflow
- Introduce CommandFrameDecoder in kachedb-proto-resp with two-pass incremental parsing and zero heap allocation during scanning
- Reject oversized requests upfront on header lines using checked arithmetic: 64 MiB command wire cap, 2 MiB per-argument bulk string cap, and 65,536 argument count limit
- Reset decoder state on frame boundaries to give each pipelined command an independent budget
- Isolate historical AOF recovery with parse_command_frame_for_replay to restore large valid records up to 64 MiB without tripping client ingress limits
- Fully resolves Finding H in ADR 6 Step 6
@vubon
vubon merged commit a99debf into main Nov 21, 2026
6 checks passed
@vubon
vubon deleted the fix/resp-ingress-limits branch November 21, 2026 03:21
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