Skip to content

Commit 2bf546d

Browse files
committed
fix(discord): redact JSON API error details
1 parent ffb1366 commit 2bf546d

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎extensions/discord/src/api.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,52 @@ describe("fetchDiscord", () => {
432432
expect(message).not.toContain(token);
433433
expect(message).not.toContain(uniqueSecret);
434434
expect(message).not.toContain("<html");
435+
console.log(
436+
`[discord credential redaction proof] format=html status=502 authorization_received=${authorization === `Bot ${token}`} token_present=${message.includes(token)} unique_fragment_present=${message.includes(uniqueSecret)} detail=${message}`,
437+
);
438+
} finally {
439+
await closeServer(server);
440+
}
441+
});
442+
443+
it("redacts reflected bot credentials from JSON error messages", async () => {
444+
const uniqueSecret = "discord-json-loopback-secret";
445+
const token = `proof-prefix-${uniqueSecret}-proof-suffix`;
446+
let authorization: string | undefined;
447+
const server = createServer((req, res) => {
448+
authorization = req.headers.authorization;
449+
res.writeHead(429, { "content-type": "application/json" });
450+
res.end(
451+
JSON.stringify({
452+
message: `proxy failure Authorization: ${authorization}; request rejected`,
453+
retry_after: 0,
454+
global: false,
455+
code: 20_028,
456+
}),
457+
);
458+
});
459+
const port = await listenLoopbackServer(server);
460+
461+
try {
462+
stubDiscordFetchToLoopback(`http://127.0.0.1:${port}`);
463+
464+
const error = await requestDiscord("/gateway/bot", token, {
465+
retry: { attempts: 1 },
466+
}).catch((err: unknown) => err);
467+
468+
expect(error).toBeInstanceOf(DiscordApiError);
469+
expect(authorization).toBe(`Bot ${token}`);
470+
expect((error as DiscordApiError).retryAfter).toBe(0);
471+
const message = String(error);
472+
expect(message).toContain("Discord API /gateway/bot failed (429)");
473+
expect(message).toContain("proxy failure");
474+
expect(message).toContain("Authorization: Bot");
475+
expect(message).toContain("retry after 0.0s");
476+
expect(message).not.toContain(token);
477+
expect(message).not.toContain(uniqueSecret);
478+
console.log(
479+
`[discord credential redaction proof] format=json status=429 authorization_received=${authorization === `Bot ${token}`} token_present=${message.includes(token)} unique_fragment_present=${message.includes(uniqueSecret)} retry_after=${(error as DiscordApiError).retryAfter} detail=${message}`,
480+
);
435481
} finally {
436482
await closeServer(server);
437483
}

‎extensions/discord/src/api.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Discord API module exposes the plugin public contract.
22
import { resolveFetch } from "openclaw/plugin-sdk/fetch-runtime";
3+
import { redactToolPayloadText } from "openclaw/plugin-sdk/logging-core";
34
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
45
import { readResponseTextLimited } from "openclaw/plugin-sdk/provider-http";
56
import { readResponseWithLimit } from "openclaw/plugin-sdk/response-limit-runtime";
@@ -69,7 +70,7 @@ function formatRetryAfterSeconds(value: number | undefined): string | undefined
6970
return `${rounded}s`;
7071
}
7172

72-
function formatDiscordApiErrorText(text: string, response: Response): string | undefined {
73+
function formatDiscordApiErrorTextUntrusted(text: string, response: Response): string | undefined {
7374
const trimmed = text.trim();
7475
if (!trimmed) {
7576
return undefined;
@@ -99,6 +100,13 @@ function formatDiscordApiErrorText(text: string, response: Response): string | u
99100
return retryAfter ? `${message} (retry after ${retryAfter})` : message;
100101
}
101102

103+
function formatDiscordApiErrorText(text: string, response: Response): string | undefined {
104+
const detail = formatDiscordApiErrorTextUntrusted(text, response);
105+
// Keep the final error boundary shared by JSON and text responses redacted;
106+
// upstreams can reflect the request Authorization value through either shape.
107+
return detail ? redactToolPayloadText(detail) : detail;
108+
}
109+
102110
export class DiscordApiError extends Error {
103111
status: number;
104112
retryAfter?: number;

0 commit comments

Comments
 (0)