[GH-ISSUE #2748] [Issue]: Cross-origin redirect in safeFetch re-sends provider API keys (TTS xi-api-key / image-gen Bearer) to the redirect target (key leakage) #1386

Closed
opened 2026-10-21 05:32:48 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 21, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2748

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The shared safeFetch helper follows up to 5 HTTP redirects manually (redirect: "manual" + self-issued fetch), but on each 3xx it re-issues the request to the new Location host without stripping the caller's auth headers. Because the redirect following is manual, the platform's normal cross-origin Authorization-stripping (which only applies to automatic redirect: "follow") is bypassed. Every TTS and image-generation provider call passes the user's plaintext API key in headers, so a provider/proxy that returns a cross-origin 302 (a malicious or compromised user-pasted base URL, or an on-path MITM over the permitted http:) receives the user's key. SSRF re-validation only blocks private/loopback/reserved IPs, not arbitrary public attacker hosts.

This is one root cause (safeFetch) feeding two call-site families; fixing it once fixes both subsystems.

Sub-bug 1: Image-generation Bearer keys leaked (#18)

  • How it breaks: safeFetch destructures headers once (security.ts:542) and re-uses the same headers variable for every redirect iteration. The 3xx branch (security.ts:561-570) validates the new URL and continues the loop without clearing/rewriting headers. Image call sites build { Authorization: \Bearer ${apiKey}` }(OpenAI generations ~677-685, xAI ~710-719, Stability multipart ~1065-1072, chat-completions/NovelAI ~1648-1660) and route throughimageFetch->safeFetch`.
  • Repro: Configure a custom/proxy image provider base URL that returns 302 Location: https://attacker.example/.... Generate an image. The forwarded request to attacker.example carries Authorization: Bearer <user key>.
  • Trace: image-generation.ts:679 sets the Bearer header -> imageFetch -> security.ts:555-560 fetch with redirect: "manual" -> security.ts:561-569 builds new URL(location) and continues with the same headers -> security.ts:554/555 re-sends Authorization to the redirect host.

Sub-bug 2: TTS provider keys leaked (#68/#3-critic)

  • How it breaks: Same safeFetch mechanism. TTS speak/voice-list calls pass xi-api-key (ElevenLabs, tts.routes.ts ~312-316), Authorization: Bearer (OpenAI ~318-322), or x-api-key + Bearer (NanoGPT ~324-328) into safeFetch. The POST body and method are also re-sent to the new host.
  • Repro: Point the TTS base URL at a host that 3xx-redirects cross-origin (or MITM the permitted plaintext http:). The redirect target receives the TTS provider key (and POST body).
  • Trace: tts.routes.ts:632-677 passes provider auth headers into safeFetch -> security.ts:555-560 replays them on each redirect with no same-origin guard.

Suggested Fix

In the safeFetch redirect branch, when the redirect target's origin differs from the current request origin, drop sensitive request headers (Authorization, xi-api-key, x-api-key, api-key, Cookie) and the request body before following — mirroring browser/undici cross-origin credential stripping. Only retain credentials on same-origin redirects. Consider also constraining redirects to the originally-configured provider host.

Verification checklist (all manual)

  • Reproduce key leakage with a test endpoint that returns a cross-origin 302 and capture the forwarded Authorization/xi-api-key header
  • Confirm same-origin redirects still carry credentials after the fix
  • Confirm cross-origin redirects drop Authorization/xi-api-key/x-api-key/Cookie and body after the fix
  • Verify normal (non-redirecting) OpenAI/ElevenLabs/Stability TTS and image calls still succeed
  • Run pnpm check

Affected files

  • packages/server/src/utils/security.ts
  • packages/server/src/routes/tts.routes.ts
  • packages/server/src/services/image/image-generation.ts

Audit provenance

Found by a multi-agent static audit of the TTS/voice + image-generation subsystems (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified) against the merged staging tree. Severity: HIGH.

AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.

Originally created by @kolacheee on GitHub (Jun 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2748 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The shared `safeFetch` helper follows up to 5 HTTP redirects manually (`redirect: "manual"` + self-issued fetch), but on each 3xx it re-issues the request to the new `Location` host **without stripping the caller's auth headers**. Because the redirect following is manual, the platform's normal cross-origin Authorization-stripping (which only applies to automatic `redirect: "follow"`) is bypassed. Every TTS and image-generation provider call passes the user's plaintext API key in headers, so a provider/proxy that returns a cross-origin `302` (a malicious or compromised user-pasted base URL, or an on-path MITM over the permitted `http:`) receives the user's key. SSRF re-validation only blocks private/loopback/reserved IPs, not arbitrary public attacker hosts. This is one root cause (`safeFetch`) feeding two call-site families; fixing it once fixes both subsystems. ## Sub-bug 1: Image-generation Bearer keys leaked (#18) - **How it breaks:** `safeFetch` destructures `headers` once (security.ts:542) and re-uses the same `headers` variable for every redirect iteration. The 3xx branch (security.ts:561-570) validates the new URL and `continue`s the loop without clearing/rewriting `headers`. Image call sites build `{ Authorization: \`Bearer ${apiKey}\` }` (OpenAI generations ~677-685, xAI ~710-719, Stability multipart ~1065-1072, chat-completions/NovelAI ~1648-1660) and route through `imageFetch` -> `safeFetch`. - **Repro:** Configure a custom/proxy image provider base URL that returns `302 Location: https://attacker.example/...`. Generate an image. The forwarded request to attacker.example carries `Authorization: Bearer <user key>`. - **Trace:** image-generation.ts:679 sets the Bearer header -> imageFetch -> security.ts:555-560 fetch with `redirect: "manual"` -> security.ts:561-569 builds `new URL(location)` and `continue`s with the same `headers` -> security.ts:554/555 re-sends Authorization to the redirect host. ## Sub-bug 2: TTS provider keys leaked (#68/#3-critic) - **How it breaks:** Same `safeFetch` mechanism. TTS speak/voice-list calls pass `xi-api-key` (ElevenLabs, tts.routes.ts ~312-316), `Authorization: Bearer` (OpenAI ~318-322), or `x-api-key` + Bearer (NanoGPT ~324-328) into `safeFetch`. The POST body and method are also re-sent to the new host. - **Repro:** Point the TTS base URL at a host that 3xx-redirects cross-origin (or MITM the permitted plaintext `http:`). The redirect target receives the TTS provider key (and POST body). - **Trace:** tts.routes.ts:632-677 passes provider auth `headers` into `safeFetch` -> security.ts:555-560 replays them on each redirect with no same-origin guard. ## Suggested Fix In the `safeFetch` redirect branch, when the redirect target's origin differs from the current request origin, drop sensitive request headers (`Authorization`, `xi-api-key`, `x-api-key`, `api-key`, `Cookie`) and the request body before following — mirroring browser/undici cross-origin credential stripping. Only retain credentials on same-origin redirects. Consider also constraining redirects to the originally-configured provider host. ## Verification checklist (all manual) - [ ] Reproduce key leakage with a test endpoint that returns a cross-origin 302 and capture the forwarded Authorization/xi-api-key header - [ ] Confirm same-origin redirects still carry credentials after the fix - [ ] Confirm cross-origin redirects drop Authorization/xi-api-key/x-api-key/Cookie and body after the fix - [ ] Verify normal (non-redirecting) OpenAI/ElevenLabs/Stability TTS and image calls still succeed - [ ] Run `pnpm check` --- ### Affected files - `packages/server/src/utils/security.ts` - `packages/server/src/routes/tts.routes.ts` - `packages/server/src/services/image/image-generation.ts` ### Audit provenance Found by a multi-agent static audit of the TTS/voice + image-generation subsystems (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified) against the merged `staging` tree. Severity: **HIGH**. > AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 21, 2026):

Addressed on PR #2747. safeFetch now tracks redirect state across manual redirects and, on a cross-origin hop, strips credential-like headers (Authorization/proxy auth/Cookie/xi-api-key/x-api-key/api-key) plus content headers and drops the request body before following. Same-origin redirects keep the original headers/body. This central fix covers the TTS and image-generation provider calls that route through safeFetch. Deferred full pnpm validation until the end of the issue sweep, per maintainer request.

<!-- gh-comment-id:4763256734 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Addressed on PR #2747. safeFetch now tracks redirect state across manual redirects and, on a cross-origin hop, strips credential-like headers (Authorization/proxy auth/Cookie/xi-api-key/x-api-key/api-key) plus content headers and drops the request body before following. Same-origin redirects keep the original headers/body. This central fix covers the TTS and image-generation provider calls that route through safeFetch. Deferred full pnpm validation until the end of the issue sweep, per maintainer request.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1386
No description provided.