[GH-ISSUE #2656] [Issue]: Providers send auth headers built from empty/blank API keys (decrypt failure or keyless endpoint) → silent 401s #1307

Closed
opened 2026-10-21 05:31:16 +07:00 by bsakaguchi · 0 comments
Owner

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

Originally assigned to: @kolacheee on GitHub.

Summary

The LLM provider adapters build their auth headers unconditionally from the connection's API key string. When that string is empty — either because decryptApiKey() failed to decrypt a stored key, or because the connection is intentionally keyless (local/custom endpoint) — the outbound request still carries a blank credential (Authorization: Bearer / x-api-key: / x-goog-api-key:), producing opaque 401s on every generation with no actionable signal to the user.

Expected behavior

  • If a stored API key fails to decrypt (e.g. the encryption key changed), generation should fail with a clear, user-facing error such as "API key could not be decrypted — please re-enter it," distinguishable from a wrong-key 401.
  • For intentionally keyless endpoints, no Authorization/x-api-key header should be sent at all — a blank credential is worse than none.

Actual behavior

  • decryptApiKey() returns "" both when the stored value is empty and when decryption throws, logging only a server-side logger.warn.
  • Every consumer passes that "" straight into createLLMProvider() with no emptiness guard.
  • Providers then emit a blank credential header; the API returns 401 and the UI shows a generic auth error indistinguishable from a wrong key.

How it breaks the pipeline

Code trace (staging @ b826f8f4):

  • decryptApiKey() — packages/server/src/utils/crypto.ts:65 — if (!encrypted) return ""; (line 66) and the catch { logger.warn(...); return ""; } (lines 78–82) both yield "". The only difference between "no key" and "key undecryptable" is a warn log.
  • Consumers in packages/server/src/services/storage/connections.storage.ts (getWithKey, getDefaultForAgents, getDefaultForImageGeneration, listRandomPool) forward the decrypted value without an emptiness check.
  • Header construction is unconditional:
    • OpenAI: packages/server/src/services/llm/providers/openai.provider.ts:439 → Authorization: \Bearer ${this.apiKey}`(insidebuildHeaders()at:436`)
    • Anthropic: packages/server/src/services/llm/providers/anthropic.provider.ts:280 and :437 → "x-api-key": this.apiKey
    • Google: packages/server/src/services/llm/providers/google.provider.ts:140, :407, :567 → "x-goog-api-key": ...

Result: the request goes out with an empty token; the API rejects it with 401; the failure is indistinguishable from a wrong key.

Steps to reproduce

Manifestation A — decrypt failure (high impact):

  1. Save a connection with a valid API key (encrypted at rest using <DATA_DIR>/.encryption-key).
  2. Change the encryption key out from under it: delete/regenerate <DATA_DIR>/.encryption-key, move DATA_DIR, or set/unset ENCRYPTION_KEY to a different value; restart the server.
  3. Send any message.
  4. Observe: the request is sent with an empty Authorization/x-api-key header → 401. The UI reports a generic auth failure; only a [CRYPTO] Failed to decrypt API key warn appears server-side.

Manifestation B — keyless endpoint (low impact):

  1. Create a "custom"/local OpenAI-compatible connection with an empty API key.
  2. Send a message.
  3. Observe: the outbound request includes Authorization: Bearer (empty token); some local servers/proxies reject the malformed header.

Suggested fix

  • In decryptApiKey(), distinguish "empty stored key" from "decrypt failure." On decrypt failure, throw or return a typed sentinel so callers can surface a clear "key could not be decrypted — re-enter it" error instead of silently sending a blank credential.
  • In each provider's header builder, only attach the auth header when the key is non-empty (skip Authorization/x-api-key/x-goog-api-key for blank keys) so keyless endpoints get a clean request.
  • Optionally, guard at the generate route: if a connection has a non-empty apiKeyEncrypted but the decrypted key is empty, refuse to send and surface the decrypt error.

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Identified via a systematic source-level review of the connection-settings + preset → LLM-request pipeline on the staging branch; file/line references are against b826f8f4. The decrypt-failure manifestation is especially likely after a reinstall, moving the data directory, or changing ENCRYPTION_KEY. Filed alongside a set of related pipeline issues.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jun 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2656 Originally assigned to: @kolacheee on GitHub. ## Summary The LLM provider adapters build their auth headers unconditionally from the connection's API key string. When that string is empty — either because `decryptApiKey()` failed to decrypt a stored key, or because the connection is intentionally keyless (local/custom endpoint) — the outbound request still carries a blank credential (`Authorization: Bearer ` / `x-api-key:` / `x-goog-api-key:`), producing opaque 401s on every generation with no actionable signal to the user. ## Expected behavior - If a stored API key fails to decrypt (e.g. the encryption key changed), generation should fail with a clear, user-facing error such as "API key could not be decrypted — please re-enter it," distinguishable from a wrong-key 401. - For intentionally keyless endpoints, no `Authorization`/`x-api-key` header should be sent at all — a blank credential is worse than none. ## Actual behavior - `decryptApiKey()` returns `""` both when the stored value is empty **and** when decryption throws, logging only a server-side `logger.warn`. - Every consumer passes that `""` straight into `createLLMProvider()` with no emptiness guard. - Providers then emit a blank credential header; the API returns 401 and the UI shows a generic auth error indistinguishable from a wrong key. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`): - `decryptApiKey()` — `packages/server/src/utils/crypto.ts:65` — `if (!encrypted) return "";` (line 66) and the `catch { logger.warn(...); return ""; }` (lines 78–82) both yield `""`. The only difference between "no key" and "key undecryptable" is a warn log. - Consumers in `packages/server/src/services/storage/connections.storage.ts` (`getWithKey`, `getDefaultForAgents`, `getDefaultForImageGeneration`, `listRandomPool`) forward the decrypted value without an emptiness check. - Header construction is unconditional: - OpenAI: `packages/server/src/services/llm/providers/openai.provider.ts:439` → `Authorization: \`Bearer ${this.apiKey}\`` (inside `buildHeaders()` at `:436`) - Anthropic: `packages/server/src/services/llm/providers/anthropic.provider.ts:280` and `:437` → `"x-api-key": this.apiKey` - Google: `packages/server/src/services/llm/providers/google.provider.ts:140`, `:407`, `:567` → `"x-goog-api-key": ...` Result: the request goes out with an empty token; the API rejects it with 401; the failure is indistinguishable from a wrong key. ## Steps to reproduce **Manifestation A — decrypt failure (high impact):** 1. Save a connection with a valid API key (encrypted at rest using `<DATA_DIR>/.encryption-key`). 2. Change the encryption key out from under it: delete/regenerate `<DATA_DIR>/.encryption-key`, move `DATA_DIR`, or set/unset `ENCRYPTION_KEY` to a different value; restart the server. 3. Send any message. 4. Observe: the request is sent with an empty `Authorization`/`x-api-key` header → 401. The UI reports a generic auth failure; only a `[CRYPTO] Failed to decrypt API key` warn appears server-side. **Manifestation B — keyless endpoint (low impact):** 1. Create a "custom"/local OpenAI-compatible connection with an empty API key. 2. Send a message. 3. Observe: the outbound request includes `Authorization: Bearer ` (empty token); some local servers/proxies reject the malformed header. ## Suggested fix - In `decryptApiKey()`, distinguish "empty stored key" from "decrypt failure." On decrypt failure, throw or return a typed sentinel so callers can surface a clear "key could not be decrypted — re-enter it" error instead of silently sending a blank credential. - In each provider's header builder, only attach the auth header when the key is non-empty (skip `Authorization`/`x-api-key`/`x-goog-api-key` for blank keys) so keyless endpoints get a clean request. - Optionally, guard at the generate route: if a connection has a non-empty `apiKeyEncrypted` but the decrypted key is empty, refuse to send and surface the decrypt error. ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Identified via a systematic source-level review of the connection-settings + preset → LLM-request pipeline on the staging branch; file/line references are against `b826f8f4`. The decrypt-failure manifestation is especially likely after a reinstall, moving the data directory, or changing `ENCRYPTION_KEY`. Filed alongside a set of related pipeline issues. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:31:16 +07:00
  • closed this issue
  • added the
    in-pr
    label
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#1307
No description provided.