private-routes.test.ts (5179B)
1 import { render } from 'svelte/server'; 2 import { describe, expect, it, vi } from 'vitest'; 3 import { canonicalLocalId, newLocalId } from '../../src/lib/private-handles'; 4 import { 5 conversationHref, 6 draftHref, 7 safeContextBack, 8 staticHref 9 } from '../../src/lib/routes'; 10 import { match } from '../../src/params/local_id'; 11 import ContextBack from '../../src/lib/components/ContextBack.svelte'; 12 import { naddrEncode } from 'applesauce-core/helpers/pointers'; 13 import oracle from './reference-oracle.json'; 14 15 const id = '6b65d774-26f1-4e7e-a414-387d347b3106'; 16 17 describe('opaque private resource URLs', () => { 18 it('generates canonical UUIDs with the platform randomUUID operation', () => { 19 const random = vi.spyOn(globalThis.crypto, 'randomUUID'); 20 const ids = Array.from({ length: 32 }, () => newLocalId()); 21 expect(random).toHaveBeenCalledTimes(32); 22 expect(new Set(ids).size).toBe(32); 23 for (const value of ids) { 24 expect(canonicalLocalId(value)).toBe(value); 25 expect(match(value!)).toBe(true); 26 } 27 random.mockRestore(); 28 }); 29 it('fails safely when platform randomness is unavailable', () => { 30 const random = vi 31 .spyOn(globalThis.crypto, 'randomUUID') 32 .mockImplementation(() => { 33 throw new Error('Unavailable'); 34 }); 35 expect(newLocalId()).toBeUndefined(); 36 random.mockRestore(); 37 }); 38 for (const value of [ 39 undefined, 40 null, 41 {}, 42 '', 43 id.toUpperCase(), 44 ` ${id}`, 45 `${id}/edit`, 46 `${id}?body=hello`, 47 `${id}#peer`, 48 id.replace('-4e7e-', '-7e7e-'), 49 id.replace('-a414-', '-0414-'), 50 '0'.repeat(64), 51 'x'.repeat(8193) 52 ]) 53 it(`rejects a noncanonical private handle ${JSON.stringify(value)?.slice(0, 80) ?? 'undefined'}`, () => { 54 expect(canonicalLocalId(value)).toBeUndefined(); 55 expect(draftHref(value)).toBeUndefined(); 56 expect(conversationHref(value)).toBeUndefined(); 57 if (typeof value === 'string') expect(match(value)).toBe(false); 58 }); 59 it('puts only the opaque token into both private resource paths', () => { 60 expect(draftHref(id)).toBe(`/selling/drafts/${id}`); 61 expect(conversationHref(id)).toBe(`/messages/${id}`); 62 for (const href of [draftHref(id), conversationHref(id)]) { 63 const url = new URL(href!, 'https://example.invalid'); 64 expect(url.search).toBe(''); 65 expect(url.hash).toBe(''); 66 expect(url.pathname.endsWith(id)).toBe(true); 67 } 68 }); 69 it('treats unknown but well-formed handles as syntax, never owner/data authority', () => { 70 expect(match(id)).toBe(true); 71 expect(draftHref({ id, owner: 'alice', body: 'secret' })).toBeUndefined(); 72 expect( 73 conversationHref({ id, peer: 'bob', quantity: '2', listing: 'carrots' }) 74 ).toBeUndefined(); 75 expect(safeContextBack(`/selling/drafts/${id}`)).toBeUndefined(); 76 expect(safeContextBack(`/messages/${id}`)).toBeUndefined(); 77 }); 78 }); 79 80 describe('explicit contextual navigation', () => { 81 it('keeps only canonical public listing context and strips relay hints', () => { 82 const coordinate = { 83 kind: 30402, 84 pubkey: oracle.key_cases[0].pubkey, 85 identifier: 'Carrots:菜' 86 }; 87 const canonical = naddrEncode(coordinate); 88 const hinted = naddrEncode({ 89 ...coordinate, 90 relays: ['wss://example.invalid'] 91 }); 92 expect(safeContextBack(`/products/${hinted.toUpperCase()}`)).toBe( 93 `/products/${canonical}` 94 ); 95 const output = render(ContextBack, { 96 props: { href: `/products/${hinted}` } 97 }).body; 98 expect(output).toContain(`href="/products/${canonical}"`); 99 expect(output).toContain('Back to listing'); 100 for (const href of [ 101 `/products/${hinted}/edit`, 102 `/products/${hinted}?peer=alice`, 103 '/products/' + 'x'.repeat(2049) 104 ]) 105 expect(safeContextBack(href)).toBeUndefined(); 106 }); 107 for (const href of [ 108 '/', 109 '/search', 110 '/sell', 111 '/selling', 112 '/messages', 113 '/about', 114 '/privacy' 115 ]) 116 it(`constructs the declared static route ${href}`, () => { 117 expect(staticHref(href)).toBe(href); 118 }); 119 for (const href of [ 120 'https://example.invalid/', 121 '//example.invalid/', 122 '/orders', 123 '/search?returnTo=https://example.invalid', 124 '/messages?peer=alice', 125 '/selling#body', 126 '/search/../messages', 127 '/%2f%2fexample.invalid', 128 '\\evil', 129 {} 130 ]) 131 it(`rejects arbitrary return context ${JSON.stringify(href)}`, () => { 132 expect(safeContextBack(href)).toBeUndefined(); 133 expect(staticHref(href)).toBeUndefined(); 134 const output = render(ContextBack, { 135 props: { href, fallback: '/messages' } 136 }).body; 137 expect(output).toContain('href="/messages"'); 138 expect(output).toContain('Back to messages'); 139 }); 140 it('renders a safe fallback without acquiring randomness or creating local data', () => { 141 const random = vi.spyOn(globalThis.crypto, 'randomUUID'); 142 const output = render(ContextBack, { 143 props: { href: '/messages/unknown', fallback: '//evil' } 144 }).body; 145 expect(output).toContain('href="/search"'); 146 expect(output).toContain('Search food'); 147 expect(random).not.toHaveBeenCalled(); 148 random.mockRestore(); 149 }); 150 });