Skip to content

[WRONG BRANCH] fix(usage): bound concurrent filtered scans - #520

Draft
luvs01 wants to merge 1 commit into
mainfrom
codex/propose-fix-for-usage-scan-vulnerability
Draft

luvs01 wants to merge 1 commit into
mainfrom
codex/propose-fix-for-usage-scan-vulnerability

Conversation

@luvs01

@luvs01 luvs01 commented Sep 14, 2026

Copy link
Copy Markdown
Owner

Motivation

  • A newly exposed GET /v1/usage path could be abused by an authenticated client key to spawn many distinct whole-ledger scans, amplifying file-descriptor, memory, and CPU use and enabling an authenticated DoS.
  • The intent of the change is to limit distinct in-flight filtered usage scans while preserving existing single-flight sharing for identical queries and preventing unbounded resource amplification.

Description

  • Add a process-wide cap MAX_CONCURRENT_FILTERED_AGGREGATES = 4 and reject new distinct filtered aggregate flights when that cap is reached in src/server/management/usage-aggregate-cache.ts by throwing an error before starting a refresh.
  • Preserve the existing single-flight behaviour so identical filter/window queries still join one in-flight Promise and reuse its result.
  • Add a regression test tests/usage/usage-aggregate-cache.test.ts that asserts four distinct blocked scans run concurrently, the fifth distinct scan is rejected, and a duplicate caller joins the existing flight.
  • Update structure/gui-and-management-api.md to document the filtered-scan concurrency invariant so the architecture notes reflect the runtime bound.

Testing

  • Ran the focused tests with the repository Bun binary: node_modules/.bin/bun test tests/usage/usage-aggregate-cache.test.ts tests/server/hub-usage.test.ts, and the changed-subsystem tests passed (focused run passed).
  • Ran node_modules/.bin/bun run typecheck, node_modules/.bin/bun run structure:check, and node_modules/.bin/bun run privacy:scan, which succeeded after a small docs formatting adjustment to satisfy the structure check.
  • Ran the full test suite with node_modules/.bin/bun run test; the focused changes are covered and pass, while the complete parallel suite contains unrelated failures in the environment and serial fixtures (these failures are out of scope for this focused change).

Codex Task

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-14T00:57:16.745751Z b726731 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 4c7e6145-8ad0-4b57-8b73-d9026f4db7d5


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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b72673105b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +287 to +288
if (filteredFlights.size >= MAX_CONCURRENT_FILTERED_AGGREGATES) {
throw new Error("too many concurrent filtered usage aggregates");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Surface scan saturation as a request failure

When four distinct scans are active, a filtered management /api/usage request reaches this throw, but logs-usage-routes.ts catches it and returns HTTP 200 with zero totals and error: "read_failed". Consumers such as gui/src/pages/Usage.tsx and src/cli/observe.ts accept successful responses without checking that field, so temporary saturation is displayed as genuine zero usage. Translate this overload condition to a non-success response, or otherwise ensure clients reject the synthetic summary.

Useful? React with 👍 / 👎.

Comment on lines +287 to +288
if (filteredFlights.size >= MAX_CONCURRENT_FILTERED_AGGREGATES) {
throw new Error("too many concurrent filtered usage aggregates");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check retained aggregates before enforcing the scan cap

When four other keys are scanning, this guard also rejects a request whose own key already has a current entry in retainedFilteredAggregates, because the unchanged-cache check does not happen until refreshFilteredAggregate. That request would perform no ledger scan, yet four slow callers can deny the otherwise available cached report; enforce the limit only on the rebuild/append path that actually invokes the scanner. The changed structure contract specifically describes this as a bound on concurrent filtered scans.

AGENTS.md reference: structure/AGENTS.md:L9-L10

Useful? React with 👍 / 👎.

@github-actions

Copy link
Copy Markdown

Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Sep 14, 2026
@github-actions github-actions Bot changed the title fix(usage): bound concurrent filtered scans [WRONG BRANCH] fix(usage): bound concurrent filtered scans Sep 14, 2026
@github-actions

github-actions Bot commented Sep 14, 2026

Copy link
Copy Markdown

⏳ DRAFT

  • wrong target branch (main); retarget to dev.

What to do

  • Retarget this PR to dev — all contributions go to dev.

Its title has been prefixed with [WRONG BRANCH].
This pull request was already a draft. Its draft status will be preserved after every issue above is resolved.

@github-actions
github-actions Bot marked this pull request as draft September 14, 2026 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aardvark bug Something isn't working codex

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant