[GH-ISSUE #2640] [Issue]: retry save_lorebook_entry tool writer bypasses the agent write-approval gate #1301

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

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

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Medium — security (unapproved agent write / prompt-injection surface). When agentWriteApprovalRequired ("Review Agent Outputs") is enabled, the retry route gates only the structured lorebook_update result type through approval. The newer tool-based save_lorebook_entry writer writes to the lorebook DB immediately during agent execution with no approval check, so a hallucinating or prompt-injected agent can create/overwrite lorebook entries the user expected to review first.

Expected behavior

With "Review Agent Outputs" on, the toggle promises (ChatSettingsDrawer): "Lorebook, summary, character card updates, and reviewable writer-agent outputs wait for your approval." Every agent-driven lorebook write — including save_lorebook_entry tool calls — should be held as a proposal for approval, not committed inline.

Actual behavior

packages/server/src/routes/generate/retry-agents-route.ts — attachRetryLorebookWriterToolContexts (1103–1211) attaches a save_lorebook_entry tool whose executeToolCall calls lorebooksStore.createEntry (1153) / updateEntry (1188) immediately, and it is wired unconditionally at line 2768 with no reference to requireAgentWriteApproval (read at line 2682). The approval flag only gates the structured path (markRetryLorebookResultForApproval, 2850–2856). Dispatch is direct:

// packages/server/src/services/tools/tool-executor.ts:231
case "save_lorebook_entry":
  return saveLorebookEntry(args, context.saveLorebookEntry);

The same un-gated writer also backs the main generate route: packages/server/src/services/generation/tool-resolution-runtime.ts:366 createLorebookEntryWriter, wired at line 667 in resolveGenerationTools, which never receives the approval flag. So the gap is codebase-wide, not retry-only.

Steps to reproduce

  1. Enable "Review Agent Outputs" (agentWriteApprovalRequired) on a chat.
  2. Give an agent the save_lorebook_entry / lorebook-write tool.
  3. Run or retry that agent so it emits a save_lorebook_entry tool call.
  4. Observe the lorebook entry is created/overwritten with no approval prompt.

Environment

  • Marinara Engine version: staging @ 3dc062e2
  • 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: gate both writers behind requireAgentWriteApproval — the retry attachRetryLorebookWriterToolContexts and the shared createLorebookEntryWriter in tool-resolution-runtime.ts. When approval is required, the tool should return a proposal/envelope (mirroring buildLorebookWriteApprovalProposal / markRetryLorebookResultForApproval) instead of performing the create/update immediately.

Regression notes: staging-only new code, not a regression — main's retry-agents-route.ts contains none of attachRetryLorebookWriterToolContexts, save_lorebook_entry, or agentWriteApprovalRequired (the whole approval feature and the tool writer are new on staging). This is an incompleteness in the new approval feature. Found via static audit.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 19, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2640 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Medium — security (unapproved agent write / prompt-injection surface).** When `agentWriteApprovalRequired` ("Review Agent Outputs") is enabled, the retry route gates only the **structured** `lorebook_update` result type through approval. The newer tool-based `save_lorebook_entry` writer writes to the lorebook DB *immediately* during agent execution with no approval check, so a hallucinating or prompt-injected agent can create/overwrite lorebook entries the user expected to review first. ## Expected behavior With "Review Agent Outputs" on, the toggle promises (ChatSettingsDrawer): *"Lorebook, summary, character card updates, and reviewable writer-agent outputs wait for your approval."* Every agent-driven lorebook write — including `save_lorebook_entry` tool calls — should be held as a proposal for approval, not committed inline. ## Actual behavior `packages/server/src/routes/generate/retry-agents-route.ts` — `attachRetryLorebookWriterToolContexts` (1103–1211) attaches a `save_lorebook_entry` tool whose `executeToolCall` calls `lorebooksStore.createEntry` (1153) / `updateEntry` (1188) immediately, and it is wired **unconditionally** at line 2768 with no reference to `requireAgentWriteApproval` (read at line 2682). The approval flag only gates the structured path (`markRetryLorebookResultForApproval`, 2850–2856). Dispatch is direct: ```ts // packages/server/src/services/tools/tool-executor.ts:231 case "save_lorebook_entry": return saveLorebookEntry(args, context.saveLorebookEntry); ``` The same un-gated writer also backs the main generate route: `packages/server/src/services/generation/tool-resolution-runtime.ts:366 createLorebookEntryWriter`, wired at line 667 in `resolveGenerationTools`, which never receives the approval flag. So the gap is codebase-wide, not retry-only. ## Steps to reproduce 1. Enable "Review Agent Outputs" (`agentWriteApprovalRequired`) on a chat. 2. Give an agent the `save_lorebook_entry` / lorebook-write tool. 3. Run or retry that agent so it emits a `save_lorebook_entry` tool call. 4. Observe the lorebook entry is created/overwritten with no approval prompt. ## Environment - Marinara Engine version: `staging @ 3dc062e2` - 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: gate **both** writers behind `requireAgentWriteApproval` — the retry `attachRetryLorebookWriterToolContexts` and the shared `createLorebookEntryWriter` in `tool-resolution-runtime.ts`. When approval is required, the tool should return a proposal/envelope (mirroring `buildLorebookWriteApprovalProposal` / `markRetryLorebookResultForApproval`) instead of performing the create/update immediately. Regression notes: staging-only new code, not a regression — `main`'s `retry-agents-route.ts` contains none of `attachRetryLorebookWriterToolContexts`, `save_lorebook_entry`, or `agentWriteApprovalRequired` (the whole approval feature and the tool writer are new on staging). This is an incompleteness in the new approval feature. Found via static audit. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:31:08 +07:00
Author
Owner

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

Sibling of #2639 — both live in retry-agents-route.ts. I'm planning to land all of #2639, #2640, #2641, #2642, #2643 in a single agents-subsystem hardening PR.

<!-- gh-comment-id:4751269484 --> @Romuromylus commented on GitHub (Jun 19, 2026): Sibling of #2639 — both live in `retry-agents-route.ts`. I'm planning to land all of #2639, #2640, #2641, #2642, #2643 in a single agents-subsystem hardening PR.
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#1301
No description provided.