|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#6628] The `delegatable` JSDoc may name a lint rule only if that rule exists. |
| 5 | + * |
| 6 | + * That JSDoc is an authoring surface, not a comment: it is the TSDoc an author |
| 7 | + * (often an AI author, ADR-0033) hovers at the exact moment they type |
| 8 | + * `delegatable:`. For a whole major it closed with |
| 9 | + * |
| 10 | + * "(enforced by the `security-delegatable-admin-position` lint rule and the |
| 11 | + * D12 gate)" |
| 12 | + * |
| 13 | + * and the first of those two enforcers had never been written — the string |
| 14 | + * occurred exactly once in the repository, in that sentence. The runtime half |
| 15 | + * was real (`plugin-security`'s delegated-admin gate, step 6), so the invariant |
| 16 | + * held; what was false was WHEN it holds. The sentence promised an author-time |
| 17 | + * gate, so an author pairing `delegatable: true` with an `adminScope`-carrying |
| 18 | + * set believed `os lint` would stop them. It does not: the package publishes |
| 19 | + * clean and the mistake surfaces later, in a different package, as a runtime |
| 20 | + * deny phrased as a fact about the position rather than as a fix for the |
| 21 | + * authoring error. |
| 22 | + * |
| 23 | + * This is the `validate-security-posture.ts` header's own hazard one layer out. |
| 24 | + * That file records how alias tolerance "silently downgraded a NAMED rejection |
| 25 | + * into an inert branch — and an inert branch in a security linter reads, to the |
| 26 | + * next author, as a gate that is watching (#4984, #5009, #5017)". A rule that is |
| 27 | + * named but absent reads the same way, and is cheaper to write by accident: |
| 28 | + * prose costs nothing to add and no compiler checks it. |
| 29 | + * |
| 30 | + * So the authority here is machine-readable, never a hand-copied list — the |
| 31 | + * rule-id constants `packages/lint` actually exports, read off its `src/` |
| 32 | + * directory the way `rule-id-barrel-exports.test.ts` (#5648) reads it. A gate |
| 33 | + * name the rule table does not back turns this red. |
| 34 | + * |
| 35 | + * ⛔ Scope: the relation, not the wording. Rewording this JSDoc freely is fine — |
| 36 | + * what it may not do is name a `security-*` rule that no rule file declares, or |
| 37 | + * stop locating the containment check at runtime. The second half matters |
| 38 | + * because "no author-time rule" is only safe to say next to "the D12 gate does |
| 39 | + * enforce this, at delegation time"; drop that and the text overcorrects into |
| 40 | + * implying the invariant is unenforced, which is the opposite lie. |
| 41 | + * |
| 42 | + * Deliberately NOT asserted: that no author-time rule exists. Whether ADR-0091 |
| 43 | + * D3 should grow one is a product decision (the finding left it open); if that |
| 44 | + * rule is ever written, this pin stays green the moment the JSDoc names it, |
| 45 | + * because the name will resolve against the same rule table. |
| 46 | + */ |
| 47 | + |
| 48 | +import { readFileSync, readdirSync } from 'node:fs'; |
| 49 | +import { dirname, join, resolve } from 'node:path'; |
| 50 | +import { fileURLToPath } from 'node:url'; |
| 51 | + |
| 52 | +import { describe, it, expect } from 'vitest'; |
| 53 | + |
| 54 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 55 | +/** …/packages/spec/src/identity → repo root */ |
| 56 | +const REPO_ROOT = resolve(HERE, '../../../..'); |
| 57 | +const LINT_SRC = join(REPO_ROOT, 'packages', 'lint', 'src'); |
| 58 | +const POSITION_SOURCE = join(HERE, 'position.zod.ts'); |
| 59 | + |
| 60 | +/** `export const NAME = 'security-…';` — how every security rule id is declared. */ |
| 61 | +const EXPORTED_SECURITY_RULE_ID = /^export const [A-Z][A-Z0-9_]* = '(security-[a-z0-9-]+)';\s*$/; |
| 62 | + |
| 63 | +/** |
| 64 | + * Every `security-*` rule id `packages/lint` declares, found by reading its |
| 65 | + * `src/` directory rather than by naming files. A new security rule in a new |
| 66 | + * file is therefore authoritative the moment it exists — the property a |
| 67 | + * hand-maintained list here would quietly lose. |
| 68 | + */ |
| 69 | +function declaredSecurityRuleIds(): Set<string> { |
| 70 | + const ids = new Set<string>(); |
| 71 | + for (const file of readdirSync(LINT_SRC)) { |
| 72 | + if (!file.endsWith('.ts') || file.endsWith('.test.ts')) continue; |
| 73 | + for (const line of readFileSync(join(LINT_SRC, file), 'utf8').split('\n')) { |
| 74 | + const m = EXPORTED_SECURITY_RULE_ID.exec(line); |
| 75 | + if (m) ids.add(m[1]); |
| 76 | + } |
| 77 | + } |
| 78 | + return ids; |
| 79 | +} |
| 80 | + |
| 81 | +/** The JSDoc block attached to the authorable `delegatable` key. */ |
| 82 | +function delegatableDoc(): string { |
| 83 | + const source = readFileSync(POSITION_SOURCE, 'utf8'); |
| 84 | + const key = source.indexOf('delegatable: z.boolean()'); |
| 85 | + expect(key, 'the `delegatable` key declaration moved — re-anchor this pin').toBeGreaterThan(-1); |
| 86 | + const open = source.lastIndexOf('/**', key); |
| 87 | + const close = source.indexOf('*/', open); |
| 88 | + expect(open, 'no JSDoc block precedes `delegatable`').toBeGreaterThan(-1); |
| 89 | + expect(close, 'unterminated JSDoc block').toBeLessThan(key); |
| 90 | + return source.slice(open, close + 2); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * The `security-*` rule ids a piece of prose names, minus the ones the rule |
| 95 | + * table backs. Rule ids are matched by their backticked, multi-segment slug |
| 96 | + * shape: `security-owd-alias` is a rule id, while the cloud product name |
| 97 | + * `security-enterprise` (one segment, and never called a rule) is prose. |
| 98 | + */ |
| 99 | +function unbackedRuleIds(prose: string, backed: Set<string>): string[] { |
| 100 | + const named = [...prose.matchAll(/`(security-[a-z0-9]+(?:-[a-z0-9]+)+)`/g)].map((m) => m[1]); |
| 101 | + return [...new Set(named.filter((id) => !backed.has(id)))]; |
| 102 | +} |
| 103 | + |
| 104 | +describe('`delegatable` JSDoc names only enforcers that exist (#6628)', () => { |
| 105 | + it('reads a real rule table off `packages/lint`', () => { |
| 106 | + const ids = declaredSecurityRuleIds(); |
| 107 | + // A floor, not an exact count — new security rules are expected. Its only |
| 108 | + // job is to fail loudly if the extraction above stops finding anything, |
| 109 | + // which would turn the self-test below vacuously green. |
| 110 | + expect(ids.size).toBeGreaterThanOrEqual(12); |
| 111 | + // The control the finding itself used: an ADR-0091 D3 author-time rule that |
| 112 | + // DID land, proving the absence of the phantom was specific to that one |
| 113 | + // rule and not an artefact of the linter skipping ADR-0091. |
| 114 | + expect([...ids]).toContain('security-delegation-missing-reason'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('would reject a gate name the rule table does not back (self-test)', () => { |
| 118 | + // The historical sentence's shape, proving the predicate has teeth |
| 119 | + // regardless of what the JSDoc currently says — without this, "no unbacked |
| 120 | + // ids" below could pass simply because the prose stopped naming rules. |
| 121 | + // |
| 122 | + // The rule name here is deliberately SYNTHETIC rather than #6628's literal |
| 123 | + // `security-delegatable-admin-position`. Whether ADR-0091 D3 should grow |
| 124 | + // that author-time rule is an open product decision the finding declined to |
| 125 | + // make; asserting its name is unbacked would quietly make this test the |
| 126 | + // thing that breaks when someone implements it. What needs pinning is the |
| 127 | + // predicate, not what any one unwritten rule would be called. |
| 128 | + const before = |
| 129 | + 'so a delegatable position must never distribute an `adminScope`-carrying ' + |
| 130 | + 'set (enforced by the `security-no-such-rule-exists` lint rule and the D12 gate).'; |
| 131 | + expect(unbackedRuleIds(before, declaredSecurityRuleIds())).toEqual([ |
| 132 | + 'security-no-such-rule-exists', |
| 133 | + ]); |
| 134 | + }); |
| 135 | + |
| 136 | + it('names no rule that `packages/lint` does not declare', () => { |
| 137 | + expect(unbackedRuleIds(delegatableDoc(), declaredSecurityRuleIds())).toEqual([]); |
| 138 | + }); |
| 139 | + |
| 140 | + it('still locates the D12 containment check at runtime', () => { |
| 141 | + const doc = delegatableDoc(); |
| 142 | + // Both halves, together: the gate that does enforce it, and WHEN. Naming |
| 143 | + // D12 without placing it at runtime is the sentence this pin was written |
| 144 | + // for; placing it at runtime without naming D12 reads as unenforced. |
| 145 | + expect(doc).toContain('D12'); |
| 146 | + expect(doc).toMatch(/runtime/i); |
| 147 | + }); |
| 148 | +}); |
0 commit comments