feat(security): safeFetch wrapper with manual redirect and timeout defaults

Closes issue 218. Flips the convention from "remember to opt in to
redirect: manual" to "remember to opt out". Every outbound fetch that
doesn't pin a hard-coded host now routes through safeFetch and inherits
AbortSignal.timeout(15_000) so a tar-pit upstream can no longer pin a
worker indefinitely. The local AI client also gates on
requirePublicHost: true since its host is user-supplied; operators
running self-hosted Ollama / LM Studio on an RFC1918 address keep their
escape hatch via ALLOW_LOCAL_AI_PRIVATE_HOSTS=true (also enforced at
write-time in /api/user/ai-provider).

14 outbound call sites migrated. The four that already pinned manual
redirect (the historic templates) keep their semantics; the wrapper
preserves an explicit init.redirect. Adds undici as an explicit
dependency so the connect-time pin landing alongside this can rely on
the documented API rather than a transitive hop.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.7 committed 2026-05-28 22:32:11 +02:00
1 parent bf6dfb8f6d
commit 425503e095
24 files changed
+480 -148

No files matched your search

+10 -6
View File
@@ -2,6 +2,7 @@ import type { WideEvent } from "./types";
import { getLoggingConfig } from "./config";
import { shouldEmit } from "./sampler";
import { appendLogEvent } from "./in-memory-buffer";
import { safeFetch } from "@/lib/safe-fetch";
/** Event auf stdout als einzelne JSON-Zeile schreiben */
function emitToStdout(event: WideEvent): void {
@@ -64,12 +65,15 @@ async function flushLokiBuffer(): Promise<void> {
}
try {
await fetch(`${config.lokiEndpoint}/loki/api/v1/push`, {
method: "POST",
headers,
body: JSON.stringify({ streams }),
signal: AbortSignal.timeout(10_000),
});
await safeFetch(
`${config.lokiEndpoint}/loki/api/v1/push`,
{
method: "POST",
headers,
body: JSON.stringify({ streams }),
},
{ timeoutMs: 10_000 },
);
} catch {
// Events gehen verloren — akzeptabel fuer Logging
}