Skip to content

fix(server): isolate pre-auth rate-limit cardinality#61

Merged
khaliqgant merged 2 commits into
mainfrom
fix/preauth-rate-limit-isolation
Jul 13, 2026
Merged

fix(server): isolate pre-auth rate-limit cardinality#61
khaliqgant merged 2 commits into
mainfrom
fix/preauth-rate-limit-isolation

Conversation

@khaliqgant

@khaliqgant khaliqgant commented Jul 13, 2026

Copy link
Copy Markdown
Member

Summary

  • isolate unauthenticated identity-create API-key hashes in a fixed-memory count-min sketch limiter
  • preserve exact fail-closed rate limiting for authenticated organization/key identities
  • add 10,000-key cardinality and HTTP hash-spray isolation regressions

Context

This is the late Cubic P2 follow-up from #59. The operator merged #59 before the final review comment was addressed, so this PR carries only commit 16a0c365 rebased onto current main. It should land before the real npm publish associated with #60.

Validation

  • focused server regression tests: 114/114 passed
  • CI-equivalent build: 8/8 packages passed
  • CI-equivalent typecheck: 11/11 packages passed
  • CI-equivalent tests: 15/15 packages passed (server 393/393)
  • follow-up tree is identical to the fully validated 16a0c365 tree

Do not auto-merge; operator verification is required.

Review in cubic

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Warning

Review limit reached

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

Next review available in: 9 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 Plus

Run ID: e222d483-bbcc-493f-a140-8593add5647b

📥 Commits

Reviewing files that changed from the base of the PR and between 91e9abc and 0203821.

📒 Files selected for processing (7)
  • packages/server/src/__tests__/api-key-auth-middleware.test.ts
  • packages/server/src/__tests__/rate-limit.test.ts
  • packages/server/src/__tests__/test-helpers.ts
  • packages/server/src/env.ts
  • packages/server/src/lib/rate-limit.ts
  • packages/server/src/middleware/api-key-auth.ts
  • packages/server/src/server.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/preauth-rate-limit-isolation

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.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces a new FixedWindowSketchRateLimiter class to handle untrusted, high-cardinality keys using a count-min sketch algorithm, preventing memory exhaustion from API key spray attacks. This pre-auth rate limiter is integrated into the API key authentication middleware and server configuration, with corresponding tests added. The review feedback suggests minor code simplifications in the new rate limiter by removing redundant nullish coalescing operators and fallback checks where array values are guaranteed to be defined.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +219 to +221
if (row[index] !== 0xffff_ffff) {
row[index] = (row[index] ?? 0) + 1;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Since row is a Uint32Array and index is guaranteed to be within bounds, row[index] will always be a number and never undefined. The nullish coalescing operator ?? 0 is redundant here and can be simplified.

Suggested change
if (row[index] !== 0xffff_ffff) {
row[index] = (row[index] ?? 0) + 1;
}
if (row[index] !== 0xffff_ffff) {
row[index] += 1;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed in 0203821: the typed-array counter now increments directly under the validated in-bounds invariant.

Comment on lines +240 to +250
private estimate(key: string): number {
let estimate = Number.POSITIVE_INFINITY;
for (let rowIndex = 0; rowIndex < this.rows.length; rowIndex += 1) {
const row = this.rows[rowIndex];
if (!row) {
continue;
}
estimate = Math.min(estimate, row[this.hashIndex(key, rowIndex)] ?? 0);
}
return Number.isFinite(estimate) ? estimate : 0;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Since this.rows is initialized with a length of depth (which is validated to be >= 1), the loop is guaranteed to run at least once and estimate will always be updated to a finite number from the Uint32Array. Therefore, both the ?? 0 fallback and the Number.isFinite check are redundant and can be simplified.

Suggested change
private estimate(key: string): number {
let estimate = Number.POSITIVE_INFINITY;
for (let rowIndex = 0; rowIndex < this.rows.length; rowIndex += 1) {
const row = this.rows[rowIndex];
if (!row) {
continue;
}
estimate = Math.min(estimate, row[this.hashIndex(key, rowIndex)] ?? 0);
}
return Number.isFinite(estimate) ? estimate : 0;
}
private estimate(key: string): number {
let estimate = Number.POSITIVE_INFINITY;
for (let rowIndex = 0; rowIndex < this.rows.length; rowIndex += 1) {
const row = this.rows[rowIndex];
if (!row) {
continue;
}
estimate = Math.min(estimate, row[this.hashIndex(key, rowIndex)]);
}
return estimate;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed in 0203821: construction guarantees at least one initialized row, so estimate now returns the loop result directly.

@khaliqgant
khaliqgant merged commit a9986d9 into main Jul 13, 2026
4 checks passed
@khaliqgant
khaliqgant deleted the fix/preauth-rate-limit-isolation branch July 13, 2026 20:27
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