Skip to content

Commit c41d2e1

Browse files
committed
fix(client): strict numeric validation and past-date guard in parseRetryAfter
1 parent 3fbf7f2 commit c41d2e1

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/core/client.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ const DEFAULT_USER_AGENT = "regxa/0.1.0";
1919
*/
2020
export function parseRetryAfter(header: string | null | undefined): number {
2121
if (!header) return 60;
22+
const trimmed = header.trim();
23+
if (!trimmed) return 60;
2224

23-
const numeric = Number.parseInt(header, 10);
24-
if (!Number.isNaN(numeric) && numeric >= 0) return numeric;
25+
if (/^\d+$/.test(trimmed)) return Number(trimmed);
2526

26-
const timestamp = Date.parse(header);
27+
const timestamp = /[a-z]/i.test(trimmed) ? Date.parse(trimmed) : NaN;
2728
if (!Number.isNaN(timestamp)) {
2829
const seconds = Math.ceil((timestamp - Date.now()) / 1000);
29-
return seconds > 0 ? seconds : 60;
30+
return Math.max(seconds, 0);
3031
}
3132

3233
return 60;

test/unit/client.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ describe("parseRetryAfter", () => {
2828
expect(result).toBeLessThanOrEqual(90);
2929
});
3030

31-
it("falls back to 60 for HTTP-date in the past", () => {
32-
expect(parseRetryAfter("Wed, 21 Oct 2015 07:28:00 GMT")).toBe(60);
31+
it("returns 0 for HTTP-date in the past", () => {
32+
expect(parseRetryAfter("Wed, 21 Oct 2015 07:28:00 GMT")).toBe(0);
3333
});
3434

3535
it("returns 60 for garbage input", () => {
@@ -39,4 +39,16 @@ describe("parseRetryAfter", () => {
3939
it("handles large numeric values", () => {
4040
expect(parseRetryAfter("3600")).toBe(3600);
4141
});
42+
43+
it("rejects partial numeric like '120s'", () => {
44+
expect(parseRetryAfter("120s")).toBe(60);
45+
});
46+
47+
it("rejects decimal like '1.5'", () => {
48+
expect(parseRetryAfter("1.5")).toBe(60);
49+
});
50+
51+
it("returns 60 for whitespace-only", () => {
52+
expect(parseRetryAfter(" ")).toBe(60);
53+
});
4254
});

0 commit comments

Comments
 (0)