|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The author-time copy of the super-user bypass answers exactly as the |
| 5 | + * runtime's own reading does, over one case matrix. |
| 6 | + * |
| 7 | + * `holdsSuperuserBypass` (`@objectstack/core`, `security/effective-object-permissions.ts`) |
| 8 | + * is the ONE reading of "does this set hold View All / Modify All on this |
| 9 | + * object": the row bypass and both field merges ask it (ADR-0066 D2, ruling |
| 10 | + * of record 6573563853 letter A). `@objectstack/lint` depends on |
| 11 | + * `@objectstack/spec` alone, so its `security-superuser-field-mask-inert` |
| 12 | + * rule reads the bypass through a verbatim COPY exported under the same name. |
| 13 | + * A copy is a second reading the day the two drift, and a drifted lint either |
| 14 | + * warns about a mask the runtime enforces or stays silent about one it |
| 15 | + * ignores. This package depends on both, so the parity is pinned here: a |
| 16 | + * change to either function reds this file until the other follows. |
| 17 | + * |
| 18 | + * The matrix is the cartesian product of what the lookup reads: the set's own |
| 19 | + * entry for the object and its `'*'` entry, each absent, `null`, empty, or |
| 20 | + * carrying View All, Modify All, both, or a non-boolean truthy / falsy bit |
| 21 | + * (the reading is `Boolean(...)`, not `=== true`); the object asked about; and |
| 22 | + * the bit. Plus the sets with no usable `objects` at all. |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, expect, it } from 'vitest'; |
| 26 | +import { holdsSuperuserBypass as runtimeReading } from '@objectstack/core'; |
| 27 | +import { holdsSuperuserBypass as authoringCopy } from '@objectstack/lint'; |
| 28 | + |
| 29 | +const ENTRIES: ReadonlyArray<[string, unknown]> = [ |
| 30 | + ['absent', undefined], |
| 31 | + ['null', null], |
| 32 | + ['empty', {}], |
| 33 | + ['view', { allowRead: true, viewAllRecords: true }], |
| 34 | + ['modify', { allowRead: true, allowEdit: true, modifyAllRecords: true }], |
| 35 | + ['both', { viewAllRecords: true, modifyAllRecords: true }], |
| 36 | + ['neither, spelled false', { viewAllRecords: false, modifyAllRecords: false }], |
| 37 | + ['view, truthy non-boolean', { viewAllRecords: 1 }], |
| 38 | + ['modify, truthy non-boolean', { modifyAllRecords: 'yes' }], |
| 39 | + ['modify, falsy non-boolean', { modifyAllRecords: 0 }], |
| 40 | +]; |
| 41 | +const OBJECTS = ['deal', '*'] as const; |
| 42 | +const BITS = ['view', 'modify'] as const; |
| 43 | + |
| 44 | +type Case = { label: string; set: unknown; objectName: string; bit: 'view' | 'modify' }; |
| 45 | + |
| 46 | +function matrix(): Case[] { |
| 47 | + const cases: Case[] = []; |
| 48 | + for (const [ownLabel, own] of ENTRIES) { |
| 49 | + for (const [wildLabel, wild] of ENTRIES) { |
| 50 | + const objects: Record<string, unknown> = {}; |
| 51 | + if (own !== undefined) objects.deal = own; |
| 52 | + if (wild !== undefined) objects['*'] = wild; |
| 53 | + for (const objectName of OBJECTS) { |
| 54 | + for (const bit of BITS) { |
| 55 | + cases.push({ label: `deal: ${ownLabel} · '*': ${wildLabel} · ask ${objectName} for ${bit}`, set: { objects }, objectName, bit }); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + for (const set of [null, undefined, {}, { objects: null }, { objects: 'deal' }]) { |
| 61 | + for (const bit of BITS) cases.push({ label: `set ${JSON.stringify(set) ?? 'undefined'} · ${bit}`, set, objectName: 'deal', bit }); |
| 62 | + } |
| 63 | + return cases; |
| 64 | +} |
| 65 | + |
| 66 | +const CASES = matrix(); |
| 67 | +type Reading = (set: never, objectName: string, bit: 'view' | 'modify') => boolean; |
| 68 | +const ask = (fn: Reading, c: Case) => fn(c.set as never, c.objectName, c.bit); |
| 69 | + |
| 70 | +describe('holdsSuperuserBypass — the lint copy and the runtime reading agree', () => { |
| 71 | + it('the matrix is the product it claims, and it exercises both answers', () => { |
| 72 | + // 10 own entries x 10 wildcard entries x 2 objects x 2 bits, plus 5 sets x 2 bits. |
| 73 | + expect(CASES).toHaveLength(410); |
| 74 | + const answers = CASES.map((c) => ask(runtimeReading as Reading, c)); |
| 75 | + expect(answers.filter(Boolean).length).toBeGreaterThan(0); |
| 76 | + expect(answers.filter((a) => !a).length).toBeGreaterThan(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it('answers every case of the matrix alike', () => { |
| 80 | + const diverging = CASES.filter((c) => ask(authoringCopy as Reading, c) !== ask(runtimeReading as Reading, c)).map( |
| 81 | + (c) => `${c.label}: runtime ${ask(runtimeReading as Reading, c)}, lint ${ask(authoringCopy as Reading, c)}`, |
| 82 | + ); |
| 83 | + expect(diverging).toEqual([]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('a few readings the rule depends on, spelled out', () => { |
| 87 | + for (const fn of [runtimeReading, authoringCopy] as Reading[]) { |
| 88 | + const read = (set: unknown, bit: 'view' | 'modify') => fn(set as never, 'deal', bit); |
| 89 | + // Modify All implies View All; View All never implies Modify All. |
| 90 | + expect(read({ objects: { deal: { modifyAllRecords: true } } }, 'view')).toBe(true); |
| 91 | + expect(read({ objects: { deal: { viewAllRecords: true } } }, 'modify')).toBe(false); |
| 92 | + // The wildcard answers for an object the set does not name ... |
| 93 | + expect(read({ objects: { '*': { modifyAllRecords: true } } }, 'modify')).toBe(true); |
| 94 | + // ... and an entry that names the object answers alone: a lookup, never a merge. |
| 95 | + expect(read({ objects: { '*': { modifyAllRecords: true }, deal: { allowRead: true } } }, 'modify')).toBe(false); |
| 96 | + } |
| 97 | + }); |
| 98 | +}); |
0 commit comments