-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(web): wire three unused rate-limit ids and guard against new ones #2040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+145
−0
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { execFileSync } from "node:child_process"; | ||
| import { readFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { RATE_LIMIT_IDS } from "@/lib/rate-limit"; | ||
|
|
||
| const WEB_ROOT = join(__dirname, "..", ".."); | ||
| const DECLARATION_FILE = "lib/rate-limit.ts"; | ||
| const TEST_DIR = "__tests__/"; | ||
|
|
||
| /** | ||
| * Ids that are intentionally declared ahead of being wired. Each entry needs a | ||
| * reason; removing one from this list and wiring the endpoint is the goal. | ||
| * Keeping it explicit means a NEW unwired id fails the test instead of quietly | ||
| * joining an already-failing count. | ||
| */ | ||
| const KNOWN_UNWIRED: Record<string, string> = { | ||
| AUTH_OTP_VERIFY: "no OTP verification route located yet (see #2039)", | ||
| AUTH_OTP_SEND: "no OTP send route located yet (see #2039)", | ||
| MESSENGER_MESSAGE: "anonymous support chat route not located yet (see #2039)", | ||
| DESKTOP_LOGS: "desktop log forwarding route not located yet (see #2039)", | ||
| }; | ||
|
|
||
| /** | ||
| * Every key referenced somewhere other than its own declaration. | ||
| * | ||
| * Uses git's own file list so the search sees exactly the tracked sources and | ||
| * never walks node_modules or .next. Memoized: the scan reads every tracked | ||
| * TS/TSX file, and each test in this suite needs the same answer. | ||
| */ | ||
| let referencedCache: Set<string> | undefined; | ||
|
|
||
| function referencedKeys(): Set<string> { | ||
| if (referencedCache) return referencedCache; | ||
|
|
||
| const tracked = execFileSync( | ||
| "git", | ||
| ["ls-files", "-z", "*.ts", "*.tsx"], | ||
| { cwd: WEB_ROOT, encoding: "utf8", maxBuffer: 32 * 1024 * 1024 }, | ||
| ) | ||
| .split("\0") | ||
| .filter( | ||
| (file) => | ||
| file && | ||
| file !== DECLARATION_FILE && | ||
| // Only runtime call sites count. A test that merely names an id | ||
| // would otherwise make an unwired endpoint look protected — and | ||
| // this file itself references every key, which would make the | ||
| // guard vacuous. | ||
| !file.startsWith(TEST_DIR), | ||
| ); | ||
|
|
||
| const found = new Set<string>(); | ||
| const keys = Object.keys(RATE_LIMIT_IDS); | ||
|
|
||
| for (const file of tracked) { | ||
| let source: string; | ||
| try { | ||
| source = readFileSync(join(WEB_ROOT, file), "utf8"); | ||
| } catch (error) { | ||
| // A tracked file missing from the worktree is expected (deleted but | ||
| // still in the index). Anything else - a permission problem, an I/O | ||
| // error - would silently shrink the scan and turn this guard into a | ||
| // no-op, so it has to surface. | ||
| if ((error as NodeJS.ErrnoException).code === "ENOENT") continue; | ||
| throw error; | ||
| } | ||
|
|
||
| for (const key of keys) { | ||
| if (source.includes(`RATE_LIMIT_IDS.${key}`)) found.add(key); | ||
| } | ||
| } | ||
|
|
||
| referencedCache = found; | ||
| return found; | ||
| } | ||
|
|
||
| describe("RATE_LIMIT_IDS", () => { | ||
| it("has a unique rule id per key", () => { | ||
| const ids = Object.values(RATE_LIMIT_IDS); | ||
|
|
||
| expect(new Set(ids).size).toBe(ids.length); | ||
| }); | ||
|
|
||
| it("every declared id is actually called somewhere", () => { | ||
| // A declared-but-uncalled id looks like protection in code review and in | ||
| // the Vercel Firewall dashboard while the endpoint runs unlimited. The | ||
| // helper already fails open when a dashboard rule is missing, so an | ||
| // unreferenced constant is a second, quieter way to have no limit. | ||
| const referenced = referencedKeys(); | ||
| const unused = Object.keys(RATE_LIMIT_IDS).filter( | ||
| (key) => !referenced.has(key) && !(key in KNOWN_UNWIRED), | ||
| ); | ||
|
|
||
| expect(unused).toEqual([]); | ||
| }); | ||
|
|
||
| it("does not carry a stale allowance for an id that is now wired", () => { | ||
| // Keeps the allow-list honest: once an endpoint is wired, its entry has | ||
| // to be deleted rather than lingering and masking a future regression. | ||
| const referenced = referencedKeys(); | ||
| const staleAllowances = Object.keys(KNOWN_UNWIRED).filter((key) => | ||
| referenced.has(key), | ||
| ); | ||
|
|
||
| expect(staleAllowances).toEqual([]); | ||
| }); | ||
|
|
||
| it("only allows ids that actually exist", () => { | ||
| const unknown = Object.keys(KNOWN_UNWIRED).filter( | ||
| (key) => !(key in RATE_LIMIT_IDS), | ||
| ); | ||
|
|
||
| expect(unknown).toEqual([]); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
referencedKeys()shells out to git + reads every TS/TSX file; since it’s called in multiple tests, memoizing the result would keep this suite from doing the full scan more than once.