Skip to content

Commit 682fbeb

Browse files
committed
Fix security issues in events endpoint: add authentication and fix memory leak
1 parent 54d6e05 commit 682fbeb

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/gateway/server-http.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ export function createGatewayHttpServer(opts: {
505505
}
506506
if (
507507
await handleEventsHttpRequest(req, res, {
508+
auth: resolvedAuth,
508509
trustedProxies,
509510
allowRealIpFallback,
510511
})

src/gateway/server/events-http.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import type { IncomingMessage, ServerResponse } from "node:http";
2+
import type { ResolvedGatewayAuth } from "../server/auth.js";
3+
import { authorizeGatewayHttpRequestOrReply } from "../server/auth.js";
24
import { setSseHeaders, sendJson, sendMethodNotAllowed } from "../http-common.js";
35
import { readJsonBody } from "../hooks.js";
46
import { resolveClientIp } from "../net.js";
57

68
type EventsHttpOptions = {
9+
auth: ResolvedGatewayAuth;
710
trustedProxies?: string[];
811
allowRealIpFallback?: boolean;
912
};
@@ -163,15 +166,30 @@ function checkRateLimit(ip: string): { allowed: boolean; retryAfter?: number } {
163166
return { allowed: false, retryAfter: Math.max(1, Math.ceil(retryAfterMs / 1000)) };
164167
}
165168
recent.push(now);
166-
rateLimitTimestamps.set(ip, recent);
169+
if (recent.length === 0) {
170+
rateLimitTimestamps.delete(ip);
171+
} else {
172+
rateLimitTimestamps.set(ip, recent);
173+
}
167174
return { allowed: true };
168175
}
169176

170177
export async function handleEventsHttpRequest(
171178
req: IncomingMessage,
172179
res: ServerResponse,
173-
opts: EventsHttpOptions = {},
180+
opts: EventsHttpOptions,
174181
): Promise<boolean> {
182+
const requestAuth = await authorizeGatewayHttpRequestOrReply({
183+
req,
184+
res,
185+
auth: opts.auth,
186+
trustedProxies: opts.trustedProxies,
187+
allowRealIpFallback: opts.allowRealIpFallback,
188+
});
189+
if (!requestAuth) {
190+
return true;
191+
}
192+
175193
const url = new URL(req.url ?? "/", `http://${req.headers.host || "localhost"}`);
176194
const pathname = url.pathname;
177195

0 commit comments

Comments
 (0)