|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#29985 codemod] The "Data CRUD" facet of |
| 5 | + * `ObjectStackProtocolImplementation`: the bodies of `probeRecord` moved out of |
| 6 | + * `protocol.ts` whole by `scripts/codemod-split-protocol.mjs`. |
| 7 | + * |
| 8 | + * A MOVE, not a redesign. `ProtocolDataCrudFacet` is a stateless facet over the host |
| 9 | + * (ruling 6624345766): each static method below is the body of the class |
| 10 | + * member of the same name as `packages/metadata-protocol/scripts/host-split.mjs` |
| 11 | + * moves one (ruling E, item E1), with `this.<member>` read as `host.<member>` |
| 12 | + * and nothing else changed. The facet holds no state: the fields stay on the |
| 13 | + * class, and a body reaches them through the host (`protocol-host.ts`). |
| 14 | + * |
| 15 | + * The class keeps every member as a one-line delegation. A public member |
| 16 | + * keeps its docblock there, which the published `.d.ts` prints; a private |
| 17 | + * member's docblock moved here with its body (docblock ownership). |
| 18 | + * |
| 19 | + * What a body reads from elsewhere, it imports from where `protocol.ts` |
| 20 | + * imports it. Nothing re-exports this module, so the published surface does |
| 21 | + * not move. |
| 22 | + */ |
| 23 | + |
| 24 | +import type { ProtocolHost } from './protocol-host.js'; |
| 25 | + |
| 26 | +export class ProtocolDataCrudFacet { |
| 27 | + /** |
| 28 | + * [#4435] Does this row EXIST? A fact about the database — deliberately |
| 29 | + * NOT "may this caller see it". |
| 30 | + * |
| 31 | + * Read with a system context so no row-level policy narrows it. That is |
| 32 | + * load-bearing, and the first version of this fix got it wrong: probing |
| 33 | + * with the CALLER's context turns the existence gate into an authorization |
| 34 | + * gate, because a row the caller cannot read comes back `null` and the |
| 35 | + * PATCH answers 404. Two things break when it does. |
| 36 | + * |
| 37 | + * 1. It silently changes RLS semantics. Whether an unreadable row may be |
| 38 | + * written by id is the #1994 pre-image check's decision, made inside |
| 39 | + * `engine.update` where the write policy lives. A probe in front of it |
| 40 | + * quietly adds a second, different rule — scope creep into the security |
| 41 | + * model, from a bug fix about missing records. |
| 42 | + * |
| 43 | + * 2. It disarms a revert-provable security proof. `@proof: rls-by-id-write` |
| 44 | + * (`packages/qa/dogfood/test/rls-fixture.dogfood.test.ts`, referenced by |
| 45 | + * the `permission.rowLevelSecurity.using` liveness ledger entry) boots a |
| 46 | + * fixture whose member can read nothing but has no write policy, and |
| 47 | + * asserts the runner reports `rls-hole`. A caller-scoped probe 404s that |
| 48 | + * PATCH, so the RED half goes green — and the gate can no longer prove |
| 49 | + * it is able to go red. If the #1994 fix were ever reverted, this probe |
| 50 | + * would MASK it. Accidentally hardening one path is not worth |
| 51 | + * permanently blinding the gate that watches the whole class. |
| 52 | + * |
| 53 | + * So the probe answers existence only, and authorization stays exactly |
| 54 | + * where it was. The one behaviour this adds is the 404 the issue asked for: |
| 55 | + * an id that names no row at all. |
| 56 | + * |
| 57 | + * ⛔ And what it finds never answers IN FRONT of the write's |
| 58 | + * authorization. Its readers are OCC's `updated_at` comparison, asked only |
| 59 | + * when the caller sent a token ({@link assertVersionMatch}) and answering |
| 60 | + * only for a row the caller's own read returns ({@link assertVersionOf}), |
| 61 | + * and the by-id update's existence check, asked only after `engine.update` |
| 62 | + * has judged and resolved the write (`updateData`, and the bulk faces |
| 63 | + * through {@link assertRecordExists}) — so an update by id answers the same |
| 64 | + * refusal whether or not the id exists, as delete does. With a mismatched |
| 65 | + * token, a caller whose read of the row is refused gets that read refusal. |
| 66 | + */ |
| 67 | + static async probeRecord(host: Pick<ProtocolHost, 'engine'>, object: string, id: string): Promise<any> { |
| 68 | + return host.engine.findOne(object, { where: { id }, context: { isSystem: true } }); |
| 69 | + } |
| 70 | +} |
0 commit comments