|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// `objectstack serve` ↔ `@objectstack/verify`'s `bootStack`: the two boot paths |
| 4 | +// must construct their `SecurityPlugin` from the SAME resolution — #7001. |
| 5 | +// |
| 6 | +// The defect this file mechanises: two boot paths disagreed about whether an |
| 7 | +// application's declared default permission profile exists. |
| 8 | +// |
| 9 | +// • `serve.ts` read `appDefaultPermissionSetName(config.permissions)` and |
| 10 | +// passed it as `fallbackPermissionSet`. |
| 11 | +// • `bootStack` constructed a vanilla `new SecurityPlugin()` and never read |
| 12 | +// `config.permissions` at all. |
| 13 | +// |
| 14 | +// So the profile an app declared was in force when a human ran the CLI and |
| 15 | +// silently absent when the app's own dogfood suite booted it — a |
| 16 | +// `declared ≠ enforced` split inside the harness that exists to catch that |
| 17 | +// split. Green tests, different production behaviour. It stayed invisible until |
| 18 | +// #5491 removed `member_default`'s `'*'` wildcard, because until then the floor |
| 19 | +// underneath granted everything anyway and the fallback was never load-bearing. |
| 20 | +// |
| 21 | +// The runtime halves are pinned where they run: the harness's wiring and its |
| 22 | +// behavioural consequence in `packages/verify/src/harness.app-default-profile.test.ts`, |
| 23 | +// the helper's own contract in |
| 24 | +// `packages/plugins/plugin-security/src/app-default-permission-set.test.ts`. |
| 25 | +// Neither can see THIS file's failure mode, which is the one that actually |
| 26 | +// happened: nothing in the repo pins `serve.ts`'s side, so re-open-coding the |
| 27 | +// wiring here — or dropping it — would be green everywhere while the paths |
| 28 | +// separate again. Hence a source scan, in the shape of this package's |
| 29 | +// `serve-email-config-parity.contract.test.ts`: the grep that would have caught |
| 30 | +// it, mechanised, so the second divergence fails a build instead of waiting for |
| 31 | +// someone to run it. |
| 32 | +// |
| 33 | +// It is deliberately a scan of BOTH files rather than an assertion about one. |
| 34 | +// A one-sided pin is satisfiable by editing the other side, which is exactly |
| 35 | +// how two mirrored literals drift. |
| 36 | + |
| 37 | +import { describe, it, expect } from 'vitest'; |
| 38 | +import { readFileSync } from 'node:fs'; |
| 39 | +import path from 'node:path'; |
| 40 | +import { fileURLToPath } from 'node:url'; |
| 41 | +import { appSecurityPluginOptions, appDefaultPermissionSetName } from '@objectstack/plugin-security'; |
| 42 | + |
| 43 | +const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 44 | + |
| 45 | +/** |
| 46 | + * `packages/cli/src/commands/` → `packages/`. The sibling read is what makes |
| 47 | + * this a PARITY assertion instead of a single-file lint; `@objectstack/verify` |
| 48 | + * is a real dependency of this package, and the comparison is test-only, so no |
| 49 | + * runtime edge is added. Tests never ship (`files: ["dist"]`). |
| 50 | + */ |
| 51 | +const PACKAGES_DIR = path.resolve(HERE, '../../..'); |
| 52 | + |
| 53 | +/** |
| 54 | + * Absence must be loud (AGENTS.md, Route & surface ownership §3). A scan that |
| 55 | + * silently reports success because it could not find the file it scans is worse |
| 56 | + * than no scan — it is this very gate's failure mode, one level up. |
| 57 | + */ |
| 58 | +function readBootPath(relative: string): string { |
| 59 | + const full = path.join(PACKAGES_DIR, relative); |
| 60 | + try { |
| 61 | + return readFileSync(full, 'utf8'); |
| 62 | + } catch (e) { |
| 63 | + throw new Error( |
| 64 | + `serve↔verify parity scan cannot read its subject '${relative}' (looked at ${full}). ` + |
| 65 | + 'The file moved or was renamed — repoint this scan; do NOT delete it, the two boot ' + |
| 66 | + `paths still have to agree. (${(e as Error).message})`, |
| 67 | + ); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Comments stripped, because this scan is about what the two files DO. |
| 73 | + * |
| 74 | + * Both boot sites are heavily commented — with the very construction shapes |
| 75 | + * being asserted about, since each explains what it replaced — so a scan over |
| 76 | + * raw text measures the prose and reports on it. (It did: the first run of this |
| 77 | + * file counted six constructions where the code has two.) Worse, the |
| 78 | + * comment-inclusive form would forbid the next author from ever *describing* |
| 79 | + * the old wiring, which is the opposite of what these files need. |
| 80 | + * |
| 81 | + * Approximate by design, and safe here: the result feeds nothing but the |
| 82 | + * `new SecurityPlugin(...)` regex below, so a `//` mangled out of a string |
| 83 | + * literal (`'http://localhost:3000'` in `harness.ts`) cannot affect a verdict. |
| 84 | + * Do not reuse this for anything that reads string contents. |
| 85 | + */ |
| 86 | +function stripComments(source: string): string { |
| 87 | + return source.replace(/\/\*[\s\S]*?\*\//g, ' ').replace(/(^|[^:])\/\/[^\n]*/g, '$1'); |
| 88 | +} |
| 89 | + |
| 90 | +const BOOT_PATHS: Array<{ label: string; relative: string; source: string }> = [ |
| 91 | + { label: 'objectstack serve', relative: 'cli/src/commands/serve.ts' }, |
| 92 | + { label: 'verify bootStack', relative: 'verify/src/harness.ts' }, |
| 93 | +].map((p) => ({ ...p, source: stripComments(readBootPath(p.relative)) })); |
| 94 | + |
| 95 | +/** |
| 96 | + * Every `new SecurityPlugin(...)` construction in a file, with its argument. |
| 97 | + * |
| 98 | + * Walks parentheses rather than matching `\(([^)]*)\)` — the argument is itself |
| 99 | + * a call (`appSecurityPluginOptions(config)`), so a non-nesting match stops at |
| 100 | + * the INNER `)` and silently reports `appSecurityPluginOptions(config`. That |
| 101 | + * truncation compares equal across both files, so the naive form would have |
| 102 | + * passed while measuring something that is not the argument. |
| 103 | + */ |
| 104 | +function securityPluginConstructions(source: string): string[] { |
| 105 | + const NEW = 'new SecurityPlugin('; |
| 106 | + const found: string[] = []; |
| 107 | + for (let i = source.indexOf(NEW); i !== -1; i = source.indexOf(NEW, i + 1)) { |
| 108 | + let depth = 1; |
| 109 | + let j = i + NEW.length; |
| 110 | + for (; j < source.length && depth > 0; j++) { |
| 111 | + if (source[j] === '(') depth++; |
| 112 | + else if (source[j] === ')') depth--; |
| 113 | + } |
| 114 | + if (depth !== 0) throw new Error(`unbalanced \`${NEW}…\` at offset ${i} — the scan cannot read this file`); |
| 115 | + found.push(source.slice(i + NEW.length, j - 1).trim()); |
| 116 | + } |
| 117 | + return found; |
| 118 | +} |
| 119 | + |
| 120 | +describe('serve ↔ bootStack construct SecurityPlugin from one resolution (#7001)', () => { |
| 121 | + // Asserting on the CONSTRUCTION rather than on the file's text, because the |
| 122 | + // text form does not go red on the defect. Reverting `harness.ts` to its |
| 123 | + // pre-#7001 `new SecurityPlugin()` left the `appSecurityPluginOptions` import |
| 124 | + // standing, so a `expect(source).toContain('appSecurityPluginOptions')` check |
| 125 | + // stayed GREEN over a boot path that had stopped calling it — a mention is |
| 126 | + // not a call. Measured, not reasoned: that ablation was run, and this is the |
| 127 | + // shape that failed correctly. |
| 128 | + it.each(BOOT_PATHS)('$label constructs SecurityPlugin exactly once, via the helper', ({ source }) => { |
| 129 | + expect(securityPluginConstructions(source)).toEqual(['appSecurityPluginOptions(config)']); |
| 130 | + }); |
| 131 | + |
| 132 | + it.each(BOOT_PATHS)('$label does not re-open-code the resolution', ({ source }) => { |
| 133 | + // The open-coded shape #7001 replaced, in either spelling. Reaching for the |
| 134 | + // NAME helper at a boot site means rebuilding `name ? {...} : undefined` by |
| 135 | + // hand — the half that was a decision, not formatting, and the half the |
| 136 | + // other path never grew. |
| 137 | + expect(source).not.toContain('appDefaultPermissionSetName'); |
| 138 | + expect(source).not.toMatch(/fallbackPermissionSet\s*:/); |
| 139 | + }); |
| 140 | + |
| 141 | + it('and the two paths agree with EACH OTHER, not merely with a literal', () => { |
| 142 | + // The parity claim proper. The per-path assertions above both compare to |
| 143 | + // the same hard-coded string, which a single careless edit could "fix" on |
| 144 | + // both sides at once; this one compares the paths to one another, so |
| 145 | + // divergence is red however the argument is spelled. |
| 146 | + const [serve, verify] = BOOT_PATHS.map(({ source }) => securityPluginConstructions(source)); |
| 147 | + // A path that stopped constructing one at all would otherwise satisfy any |
| 148 | + // "the two agree" claim over two empty sets. |
| 149 | + expect(serve.length, 'serve constructs a SecurityPlugin').toBeGreaterThan(0); |
| 150 | + expect(verify).toEqual(serve); |
| 151 | + }); |
| 152 | +}); |
| 153 | + |
| 154 | +describe('the shared resolution, exercised (#7001)', () => { |
| 155 | + // The scan above proves both paths call one helper; these prove the helper |
| 156 | + // they call answers correctly. Neither claim implies the other, and a source |
| 157 | + // scan alone would be green over a helper that returned nonsense. |
| 158 | + const declared = { |
| 159 | + permissions: [ |
| 160 | + { name: 'ignored_not_default', isDefault: false }, |
| 161 | + { name: 'app_member_default', isDefault: true }, |
| 162 | + ], |
| 163 | + }; |
| 164 | + |
| 165 | + it('carries an app-declared isDefault profile into the constructor options', () => { |
| 166 | + expect(appSecurityPluginOptions(declared)).toEqual({ fallbackPermissionSet: 'app_member_default' }); |
| 167 | + expect(appDefaultPermissionSetName(declared.permissions)).toBe('app_member_default'); |
| 168 | + }); |
| 169 | + |
| 170 | + it('yields undefined when nothing is declared, so the plugin keeps its own derivation', () => { |
| 171 | + // Deliberately NOT `{ fallbackPermissionSet: undefined }`: the constructor |
| 172 | + // reads an explicit `undefined` as "derive from the built-in sets" only |
| 173 | + // because the KEY is absent — `fallbackPermissionSet: null` means "no |
| 174 | + // baseline at all". Passing the object shape would work today and is one |
| 175 | + // refactor away from silently disabling the platform baseline. |
| 176 | + expect(appSecurityPluginOptions({ permissions: [{ name: 'plain' }] })).toBeUndefined(); |
| 177 | + expect(appSecurityPluginOptions({})).toBeUndefined(); |
| 178 | + expect(appSecurityPluginOptions(undefined)).toBeUndefined(); |
| 179 | + }); |
| 180 | +}); |
0 commit comments