Skip to content

Commit f943c76

Browse files
bmendonca3bmendonca3Takhoffman
authored
security(feishu): bound unauthenticated webhook rate-limit state (#26050) thanks @bmendonca3
Verified: - pnpm install --frozen-lockfile - pnpm build - pnpm check - pnpm test:macmini Co-authored-by: bmendonca3 <[email protected]> Co-authored-by: Tak Hoffman <[email protected]>
1 parent 3882b8a commit f943c76

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Docs: https://docs.openclaw.ai
1313

1414
### Fixes
1515

16+
- Security/Feishu webhook ingress: bound unauthenticated webhook rate-limit state with stale-window pruning and a hard key cap to prevent unbounded pre-auth memory growth from rotating source keys. (#26050) Thanks @bmendonca3.
1617
- Telegram/Reply media context: include replied media files in inbound context when replying to media, defer reply-media downloads to debounce flush, gate reply-media fetch behind DM authorization, and preserve replied media when non-vision sticker fallback runs (including cached-sticker paths). (#28488) Thanks @obviyus.
1718
- Gateway/WS: close repeated post-handshake `unauthorized role:*` request floods per connection and sample duplicate rejection logs, preventing a single misbehaving client from degrading gateway responsiveness. (#20168) Thanks @acy103, @vibecodooor, and @vincentkoc.
1819
- Gateway/Auth: improve device-auth v2 migration diagnostics so operators get clearer guidance when legacy clients connect. (#28305) Thanks @vincentkoc.

extensions/feishu/src/monitor.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ const FEISHU_WEBHOOK_MAX_BODY_BYTES = 1024 * 1024;
2727
const FEISHU_WEBHOOK_BODY_TIMEOUT_MS = 30_000;
2828
const FEISHU_WEBHOOK_RATE_LIMIT_WINDOW_MS = 60_000;
2929
const FEISHU_WEBHOOK_RATE_LIMIT_MAX_REQUESTS = 120;
30+
const FEISHU_WEBHOOK_RATE_LIMIT_MAX_TRACKED_KEYS = 4_096;
3031
const FEISHU_WEBHOOK_COUNTER_LOG_EVERY = 25;
3132
const feishuWebhookRateLimits = new Map<string, { count: number; windowStartMs: number }>();
3233
const feishuWebhookStatusCounters = new Map<string, number>();
34+
let lastWebhookRateLimitCleanupMs = 0;
3335

3436
function isJsonContentType(value: string | string[] | undefined): boolean {
3537
const first = Array.isArray(value) ? value[0] : value;
@@ -40,10 +42,47 @@ function isJsonContentType(value: string | string[] | undefined): boolean {
4042
return mediaType === "application/json" || Boolean(mediaType?.endsWith("+json"));
4143
}
4244

43-
function isWebhookRateLimited(key: string, nowMs: number): boolean {
45+
function trimWebhookRateLimitState(): void {
46+
while (feishuWebhookRateLimits.size > FEISHU_WEBHOOK_RATE_LIMIT_MAX_TRACKED_KEYS) {
47+
const oldestKey = feishuWebhookRateLimits.keys().next().value;
48+
if (typeof oldestKey !== "string") {
49+
break;
50+
}
51+
feishuWebhookRateLimits.delete(oldestKey);
52+
}
53+
}
54+
55+
function maybePruneWebhookRateLimitState(nowMs: number): void {
56+
if (
57+
feishuWebhookRateLimits.size === 0 ||
58+
nowMs - lastWebhookRateLimitCleanupMs < FEISHU_WEBHOOK_RATE_LIMIT_WINDOW_MS
59+
) {
60+
return;
61+
}
62+
lastWebhookRateLimitCleanupMs = nowMs;
63+
for (const [key, state] of feishuWebhookRateLimits) {
64+
if (nowMs - state.windowStartMs >= FEISHU_WEBHOOK_RATE_LIMIT_WINDOW_MS) {
65+
feishuWebhookRateLimits.delete(key);
66+
}
67+
}
68+
}
69+
70+
export function clearFeishuWebhookRateLimitStateForTest(): void {
71+
feishuWebhookRateLimits.clear();
72+
lastWebhookRateLimitCleanupMs = 0;
73+
}
74+
75+
export function getFeishuWebhookRateLimitStateSizeForTest(): number {
76+
return feishuWebhookRateLimits.size;
77+
}
78+
79+
export function isWebhookRateLimitedForTest(key: string, nowMs: number): boolean {
80+
maybePruneWebhookRateLimitState(nowMs);
81+
4482
const state = feishuWebhookRateLimits.get(key);
4583
if (!state || nowMs - state.windowStartMs >= FEISHU_WEBHOOK_RATE_LIMIT_WINDOW_MS) {
4684
feishuWebhookRateLimits.set(key, { count: 1, windowStartMs: nowMs });
85+
trimWebhookRateLimitState();
4786
return false;
4887
}
4988

@@ -54,6 +93,10 @@ function isWebhookRateLimited(key: string, nowMs: number): boolean {
5493
return false;
5594
}
5695

96+
function isWebhookRateLimited(key: string, nowMs: number): boolean {
97+
return isWebhookRateLimitedForTest(key, nowMs);
98+
}
99+
57100
function recordWebhookStatus(
58101
runtime: RuntimeEnv | undefined,
59102
accountId: string,

extensions/feishu/src/monitor.webhook-security.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ vi.mock("./client.js", () => ({
2323
createEventDispatcher: vi.fn(() => ({ register: vi.fn() })),
2424
}));
2525

26-
import { monitorFeishuProvider, stopFeishuMonitor } from "./monitor.js";
26+
import {
27+
clearFeishuWebhookRateLimitStateForTest,
28+
getFeishuWebhookRateLimitStateSizeForTest,
29+
isWebhookRateLimitedForTest,
30+
monitorFeishuProvider,
31+
stopFeishuMonitor,
32+
} from "./monitor.js";
2733

2834
async function getFreePort(): Promise<number> {
2935
const server = createServer();
@@ -114,6 +120,7 @@ async function withRunningWebhookMonitor(
114120
}
115121

116122
afterEach(() => {
123+
clearFeishuWebhookRateLimitStateForTest();
117124
stopFeishuMonitor();
118125
});
119126

@@ -180,4 +187,23 @@ describe("Feishu webhook security hardening", () => {
180187
},
181188
);
182189
});
190+
191+
it("caps tracked webhook rate-limit keys to prevent unbounded growth", () => {
192+
const now = 1_000_000;
193+
for (let i = 0; i < 4_500; i += 1) {
194+
isWebhookRateLimitedForTest(`/feishu-rate-limit:key-${i}`, now);
195+
}
196+
expect(getFeishuWebhookRateLimitStateSizeForTest()).toBeLessThanOrEqual(4_096);
197+
});
198+
199+
it("prunes stale webhook rate-limit state after window elapses", () => {
200+
const now = 2_000_000;
201+
for (let i = 0; i < 100; i += 1) {
202+
isWebhookRateLimitedForTest(`/feishu-rate-limit-stale:key-${i}`, now);
203+
}
204+
expect(getFeishuWebhookRateLimitStateSizeForTest()).toBe(100);
205+
206+
isWebhookRateLimitedForTest("/feishu-rate-limit-stale:fresh", now + 60_001);
207+
expect(getFeishuWebhookRateLimitStateSizeForTest()).toBe(1);
208+
});
183209
});

0 commit comments

Comments
 (0)