Skip to content

Commit 2478f8e

Browse files
test(plugin-security): pin the authored-policy outcome and the skipped sharing relief on a controlled_by_parent predicate write (#38338)
Fixes #36367 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:用户无感;只补测试,产品行为不变。 - 为什么做:为「共享授权与字段权限改造」专题补上上一单(批量与逐条一致)留下的两处测试空白:明细上应用自写的修改/删除规则,以及批量改删时不再去问共享服务。 - 出错会怎样:只动一个测试文件,不影响任何用户;有问题可以直接撤回。 - 要你做什么:无。 ## What changes Test-only. One file: `packages/plugins/plugin-security/src/predicate-write-floor-deferral.test.ts`, on its own real-engine rig (real `SecurityPlugin`, `ObjectQL`, SQL driver over better-sqlite3, `SharingService` and its middleware). It carries nit N3 of contract record 7076369115 on PR #36282. The rig gains one detail, `pwf_line_staged`: the `controlled_by_parent` detail of `pwf_order`, with a `stage` column. Two permission sets carry REP_SET's grants and one app-authored policy each: `update` with `using: "stage == 'open'"`, and the same for `delete`. `seedLines` takes per-row overrides. On the staged matrix the order's owner ALICE created r1 and r3, which are closed. CAROL created r2 and r4, which are open. **The authored-policy outcome** (new describe inside the `controlled_by_parent` block, real engine, row for row against the by-id oracle): - `update`: under the update policy, the predicate update writes exactly r2 and r4, which is what a by-id update of each writes. It skips r1 and r3, which ALICE created and the policy does not admit. - `delete`: the same under the delete policy. - CONTROL, each verb: a policy on the other verb binds nothing, so every row is written, the closed rows ALICE created included. So it is the policy that keeps r1 and r3 out, not the master gate. **The skipped sharing relief** (second new describe): - End to end, each verb: a vouched predicate write by the master's owner asks the sharing service no write verdict about the detail, per row or set form. It writes beyond the floor, with no authored policy (all four rows) and with one (r2 and r4). A control: a by-id write of one row of the same detail does ask, so the recorder sees verdicts on that object. - Why it can't be seen, measured on this rig: the skip on its own moves no row and asks nothing here. One case pins each of three facts that make that true: 1. With no authored policy, the vouched Layer 1 is empty, so the relief has no Layer 1 to work on. 2. With an authored `using`, the vouched Layer 1 is the policy alone (`{ stage: 'open' }`). Replacing only the floor (the relief's own test) keeps the authored term, so the relief returns before it asks anything. 3. Record sharing answers `abstain` for every row of both details, for both verbs. So a verdict, even if one were asked, would replace no floor. No product code is touched. Every pin measures the outcome PR #36282 declares. No product defect was measured. ## Tests Head `198443d7b0`. The suite, typecheck and gate runs below were taken at this head, after its last commit. <details><summary>The file, the plugin-security suite and typecheck</summary> - `pnpm --filter @objectstack/plugin-security exec vitest run --maxWorkers=2 src/predicate-write-floor-deferral.test.ts`, under the verify lock: `Tests 116 passed (116)` (109 before, plus 7 new), `VERDICT command-exit 0`. - `pnpm --filter @objectstack/plugin-security exec vitest run --maxWorkers=2`, under the verify lock: `Test Files 290 passed (290)`, `Tests 7337 passed | 45 skipped (7382)`, `VERDICT command-exit 0`. - `pnpm --filter @objectstack/plugin-security typecheck` (tsc, scripts project, `check:test-typecheck`), under the verify lock: `VERDICT command-exit 0`. The file is in the `tsconfig.test.json` program (`--listFiles`: 1 hit). </details> <details><summary>Ablations, through scripts/ablation-replace.mjs, each restored and proved by blob hash</summary> The test imports plugin-security's source, so there is no dist hop. On every leg the anchor went from 1 to 0 and the blob changed. After each restore the blob equals HEAD and `git diff HEAD` is empty. The whole file is run each time; the counts below are the 7 new cases. 1. **A1, the vouch taken off step 3's Layer 1** (`security-plugin-authz-middleware.ts`, `predicateMasterGateRowIds ? { masterGateCoversThisWrite: true } : undefined` becomes `undefined`): 22 failed / 94 passed. 6 of the 7 new cases are red. The authored-policy update and delete wrote r1 to r4 by predicate (floor OR policy, the outcome before PR #36282) against r2 and r4 by id. The other-verb controls wrote only r1 and r3. The end-to-end relief case wrote only r1 and r3. The "why" case stays green, as expected: it calls the composition directly with the vouch option. 2. **A2, the relief's skip removed** (`&& !predicateMasterGateRowIds` dropped from the relief's guard): 116 passed. This is the outcome the "why" case pins: on its own, the skip is not visible here. 3. **A3, the floor's yield to the master gate removed** (`security-plugin-rls-filter.ts`, the `collected.filter(... !isPlatformOwnershipFloorPolicy ...)` under `platformFloorYieldsToObjectWriteModel` becomes a no-op): 26 failed / 90 passed, all 7 new cases red. The "why" case read the vouched Layer 1 as `{ created_by: 'usr_alice' }` instead of null. 4. **A4, A2 and A3 together** (A2 held with `--hold`, A3 wrapped, then A2 restored): 26 failed / 90 passed, all 7 new cases red. The verdict assertion itself now fails, on both verbs: `pwf_line: verdicts the predicate write asked: expected [ { verb: 'edit', … }, … ] to deeply equal []` (and `'delete'` for the delete). So the recorder catches a relief verdict on a vouched write once the relief can reach one. </details> ## Acceptance notes - The card allows "or show why none is observable" for the sharing relief. A2 shows that removing the skip alone changes nothing on these fixtures, so the relief case pins the three reasons and is ablated red through A3 and A4. PR #36282 named this gap ("on these fixtures the relief would answer nothing either way"). - `scripts/pm/overlap-check.mjs` before each push: `VERDICT clean`. - The test file's own run: about 5.3 s of tests inside about 20 s of wall time, most of it module import, as before. ## Verification Read first: 1. The `[#36367]` describe "why the skip moves no row" in the test file: it holds the three facts above. 2. The `[#36367]` describe on the authored policy: the four cases and the staged seed. Two-minute check: `pnpm --filter @objectstack/plugin-security exec vitest run --maxWorkers=2 src/predicate-write-floor-deferral.test.ts -t 36367`. Expect 7 passed. <details><summary>Gates</summary> Derived with `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` at `198443d7b0`: 1 path against merge base `c279520a0c`, 54 families. Each was run once at this head, with its exit code captured before any pipe. `dispatch-gates --ran`: "54 derived famil(ies) accounted for — 52 run, 2 NOT-MEASURED (2 DERIVED from a recorded exit 3)", exit 0. - 52 exit 0. One of them took a second run: `node scripts/check-plugin-teardown-shape.mjs --self-test` first exited 3 because its pinned positive-control commit was missing from the shallow clone. Once that one commit was fetched it exited 0 (48 cases pass). - NOT MEASURED, PREREQUISITE NOT MET (exit 3), left to CI's `Lint & Repo Gates`: `pnpm check:dual-build-cjs-loads` and `pnpm check:i18n` read the `dist/` of packages outside the plugin-security closure built here (59 packages, and the CLI). - Not run locally, by rule: the repo-wide lint and `test:repo` (CI-owned; the diff touches no test in that project). `check-platform-checklist` is not owed: no exported symbol is renamed or removed. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_017xRBcSCSJLNEQQeWJhKnd1)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 191235b commit 2478f8e

1 file changed

Lines changed: 121 additions & 1 deletion

File tree

‎packages/plugins/plugin-security/src/predicate-write-floor-deferral.test.ts‎

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ const LINE_MASTER_READ = 'pwf_line_master_read';
8585
const LINE_OWNER_MAY_EDIT = 'pwf_line_owner_may_edit';
8686
/** The same detail with an OPTIONAL master: a row that names none is its owner's. */
8787
const LINE_OPTIONAL = 'pwf_line_optional';
88-
const LINES = [LINE, LINE_MASTER_READ, LINE_OWNER_MAY_EDIT, LINE_OPTIONAL] as const;
88+
/** [#36367] The same detail carrying a `stage`, for an APP-AUTHORED update / delete policy to read. */
89+
const LINE_STAGED = 'pwf_line_staged';
90+
const LINES = [LINE, LINE_MASTER_READ, LINE_OWNER_MAY_EDIT, LINE_OPTIONAL, LINE_STAGED] as const;
8991
type LineObject = (typeof LINES)[number];
9092
const ORG = 'org_pwf';
9193

@@ -189,9 +191,25 @@ const EMEA_CHECK_SET = PermissionSetSchema.parse({
189191
objects: { [DEAL]: CRUD },
190192
rowLevelSecurity: [{ name: 'pwf_stays_in_emea', object: DEAL, operation: 'update', check: "region == 'emea'" }],
191193
});
194+
/**
195+
* [#36367] APP-AUTHORED row-level write policies on the staged detail, one
196+
* verb each: only an open line may be updated, or deleted. The grants are
197+
* REP_SET's, so the caller's standing on the order is what it is there.
198+
*/
199+
const OPEN_LINES_UPDATE_SET = PermissionSetSchema.parse({
200+
name: 'pwf_open_lines_update',
201+
objects: onOrderAndLines(CRUD),
202+
rowLevelSecurity: [{ name: 'pwf_open_lines_update_only', object: LINE_STAGED, operation: 'update', using: "stage == 'open'" }],
203+
});
204+
const OPEN_LINES_DELETE_SET = PermissionSetSchema.parse({
205+
name: 'pwf_open_lines_delete',
206+
objects: onOrderAndLines(CRUD),
207+
rowLevelSecurity: [{ name: 'pwf_open_lines_delete_only', object: LINE_STAGED, operation: 'delete', using: "stage == 'open'" }],
208+
});
192209
const SETS = [
193210
MEMBER_DEFAULT, REP_SET, MANAGER_SET, NARROW_SET, WIDENER_SET, EMEA_CHECK_SET,
194211
MODIFY_ALL_WIDENER_SET, WIDENER_EMEA_CHECK_SET, MEMO_WIDENER_SET, UPDATE_WIDENER_SET, DELETE_WIDENER_SET,
212+
OPEN_LINES_UPDATE_SET, OPEN_LINES_DELETE_SET,
195213
];
196214

197215
/** The execution context `resolveAuthzContext` hands the middleware for an organization member holding the sets. */
@@ -312,6 +330,10 @@ beforeAll(async () => {
312330
sharing: { controlledByParent: { ownerMayEdit: true } },
313331
}),
314332
lineObject(LINE_OPTIONAL, 'Order line, order optional', {}, false),
333+
{
334+
...lineObject(LINE_STAGED, 'Order line, with a stage'),
335+
fields: { ...lineObject(LINE_STAGED, '').fields, stage: { name: 'stage', type: 'text' } },
336+
},
315337
],
316338
} as never);
317339
await engine.syncSchemas();
@@ -1254,6 +1276,8 @@ describe('[#35802] a predicate write of a controlled_by_parent detail writes the
12541276
underOtherOrder?: readonly Role[];
12551277
/** Rows that name no order (the optional-master detail only). */
12561278
noOrder?: readonly Role[];
1279+
/** Per-row column values beside the matrix's (the staged detail's `stage`). */
1280+
overrides?: Overrides;
12571281
};
12581282

12591283
async function seedLines(object: LineObject, seed: LineSeed = {}): Promise<{ batch: string; ids: Ids }> {
@@ -1284,6 +1308,7 @@ describe('[#35802] a predicate write of a controlled_by_parent detail writes the
12841308
order: seed.noOrder?.includes(role) ? null : seed.underOtherOrder?.includes(role) ? otherOrder : order,
12851309
organization_id: ORG,
12861310
...MATRIX[role],
1311+
...seed.overrides?.[role],
12871312
})), SYS);
12881313
return { batch, ids };
12891314
}
@@ -1381,6 +1406,101 @@ describe('[#35802] a predicate write of a controlled_by_parent detail writes the
13811406
});
13821407
}
13831408

1409+
// [#36367 · ruling record 7073030861, item 1] With the floor handed to the
1410+
// master gate, an APP-AUTHORED write-class policy with a `using` on the
1411+
// detail bounds the predicate write alone: the rows the caller created that
1412+
// the policy does not admit are no longer matched through the floor as one
1413+
// more OR term. By id they were already refused, so this is the by-id
1414+
// comparison again. On the staged detail ALICE, the order's owner, created
1415+
// r1 and r3 and they are closed; CAROL created r2 and r4 and they are open.
1416+
describe('[#36367] an app-authored update / delete policy with a using on the detail bounds the vouched write alone', () => {
1417+
const STAGES: Overrides = { r1: { stage: 'closed' }, r2: { stage: 'open' }, r3: { stage: 'closed' }, r4: { stage: 'open' } };
1418+
const POLICY_ON = { update: OPEN_LINES_UPDATE_SET, delete: OPEN_LINES_DELETE_SET } as const;
1419+
const OTHER_VERB = { update: 'delete', delete: 'update' } as const;
1420+
1421+
for (const verb of ['update', 'delete'] as const) {
1422+
it(`${verb}: the master's owner writes the open r2 and r4 the policy admits, and skips the closed r1 and r3 she created`, async () => {
1423+
expect([MATRIX.r1.created_by, MATRIX.r3.created_by]).toEqual([ALICE, ALICE]);
1424+
await expectLinesMatchById(LINE_STAGED, verb, memberOf(ALICE, POLICY_ON[verb].name), ['r2', 'r4'], { overrides: STAGES });
1425+
});
1426+
1427+
it(`${verb}: CONTROL — a policy on the other verb binds nothing here, so every row is written, the closed ones included`, async () => {
1428+
await expectLinesMatchById(LINE_STAGED, verb, memberOf(ALICE, POLICY_ON[OTHER_VERB[verb]].name), ALL, { overrides: STAGES });
1429+
});
1430+
}
1431+
});
1432+
1433+
// [#36367] The #35802 sharing relief (`predicateWriteFloorRelief`, which
1434+
// asks {@link predicateWriteFloorDropIds}) replaces the floor row by row; a
1435+
// write the master gate vouched for has no floor left to replace, and the
1436+
// middleware skips the relief for it. The first case is that outcome end to
1437+
// end. The skip itself moves no row on any fixture here, so the second case
1438+
// pins why: each of three facts alone would make an asked verdict change
1439+
// nothing.
1440+
describe('[#36367] a vouched predicate write asks no sharing write verdict', () => {
1441+
const STAGED: LineSeed = { overrides: { r1: { stage: 'closed' }, r2: { stage: 'open' }, r3: { stage: 'closed' }, r4: { stage: 'open' } } };
1442+
const policySetFor = (verb: 'update' | 'delete') => (verb === 'update' ? OPEN_LINES_UPDATE_SET : OPEN_LINES_DELETE_SET);
1443+
const askedAbout = (object: string, fromRow: number, fromSet: number) => [
1444+
...verdictCalls.slice(fromRow).filter((c) => c.object === object),
1445+
...setFormCalls.slice(fromSet).filter((c) => c.object === object),
1446+
];
1447+
1448+
for (const verb of ['update', 'delete'] as const) {
1449+
it(`${verb}: the master's owner writes beyond the floor and is asked no verdict, with or without an authored policy; a by-id write of the same detail is`, async () => {
1450+
const runs: [LineObject, object, LineSeed, readonly Role[]][] = [
1451+
[LINE, memberOf(ALICE, REP_SET.name), {}, ALL],
1452+
[LINE_STAGED, memberOf(ALICE, policySetFor(verb).name), STAGED, ['r2', 'r4']],
1453+
];
1454+
for (const [object, caller, seed, written] of runs) {
1455+
const { batch, ids } = await seedLines(object, seed);
1456+
const fromRow = verdictCalls.length;
1457+
const fromSet = setFormCalls.length;
1458+
await predicateWrite(object, verb, batch, caller);
1459+
expect(askedAbout(object, fromRow, fromSet), `${object}: verdicts the predicate write asked`).toEqual([]);
1460+
// The gate vouched: CAROL's r2 and r4 are written, which the floor alone refuses.
1461+
expect(await changedRows(object, ids, verb), `${object}: the rows written`).toEqual([...written]);
1462+
1463+
// The recorder sees this detail: a by-id write of one of its rows asks.
1464+
const byId = await seedLines(object, seed);
1465+
const fromById = verdictCalls.length;
1466+
expect(await byIdAdmits(object, verb, byId.ids, caller, ['r2'])).toEqual(['r2']);
1467+
expect(askedAbout(object, fromById, setFormCalls.length).length, `${object}: verdicts a by-id ${verb} asked`).toBeGreaterThan(0);
1468+
}
1469+
});
1470+
}
1471+
1472+
it('why the skip moves no row: no floor in the vouched Layer 1, the floor-only replacement is not empty beside a policy, and sharing abstains', async () => {
1473+
const plugin = securityPlugin as any;
1474+
const rep = memberOf(ALICE, REP_SET.name);
1475+
for (const verb of ['update', 'delete'] as const) {
1476+
// No authored policy: the vouched Layer 1 is empty, so the relief,
1477+
// which relieves a Layer 1, has none to start from.
1478+
const bare = await plugin.computeLayeredRlsFilter([REP_SET, MEMBER_DEFAULT], LINE, verb, rep, { masterGateCoversThisWrite: true });
1479+
expect(bare.layer1 ?? null, `${verb}: the vouched Layer 1 without a policy`).toBeNull();
1480+
1481+
// An authored `using`: the vouched Layer 1 is that policy alone, and
1482+
// replacing the floor alone (the relief's own test) keeps the authored
1483+
// term, so the relief answers before any verdict is asked.
1484+
const set = policySetFor(verb);
1485+
const authored = memberOf(ALICE, set.name);
1486+
const vouched = await plugin.computeLayeredRlsFilter([set, MEMBER_DEFAULT], LINE_STAGED, verb, authored, { masterGateCoversThisWrite: true });
1487+
expect(vouched.layer1, `${verb}: the vouched Layer 1 beside a policy`).toEqual({ stage: 'open' });
1488+
const replaced = await plugin.computeLayeredRlsFilter([set, MEMBER_DEFAULT], LINE_STAGED, verb, authored, { dropPlatformOwnershipFloor: true });
1489+
expect(replaced.layer1, `${verb}: the floor-only replacement beside a policy`).toBeTruthy();
1490+
1491+
// And an asked verdict would replace no floor: record sharing abstains
1492+
// on every row of a controlled_by_parent detail.
1493+
for (const object of [LINE, LINE_STAGED] as const) {
1494+
const { ids } = await seedLines(object, object === LINE_STAGED ? STAGED : {});
1495+
const verdicts = verb === 'update'
1496+
? await sharing!.checkEditMany!(object, Object.values(ids), { ...rep } as never)
1497+
: await sharing!.checkDeleteMany!(object, Object.values(ids), { ...rep } as never);
1498+
expect(verdicts, `${verb} on ${object}: the sharing verdicts`).toEqual(['abstain', 'abstain', 'abstain', 'abstain']);
1499+
}
1500+
}
1501+
});
1502+
});
1503+
13841504
it('all matched rows or none: a newly matched row a beforeDelete hook refuses refuses the whole delete', async () => {
13851505
// Before the ruling ALICE's predicate delete matched r1 and r3 alone (the
13861506
// floor) and answered success. r2, which CAROL created, is now matched as

0 commit comments

Comments
 (0)