[GH-ISSUE #6133] A chat can store an on/off choice for a lorebook entry it is not allowed to see #3410

Closed
opened 2026-10-21 06:02:48 +07:00 by bsakaguchi · 2 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

The route that saves a per-chat lorebook entry switch accepts any entry id that exists, without checking the entry belongs to a lorebook this chat can actually use. Saving a switch for an entry in a book the chat has never attached succeeds and leaves a meaningless setting on the chat forever.

How to reproduce (try it yourself)

The chat settings panel only lists entries of books already attached to the chat, so this cannot be clicked through - it needs a direct request. With the app running on :7860:

  1. Create a lorebook that is switched off everywhere and never attached to anything:
    LB=$(curl -s -X POST http://127.0.0.1:7860/api/lorebooks -H 'content-type: application/json' -d '{"name":"stray","scope":{"mode":"disabled"}}' | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')
  2. Add an entry to it:
    EN=$(curl -s -X POST http://127.0.0.1:7860/api/lorebooks/$LB/entries -H 'content-type: application/json' -d '{"name":"stray entry","content":"x"}' | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')
  3. Pick any chat that has never attached that book and save a switch for that entry:
    curl -s -w '\n%{http_code}\n' -X PATCH http://127.0.0.1:7860/api/chats/<anyChatId>/lorebook-entries/$EN -H 'content-type: application/json' -d '{"enabled":false}'

What you'll see: 200, and the returned chat's metadata.entryStateOverrides now holds a key for an entry in a book this chat cannot see. A made-up entry id already returns 404 and a made-up chat id already returns 404, so the scope check is the one guard that is missing.

What should happen: 404 (or 409). The route should read the entry's lorebookId and refuse unless that book is in the chat's resolvable set - attached books, plus character, persona, global and chat-scoped books, minus the ones excluded for this chat.

Where it is

At staging 52799569c, packages/server/src/routes/chats.routes.ts:1364-1388, the handler for PATCH /:id/lorebook-entries/:entryId. It checks three things and nothing else: the body shape, that the entry exists, and that an entry being switched on is not switched off globally.

packages/server/src/routes/chats.routes.ts:1367 - const entry = await createLorebooksStorage(app.db).getEntry(req.params.entryId); is a bare lookup by id. Compare getFolder at packages/server/src/services/storage/lorebooks.storage.ts:846-853, which takes an optional lorebookId precisely to stop this kind of cross-object addressing.

What's happening

The handler treats the entry id as a global address. It never asks which lorebook the entry belongs to, so it never compares that book against the chat's own lorebook settings, and the merge at :1374-1386 writes the key unconditionally.

The stored setting is inert for the standard prompt path - these switches are only ever subtractive there (packages/server/src/services/lorebook/index.ts:1124-1141 removes an entry when the switch says off; a switch saying on never adds one), and the knowledge-router path works from entries already limited to the books the chat resolved. So this is hardening against meaningless state rather than a content leak, and it is not reachable from the interface: packages/client/src/components/chat/ChatSettingsDrawer.tsx:7229 mounts the entry editor only for a book already active on the chat.

Suggested fix

Resolve entry.lorebookId in the handler and reject the write unless that book is in the chat's resolvable set, reusing the same scope resolution the prompt builder already performs rather than re-deriving it. getFolder's optional-lorebookId shape is the precedent for scoping a lookup to its parent. Add a case to scripts/regressions/chat-lorebook-entry-scope.regression.ts, which today covers per-chat isolation and the global-off floor but not scope validation.

Context

The route was added by PR #5981 (merged 2026-09-09) for issue #5954. Neither asked for scope validation, so this is a gap in a new feature rather than a regression, and since the write is inert and unreachable from the interface it is closer to housekeeping than to a malfunction. It is filed separately from the related cleanup issue because the fix lives in a different file. Unreleased on staging (latest release is v2.4.4).

Found by an AI-assisted sweep of merged PR #5981 against issue #5954, then re-verified against staging 52799569c on 2026-09-13. Please verify before fixing.

Related issue from the same sweep: #6132 (the cleanup gap for the same stored switches).

Originally created by @kolacheee on GitHub (Sep 13, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6133 Originally assigned to: @SpicyMarinara on GitHub. The route that saves a per-chat lorebook entry switch accepts any entry id that exists, without checking the entry belongs to a lorebook this chat can actually use. Saving a switch for an entry in a book the chat has never attached succeeds and leaves a meaningless setting on the chat forever. ## How to reproduce (try it yourself) The chat settings panel only lists entries of books already attached to the chat, so this cannot be clicked through - it needs a direct request. With the app running on :7860: 1. Create a lorebook that is switched off everywhere and never attached to anything: `LB=$(curl -s -X POST http://127.0.0.1:7860/api/lorebooks -H 'content-type: application/json' -d '{"name":"stray","scope":{"mode":"disabled"}}' | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')` 2. Add an entry to it: `EN=$(curl -s -X POST http://127.0.0.1:7860/api/lorebooks/$LB/entries -H 'content-type: application/json' -d '{"name":"stray entry","content":"x"}' | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')` 3. Pick any chat that has never attached that book and save a switch for that entry: `curl -s -w '\n%{http_code}\n' -X PATCH http://127.0.0.1:7860/api/chats/<anyChatId>/lorebook-entries/$EN -H 'content-type: application/json' -d '{"enabled":false}'` **What you'll see:** 200, and the returned chat's `metadata.entryStateOverrides` now holds a key for an entry in a book this chat cannot see. A made-up entry id already returns 404 and a made-up chat id already returns 404, so the scope check is the one guard that is missing. **What should happen:** 404 (or 409). The route should read the entry's `lorebookId` and refuse unless that book is in the chat's resolvable set - attached books, plus character, persona, global and chat-scoped books, minus the ones excluded for this chat. ## Where it is At staging `52799569c`, `packages/server/src/routes/chats.routes.ts:1364-1388`, the handler for `PATCH /:id/lorebook-entries/:entryId`. It checks three things and nothing else: the body shape, that the entry exists, and that an entry being switched on is not switched off globally. `packages/server/src/routes/chats.routes.ts:1367` - `const entry = await createLorebooksStorage(app.db).getEntry(req.params.entryId);` is a bare lookup by id. Compare `getFolder` at `packages/server/src/services/storage/lorebooks.storage.ts:846-853`, which takes an optional `lorebookId` precisely to stop this kind of cross-object addressing. ## What's happening The handler treats the entry id as a global address. It never asks which lorebook the entry belongs to, so it never compares that book against the chat's own lorebook settings, and the merge at `:1374-1386` writes the key unconditionally. The stored setting is inert for the standard prompt path - these switches are only ever subtractive there (`packages/server/src/services/lorebook/index.ts:1124-1141` removes an entry when the switch says off; a switch saying on never adds one), and the knowledge-router path works from entries already limited to the books the chat resolved. So this is hardening against meaningless state rather than a content leak, and it is not reachable from the interface: `packages/client/src/components/chat/ChatSettingsDrawer.tsx:7229` mounts the entry editor only for a book already active on the chat. ## Suggested fix Resolve `entry.lorebookId` in the handler and reject the write unless that book is in the chat's resolvable set, reusing the same scope resolution the prompt builder already performs rather than re-deriving it. `getFolder`'s optional-`lorebookId` shape is the precedent for scoping a lookup to its parent. Add a case to `scripts/regressions/chat-lorebook-entry-scope.regression.ts`, which today covers per-chat isolation and the global-off floor but not scope validation. ## Context The route was added by PR #5981 (merged 2026-09-09) for issue #5954. Neither asked for scope validation, so this is a gap in a new feature rather than a regression, and since the write is inert and unreachable from the interface it is closer to housekeeping than to a malfunction. It is filed separately from the related cleanup issue because the fix lives in a different file. Unreleased on staging (latest release is v2.4.4). Found by an AI-assisted sweep of merged PR #5981 against issue #5954, then re-verified against staging 52799569c on 2026-09-13. Please verify before fixing. Related issue from the same sweep: #6132 (the cleanup gap for the same stored switches).
bsakaguchi 2026-10-21 06:02:48 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Sep 13, 2026):

@SpicyMarinara is taking this report through the issue sweep in draft PR #6137, with Codex assisting. The affected current-code path has been checked; implementation and focused regression verification are in progress.

<!-- gh-comment-id:5651924326 --> @SpicyMarinara commented on GitHub (Sep 13, 2026): @SpicyMarinara is taking this report through the issue sweep in draft PR #6137, with Codex assisting. The affected current-code path has been checked; implementation and focused regression verification are in progress.
Author
Owner

@SpicyMarinara commented on GitHub (Sep 13, 2026):

Fixed in #6137, now merged to staging (e044997a2). Chat-local lore-entry toggles enforce book visibility and generation respects the shared-off floor.

<!-- gh-comment-id:5652785521 --> @SpicyMarinara commented on GitHub (Sep 13, 2026): Fixed in #6137, now merged to staging (`e044997a2`). Chat-local lore-entry toggles enforce book visibility and generation respects the shared-off floor.
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#3410
No description provided.