Skip to content

Commit d290c1f

Browse files
fix(github): a proxy's 403 with Retry-After is no longer reported as a rate limit (#33676)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent f86e21f commit d290c1f

3 files changed

Lines changed: 71 additions & 3 deletions

File tree

‎packages/source-control-github/src/server/GitHubRequestPolicy.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ function retryAtFrom(
147147
return Number.isFinite(reset) && reset > now ? reset : undefined;
148148
}
149149

150-
/** HTTP 200 GraphQL rate limits use RATE_LIMITED or untyped errors with quota headers. */
150+
/**
151+
* HTTP 200 GraphQL rate limits use RATE_LIMITED or untyped errors with quota headers. A 403 is a
152+
* limit only when its quota, body or GraphQL errors name one (older hosts say "abuse detection
153+
* mechanism"): a proxy in front of GitHub Enterprise can refuse with `Retry-After` alone.
154+
*/
151155
export function isRateLimited(input: {
152156
readonly status: number;
153157
readonly headers: Readonly<Record<string, string | undefined>>;
@@ -167,11 +171,15 @@ export function isRateLimited(input: {
167171
))) ||
168172
(status === 403 &&
169173
(headers["x-ratelimit-remaining"] === "0" ||
170-
headers["retry-after"] !== undefined ||
171-
/rate limit/i.test(input.body ?? "")))
174+
namesLimit(input.body) ||
175+
(errors ?? []).some((error) => namesLimit(error.message))))
172176
);
173177
}
174178

179+
function namesLimit(text: unknown): boolean {
180+
return typeof text === "string" && /rate limit|abuse detection mechanism/i.test(text);
181+
}
182+
175183
export type GitHubAnswer<A> = {
176184
readonly status: number | undefined;
177185
readonly headers: Readonly<Record<string, string | undefined>>;

‎packages/source-control-github/src/server/GitHubRest.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,58 @@ describe("GitHubRest", () => {
510510
}).pipe(Effect.provide(layer));
511511
});
512512

513+
it.effect("reads a proxy's 403 with Retry-After as a refusal and keeps the host open", () => {
514+
const pages = [
515+
"<html><head><title>403 Forbidden</title></head><body>nginx</body></html>",
516+
"<html><body>Request blocked by proxy abuse detection; contact your administrator</body></html>",
517+
];
518+
const { layer, requests } = harness(
519+
() =>
520+
new Response(pages.shift(), {
521+
status: 403,
522+
headers: { "content-type": "text/html", "retry-after": "5" },
523+
}),
524+
);
525+
return Effect.gen(function* () {
526+
yield* TestClock.setTime(NOW);
527+
const api = yield* GitHubRest.GitHubRest;
528+
const read = {
529+
host: "ghes.example.internal",
530+
operation: "checks",
531+
path: "repos/acme/web/pulls/7",
532+
};
533+
const first = yield* Effect.flip(api.rest(read));
534+
expect(first).toMatchObject({ _tag: "GitHubApiResponseError", status: 403 });
535+
const second = yield* Effect.flip(api.rest(read));
536+
expect(second).toMatchObject({ _tag: "GitHubApiResponseError", status: 403 });
537+
expect(requests).toHaveLength(2);
538+
}).pipe(Effect.provide(layer));
539+
});
540+
541+
it.effect("pauses the host on an abuse-detection 403 that never says rate limit", () => {
542+
const { layer, requests } = harness(() =>
543+
json(
544+
{
545+
message: "You have triggered an abuse detection mechanism. Please wait before retrying.",
546+
documentation_url:
547+
"https://docs.github.com/rest/overview/resources-in-the-rest-api#abuse-rate-limits",
548+
},
549+
{ status: 403, headers: { "retry-after": "60", "x-ratelimit-remaining": "4878" } },
550+
),
551+
);
552+
return Effect.gen(function* () {
553+
yield* TestClock.setTime(NOW);
554+
const api = yield* GitHubRest.GitHubRest;
555+
const read = { host: "github.com", operation: "checks", path: "repos/acme/web/pulls/7" };
556+
expect(yield* Effect.flip(api.rest(read))).toMatchObject({
557+
_tag: "GitHubApiRateLimitError",
558+
retryAt: NOW + 60_000,
559+
});
560+
expect((yield* Effect.flip(api.rest(read)))._tag).toBe("SourceControlRateLimitPausedError");
561+
expect(requests).toHaveLength(1);
562+
}).pipe(Effect.provide(layer));
563+
});
564+
513565
it.effect("drops a refused token so the next request asks the source again", () => {
514566
const { layer, requests, invalidations } = harness((request) =>
515567
request.headers.authorization === "Bearer first"

‎packages/source-control-github/src/server/gitHubGraphQlClient.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ describe("GitHubGraphQlAuth", () => {
223223
const answers = [
224224
json({ message: "You have exceeded a secondary rate limit." }, { status: 403 }),
225225
json({ message: "Bad credentials" }, { status: 401 }),
226+
json(
227+
{ data: null, errors: [{ message: "You have exceeded a secondary rate limit." }] },
228+
{ status: 403, headers: { "retry-after": "60" } },
229+
),
226230
];
227231
const { layer, invalidations } = harness(() => answers.shift()!);
228232
return Effect.gen(function* () {
@@ -231,6 +235,10 @@ describe("GitHubGraphQlAuth", () => {
231235
expect((yield* Effect.flip(readStats("github.example")))._tag).toBe(
232236
"GitHubApiAuthenticationError",
233237
);
238+
expect(yield* Effect.flip(readStats("github.acme.test"))).toMatchObject({
239+
_tag: "GitHubApiRateLimitError",
240+
retryAt: NOW + 60_000,
241+
});
234242
expect(invalidations()).toBe(1);
235243
}).pipe(Effect.provide(layer));
236244
});

0 commit comments

Comments
 (0)