fix(discord): validate error code integers · openclaw/openclaw@9ae38ac
steipete
·
2026-05-29
·
via Recent Commits to openclaw:main
File tree
extensions/discord/src/internal
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import { parseStrictNonNegativeInteger } from "openclaw/plugin-sdk/number-runtime"; |
1 | 2 | import { parseDiscordRetryAfterBodySeconds, parseRetryAfterHeaderSeconds } from "../retry-after.js"; |
2 | 3 | |
3 | 4 | export function readDiscordCode(body: unknown): number | undefined { |
4 | 5 | const value = |
5 | 6 | body && typeof body === "object" && "code" in body |
6 | 7 | ? (body as { code?: unknown }).code |
7 | 8 | : undefined; |
8 | | -if (typeof value === "number" && Number.isFinite(value)) { |
9 | | -return value; |
10 | | -} |
11 | | -if (typeof value === "string" && /^\d+$/.test(value)) { |
12 | | -return Number(value); |
13 | | -} |
14 | | -return undefined; |
| 9 | +return parseStrictNonNegativeInteger(value); |
15 | 10 | } |
16 | 11 | |
17 | 12 | export function readDiscordMessage(body: unknown, fallback: string): string { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -554,6 +554,41 @@ describe("RequestClient", () => {
|
554 | 554 | }); |
555 | 555 | }); |
556 | 556 | |
| 557 | +it("ignores unsafe numeric Discord error code strings", async () => { |
| 558 | +const client = new RequestClient("test-token", { |
| 559 | +queueRequests: false, |
| 560 | +fetch: async () => |
| 561 | +new Response( |
| 562 | +JSON.stringify({ |
| 563 | +message: "Slow down", |
| 564 | +retry_after: 1, |
| 565 | +global: false, |
| 566 | +code: "9007199254740993", |
| 567 | +}), |
| 568 | +{ status: 429 }, |
| 569 | +), |
| 570 | +}); |
| 571 | + |
| 572 | +await expect(client.post("/applications/app/commands", { body: {} })).rejects.toMatchObject({ |
| 573 | +discordCode: undefined, |
| 574 | +}); |
| 575 | +}); |
| 576 | + |
| 577 | +it("ignores unsafe numeric Discord error codes", async () => { |
| 578 | +const client = new RequestClient("test-token", { |
| 579 | +queueRequests: false, |
| 580 | +fetch: async () => |
| 581 | +new Response( |
| 582 | +'{"message":"Slow down","retry_after":1,"global":false,"code":9007199254740993}', |
| 583 | +{ status: 429 }, |
| 584 | +), |
| 585 | +}); |
| 586 | + |
| 587 | +await expect(client.post("/applications/app/commands", { body: {} })).rejects.toMatchObject({ |
| 588 | +discordCode: undefined, |
| 589 | +}); |
| 590 | +}); |
| 591 | + |
557 | 592 | it("parses HTTP-date Retry-After headers on rate limit errors", async () => { |
558 | 593 | vi.useFakeTimers(); |
559 | 594 | vi.setSystemTime(new Date("2026-05-01T12:00:00.000Z")); |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。