fix: safely get IP

This commit is contained in:
2026-03-27 19:33:47 +01:00
parent 1ea168807f
commit 3a5c8570ee

View File

@@ -46,6 +46,15 @@ if (!building) {
}); });
} }
/** Safe IP getter — returns null for internal requests missing the ADDRESS_HEADER */
function getIp(event: Parameters<Handle>[0]['event']): string | null {
try {
return event.getClientAddress();
} catch {
return null;
}
}
// ── Request logging middleware ── // ── Request logging middleware ──
// Paths that are too noisy to log every time (healthchecks, polling) // Paths that are too noisy to log every time (healthchecks, polling)
const SILENT_PATHS = new Set(['/api/health', '/api/queue']); const SILENT_PATHS = new Set(['/api/health', '/api/queue']);
@@ -78,7 +87,7 @@ const handleRequestLogging: Handle = async ({ event, resolve }) => {
status, status,
duration, duration,
userId: userId ?? null, userId: userId ?? null,
ip: event.getClientAddress() ip: getIp(event)
}, },
`${event.request.method} ${path} ${status} ${duration}ms` `${event.request.method} ${path} ${status} ${duration}ms`
); );
@@ -97,7 +106,7 @@ const handleRequestLogging: Handle = async ({ event, resolve }) => {
const handleBetterAuth: Handle = async ({ event, resolve }) => { const handleBetterAuth: Handle = async ({ event, resolve }) => {
// Rate limiting for API routes // Rate limiting for API routes
if (event.url.pathname.startsWith('/api/')) { if (event.url.pathname.startsWith('/api/')) {
const ip = event.getClientAddress(); const ip = getIp(event) ?? 'unknown';
// Stricter limit for auth endpoints: 10 req/min per IP // Stricter limit for auth endpoints: 10 req/min per IP
if (event.url.pathname.startsWith('/api/auth/')) { if (event.url.pathname.startsWith('/api/auth/')) {