also looking at this
Promote main to production: signed-out chrome and the subscriber tables
#17
54 files
+4252
-1353
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -0,0 +1,141 @@ | ||
| 1 | /** | |
| 2 | * A page that cannot serve you must not offer you its controls. | |
| 3 | * | |
| 4 | * Four pages across this estate rendered their signed-in chrome directly above | |
| 5 | * their own signed-out gate: "+ New app" and "Log out" over a panel reading | |
| 6 | * "You're signed out", a fixed "Log out" button floating over the sign-in | |
| 7 | * prompt, a full reporting toolbar - range presets, Compare, CSV export - above | |
| 8 | * "Sign in to view analytics". Nothing leaked. Every one of those controls | |
| 9 | * answers 401, or logs nobody out, or re-queries a report whose payload comes | |
| 10 | * back with every number at zero. The bug is what it says to the visitor: a | |
| 11 | * page offering controls it cannot honour reads as broken rather than as | |
| 12 | * locked, and "Log out" in particular claims the visitor is signed in while the | |
| 13 | * panel beneath it says they are not. | |
| 14 | * | |
| 15 | * Two legitimate vehicles, and which one is right depends on where the viewer | |
| 16 | * comes from: | |
| 17 | * | |
| 18 | * - A page with a `<script server>` block that resolves the auth-token cookie | |
| 19 | * knows the viewer BEFORE paint, so it branches: `@if (user)`. Nothing wrong | |
| 20 | * is ever sent to the browser. | |
| 21 | * - A page whose viewer arrives from /api/me after paint cannot branch on the | |
| 22 | * server, so it binds: `:show="!!user()"`. The signal is null until the | |
| 23 | * request answers, which is why the binding must be positive ("show when | |
| 24 | * there is a viewer") rather than negated - a negated guard would paint the | |
| 25 | * control during the flight and hide it afterwards, which is the wrong way | |
| 26 | * round. | |
| 27 | * | |
| 28 | * Both count here. What does not count is no guard at all. | |
| 29 | */ | |
| 30 | import { describe, expect, test } from 'bun:test' | |
| 31 | import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs' | |
| 32 | import { join } from 'node:path' | |
| 33 | ||
| 34 | const ROOT = join(import.meta.dir, '../..') | |
| 35 | ||
| 36 | /** | |
| 37 | * Controls that only mean something to a signed-in viewer. | |
| 38 | * | |
| 39 | * Deliberately narrow. A link to /pricing or /login is fine to show anyone, and | |
| 40 | * so is the theme toggle - it works signed out, and hiding it would be its own | |
| 41 | * small bug. These are the ones that either act on a session that does not | |
| 42 | * exist or re-read data the visitor has not been granted. | |
| 43 | */ | |
| 44 | const AFFORDANCE = /@click="(?:logout|signOut)"|>\s*Log out\s*<|@click="exportCsv"|Export this report/i | |
| 45 | ||
| 46 | /** | |
| 47 | * Names that establish "this viewer is allowed to be here". | |
| 48 | * | |
| 49 | * `signedOut` belongs in the list: a block guarded by it is the signed-out | |
| 50 | * branch, and anything inside it was written for exactly that case. | |
| 51 | */ | |
| 52 | const VIEWER = '(?:user|viewGranted|ownsActive|signedIn|signedOut|isAuthed|authed|loggedIn|viewer)' | |
| 53 | const GUARD_IF = new RegExp(`@if\\s*\\(\\s*!?\\s*${VIEWER}\\b`) | |
| 54 | const SHOW_BIND = new RegExp(`:show="[^"]*${VIEWER}\\b[^"]*"`) | |
| 55 | const IF_OPEN = /@if\s*\(/g | |
| 56 | const IF_CLOSE = /@endif/g | |
| 57 | ||
| 58 | function views(dir: string): string[] { | |
| 59 | const out: string[] = [] | |
| 60 | if (!existsSync(dir)) return out | |
| 61 | for (const entry of readdirSync(dir, { withFileTypes: true })) { | |
| 62 | const path = join(dir, entry.name) | |
| 63 | let directory = entry.isDirectory() | |
| 64 | if (entry.isSymbolicLink()) { | |
| 65 | try { directory = statSync(path).isDirectory() } | |
| 66 | catch { continue } | |
| 67 | } | |
| 68 | if (directory) out.push(...views(path)) | |
| 69 | else if (entry.name.endsWith('.stx')) out.push(path) | |
| 70 | } | |
| 71 | return out | |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * stx comments, stripped first. | |
| 76 | * | |
| 77 | * The prose above each of these guards describes the control it guards, in the | |
| 78 | * words the detector matches - the comment on the fixed chrome says the words | |
| 79 | * "Log out" - so leaving comments in makes a file fail on its own explanation. | |
| 80 | * The busy-state gate learned this the same way. | |
| 81 | */ | |
| 82 | function code(source: string): string { | |
| 83 | return source.replace(/\{\{--[\s\S]*?--\}\}/g, '') | |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Walk the file tracking @if depth, recording for each open block whether it | |
| 88 | * establishes a viewer. A line is covered when ANY enclosing block does, which | |
| 89 | * is what lets a guard wrap a whole header rather than each button inside it. | |
| 90 | */ | |
| 91 | function unguarded(source: string): string[] { | |
| 92 | const found: string[] = [] | |
| 93 | const stack: boolean[] = [] | |
| 94 | const lines = code(source).split('\n') | |
| 95 | for (const [index, line] of lines.entries()) { | |
| 96 | for (const match of line.matchAll(IF_OPEN)) | |
| 97 | stack.push(GUARD_IF.test(line.slice(match.index))) | |
| 98 | for (const _ of line.matchAll(IF_CLOSE)) stack.pop() | |
| 99 | if (AFFORDANCE.test(line) && !stack.some(Boolean) && !SHOW_BIND.test(line)) | |
| 100 | found.push(`${index + 1}: ${line.trim()}`) | |
| 101 | } | |
| 102 | return found | |
| 103 | } | |
| 104 | ||
| 105 | describe('signed-in controls are guarded', () => { | |
| 106 | const files = views(join(ROOT, 'resources')) | |
| 107 | ||
| 108 | test('every view carrying one is reachable only by a viewer', () => { | |
| 109 | const offenders: string[] = [] | |
| 110 | for (const file of files) { | |
| 111 | const hits = unguarded(readFileSync(file, 'utf8')) | |
| 112 | for (const hit of hits) | |
| 113 | offenders.push(`${file.slice(ROOT.length + 1)}:${hit}`) | |
| 114 | } | |
| 115 | expect(offenders).toEqual([]) | |
| 116 | }) | |
| 117 | ||
| 118 | test('the sweep actually reads this app\'s views', () => { | |
| 119 | // The blind spot that makes a gate worthless: a path that resolves to | |
| 120 | // nothing passes forever. validate-ui shipped a breakpoint check that could | |
| 121 | // not fail in any of the six apps for exactly this reason. | |
| 122 | expect(files.length).toBeGreaterThan(5) | |
| 123 | expect(files.every(file => file.endsWith('.stx'))).toBe(true) | |
| 124 | }) | |
| 125 | ||
| 126 | test('the detector still recognises both guards, and the absence of one', () => { | |
| 127 | const guarded = '@if (user)\n <button @click="logout">Log out</button>\n@endif' | |
| 128 | const bound = '<button @click="logout" :show="!!user()">Log out</button>' | |
| 129 | const bare = '<button @click="logout">Log out</button>' | |
| 130 | const wrongGuard = '@if (isAll)\n <button @click="logout">Log out</button>\n@endif' | |
| 131 | ||
| 132 | expect(unguarded(guarded)).toEqual([]) | |
| 133 | expect(unguarded(bound)).toEqual([]) | |
| 134 | expect(unguarded(bare)).toHaveLength(1) | |
| 135 | expect(unguarded(wrongGuard)).toHaveLength(1) | |
| 136 | }) | |
| 137 | ||
| 138 | test('a comment naming the control does not fail the file', () => { | |
| 139 | expect(unguarded('{{-- the Log out button is guarded below --}}\n@if (user)\n <button @click="logout">Log out</button>\n@endif')).toEqual([]) | |
| 140 | }) | |
| 141 | }) | |