LibreChat/packages/api/src/middleware
Danny Avila 09da878a47
🚰 fix: Throttle Shared Link Reads and Aggregate Audit Findings (#15605)
* 🪵 fix: Aggregate Audit Findings and Rate Limit Shared Link Reads

Audit-mode content filter rules wrote one `logger.info` per matching
fragment, and inspection deliberately continues past every finding. A
shared conversation full of distinct matching strings therefore produced
one log write per fragment, regenerated on every retrieval of
`GET /api/share/:shareId` — a public, unauthenticated route with no
limiter. The 4,096-entry inspection dedupe set is a memory bound, not an
event budget: once full, further values are inspected and logged again.

Audit metadata carries no inspected text — only rule, source, field and
provenance — so thousands of those writes were byte-identical lines.
Findings are now counted inside an aggregation scope and reported once
per distinct key with an `occurrences` count when the scope ends:

- `assertModelBoundContent` opens a scope when audit rules are configured,
  bounding one inspection pass (a single message's fragments).
- `assertConversationImportContentAllowed` opens the outer scope for
  import and shared-link snapshots, so per-message inspections nest into
  one report instead of one per message.
- `createShareContentPreflight` spans the shared-file metadata pass too.

Nested scopes reuse the outermost aggregation, and outside any scope the
previous immediate write is kept.

`GET /api/share/:shareId` re-inspects the whole snapshot on every request,
so it now carries IP and user limiters like the neighbouring fork route
(`SHARE_IP_MAX`/`SHARE_IP_WINDOW`, `SHARE_USER_MAX`/`SHARE_USER_WINDOW`,
`SHARE_VIOLATION_SCORE`); the user limiter skips anonymous viewers.

* 🔒 fix: Address Codex Findings on Share Audit Aggregation

- The aggregation key joined rule fields with a space, but the filter
  schema allows spaces in custom pattern ids and labels, so
  `{id: 'a', label: 'b c'}` and `{id: 'a b', label: 'c'}` collided on the
  same source/field/provenance and merged two rules' counts under one
  identity. Serialize the tuple instead.
- `SHARE_VIOLATION_SCORE` left unset reached `logViolation` as
  `undefined`, which selects that function's default score of 1 rather
  than the documented zero. With default ban settings a viewer refreshing
  a rate-limited shared link would be banned after 20 rejections. Default
  the score to 0 in code, not only in `.env.example`.
- `removePorts` returns a full IPv6 address, so an attacker holding a
  prefix could rotate the host portion for a fresh bucket on every
  request — and for anonymous viewers the IP limiter is the only bound.
  Derive the key through `ipKeyGenerator` so IPv6 clients group by /56.
2026-09-04 12:38:34 -04:00
..
__tests__ 🔐 fix: Preserve Structured JWT Auth Context (#14652) 2026-08-06 08:12:00 -04:00
access.spec.ts 🧲 fix: Anchor Memory Partitions to Accessible Agents (#15475) 2026-09-01 18:37:52 -04:00
access.ts 🧲 fix: Anchor Memory Partitions to Accessible Agents (#15475) 2026-09-01 18:37:52 -04:00
admin.spec.ts
admin.ts
auth.spec.ts 🔐 fix: Preserve Structured JWT Auth Context (#14652) 2026-08-06 08:12:00 -04:00
auth.ts 🔐 fix: Preserve Structured JWT Auth Context (#14652) 2026-08-06 08:12:00 -04:00
balance.spec.ts
balance.ts ⏱️ feat: Run Scheduled Chats Through Durable Agent Triggers (#14939) 2026-08-20 11:51:30 -04:00
capabilities.integration.spec.ts
capabilities.spec.ts 🪢 feat: show Langfuse session link in shared chats (#15273) 2026-08-27 14:29:57 -04:00
capabilities.ts 🪢 feat: show Langfuse session link in shared chats (#15273) 2026-08-27 14:29:57 -04:00
checkBalance.spec.ts
checkBalance.ts
concurrency.ts
contentFilter.spec.ts 🛂 feat: Filter Model-Bound Content by Source (#14425) 2026-08-21 22:43:32 -04:00
contentFilter.ts 🛂 feat: Filter Model-Bound Content by Source (#14425) 2026-08-21 22:43:32 -04:00
email.spec.ts
email.ts
error.spec.ts 🪪 fix: Preserve Stored Access Token Expiry Over ID Token Exp (#14982) 2026-08-18 22:02:33 -04:00
error.ts 🪪 fix: Preserve Stored Access Token Expiry Over ID Token Exp (#14982) 2026-08-18 22:02:33 -04:00
feedback.spec.ts 🙈 feat: Config Option to Hide Response Feedback Buttons (#15085) 2026-08-21 11:16:44 -04:00
feedback.ts 🙈 feat: Config Option to Hide Response Feedback Buttons (#15085) 2026-08-21 11:16:44 -04:00
generationRetry.spec.ts 🪪 fix: Admit Confirmed Generation Retries Before Message Limits (#15341) 2026-08-29 13:41:26 -04:00
generationRetry.ts 🪪 fix: Admit Confirmed Generation Retries Before Message Limits (#15341) 2026-08-29 13:41:26 -04:00
index.ts 🔐 feat: Add M2M Authentication for Agent Management (#15555) 2026-09-03 20:07:00 -04:00
json.spec.ts
json.ts
management.database.spec.ts 🔐 feat: Add M2M Authentication for Agent Management (#15555) 2026-09-03 20:07:00 -04:00
management.spec.ts 🔐 feat: Add M2M Authentication for Agent Management (#15555) 2026-09-03 20:07:00 -04:00
management.ts 🔐 feat: Add M2M Authentication for Agent Management (#15555) 2026-09-03 20:07:00 -04:00
messageFilterPii.spec.ts 🧢 fix: Cap Batched HITL Answers Before PII and Moderation (#15257) 2026-08-26 10:10:19 -04:00
messageFilterPii.ts 🧢 fix: Cap Batched HITL Answers Before PII and Moderation (#15257) 2026-08-26 10:10:19 -04:00
messageValidation.ts 🧵 fix: Close Child-Thread Read and Search-Cleanup Gaps (#15055) 2026-08-21 00:44:37 -04:00
modelBoundContent.spec.ts 🔭 feat: Audit PII Filter Findings (#15508) 2026-09-02 10:36:06 -04:00
modelBoundContent.ts 🚰 fix: Throttle Shared Link Reads and Aggregate Audit Findings (#15605) 2026-09-04 12:38:34 -04:00
notFound.ts
preAuthTenant.spec.ts 🧩 fix: Align Tenant and MCP Configuration Resolution (#14904) 2026-08-16 22:30:46 -04:00
preAuthTenant.ts 🧩 fix: Align Tenant and MCP Configuration Resolution (#14904) 2026-08-16 22:30:46 -04:00
remoteAgentAuth.spec.ts 🔐 feat: Add M2M Authentication for Agent Management (#15555) 2026-09-03 20:07:00 -04:00
remoteAgentAuth.ts 🔐 feat: Add M2M Authentication for Agent Management (#15555) 2026-09-03 20:07:00 -04:00
share.spec.ts
share.ts 🧳 feat: Register Principal-Owned Code Environments (#15365) 2026-08-30 19:33:19 -04:00
tenant.ts 🔐 fix: Preserve Structured JWT Auth Context (#14652) 2026-08-06 08:12:00 -04:00