Skip to content

Commit 05a918b

Browse files
committed
fix(nextcloud-talk): isolate proxy webhook rate limits
1 parent 4473674 commit 05a918b

5 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎extensions/nextcloud-talk/src/monitor-runtime.abort.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ describe("Nextcloud Talk monitor abort", () => {
2525
}));
2626
const monitor = await monitorNextcloudTalkProvider({
2727
config: {
28+
gateway: {
29+
trustedProxies: ["127.0.0.1"],
30+
allowRealIpFallback: true,
31+
},
2832
channels: {
2933
"nextcloud-talk": {
3034
baseUrl: "https://cloud.example.com",
@@ -42,6 +46,12 @@ describe("Nextcloud Talk monitor abort", () => {
4246
expect(createSpool).toHaveBeenCalledWith(
4347
expect.objectContaining({ abortSignal: abortController.signal }),
4448
);
49+
expect(createServer).toHaveBeenCalledWith(
50+
expect.objectContaining({
51+
trustedProxies: ["127.0.0.1"],
52+
allowRealIpFallback: true,
53+
}),
54+
);
4555
expect(statusSink).toHaveBeenCalledExactlyOnceWith({
4656
running: true,
4757
connected: true,

‎extensions/nextcloud-talk/src/monitor-runtime.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ export async function monitorNextcloudTalkProvider(
109109
onError: (error) => {
110110
logger.error(`[nextcloud-talk:${account.accountId}] webhook error: ${error.message}`);
111111
},
112+
trustedProxies: cfg.gateway?.trustedProxies,
113+
allowRealIpFallback: cfg.gateway?.allowRealIpFallback,
112114
abortSignal: opts.abortSignal,
113115
});
114116

‎extensions/nextcloud-talk/src/monitor.replay.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,41 @@ describe("createNextcloudTalkWebhookServer auth rate limiting", () => {
330330
expect(await lastResponse?.text()).toBe("Too Many Requests");
331331
});
332332

333+
it("isolates failed-auth limits by forwarded client behind a trusted proxy", async () => {
334+
const harness = await startWebhookServer({
335+
path: "/nextcloud-auth-rate-limit-trusted-proxy",
336+
authRateLimit: { maxRequests: 1 },
337+
trustedProxies: ["127.0.0.1"],
338+
onMessage: vi.fn(),
339+
});
340+
const { body, headers } = createSignedCreateMessageRequest();
341+
const attackerHeaders = {
342+
...headers,
343+
"x-forwarded-for": "198.51.100.10",
344+
"x-nextcloud-talk-signature": "invalid-signature",
345+
};
346+
347+
const firstAttack = await fetch(harness.webhookUrl, {
348+
method: "POST",
349+
headers: attackerHeaders,
350+
body,
351+
});
352+
const blockedAttack = await fetch(harness.webhookUrl, {
353+
method: "POST",
354+
headers: attackerHeaders,
355+
body,
356+
});
357+
const legitimateDelivery = await fetch(harness.webhookUrl, {
358+
method: "POST",
359+
headers: { ...headers, "x-forwarded-for": "198.51.100.11" },
360+
body,
361+
});
362+
363+
expect(firstAttack.status).toBe(401);
364+
expect(blockedAttack.status).toBe(429);
365+
expect(legitimateDelivery.status).toBe(200);
366+
});
367+
333368
it("does not rate limit valid signed webhook bursts from the same source", async () => {
334369
const maxRequests = 1;
335370
const harness = await startWebhookServer({

‎extensions/nextcloud-talk/src/monitor.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
createAuthRateLimiter,
77
isRequestBodyLimitError,
88
readRequestBodyWithLimit,
9+
resolveRequestClientIp,
910
requestBodyErrorToText,
1011
} from "openclaw/plugin-sdk/webhook-ingress";
1112
import { extractNextcloudTalkHeaders, verifyNextcloudTalkSignature } from "./signature.js";
@@ -144,7 +145,8 @@ export function createNextcloudTalkWebhookServer(opts: NextcloudTalkWebhookServe
144145
return;
145146
}
146147

147-
const clientIp = req.socket.remoteAddress ?? "unknown";
148+
const clientIp =
149+
resolveRequestClientIp(req, opts.trustedProxies, opts.allowRealIpFallback) ?? "unknown";
148150
if (!webhookAuthRateLimiter.check(clientIp, WEBHOOK_AUTH_RATE_LIMIT_SCOPE).allowed) {
149151
res.writeHead(429);
150152
res.end("Too Many Requests");

‎extensions/nextcloud-talk/src/types.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import type {
33
ChannelDeliveryStreamingConfig,
44
MessageReceipt,
55
} from "openclaw/plugin-sdk/channel-outbound";
6-
import type { DmConfig, DmPolicy, GroupPolicy, SecretInput } from "../runtime-api.js";
6+
import type {
7+
DmConfig,
8+
DmPolicy,
9+
GroupPolicy,
10+
OpenClawConfig,
11+
SecretInput,
12+
} from "../runtime-api.js";
713

814
export type NextcloudTalkRoomConfig = {
915
requireMention?: boolean;
@@ -88,6 +94,7 @@ export type CoreConfig = {
8894
channels?: {
8995
"nextcloud-talk"?: NextcloudTalkConfig;
9096
};
97+
gateway?: OpenClawConfig["gateway"];
9198
[key: string]: unknown;
9299
};
93100

@@ -179,6 +186,8 @@ export type NextcloudTalkWebhookServerOptions = {
179186
};
180187
readBody?: (req: import("node:http").IncomingMessage, maxBodyBytes: number) => Promise<string>;
181188
isBackendAllowed?: (backend: string) => boolean;
189+
trustedProxies?: string[];
190+
allowRealIpFallback?: boolean;
182191
onWebhook: (rawBody: string) => Promise<"accepted" | "ignored">;
183192
onError?: (error: Error) => void;
184193
abortSignal?: AbortSignal;

0 commit comments

Comments
 (0)