|
| 1 | +// Copyright (c) Mapbox, Inc. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 5 | +import { GeojsonPreviewUIResource } from '../../../src/resources/ui-apps/GeojsonPreviewUIResource.js'; |
| 6 | + |
| 7 | +const uri = new URL('ui://mapbox/geojson-preview/index.html'); |
| 8 | + |
| 9 | +// Build a Mapbox-style 3-part JWT whose payload carries the username (`u`). |
| 10 | +function makeToken(prefix: 'sk' | 'pk' | 'tk', username: string): string { |
| 11 | + const payload = Buffer.from(JSON.stringify({ u: username })).toString( |
| 12 | + 'base64' |
| 13 | + ); |
| 14 | + return `${prefix}.${payload}.sig`; |
| 15 | +} |
| 16 | + |
| 17 | +function embeddedToken(html: string): string | null { |
| 18 | + const m = html.match(/var TOKEN = '([^']*)'/); |
| 19 | + return m ? m[1] : null; |
| 20 | +} |
| 21 | + |
| 22 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 23 | +function extra(token?: string): any { |
| 24 | + return token ? { authInfo: { token } } : {}; |
| 25 | +} |
| 26 | + |
| 27 | +async function readHtml( |
| 28 | + resource: GeojsonPreviewUIResource, |
| 29 | + token?: string |
| 30 | +): Promise<string> { |
| 31 | + const result = await resource['readCallback'](uri, extra(token)); |
| 32 | + return result.contents[0].text as string; |
| 33 | +} |
| 34 | + |
| 35 | +// Stub global fetch to mirror real Mapbox behaviour: POST tokens/v2/{username} |
| 36 | +// mints a `tk` token for THAT account. Returns the mock for call assertions. |
| 37 | +function stubMintingFetch() { |
| 38 | + const fn = vi.fn(async (input: string | URL | Request) => { |
| 39 | + const url = String(input); |
| 40 | + const username = decodeURIComponent( |
| 41 | + url.match(/tokens\/v2\/([^?]+)/)?.[1] ?? '' |
| 42 | + ); |
| 43 | + return new Response(JSON.stringify({ token: makeToken('tk', username) }), { |
| 44 | + status: 200 |
| 45 | + }); |
| 46 | + }); |
| 47 | + vi.stubGlobal('fetch', fn); |
| 48 | + return fn; |
| 49 | +} |
| 50 | + |
| 51 | +describe('GeojsonPreviewUIResource — AGI-905 cross-account token leak', () => { |
| 52 | + afterEach(() => { |
| 53 | + vi.unstubAllGlobals(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('embeds only the caller’s own minted token, never another account’s (regression)', async () => { |
| 57 | + const fetchMock = stubMintingFetch(); |
| 58 | + const resource = new GeojsonPreviewUIResource(); |
| 59 | + |
| 60 | + const htmlA = await readHtml(resource, makeToken('sk', 'accountA')); |
| 61 | + const htmlB = await readHtml(resource, makeToken('sk', 'accountB')); |
| 62 | + |
| 63 | + const tokA = embeddedToken(htmlA); |
| 64 | + const tokB = embeddedToken(htmlB); |
| 65 | + |
| 66 | + // Each caller receives a token minted for their own account. |
| 67 | + expect(tokA).toBe(makeToken('tk', 'accountA')); |
| 68 | + expect(tokB).toBe(makeToken('tk', 'accountB')); |
| 69 | + |
| 70 | + // B must never receive A's token. |
| 71 | + expect(tokB).not.toBe(tokA); |
| 72 | + expect(htmlB).not.toContain(tokA as string); |
| 73 | + |
| 74 | + // No process-global cache: each read mints fresh. |
| 75 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 76 | + }); |
| 77 | + |
| 78 | + it('mints a fresh token on every read (no shared cache, even for the same caller)', async () => { |
| 79 | + const fetchMock = stubMintingFetch(); |
| 80 | + const resource = new GeojsonPreviewUIResource(); |
| 81 | + const sk = makeToken('sk', 'acct'); |
| 82 | + |
| 83 | + await readHtml(resource, sk); |
| 84 | + await readHtml(resource, sk); |
| 85 | + |
| 86 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 87 | + }); |
| 88 | + |
| 89 | + it('does not embed a token minted for a different account (identity assertion)', async () => { |
| 90 | + // Simulate a (hypothetical) backend returning a token for someone else. |
| 91 | + vi.stubGlobal( |
| 92 | + 'fetch', |
| 93 | + vi.fn( |
| 94 | + async () => |
| 95 | + new Response(JSON.stringify({ token: makeToken('tk', 'attacker') }), { |
| 96 | + status: 200 |
| 97 | + }) |
| 98 | + ) |
| 99 | + ); |
| 100 | + const resource = new GeojsonPreviewUIResource(); |
| 101 | + |
| 102 | + const html = await readHtml(resource, makeToken('sk', 'victim')); |
| 103 | + |
| 104 | + expect(embeddedToken(html)).toBe(''); |
| 105 | + }); |
| 106 | + |
| 107 | + it('renders without a token when minting fails (graceful degradation)', async () => { |
| 108 | + vi.stubGlobal( |
| 109 | + 'fetch', |
| 110 | + vi.fn(async () => new Response('forbidden', { status: 403 })) |
| 111 | + ); |
| 112 | + const resource = new GeojsonPreviewUIResource(); |
| 113 | + |
| 114 | + const result = await resource['readCallback']( |
| 115 | + uri, |
| 116 | + extra(makeToken('sk', 'acct')) |
| 117 | + ); |
| 118 | + |
| 119 | + expect(result.contents).toHaveLength(1); |
| 120 | + expect(embeddedToken(result.contents[0].text as string)).toBe(''); |
| 121 | + }); |
| 122 | + |
| 123 | + it('passes a pk token through unchanged without minting', async () => { |
| 124 | + const fetchMock = stubMintingFetch(); |
| 125 | + const resource = new GeojsonPreviewUIResource(); |
| 126 | + const pk = makeToken('pk', 'acct'); |
| 127 | + |
| 128 | + const html = await readHtml(resource, pk); |
| 129 | + |
| 130 | + expect(embeddedToken(html)).toBe(pk); |
| 131 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('renders without a token when no token is provided', async () => { |
| 135 | + const saved = process.env.MAPBOX_ACCESS_TOKEN; |
| 136 | + delete process.env.MAPBOX_ACCESS_TOKEN; |
| 137 | + try { |
| 138 | + const fetchMock = stubMintingFetch(); |
| 139 | + const resource = new GeojsonPreviewUIResource(); |
| 140 | + |
| 141 | + const html = await readHtml(resource); |
| 142 | + |
| 143 | + expect(embeddedToken(html)).toBe(''); |
| 144 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 145 | + } finally { |
| 146 | + if (saved !== undefined) process.env.MAPBOX_ACCESS_TOKEN = saved; |
| 147 | + } |
| 148 | + }); |
| 149 | +}); |
0 commit comments