[GH-ISSUE #2976] [Issue]: createLorebookSchema lacks the scope-conflict validation updateLorebookSchema enforces, so POST silently drops fields and can leak a global character book to every chat #1553

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

When creating a lorebook (POST /lorebooks), contradictory scope fields are accepted silently, whereas the exact same payload sent as a PATCH is rejected with 400. Two concrete bad outcomes:

  1. Supplying both characterId and characterIds silently drops the single characterId (the array wins), so the book links to the wrong character set with no error.
  2. Supplying isGlobal: true together with characterId/personaId stores the row as global, and listActiveEntries then returns it for every chat (the isGlobal short-circuit runs before the character/persona checks) — leaking a book the caller intended to scope to one character into all chats.

The update schema's superRefine catches both; the create schema has no superRefine at all.

How to reproduce (try it yourself)

  1. POST /api/lorebooks with {"name":"Test","isGlobal":true,"characterId":"char-1"}. It is created with no 400.
  2. Open any unrelated chat with no link to char-1 and run a generation/scan — the lorebook's entries activate because isGlobal wins.
  3. POST /api/lorebooks with {"name":"Test2","characterId":"A","characterIds":["B"]} — the created book links only to B; A is silently lost.
  4. Send either body to PATCH /api/lorebooks/:id instead — it is rejected with 400.

Expected: POST rejects the same contradictory payloads PATCH already rejects.

Where it is

  • packages/shared/src/schemas/lorebook.schema.ts:67-94 (createLorebookSchema, no superRefine) vs :96-154 (updateLorebookSchema with superRefine; see :147-153 for the isGlobal check).
  • packages/server/src/services/storage/lorebooks.storage.ts:51-55 (resolveLinkIds prefers the array, drops the scalar), :303 + :318 (create stores characterIds[0]), :321 (stores isGlobal verbatim), :453 (listActiveEntries: if (b.isGlobal) return true; short-circuits scope filtering before the character/persona/chat checks at :455-463).
  • Routes: packages/server/src/routes/lorebooks.routes.ts:398 (POST uses create schema) vs :410 (PATCH uses update schema).

What's happening

createLorebookSchema is a plain z.object with no .superRefine, so the cross-field invariants (characterId XOR characterIds, personaId XOR personaIds, isGlobal excludes character/persona links) are only validated on update. Storage create() then uses resolveLinkIds(input.characterIds, input.characterId), which prefers the array and discards the scalar, and stores isGlobal verbatim — so conflicting input is accepted and partially discarded instead of rejected.

Suggested fix

Extract the superRefine body from updateLorebookSchema into a shared refinement and apply it to createLorebookSchema as well (e.g. createLorebookSchema = baseObject.superRefine(sameChecks)), so POST rejects the same contradictory payloads PATCH already rejects. Note this only gates the public route — internal callers (importSTLorebook, the keeper's persistLorebookKeeperUpdates) call storage.create() directly and bypass the schema, so they are unaffected.


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/2976 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** When creating a lorebook (`POST /lorebooks`), contradictory scope fields are accepted silently, whereas the exact same payload sent as a `PATCH` is rejected with 400. Two concrete bad outcomes: 1. Supplying both `characterId` and `characterIds` silently drops the single `characterId` (the array wins), so the book links to the wrong character set with no error. 2. Supplying `isGlobal: true` together with `characterId`/`personaId` stores the row as global, and `listActiveEntries` then returns it for **every** chat (the `isGlobal` short-circuit runs before the character/persona checks) — leaking a book the caller intended to scope to one character into all chats. The update schema's `superRefine` catches both; the create schema has no `superRefine` at all. **How to reproduce (try it yourself)** 1. `POST /api/lorebooks` with `{"name":"Test","isGlobal":true,"characterId":"char-1"}`. It is created with no 400. 2. Open any unrelated chat with no link to `char-1` and run a generation/scan — the lorebook's entries activate because `isGlobal` wins. 3. `POST /api/lorebooks` with `{"name":"Test2","characterId":"A","characterIds":["B"]}` — the created book links only to B; A is silently lost. 4. Send either body to `PATCH /api/lorebooks/:id` instead — it is rejected with 400. **Expected:** `POST` rejects the same contradictory payloads `PATCH` already rejects. **Where it is** - `packages/shared/src/schemas/lorebook.schema.ts:67-94` (`createLorebookSchema`, no `superRefine`) vs `:96-154` (`updateLorebookSchema` with `superRefine`; see `:147-153` for the `isGlobal` check). - `packages/server/src/services/storage/lorebooks.storage.ts:51-55` (`resolveLinkIds` prefers the array, drops the scalar), `:303` + `:318` (create stores `characterIds[0]`), `:321` (stores `isGlobal` verbatim), `:453` (`listActiveEntries`: `if (b.isGlobal) return true;` short-circuits scope filtering before the character/persona/chat checks at `:455-463`). - Routes: `packages/server/src/routes/lorebooks.routes.ts:398` (POST uses create schema) vs `:410` (PATCH uses update schema). **What's happening** `createLorebookSchema` is a plain `z.object` with no `.superRefine`, so the cross-field invariants (characterId XOR characterIds, personaId XOR personaIds, isGlobal excludes character/persona links) are only validated on update. Storage `create()` then uses `resolveLinkIds(input.characterIds, input.characterId)`, which prefers the array and discards the scalar, and stores `isGlobal` verbatim — so conflicting input is accepted and partially discarded instead of rejected. **Suggested fix** Extract the `superRefine` body from `updateLorebookSchema` into a shared refinement and apply it to `createLorebookSchema` as well (e.g. `createLorebookSchema = baseObject.superRefine(sameChecks)`), so `POST` rejects the same contradictory payloads `PATCH` already rejects. Note this only gates the public route — internal callers (`importSTLorebook`, the keeper's `persistLorebookKeeperUpdates`) call `storage.create()` directly and bypass the schema, so they are unaffected. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

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

Fixed in the v2.0.7 branch: createLorebookSchema now shares the same scope-conflict validation as updates, rejecting conflicting character/persona/global targets at create time. Validated with pnpm check and shared tsc.

<!-- gh-comment-id:4831161988 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in the v2.0.7 branch: createLorebookSchema now shares the same scope-conflict validation as updates, rejecting conflicting character/persona/global targets at create time. Validated with pnpm check and shared tsc.
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#1553
No description provided.