[PR #509] [MERGED] fix(diagnostics): bound the rejection-announce caches + unify CSRF 403 schema #4650

Closed
opened 2026-10-22 04:09:28 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/509
Author: @LukaTheHero
Created: 5/7/2026
Status: ✅ Merged
Merged: 5/7/2026
Merged by: @LukaTheHero

Base: main ← Head: fix/bounded-rejection-cache-and-csrf-field-consistency


📝 Commits (1)

  • 1be71cc fix(diagnostics): bound the rejection-announce caches and unify CSRF 403 schema

📊 Changes

2 files changed (+24 additions, -2 deletions)

View changed files

📝 packages/server/src/config/cors-config.ts (+9 -0)
📝 packages/server/src/middleware/csrf-protection.ts (+15 -2)

📄 Description

Summary

Two follow-ups from CodeRabbit's post-merge review of #507:

  1. Bound the one-shot log-throttle Sets in csrf-protection.ts and cors-config.ts. They were keyed by attacker-controlled strings (Origin / Referer values) so a long-lived client streaming unique values could grow process memory without bound. Both sites now hold at most 2048 entries and drop the oldest when full (Set's insertion order acts as a FIFO). The rare case of a fresh origin from a long-lived attacker eventually re-logging once is an acceptable trade-off for unbounded growth.
  2. Normalise the CSRF 403 body schema. The "Referer not trusted" branch was returning a referer field while the sibling branches returned origin. Clients parsing the body had to handle both keys. Standardised on origin (the error string still names "Referer" so the operator knows which header carried the offending value).

Why

Both items were flagged by CodeRabbit's second review pass after #507 merged:

  • Bounded cache (review comment) — flagged on the CSRF site only, but the same exact pattern existed in cors-config.ts. Fixing both for consistency rather than waiting for the next review round.
  • Schema consistency (review comment) — minor but real footgun for any client doing programmatic 403 parsing.

What changed

  • packages/server/src/middleware/csrf-protection.ts — added MAX_ANNOUNCED_REJECTED_ORIGINS = 2048 cap with FIFO eviction in announceRejectedOrigin. Renamed the 403 referer field to origin for the Referer-not-trusted branch.
  • packages/server/src/config/cors-config.ts — same cap + FIFO eviction pattern in the cross-origin announce site (proactive, not flagged but identical code).

Notes

  • No security or behaviour change beyond what's described above. The cap of 2048 is large enough that a normally-functioning install will never reach it (legitimate origins per process are typically a handful); it kicks in only under adversarial flooding.
  • Eviction is FIFO via Set.values().next() (Set preserves insertion order in JS). Could be upgraded to LRU later if we ever see real-world recycling patterns, but that's overkill for a log-throttle cache.
  • Builds clean against current main (rebased after the unrelated base-provider.ts / context-fit.test.ts merge).

Test plan

  • Manually verify the CSRF 403 body for a referer-only request now returns origin: "<value>" and not referer: "<value>". (Easiest: hit any POST to /api/... from a browser context where the page strips Origin but leaves Referer; or curl with --referer https://other.example and no Origin.)
  • Manually verify the CORS rejection log line ([cors] Rejected cross-origin request from …) still fires for cross-origin requests not in CORS_ORIGINS.
  • Manually verify the CSRF rejection log line ([csrf] Rejected request: Origin '…' is not in the trusted list. …) still fires for requests whose Origin is unknown.
  • Sanity-check that the bounded cache doesn't break the throttle: hit Marinara from the same untrusted origin twice in a row; confirm the warning fires once, not twice.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed potential unbounded memory growth by implementing bounded logging for rejected requests. Oldest entries are automatically removed when capacity is exceeded.
    • Added rate-limiting to prevent excessive security-related warning logs, improving system stability and reducing noise.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/509 **Author:** [@LukaTheHero](https://github.com/LukaTheHero) **Created:** 5/7/2026 **Status:** ✅ Merged **Merged:** 5/7/2026 **Merged by:** [@LukaTheHero](https://github.com/LukaTheHero) **Base:** `main` ← **Head:** `fix/bounded-rejection-cache-and-csrf-field-consistency` --- ### 📝 Commits (1) - [`1be71cc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1be71cc71c512566db4fdbe9767a281d4db80b60) fix(diagnostics): bound the rejection-announce caches and unify CSRF 403 schema ### 📊 Changes **2 files changed** (+24 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/config/cors-config.ts` (+9 -0) 📝 `packages/server/src/middleware/csrf-protection.ts` (+15 -2) </details> ### 📄 Description ## Summary Two follow-ups from CodeRabbit's post-merge review of #507: 1. **Bound the one-shot log-throttle Sets** in `csrf-protection.ts` and `cors-config.ts`. They were keyed by attacker-controlled strings (Origin / Referer values) so a long-lived client streaming unique values could grow process memory without bound. Both sites now hold at most 2048 entries and drop the oldest when full (Set's insertion order acts as a FIFO). The rare case of a fresh origin from a long-lived attacker eventually re-logging once is an acceptable trade-off for unbounded growth. 2. **Normalise the CSRF 403 body schema.** The "Referer not trusted" branch was returning a `referer` field while the sibling branches returned `origin`. Clients parsing the body had to handle both keys. Standardised on `origin` (the error string still names "Referer" so the operator knows which header carried the offending value). ## Why Both items were flagged by CodeRabbit's second review pass after #507 merged: - **Bounded cache** ([review comment](https://github.com/Pasta-Devs/Marinara-Engine/pull/507#discussion_r3199802801)) — flagged on the CSRF site only, but the same exact pattern existed in `cors-config.ts`. Fixing both for consistency rather than waiting for the next review round. - **Schema consistency** ([review comment](https://github.com/Pasta-Devs/Marinara-Engine/pull/507#discussion_r3199802808)) — minor but real footgun for any client doing programmatic 403 parsing. ## What changed - `packages/server/src/middleware/csrf-protection.ts` — added `MAX_ANNOUNCED_REJECTED_ORIGINS = 2048` cap with FIFO eviction in `announceRejectedOrigin`. Renamed the 403 `referer` field to `origin` for the Referer-not-trusted branch. - `packages/server/src/config/cors-config.ts` — same cap + FIFO eviction pattern in the cross-origin announce site (proactive, not flagged but identical code). ## Notes - No security or behaviour change beyond what's described above. The cap of 2048 is large enough that a normally-functioning install will never reach it (legitimate origins per process are typically a handful); it kicks in only under adversarial flooding. - Eviction is FIFO via `Set.values().next()` (Set preserves insertion order in JS). Could be upgraded to LRU later if we ever see real-world recycling patterns, but that's overkill for a log-throttle cache. - Builds clean against current `main` (rebased after the unrelated `base-provider.ts` / `context-fit.test.ts` merge). ## Test plan - [x] Manually verify the CSRF 403 body for a referer-only request now returns `origin: "<value>"` and not `referer: "<value>"`. (Easiest: hit any POST to `/api/...` from a browser context where the page strips Origin but leaves Referer; or curl with `--referer https://other.example` and no Origin.) - [x] Manually verify the CORS rejection log line (`[cors] Rejected cross-origin request from …`) still fires for cross-origin requests not in `CORS_ORIGINS`. - [x] Manually verify the CSRF rejection log line (`[csrf] Rejected request: Origin '…' is not in the trusted list. …`) still fires for requests whose Origin is unknown. - [x] Sanity-check that the bounded cache doesn't break the throttle: hit Marinara from the same untrusted origin twice in a row; confirm the warning fires once, not twice. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed potential unbounded memory growth by implementing bounded logging for rejected requests. Oldest entries are automatically removed when capacity is exceeded. * Added rate-limiting to prevent excessive security-related warning logs, improving system stability and reducing noise. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:09:28 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4650
No description provided.