|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | +import { forgetMaskedFormValues } from '../component-form-masks.ts'; |
| 4 | +import { collectSignalGraph, graphValue, type SignalDebugNg } from '../signal-graph.ts'; |
| 5 | +import { createSignalHistory } from '../signal-history.ts'; |
| 6 | +import { maskHistory, secretsOfGraph } from '../signal-masks.ts'; |
| 7 | +import { answerPeek } from '../signal-peek.ts'; |
| 8 | + |
| 9 | +function directive(selector: string, fields: Record<string, unknown>) { |
| 10 | + class Dir {} |
| 11 | + (Dir as unknown as Record<string, unknown>)['ɵdir'] = { selectors: [['', selector, '']] }; |
| 12 | + return Object.assign(new Dir(), fields); |
| 13 | +} |
| 14 | + |
| 15 | +const field = (name: string, value: unknown) => |
| 16 | + directive('formField', { state: () => ({ name: () => `login.${name}`, value: () => value }) }); |
| 17 | + |
| 18 | +class Login {} |
| 19 | + |
| 20 | +const root = { scopes: new Set(['environment', 'root']) }; |
| 21 | + |
| 22 | +/** |
| 23 | + * A component with Signal Forms fields bound with `[formField]`, and a signal |
| 24 | + * graph shaped like Angular's: one unnamed `computed` per field value. |
| 25 | + */ |
| 26 | +function setup(values: Record<string, string>) { |
| 27 | + document.body.innerHTML = ` |
| 28 | + <app-login ng-version="22.0.0"> |
| 29 | + <input id="email" /> |
| 30 | + <input id="password" /> |
| 31 | + <input id="token" /> |
| 32 | + <input id="apiKey" /> |
| 33 | + <input id="code" autocomplete="one-time-code" /> |
| 34 | + <input id="note" data-pangular="mask" /> |
| 35 | + </app-login>`; |
| 36 | + const host = document.querySelector('app-login')!; |
| 37 | + const at = (id: string) => document.getElementById(id)!; |
| 38 | + const instance = new Login(); |
| 39 | + const ng: SignalDebugNg = { |
| 40 | + getComponent: (el) => (el === host ? instance : null), |
| 41 | + getInjector: (el) => el, |
| 42 | + getDirectives: (el) => (el.id && el.id in values ? [field(el.id, values[el.id])] : []), |
| 43 | + ɵgetInjectorResolutionPath: (injector) => [injector, root], |
| 44 | + ɵgetInjectorMetadata: (injector) => |
| 45 | + injector === root |
| 46 | + ? { type: 'environment', source: null } |
| 47 | + : { type: 'element', source: at('email') }, |
| 48 | + ɵgetSignalGraph: (injector) => ({ |
| 49 | + nodes: |
| 50 | + injector === root |
| 51 | + ? [{ id: '20', kind: 'signal', label: 'lastLogin', epoch: 1, value: values['password'] }] |
| 52 | + : [ |
| 53 | + { id: '1', kind: 'signal', label: 'model', epoch: 1, value: { ...values } }, |
| 54 | + ...Object.keys(values).map((key, i) => ({ |
| 55 | + id: String(i + 2), |
| 56 | + kind: 'computed', |
| 57 | + epoch: 1, |
| 58 | + value: values[key], |
| 59 | + })), |
| 60 | + { |
| 61 | + id: '9', |
| 62 | + kind: 'computed', |
| 63 | + label: 'summary', |
| 64 | + epoch: 1, |
| 65 | + value: `token ${values['token']} for ${values['email']}`, |
| 66 | + }, |
| 67 | + ], |
| 68 | + edges: [], |
| 69 | + }), |
| 70 | + }; |
| 71 | + return { ng, host }; |
| 72 | +} |
| 73 | + |
| 74 | +const VALUES = { |
| 75 | + |
| 76 | + password: 'CNRY_pw_q7Zx', |
| 77 | + token: 'CNRY_token_k2Lm', |
| 78 | + apiKey: 'CNRY_apiKey_w9Rt', |
| 79 | + code: '845210', |
| 80 | + note: 'CNRY_note_m1Xc', |
| 81 | +}; |
| 82 | +const SECRETS = ['CNRY_pw_q7Zx', 'CNRY_token_k2Lm', 'CNRY_apiKey_w9Rt', '845210', 'CNRY_note_m1Xc']; |
| 83 | + |
| 84 | +function history() { |
| 85 | + return createSignalHistory( |
| 86 | + (value, name) => graphValue(name, value), |
| 87 | + Date.now, |
| 88 | + (value, name) => graphValue(name, value), |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +describe('signal graph form masks', () => { |
| 93 | + afterEach(() => { |
| 94 | + forgetMaskedFormValues(); |
| 95 | + document.body.innerHTML = ''; |
| 96 | + }); |
| 97 | + |
| 98 | + it('masks unnamed nodes that hold a secret field value, in the graph the page pushes', () => { |
| 99 | + const { ng } = setup(VALUES); |
| 100 | + const graph = collectSignalGraph(ng, null)!; |
| 101 | + expect(graph.component?.tag).toBe('app-login'); |
| 102 | + const text = JSON.stringify(graph); |
| 103 | + for (const secret of SECRETS) expect(text).not.toContain(secret); |
| 104 | + const byId = Object.fromEntries(graph.nodes.map((n) => [n.id, n.value])); |
| 105 | + expect(byId['2']).toBe('[email protected]'); |
| 106 | + expect(byId['3']).toBe('[redacted]'); |
| 107 | + expect(byId['9']).toBe('token [redacted] for [email protected]'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('masks a peek by selector, by id and its history, and keeps harmless values', () => { |
| 111 | + const { ng } = setup(VALUES); |
| 112 | + const signals = history(); |
| 113 | + for (const request of [{ selector: 'app-login' }, { env: 'root' }]) { |
| 114 | + const answer = answerPeek( |
| 115 | + { requestId: 'r', ...request }, |
| 116 | + 'page-1', |
| 117 | + (target) => collectSignalGraph(ng, target), |
| 118 | + signals, |
| 119 | + ); |
| 120 | + const text = JSON.stringify(answer); |
| 121 | + expect(answer.graph).not.toBeNull(); |
| 122 | + for (const secret of SECRETS) expect(text).not.toContain(secret); |
| 123 | + } |
| 124 | + // The environment graph masks the values the component's fields showed as secret. |
| 125 | + const env = collectSignalGraph(ng, { env: 'root' })!; |
| 126 | + expect(env.nodes[0]!.value).toBe('[redacted]'); |
| 127 | + const peek = answerPeek( |
| 128 | + { requestId: 'r', selector: 'app-login' }, |
| 129 | + 'page-1', |
| 130 | + (target) => collectSignalGraph(ng, target), |
| 131 | + signals, |
| 132 | + ).graph!; |
| 133 | + expect(peek.history!['2']![0]!.value).toBe('[email protected]'); |
| 134 | + expect(peek.history!['3']![0]!.value).toBe('[redacted]'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('keeps masking a secret value after the field changed', () => { |
| 138 | + const first = setup(VALUES); |
| 139 | + collectSignalGraph(first.ng, null); |
| 140 | + const { ng } = setup({ ...VALUES, password: 'CNRY_pw_next_r5Tu' }); |
| 141 | + const graph = collectSignalGraph(ng, null)!; |
| 142 | + const old = maskHistory( |
| 143 | + { '3': [{ epoch: 1, value: 'CNRY_pw_q7Zx', at: 0, source: 'initial' }] }, |
| 144 | + secretsOfGraph(graph), |
| 145 | + ); |
| 146 | + expect(old['3']![0]!.value).toBe('[redacted]'); |
| 147 | + expect(JSON.stringify(graph)).not.toContain('CNRY_pw_next_r5Tu'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('leaves history alone when the graph has no secrets', () => { |
| 151 | + const recorded = { '1': [{ epoch: 1, value: 'x', at: 0, source: 'write' as const }] }; |
| 152 | + expect(maskHistory(recorded, new Set())).toBe(recorded); |
| 153 | + }); |
| 154 | +}); |
0 commit comments