|
| 1 | +import { EventEmitter } from 'node:events'; |
| 2 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | +import { analogMiddleware, clearCalls, recentCalls } from '../analog-server-log.ts'; |
| 4 | +import type { HttpCall } from '../http-rules.ts'; |
| 5 | +import { redactCall } from '../http-redact.ts'; |
| 6 | +import { applyRouterEvent, redactUrl, type NavigationRecord } from '../router.ts'; |
| 7 | +import { explainNavigationText, type RouterPage } from '../rpc/router-tools.ts'; |
| 8 | +import { listHttpCallsText } from '../rpc/http-tools.ts'; |
| 9 | +import { lintRoutes } from '../rpc/router-config-tools.ts'; |
| 10 | +import { maskEmails } from '../text-redact.ts'; |
| 11 | + |
| 12 | +describe('emails in URLs', () => { |
| 13 | + it('masks an address in a path segment, a query value and a fragment', () => { |
| 14 | + expect(redactUrl('/invite/[email protected]')).toBe('/invite/[email]'); |
| 15 | + expect(redactUrl('/invite/[email protected]/accept')).toBe('/invite/[email]/accept'); |
| 16 | + expect(redactUrl('/[email protected]&page=2')).toBe( |
| 17 | + '/users?email=[email]&page=2', |
| 18 | + ); |
| 19 | + expect(redactUrl('/users#[email protected]')).toBe('/users#[email]'); |
| 20 | + expect(redactUrl('/users;[email protected]')).toBe('/users;contact=[email]'); |
| 21 | + expect(redactUrl('https://app.example.com/u/[email protected]?x=1')).toBe( |
| 22 | + 'https://app.example.com/u/[email]?x=1', |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + it('masks an address whose @ is percent-encoded', () => { |
| 27 | + expect(redactUrl('/invite/ada%40example.com')).toBe('/invite/[email]'); |
| 28 | + expect(redactUrl('/search?q=ada%2Bnews%40example.com')).toBe('/search?q=[email]'); |
| 29 | + expect(redactUrl('/login?next=%2Finvite%2Fada%40example.com')).toBe( |
| 30 | + '/login?next=%2Finvite%2F[email]', |
| 31 | + ); |
| 32 | + expect(redactUrl('mailto:[email protected]')).toBe('mailto:[email]'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('masks every address and keeps the text around them', () => { |
| 36 | + expect(redactUrl('/a/[email protected]/b/[email protected]?to=eve%40example.net#x')).toBe( |
| 37 | + '/a/[email]/b/[email]?to=[email]#x', |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it('leaves an @ that is not part of an address alone', () => { |
| 42 | + for (const url of [ |
| 43 | + '/@angular/core', |
| 44 | + '/npm/@scope/[email protected]/index.js', |
| 45 | + 'https://cdn.example.com/npm/[email protected]/lodash.min.js', |
| 46 | + 'https://example.com/users/@alice', |
| 47 | + 'http://localhost:4200/a:b@c', |
| 48 | + '/%40fs/etc/passwd', |
| 49 | + '/users/ada@localhost', |
| 50 | + 'ssh://[email protected]/org/repo.git', |
| 51 | + 'https://[email protected]/path', |
| 52 | + ]) { |
| 53 | + expect(redactUrl(url), url).toBe(url); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + it('still hides a URL password and keeps the host', () => { |
| 58 | + expect(redactUrl('https://ada:[email protected]/u/[email protected]')).toBe( |
| 59 | + 'https://ada:[redacted]@api.example.com/u/[email]', |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('stays fast on long text with many @ and %40 signs', () => { |
| 64 | + const nasty = [ |
| 65 | + 'a%40'.repeat(50_000), |
| 66 | + 'a@'.repeat(50_000), |
| 67 | + `${'a.'.repeat(50_000)}@`, |
| 68 | + `x@${'a.'.repeat(50_000)}1`, |
| 69 | + '%2B'.repeat(50_000), |
| 70 | + ]; |
| 71 | + const start = performance.now(); |
| 72 | + for (const text of nasty) maskEmails(text); |
| 73 | + expect(performance.now() - start).toBeLessThan(500); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe('emails in URLs shown to the panel and to agents', () => { |
| 78 | + it('masks them in a recorded HTTP call and in list-http-calls', () => { |
| 79 | + const call: HttpCall = { |
| 80 | + id: 'c1', |
| 81 | + url: '/api/users/[email protected]?to=bob%40example.org', |
| 82 | + pageUrl: '/profile/[email protected]', |
| 83 | + redirectedTo: '/api/users/ada%40example.com/v2', |
| 84 | + method: 'GET', |
| 85 | + status: 200, |
| 86 | + durationMs: 3, |
| 87 | + side: 'client', |
| 88 | + cacheHit: false, |
| 89 | + faulted: false, |
| 90 | + at: 1_000, |
| 91 | + }; |
| 92 | + const safe = redactCall(call); |
| 93 | + expect(safe.url).toBe('/api/users/[email]?to=[email]'); |
| 94 | + expect(safe.pageUrl).toBe('/profile/[email]'); |
| 95 | + expect(safe.redirectedTo).toBe('/api/users/[email]/v2'); |
| 96 | + const text = listHttpCallsText({ serverCalls: [call], pages: [] }, {}, 2_000); |
| 97 | + expect(text).toContain('/api/users/[email]'); |
| 98 | + expect(text).not.toContain('example'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('masks them in a router navigation and in explain-navigation', () => { |
| 102 | + const navigations: NavigationRecord[] = []; |
| 103 | + applyRouterEvent(navigations, { type: 0, id: 1, url: '/invite/[email protected]' }, 0); |
| 104 | + applyRouterEvent( |
| 105 | + navigations, |
| 106 | + { type: 1, id: 1, urlAfterRedirects: '/invite/ada%40example.com/welcome' }, |
| 107 | + 5, |
| 108 | + ); |
| 109 | + expect(navigations[0]).toMatchObject({ |
| 110 | + url: '/invite/[email]', |
| 111 | + finalUrl: '/invite/[email]/welcome', |
| 112 | + }); |
| 113 | + const page: RouterPage = { |
| 114 | + pageId: 'aaaa', |
| 115 | + snapshot: null, |
| 116 | + navigations, |
| 117 | + reportedAt: 1_000, |
| 118 | + changedAt: 1_000, |
| 119 | + } as unknown as RouterPage; |
| 120 | + const text = explainNavigationText({ pages: [page] }, {}, 1_000); |
| 121 | + expect(text).toContain('/invite/[email]'); |
| 122 | + expect(text).not.toContain('example'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('still flags the email-in-url lint on a navigation the page already masked', () => { |
| 126 | + const page = { |
| 127 | + pageId: 'aaaa', |
| 128 | + config: [], |
| 129 | + navigations: [{ id: 1, url: redactUrl('/invite/[email protected]'), trigger: 'imperative' }], |
| 130 | + } as unknown as Parameters<typeof lintRoutes>[0]; |
| 131 | + const finding = lintRoutes(page).find((f) => f.rule === 'email-in-url'); |
| 132 | + expect(finding?.route).toBe('/invite/[email]'); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +class FakeRes extends EventEmitter { |
| 137 | + statusCode = 200; |
| 138 | + headers: Record<string, string> = {}; |
| 139 | + getHeader(name: string) { |
| 140 | + return this.headers[name.toLowerCase()]; |
| 141 | + } |
| 142 | + write() { |
| 143 | + return true; |
| 144 | + } |
| 145 | + end() { |
| 146 | + this.emit('finish'); |
| 147 | + return this; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +describe('emails in Analog server call URLs', () => { |
| 152 | + afterEach(() => clearCalls()); |
| 153 | + |
| 154 | + it('masks them in the call URL, route and redirect location', () => { |
| 155 | + const req = { |
| 156 | + url: '/index.html', |
| 157 | + originalUrl: '/api/_analog/pages/invite/[email protected]?to=bob%40example.org', |
| 158 | + method: 'POST', |
| 159 | + headers: {}, |
| 160 | + } as never; |
| 161 | + const res = new FakeRes(); |
| 162 | + res.statusCode = 303; |
| 163 | + res.headers['location'] = '/welcome/[email protected]'; |
| 164 | + analogMiddleware('api')(req, res as never, () => undefined); |
| 165 | + res.end(); |
| 166 | + const call = recentCalls().at(-1)!; |
| 167 | + expect(call.url).toBe('/api/_analog/pages/invite/[email]?to=[email]'); |
| 168 | + expect(call.route).toBe('/invite/[email]'); |
| 169 | + expect(call.location).toBe('/welcome/[email]'); |
| 170 | + }); |
| 171 | +}); |
0 commit comments