[GH-ISSUE #3037] [Issue]: Mutating connection routes return the unmasked apiKeyEncrypted ciphertext, breaking the masking contract on the GET routes #1605

Closed
opened 2026-10-21 05:40:35 +07:00 by bsakaguchi · 2 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

The read routes deliberately mask the stored key: GET / (via storage.list) and GET /:id both overwrite apiKeyEncrypted with "••••••••" before returning. But the create/update/duplicate/image-upload routes return the raw DB row straight from storage (each ends in getById, which selects the full row), so the encrypted key blob (iv:ciphertext:authTag hex) is sent back to the client on every create, patch, duplicate, and connection-image upload. This contradicts the masking the codebase clearly intends and leaks the ciphertext into response bodies, the browser network tab, and any client-side log capture. The value is AES-GCM ciphertext, not plaintext — not directly usable without the on-disk encryption key, hence low severity — but it is still an unintended disclosure of a field the app otherwise treats as never-returnable, and it leaks key length/uniqueness and the fact that a key is set.

How to reproduce (try it yourself)

  1. POST /api/connections with a non-empty apiKey.
  2. Inspect the JSON response body.
  3. Compare against GET /api/connections/:id on the same id.

You'll see: the POST response contains apiKeyEncrypted: "<iv>:<cipher>:<tag>" (the real ciphertext) instead of the masked "••••••••". Same for PATCH /api/connections/:id (any field), POST /api/connections/:id/duplicate, and POST /api/connections/:id/image. The GET route correctly returns apiKeyEncrypted: "••••••••".

Expected: all four mutation responses mask apiKeyEncrypted identically to the GET routes.

Where it is

packages/server/src/routes/connections.routes.ts: POST / (lines 253-258, return created;), PATCH /:id (260-265, return updated;), POST /:id/image (286-288, return updated;), POST /:id/duplicate (329-333, return result;). Root cause in packages/server/src/services/storage/connections.storage.ts: getById returns the full row (lines 19-22), and create/update/duplicate all return this.getById(...) (lines 111/203/243). Masking already applied at connections.storage.ts:16 (list) and connections.routes.ts:250 (GET /:id). No response schema or onSend hook strips the field (app.ts:131-136 only sets Cache-Control).

What's happening

Masking of apiKeyEncrypted is applied ad hoc only on the two GET handlers, not centralized in storage. The write handlers forward getById's full row, which includes the unmasked apiKeyEncrypted column, bypassing the masking the read paths apply.

Suggested fix

Apply the SAME inline masking the GET route already uses (connections.routes.ts:250) to the four write handlers, at the route layer only — do NOT move masking into storage/getById. Add one route-local null-safe helper near the top of connectionsRoutes (after const storage = ...):

const maskConnection = <T extends { apiKeyEncrypted?: unknown } | null>(conn: T): T =>
  conn ? { ...conn, apiKeyEncrypted: (conn as { apiKeyEncrypted?: unknown }).apiKeyEncrypted ? "••••••••" : "" } : conn;

Then return maskConnection(created); (POST /), return maskConnection(updated); (PATCH /:id — preserves the null→200 behavior since the helper passes null through), and after the existing if (!updated)/if (!result) 404 guards, return maskConnection(updated); / return maskConnection(result); for the image and duplicate routes.

Regression avoided: do NOT mask inside getById() or any shared helper it feeds. getWithKey() (connections.storage.ts:25-28) is built on getById() and decrypts apiKeyEncrypted — a masked value would decrypt to an empty key and silently send EMPTY API keys on every provider request (test, models, generate, agents, and all internal callers). duplicate() reads getById() and copies apiKeyEncrypted into the new row — a masked value would permanently corrupt the cloned connection's stored key. The route-handler approach has neither problem: getById() keeps returning the raw row for internal decryption/duplication, and only the four client-facing responses get masked. Verified the client never reads apiKeyEncrypted back from a mutation response, so no round-trip breaks.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3037 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** The read routes deliberately mask the stored key: `GET /` (via `storage.list`) and `GET /:id` both overwrite `apiKeyEncrypted` with `"••••••••"` before returning. But the create/update/duplicate/image-upload routes return the raw DB row straight from storage (each ends in `getById`, which selects the full row), so the encrypted key blob (`iv:ciphertext:authTag` hex) is sent back to the client on every create, patch, duplicate, and connection-image upload. This contradicts the masking the codebase clearly intends and leaks the ciphertext into response bodies, the browser network tab, and any client-side log capture. The value is AES-GCM ciphertext, not plaintext — not directly usable without the on-disk encryption key, hence low severity — but it is still an unintended disclosure of a field the app otherwise treats as never-returnable, and it leaks key length/uniqueness and the fact that a key is set. **How to reproduce (try it yourself)** 1. `POST /api/connections` with a non-empty `apiKey`. 2. Inspect the JSON response body. 3. Compare against `GET /api/connections/:id` on the same id. You'll see: the POST response contains `apiKeyEncrypted: "<iv>:<cipher>:<tag>"` (the real ciphertext) instead of the masked `"••••••••"`. Same for `PATCH /api/connections/:id` (any field), `POST /api/connections/:id/duplicate`, and `POST /api/connections/:id/image`. The `GET` route correctly returns `apiKeyEncrypted: "••••••••"`. Expected: all four mutation responses mask `apiKeyEncrypted` identically to the GET routes. **Where it is** `packages/server/src/routes/connections.routes.ts`: `POST /` (lines 253-258, `return created;`), `PATCH /:id` (260-265, `return updated;`), `POST /:id/image` (286-288, `return updated;`), `POST /:id/duplicate` (329-333, `return result;`). Root cause in `packages/server/src/services/storage/connections.storage.ts`: `getById` returns the full row (lines 19-22), and `create`/`update`/`duplicate` all `return this.getById(...)` (lines 111/203/243). Masking already applied at `connections.storage.ts:16` (list) and `connections.routes.ts:250` (GET /:id). No response schema or onSend hook strips the field (`app.ts:131-136` only sets Cache-Control). **What's happening** Masking of `apiKeyEncrypted` is applied ad hoc only on the two GET handlers, not centralized in storage. The write handlers forward `getById`'s full row, which includes the unmasked `apiKeyEncrypted` column, bypassing the masking the read paths apply. **Suggested fix** Apply the SAME inline masking the GET route already uses (`connections.routes.ts:250`) to the four write handlers, **at the route layer only** — do NOT move masking into storage/`getById`. Add one route-local null-safe helper near the top of `connectionsRoutes` (after `const storage = ...`): ```ts const maskConnection = <T extends { apiKeyEncrypted?: unknown } | null>(conn: T): T => conn ? { ...conn, apiKeyEncrypted: (conn as { apiKeyEncrypted?: unknown }).apiKeyEncrypted ? "••••••••" : "" } : conn; ``` Then `return maskConnection(created);` (POST /), `return maskConnection(updated);` (PATCH /:id — preserves the null→200 behavior since the helper passes null through), and after the existing `if (!updated)`/`if (!result)` 404 guards, `return maskConnection(updated);` / `return maskConnection(result);` for the image and duplicate routes. **Regression avoided:** do NOT mask inside `getById()` or any shared helper it feeds. `getWithKey()` (`connections.storage.ts:25-28`) is built on `getById()` and decrypts `apiKeyEncrypted` — a masked value would decrypt to an empty key and silently send EMPTY API keys on every provider request (test, models, generate, agents, and all internal callers). `duplicate()` reads `getById()` and copies `apiKeyEncrypted` into the new row — a masked value would permanently corrupt the cloned connection's stored key. The route-handler approach has neither problem: `getById()` keeps returning the raw row for internal decryption/duplication, and only the four client-facing responses get masked. Verified the client never reads `apiKeyEncrypted` back from a mutation response, so no round-trip breaks. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

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

Claiming this for the current staging sweep in draft PR #3053 (branch codex/issue-sweep-3033-3052).

<!-- gh-comment-id:4834987447 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Claiming this for the current staging sweep in draft PR #3053 (branch `codex/issue-sweep-3033-3052`).
Author
Owner

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

Fixed in PR #3053 on branch codex/issue-sweep-3033-3052 (commit bfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.

<!-- gh-comment-id:4835087034 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in PR #3053 on branch `codex/issue-sweep-3033-3052` (commit bfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.
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#1605
No description provided.