Repository navigation
fix(imports): honor bounded Retry-After on hosted scanner reads #2086
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
Open
mldangelo-oai
wants to merge
6
commits into
dev/codex/hosted-scanner-findings
Choose a base branch
from
dev/codex/hosted-scanner-backpressure
base: dev/codex/hosted-scanner-findings
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+254
−36
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9c28702
fix(imports): honor bounded Retry-After on hosted scanner reads
mldangelo-oai 7ce6646
Merge branch 'dev/codex/hosted-scanner-findings' into dev/codex/hoste…
mldangelo-oai ae7b2a4
refactor(imports): simplify hosted scanner retries
mldangelo-oai 77af712
Merge current hosted scanner collection changes
mldangelo-oai c1364ac
fix(imports): measure HTTP-date retries against server time
mldangelo-oai 67d384d
Merge current hosted-scanner-findings into hosted-scanner-backpressure
mldangelo-oai 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { setImmediate } from "node:timers/promises"; | ||
| import { afterEach, expect, jest, mock, test } from "bun:test"; | ||
| import { getHostedJson } from "../src/hosted-scanner-http.js"; | ||
|
|
||
| const url = new URL( | ||
| "https://semgrep.dev/api/v1/deployments/example/findings?page=1", | ||
| ); | ||
| const now = Date.UTC(2026, 10, 6, 8, 49, 20, 250); | ||
| afterEach(() => jest.useRealTimers()); | ||
|
|
||
| function limited(hint: string, cancel: () => void) { | ||
| return new Response(new ReadableStream({ cancel }), { | ||
| status: 429, | ||
| headers: { "Retry-After": hint }, | ||
| }); | ||
| } | ||
|
|
||
| test.each([ | ||
| ["010", 10_000, null], | ||
| ["Fri, 06 Nov 2026 08:48:05 GMT", 5_000, "Fri, 06 Nov 2026 08:48:00 GMT"], | ||
| ["Fri, 06 Nov 2026 08:49:30 GMT", 9_750, null], | ||
| ["Friday, 06-Nov-26 08:49:30 GMT", 9_750, "unusable date"], | ||
| ["Fri Nov 6 08:49:30 2026", 9_750, null], | ||
| ["Sun, 06 Nov 1994 08:49:37 GMT", 0, null], | ||
| ["Sunday, 06-Nov-94 08:49:37 GMT", 0, null], | ||
| ["Sat, 31 Dec 2016 23:59:60 GMT", 0, null], | ||
| ] as const)( | ||
| "releases the 429 body and waits for Retry-After %s", | ||
| async (hint, delay, serverDate) => { | ||
| jest.useFakeTimers({ now }); | ||
| const released = Promise.withResolvers<void>(); | ||
| const fetch = mock(async () => Response.json({ complete: true })); | ||
| const response = limited(hint, released.resolve); | ||
| if (serverDate) response.headers.set("Date", serverDate); | ||
| fetch.mockResolvedValueOnce(response); | ||
| const result = getHostedJson(url, "Bearer synthetic", { fetch }); | ||
| await released.promise; | ||
| if (delay > 0) { | ||
| jest.advanceTimersByTime(delay - 1); | ||
| await setImmediate(); | ||
| expect(fetch).toHaveBeenCalledTimes(1); | ||
| jest.advanceTimersByTime(1); | ||
| } | ||
| expect(await result).toEqual({ complete: true }); | ||
| expect(fetch).toHaveBeenCalledTimes(2); | ||
| }, | ||
| ); | ||
|
|
||
| test.each(["cancel", "timeout"])( | ||
| "a %s during Retry-After prevents the second GET", | ||
| async (mode) => { | ||
| jest.useFakeTimers({ now }); | ||
| const controller = new AbortController(); | ||
| const signal = | ||
| mode === "timeout" ? AbortSignal.timeout(500) : controller.signal; | ||
| const released = Promise.withResolvers<void>(); | ||
| const fetch = mock(async () => limited("10", released.resolve)); | ||
| const result = getHostedJson(url, "Bearer synthetic", { | ||
| fetch, | ||
| signal, | ||
| }).catch((error: unknown) => error); | ||
| await released.promise; | ||
| if (mode === "timeout") jest.advanceTimersByTime(500); | ||
| else controller.abort(new Error("collection stopped")); | ||
| expect(await result).toBe(signal.reason); | ||
| jest.advanceTimersByTime(10_000); | ||
| expect(fetch).toHaveBeenCalledTimes(1); | ||
| }, | ||
| ); | ||
|
|
||
| test("unusable or long hints and other HTTP errors fail on the first response with raw details", async () => { | ||
| jest.useFakeTimers({ now }); | ||
| const detail = "vendor detail: synthetic-token\n\u001b[31mretry later"; | ||
| for (const [status, hint] of [ | ||
| [429, null], | ||
| [429, ""], | ||
| [429, "-1"], | ||
| [429, "1.5"], | ||
| [429, "1e0"], | ||
| [429, "0, 1"], | ||
| [429, "11"], | ||
| [429, "9".repeat(400)], | ||
| [429, "Fri, 06 Nov 2026 08:49:31 GMT"], | ||
| [429, "Tuesday, 06-Nov-74 08:49:30 GMT"], | ||
| [429, "2026-11-06"], | ||
| [429, "Fri, 31 Feb 2023 08:49:30 GMT"], | ||
| [429, "Fri, 30 Dec 2016 23:59:60 GMT"], | ||
| [401, "0"], | ||
| [403, "0"], | ||
| [503, "0"], | ||
| [307, "0"], | ||
| ] as const) { | ||
| const fetch = mock( | ||
| async () => | ||
| new Response(detail, { | ||
| status, | ||
| headers: hint === null ? {} : { "Retry-After": hint }, | ||
| }), | ||
| ); | ||
| await expect( | ||
| getHostedJson(url, "Bearer synthetic", { fetch }), | ||
| ).rejects.toThrow( | ||
| `GET ${url.origin}${url.pathname} failed (HTTP ${status})${hint ? `; Retry-After: ${hint}` : ""}: ${detail}`, | ||
| ); | ||
| expect(fetch).toHaveBeenCalledTimes(1); | ||
| } | ||
| }); | ||
|
|
||
| test("does not retry a fetch rejection", async () => { | ||
| const error = new TypeError("connection reset"); | ||
| const fetch = mock(async () => { | ||
| throw error; | ||
| }); | ||
| await expect(getHostedJson(url, "Bearer synthetic", { fetch })).rejects.toBe( | ||
| error, | ||
| ); | ||
| expect(fetch).toHaveBeenCalledTimes(1); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
When a scanner returns a valid
Retry-AfterHTTP date with lowercase or mixed-case day, month, orGMTtokens (for example,fri, 06 nov 2026 08:49:30 gmt), this exact canonical-string comparison producesNaN, so the eligible 429 is not retried. HTTP date grammar is case-insensitive, so normalize recognized tokens or validate the parsed fields without requiring byte-for-bytetoUTCString()equality.Useful? React with 👍 / 👎.