security(nextcloud-talk): reject unsigned webhooks before body read

This commit is contained in:
Brian Mendonca
2026-02-24 21:36:04 -07:00
committed by Peter Steinberger
parent 38c4944d76
commit 461d14557a
3 changed files with 77 additions and 2 deletions

View File

@@ -0,0 +1,73 @@
import { type AddressInfo } from "node:net";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createNextcloudTalkWebhookServer } from "./monitor.js";
type WebhookHarness = {
webhookUrl: string;
stop: () => Promise<void>;
};
const cleanupFns: Array<() => Promise<void>> = [];
afterEach(async () => {
while (cleanupFns.length > 0) {
const cleanup = cleanupFns.pop();
if (cleanup) {
await cleanup();
}
}
});
async function startWebhookServer(params: {
path: string;
maxBodyBytes: number;
readBody?: (req: import("node:http").IncomingMessage, maxBodyBytes: number) => Promise<string>;
}): Promise<WebhookHarness> {
const { server, start } = createNextcloudTalkWebhookServer({
port: 0,
host: "127.0.0.1",
path: params.path,
secret: "nextcloud-secret",
maxBodyBytes: params.maxBodyBytes,
readBody: params.readBody,
onMessage: vi.fn(),
});
await start();
const address = server.address() as AddressInfo | null;
if (!address) {
throw new Error("missing server address");
}
return {
webhookUrl: `http://127.0.0.1:${address.port}${params.path}`,
stop: () =>
new Promise<void>((resolve) => {
server.close(() => resolve());
}),
};
}
describe("createNextcloudTalkWebhookServer auth order", () => {
it("rejects missing signature headers before reading request body", async () => {
const readBody = vi.fn(async () => {
throw new Error("should not be called for missing signature headers");
});
const harness = await startWebhookServer({
path: "/nextcloud-auth-order",
maxBodyBytes: 128,
readBody,
});
cleanupFns.push(harness.stop);
const response = await fetch(harness.webhookUrl, {
method: "POST",
headers: {
"content-type": "application/json",
},
body: "{}",
});
expect(response.status).toBe(400);
expect(await response.json()).toEqual({ error: "Missing signature headers" });
expect(readBody).not.toHaveBeenCalled();
});
});

View File

@@ -92,6 +92,7 @@ export function createNextcloudTalkWebhookServer(opts: NextcloudTalkWebhookServe
opts.maxBodyBytes > 0
? Math.floor(opts.maxBodyBytes)
: DEFAULT_WEBHOOK_MAX_BODY_BYTES;
const readBody = opts.readBody ?? readNextcloudTalkWebhookBody;
const server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
if (req.url === HEALTH_PATH) {
@@ -107,8 +108,6 @@ export function createNextcloudTalkWebhookServer(opts: NextcloudTalkWebhookServe
}
try {
const body = await readNextcloudTalkWebhookBody(req, maxBodyBytes);
const headers = extractNextcloudTalkHeaders(
req.headers as Record<string, string | string[] | undefined>,
);
@@ -118,6 +117,8 @@ export function createNextcloudTalkWebhookServer(opts: NextcloudTalkWebhookServe
return;
}
const body = await readBody(req, maxBodyBytes);
const isValid = verifyNextcloudTalkSignature({
signature: headers.signature,
random: headers.random,

View File

@@ -169,6 +169,7 @@ export type NextcloudTalkWebhookServerOptions = {
path: string;
secret: string;
maxBodyBytes?: number;
readBody?: (req: import("node:http").IncomingMessage, maxBodyBytes: number) => Promise<string>;
onMessage: (message: NextcloudTalkInboundMessage) => void | Promise<void>;
onError?: (error: Error) => void;
abortSignal?: AbortSignal;