Skip to content

Commit 292f0a5

Browse files
committed
fix(discord): redact webhook scheduler keys
1 parent 323a2c2 commit 292f0a5

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎extensions/discord/src/internal/rest-errors.redaction.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ describe("Discord REST error redaction", () => {
103103
expect(discordError.discordCode).toBe(10_065);
104104
expect(isUnknownDiscordVoiceStateError(discordError)).toBe(true);
105105

106-
const rateError = await captureError(client.get("/rate-limit"));
106+
const webhookRateLimitPath = `/webhooks/app/${token}/rate-limit`;
107+
const rateError = await captureError(client.get(webhookRateLimitPath));
107108
expect(rateError).toBeInstanceOf(RateLimitError);
108109
const rateLimitError = rateError as RateLimitError;
109110
const rateLimitDetails = JSON.stringify({
@@ -121,10 +122,11 @@ describe("Discord REST error redaction", () => {
121122

122123
const bucketCount = client.getSchedulerMetrics().activeBuckets;
123124
expect(bucketCount).toBeGreaterThan(0);
124-
const repeatedRateError = await captureError(client.get("/rate-limit"));
125+
const repeatedRateError = await captureError(client.get(webhookRateLimitPath));
125126
expect(repeatedRateError).toBeInstanceOf(RateLimitError);
126127
expect((repeatedRateError as RateLimitError).bucket).toBe(rateLimitError.bucket);
127128
expect(client.getSchedulerMetrics().activeBuckets).toBe(bucketCount);
129+
expect(JSON.stringify(client.getSchedulerMetrics())).not.toContain(token);
128130

129131
const reflectedHeaderError = await captureError(client.get("/rate-limit-reflected-headers"));
130132
expect(reflectedHeaderError).toBeInstanceOf(RateLimitError);

‎extensions/discord/src/internal/rest-routes.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Discord plugin module implements rest routes behavior.
2+
import { redactIdentifier } from "openclaw/plugin-sdk/logging-core";
23
import {
34
asDateTimestampMs,
45
resolveExpiresAtMsFromDurationMs,
@@ -7,9 +8,25 @@ import {
78
type QueryValue = string | number | boolean;
89

910
const RATE_LIMIT_HEADER_NUMBER_RE = /^\d+(?:\.\d+)?$/;
11+
const DISCORD_ROUTE_IDENTIFIER_HASH_LENGTH = 32;
12+
13+
function redactWebhookTokenInPath(path: string): string {
14+
const hasLeadingSlash = path.startsWith("/");
15+
const segments = path.replace(/^\/+/, "").split("/");
16+
if (segments[0] !== "webhooks" || !segments[1] || !segments[2]) {
17+
return path;
18+
}
19+
// Webhook tokens are route identity, but they are also credentials. Keep
20+
// stable grouping without retaining the raw token in scheduler diagnostics.
21+
segments[2] = redactIdentifier(segments[2], {
22+
len: DISCORD_ROUTE_IDENTIFIER_HASH_LENGTH,
23+
});
24+
return `${hasLeadingSlash ? "/" : ""}${segments.join("/")}`;
25+
}
1026

1127
export function createRouteKey(method: string, path: string): string {
12-
return `${method.toUpperCase()} ${path.split("?")[0] ?? path}`;
28+
const pathname = path.split("?")[0] ?? path;
29+
return `${method.toUpperCase()} ${redactWebhookTokenInPath(pathname)}`;
1330
}
1431

1532
function readTopLevelRouteKey(path: string): string {
@@ -19,7 +36,11 @@ function readTopLevelRouteKey(path: string): string {
1936
return pathname;
2037
}
2138
if (first === "channels" || first === "guilds" || first === "webhooks") {
22-
return first === "webhooks" && token ? `${first}/${id}/${token}` : `${first}/${id}`;
39+
return first === "webhooks" && token
40+
? `${first}/${id}/${redactIdentifier(token, {
41+
len: DISCORD_ROUTE_IDENTIFIER_HASH_LENGTH,
42+
})}`
43+
: `${first}/${id}`;
2344
}
2445
return first;
2546
}

0 commit comments

Comments
 (0)