[GH-ISSUE #2611] [Issue]: Profile restore clobbers live secrets (API keys, agent tokens, webhook URLs) by upserting redacted snapshot rows #1284

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

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

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: High — data-loss. Restoring a profile export over an instance that still holds rows with matching primary keys silently wipes the live api_connections.apiKeyEncrypted, agent_configs secret settings, and custom_tools.webhookUrl — because the export redacts those columns and the restore upserts the redacted blanks onto existing rows. The secrets are absent from the archive, so the loss is unrecoverable from the backup.

Expected behavior

Restoring a profile to recover deleted entities should not destroy secrets on entities that still exist. A redacted/blank secret in the snapshot must never overwrite a non-empty secret already stored in the DB.

Actual behavior

The export intentionally redacts secret columns into the snapshot (sanitizeProfileTableRows): api_connections.apiKeyEncrypted -> "", agent_configs secret settings -> null (via redactAgentSecrets, SECRET_KEY_RE matching token/secret/password/api_key), custom_tools.webhookUrl -> "".

On restore, importProfileStorageSnapshot upserts each snapshot row with onConflictDoUpdate({ target: <pk>, set: cleanRow }), and for api_connections it additionally force-blanks the key on the update path:

// packages/server/src/routes/backup.routes.ts (~636-659)
// cleanRow is built from the redacted snapshot row
// api_connections path additionally sets apiKeyEncrypted = "" (~648)
await db.insert(table)
  .values(cleanRow)
  .onConflictDoUpdate({ target: primaryKey, set: cleanRow }); // ~651

Because the conflict UPDATE writes the whole redacted row, any pre-existing live row with the same primary-key id has its apiKeyEncrypted set to "", its agent secret settings set to null, and its custom_tools.webhookUrl set to "". Reached via POST /api/backup/import-profile (requirePrivilegedAccess is the only gate; previewOnly/fingerprint checks do not protect the secret columns). No DB wipe precedes the upsert and there is no merge/skip-existing/COALESCE guard. SettingsPanel.tsx advertises this exact "Restore full profiles … for recovery and migration" flow.

Steps to reproduce

  1. Configure at least one API connection (with an API key), one agent with a secret setting, and one custom tool with a webhook URL.
  2. Export the profile (JSON or ZIP).
  3. Without deleting those entities, re-import/restore that same export onto the instance (POST /api/backup/import-profile).
  4. Observe that the live apiKeyEncrypted / agent secrets / webhookUrl for the still-present rows are now blank/null, and are not present anywhere in the archive.

Environment

  • Marinara Engine version: staging @ 56f700cd (v2.0.0)
  • Install type: source
  • OS + version: n/a — static source audit
  • Browser or app shell: n/a

Logs, screenshots, or video

Static source audit, cited to file:line above.

Additional context

Fix sketch. On the conflict UPDATE path, do not overwrite the redacted secret columns: drop apiKeyEncrypted / webhookUrl / the redacted agent-secret subfields from the set object so onConflictDoUpdate preserves the existing DB value, or read the existing row and COALESCE-merge so a blank/null redacted value never overwrites a non-empty stored secret. Keep the blanks only on the fresh-insert path.

Regression notes. Not a regression — the core mechanism is byte-identical on main (backup.routes.ts has the same sanitizeProfileTableRows, the same apiKeyEncrypted="" on the import update path, and the same onConflictDoUpdate({ target: primaryKey, set: cleanRow })). The only staging-only delta is the added custom_tools.webhookUrl redaction branch, which extends the same already-broken upsert to one more column but is not the headline. Long-standing data-loss, found via static audit.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 18, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2611 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: High — data-loss.** Restoring a profile export over an instance that still holds rows with matching primary keys silently wipes the live `api_connections.apiKeyEncrypted`, `agent_configs` secret settings, and `custom_tools.webhookUrl` — because the export redacts those columns and the restore upserts the redacted blanks onto existing rows. The secrets are absent from the archive, so the loss is unrecoverable from the backup. ## Expected behavior Restoring a profile to recover deleted entities should not destroy secrets on entities that still exist. A redacted/blank secret in the snapshot must never overwrite a non-empty secret already stored in the DB. ## Actual behavior The export intentionally redacts secret columns into the snapshot (`sanitizeProfileTableRows`): `api_connections.apiKeyEncrypted -> ""`, `agent_configs` secret settings -> `null` (via `redactAgentSecrets`, `SECRET_KEY_RE` matching `token`/`secret`/`password`/`api_key`), `custom_tools.webhookUrl -> ""`. On restore, `importProfileStorageSnapshot` upserts each snapshot row with `onConflictDoUpdate({ target: <pk>, set: cleanRow })`, and for `api_connections` it additionally force-blanks the key on the update path: ```ts // packages/server/src/routes/backup.routes.ts (~636-659) // cleanRow is built from the redacted snapshot row // api_connections path additionally sets apiKeyEncrypted = "" (~648) await db.insert(table) .values(cleanRow) .onConflictDoUpdate({ target: primaryKey, set: cleanRow }); // ~651 ``` Because the conflict UPDATE writes the whole redacted row, any pre-existing live row with the same primary-key id has its `apiKeyEncrypted` set to `""`, its agent secret settings set to `null`, and its `custom_tools.webhookUrl` set to `""`. Reached via `POST /api/backup/import-profile` (`requirePrivilegedAccess` is the only gate; `previewOnly`/fingerprint checks do not protect the secret columns). No DB wipe precedes the upsert and there is no merge/skip-existing/COALESCE guard. `SettingsPanel.tsx` advertises this exact "Restore full profiles … for recovery and migration" flow. ## Steps to reproduce 1. Configure at least one API connection (with an API key), one agent with a secret setting, and one custom tool with a webhook URL. 2. Export the profile (JSON or ZIP). 3. Without deleting those entities, re-import/restore that same export onto the instance (`POST /api/backup/import-profile`). 4. Observe that the live `apiKeyEncrypted` / agent secrets / `webhookUrl` for the still-present rows are now blank/null, and are not present anywhere in the archive. ## Environment - Marinara Engine version: `staging @ 56f700cd` (v2.0.0) - Install type: source - OS + version: n/a — static source audit - Browser or app shell: n/a ## Logs, screenshots, or video Static source audit, cited to file:line above. ## Additional context **Fix sketch.** On the conflict UPDATE path, do not overwrite the redacted secret columns: drop `apiKeyEncrypted` / `webhookUrl` / the redacted agent-secret subfields from the `set` object so `onConflictDoUpdate` preserves the existing DB value, or read the existing row and COALESCE-merge so a blank/null redacted value never overwrites a non-empty stored secret. Keep the blanks only on the fresh-insert path. **Regression notes.** Not a regression — the core mechanism is byte-identical on `main` (`backup.routes.ts` has the same `sanitizeProfileTableRows`, the same `apiKeyEncrypted=""` on the import update path, and the same `onConflictDoUpdate({ target: primaryKey, set: cleanRow })`). The only staging-only delta is the added `custom_tools.webhookUrl` redaction branch, which extends the same already-broken upsert to one more column but is not the headline. Long-standing data-loss, found via static audit. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:30:41 +07:00
  • closed this issue
  • added the
    server
    bug
    labels
Author
Owner

@Romuromylus commented on GitHub (Jun 18, 2026):

Fixed in #2623, merged to staging (e1132e5b). Closing manually since the PR targeted staging rather than the default branch.

<!-- gh-comment-id:4742293734 --> @Romuromylus commented on GitHub (Jun 18, 2026): Fixed in #2623, merged to `staging` (e1132e5b). Closing manually since the PR targeted `staging` rather than the default branch.
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#1284
No description provided.