|
| 1 | +// @vitest-environment jsdom |
| 2 | +/** |
| 3 | + * The leak corpus: fake secrets in many shapes (fixtures/redaction-corpus.json) |
| 4 | + * run through the redaction path of every collector and of the server, to |
| 5 | + * prove none of them comes out readable. A new redaction path belongs in one |
| 6 | + * of the tables below; a new secret shape belongs in the fixture. |
| 7 | + */ |
| 8 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 9 | +import corpus from './fixtures/redaction-corpus.json' with { type: 'json' }; |
| 10 | +import { |
| 11 | + REDACTED, |
| 12 | + isRedactedKey, |
| 13 | + redactMessage, |
| 14 | + SecretSet, |
| 15 | + setRedaction, |
| 16 | +} from '../forms-privacy.ts'; |
| 17 | +import { redactRecord, redactText, redactUrl } from '../router.ts'; |
| 18 | +import { serialize } from '../serialize.ts'; |
| 19 | +import { redactCall, redactHeaders, redactPreview } from '../http-redact.ts'; |
| 20 | +import { redactJsonText } from '../json-text-redact.ts'; |
| 21 | +import { redactAnalogReport } from '../analog-redact.ts'; |
| 22 | +import { redactBody, redactMessage as redactServerMessage } from '../analog-server-log.ts'; |
| 23 | +import { maskedArgs } from '../capacitor-redact.ts'; |
| 24 | +import { serialize as ngrxSerialize } from '../ngrx-shared.ts'; |
| 25 | +import { consoleText, valueText } from '../errors-collector.ts'; |
| 26 | +import { serializeFormValue } from '../forms.ts'; |
| 27 | +import type { AnalogRuntimeReport } from '../analog-runtime.ts'; |
| 28 | +import type { HttpCall } from '../http-rules.ts'; |
| 29 | + |
| 30 | +afterEach(() => setRedaction()); |
| 31 | + |
| 32 | +type Path = (input: string) => unknown; |
| 33 | +type ObjectPath = (input: Record<string, unknown>) => unknown; |
| 34 | + |
| 35 | +const call = (fields: Partial<HttpCall>) => |
| 36 | + redactCall({ id: 1, method: 'GET', url: '/api', ...fields } as HttpCall); |
| 37 | + |
| 38 | +const report = (fields: Partial<AnalogRuntimeReport>) => |
| 39 | + redactAnalogReport({ |
| 40 | + pageId: 'p1', |
| 41 | + url: '/', |
| 42 | + analog: true, |
| 43 | + chain: [], |
| 44 | + hydrated: 0, |
| 45 | + transferState: false, |
| 46 | + hydrationErrors: [], |
| 47 | + configPaths: [], |
| 48 | + ...fields, |
| 49 | + }); |
| 50 | + |
| 51 | +const show = (value: unknown): string => |
| 52 | + typeof value === 'string' ? value : (JSON.stringify(value) ?? ''); |
| 53 | + |
| 54 | +/** Paths that take free text: messages, errors, previews, values. */ |
| 55 | +const PAIR_TEXT_PATHS: Record<string, Path> = { |
| 56 | + 'serialize (component inputs, signals, pipes, providers)': (t) => serialize(t), |
| 57 | + 'NgRx serialize': (t) => ngrxSerialize(t), |
| 58 | + 'router redactText (titles, guards, warnings)': (t) => redactText(t), |
| 59 | + 'router redactRecord (params and data)': (t) => redactRecord({ note: t }), |
| 60 | + 'errors valueText': (t) => valueText(t), |
| 61 | + 'errors consoleText': (t) => consoleText(['failed:', t]), |
| 62 | + 'errors valueText of an Error': (t) => valueText(new Error(t)), |
| 63 | + 'HTTP call error': (t) => call({ error: t }), |
| 64 | + 'HTTP response preview (text)': (t) => redactPreview(t), |
| 65 | + 'HTTP header value': (t) => redactHeaders({ 'x-debug': t }), |
| 66 | + 'Analog hydration error': (t) => report({ hydrationErrors: [t] }), |
| 67 | + 'Capacitor plugin argument': (t) => maskedArgs('App', { message: t }, {}), |
| 68 | +}; |
| 69 | + |
| 70 | +/** Paths that hide value shapes in text, but not `key=value` pairs. */ |
| 71 | +const SHAPE_ONLY_TEXT_PATHS: Record<string, Path> = { |
| 72 | + // Object keys are masked by shape: a key is a name, not a sentence. |
| 73 | + 'serialize, as an object key': (t) => serialize({ [t]: 1 }), |
| 74 | + 'NgRx serialize, as an object key': (t) => ngrxSerialize({ [t]: 1 }), |
| 75 | + 'forms redactMessage (field errors)': (t) => redactMessage(t), |
| 76 | + 'forms SecretSet (event log)': (t) => new SecretSet().redact(t), |
| 77 | + 'Analog server log message': (t) => redactServerMessage(t), |
| 78 | + 'Analog server log body': (t) => redactBody(t), |
| 79 | +}; |
| 80 | + |
| 81 | +/** Paths that take an object and hide its values by key. */ |
| 82 | +const OBJECT_PATHS: Record<string, ObjectPath> = { |
| 83 | + serialize: (o) => serialize(o), |
| 84 | + 'NgRx serialize': (o) => ngrxSerialize(o), |
| 85 | + 'router redactRecord': (o) => redactRecord(o), |
| 86 | + 'forms serializeFormValue': (o) => serializeFormValue(o), |
| 87 | + 'HTTP response preview (JSON)': (o) => redactPreview(JSON.stringify(o)), |
| 88 | + 'HTTP response preview (cut JSON)': (o) => redactPreview(JSON.stringify(o).slice(0, -2)), |
| 89 | + 'HTTP call preview': (o) => call({ preview: JSON.stringify(o) }), |
| 90 | + 'JSON text redaction': (o) => redactJsonText(JSON.stringify(o), isRedactedKey), |
| 91 | + 'Capacitor plugin argument': (o) => maskedArgs('Preferences', o, {}), |
| 92 | + 'Analog load() preview': (o) => |
| 93 | + report({ load: { preview: JSON.stringify(o), bytes: 10, keys: [] } }), |
| 94 | + 'Analog server log body': (o) => redactBody(JSON.stringify(o)), |
| 95 | + 'errors valueText': (o) => valueText(o), |
| 96 | + 'errors consoleText': (o) => consoleText(['state', o]), |
| 97 | + 'HTTP headers': (o) => |
| 98 | + redactHeaders(Object.fromEntries(Object.entries(o).map(([k, v]) => [k, String(v)]))), |
| 99 | +}; |
| 100 | + |
| 101 | +/** Paths that take a URL. */ |
| 102 | +const URL_PATHS: Record<string, Path> = { |
| 103 | + 'router redactUrl': (u) => redactUrl(u), |
| 104 | + 'HTTP call URL': (u) => call({ url: u }), |
| 105 | + 'HTTP redirect target': (u) => call({ redirectedTo: u }), |
| 106 | + 'Analog page URL': (u) => report({ url: u }), |
| 107 | + 'Capacitor HTTP URL': (u) => maskedArgs('CapacitorHttp', { url: u, method: 'GET' }, {}), |
| 108 | + 'Analog server log message': (u) => redactServerMessage(u), |
| 109 | +}; |
| 110 | + |
| 111 | +function expectHidden(path: string, input: string, out: unknown, secret: string) { |
| 112 | + const text = show(out); |
| 113 | + expect( |
| 114 | + text, |
| 115 | + `${path} leaked ${JSON.stringify(secret)} from ${JSON.stringify(input)}`, |
| 116 | + ).not.toContain(secret); |
| 117 | + expect(text, `${path} hid ${JSON.stringify(input)} without a marker`).toMatch( |
| 118 | + /\[redacted\]|%5Bredacted%5D/, |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +describe('the leak corpus', () => { |
| 123 | + it('has every kind of secret it promises', () => { |
| 124 | + expect(corpus.shapes.length).toBeGreaterThanOrEqual(20); |
| 125 | + expect(corpus.named.length).toBeGreaterThanOrEqual(10); |
| 126 | + for (const entry of corpus.shapes) expect(entry.value).toContain(entry.secret); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('value shapes hide themselves in any text', () => { |
| 130 | + const paths = { ...PAIR_TEXT_PATHS, ...SHAPE_ONLY_TEXT_PATHS }; |
| 131 | + for (const [path, run] of Object.entries(paths)) { |
| 132 | + it(path, () => { |
| 133 | + for (const { value, secret } of corpus.shapes) { |
| 134 | + for (const input of [value, `request failed (${value}) retry later`]) { |
| 135 | + expectHidden(path, input, run(input), secret); |
| 136 | + } |
| 137 | + } |
| 138 | + }); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + describe('values under secret names hide in key=value text', () => { |
| 143 | + for (const [path, run] of Object.entries(PAIR_TEXT_PATHS)) { |
| 144 | + it(path, () => { |
| 145 | + for (const { key, secret } of corpus.named) { |
| 146 | + for (const input of [ |
| 147 | + `${key}=${secret}`, |
| 148 | + `retry with ${key}=${secret}&user=ada`, |
| 149 | + `{"${key}":"${secret}","user":"ada"}`, |
| 150 | + `${key}: "${secret}"`, |
| 151 | + ]) { |
| 152 | + expectHidden(path, input, run(input), secret); |
| 153 | + } |
| 154 | + } |
| 155 | + }); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + describe('values under secret names hide in objects', () => { |
| 160 | + for (const [path, run] of Object.entries(OBJECT_PATHS)) { |
| 161 | + it(path, () => { |
| 162 | + for (const { key, secret } of corpus.named) { |
| 163 | + for (const input of [{ [key]: secret, user: 'ada' }, { form: { [key]: secret } }]) { |
| 164 | + if (path === 'HTTP headers' && 'form' in input) continue; |
| 165 | + expectHidden(path, JSON.stringify(input), run(input), secret); |
| 166 | + } |
| 167 | + } |
| 168 | + }); |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + describe('values under secret names hide in URLs', () => { |
| 173 | + for (const [path, run] of Object.entries(URL_PATHS)) { |
| 174 | + it(path, () => { |
| 175 | + for (const { key, secret } of corpus.named) { |
| 176 | + for (const input of [ |
| 177 | + `https://app.example.com/cb?${key}=${secret}&page=2`, |
| 178 | + `/login#${key}=${encodeURIComponent(secret)}`, |
| 179 | + ]) { |
| 180 | + expectHidden(path, input, run(input), secret); |
| 181 | + } |
| 182 | + } |
| 183 | + }); |
| 184 | + } |
| 185 | + }); |
| 186 | + |
| 187 | + describe('value shapes hide in URLs', () => { |
| 188 | + const inUrl = corpus.shapes.filter((entry) => /^(https?:|eyJ)/.test(entry.value)); |
| 189 | + for (const [path, run] of Object.entries(URL_PATHS)) { |
| 190 | + it(path, () => { |
| 191 | + for (const { value, secret } of inUrl) { |
| 192 | + const input = value.startsWith('eyJ') ? `/verify/${value}?page=2` : value; |
| 193 | + expectHidden(path, input, run(input), secret); |
| 194 | + } |
| 195 | + }); |
| 196 | + } |
| 197 | + }); |
| 198 | + |
| 199 | + it('keeps the marker every path writes the same', () => { |
| 200 | + expect(REDACTED).toBe('[redacted]'); |
| 201 | + expect(redactJsonText('{"password":"x"}', isRedactedKey)).toBe(`{"password":"${REDACTED}"}`); |
| 202 | + }); |
| 203 | +}); |
0 commit comments