import assert from 'node:assert/strict'; import { test } from 'node:test'; import { isPublicHttpsUrl } from '../src/net.ts'; import { didDocumentUrl, resolveDidPds } from '../src/identity.ts'; import { upstream } from '../src/upstream.ts'; const ENDPOINTS: [string, boolean][] = [ ['https://puffball.us-east.host.bsky.network', true], ['https://pds.example.com:8443/', true], ['https://172.32.0.1', true], ['http://pds.example.com', false], ['ftp://pds.example.com', false], ['not a url', false], ['https://user:pass@pds.example.com', false], ['https://localhost', false], ['https://LOCALHOST.', false], ['https://api.localhost', false], ['https://metadata.google.internal', false], ['https://printer.local', false], ['https://127.0.0.1', false], ['https://127.255.0.9', false], ['https://2130706433', false], ['https://0x7f.1', false], ['https://0.0.0.0', false], ['https://10.1.2.3', false], ['https://172.16.0.1', false], ['https://172.31.255.255', false], ['https://192.168.1.1', false], ['https://169.254.169.254', false], ['https://[::1]', false], ['https://[fd00::1]', false], ['https://[fe80::1]', false], ['https://[::ffff:127.0.0.1]', false], ['https://[2606:4700::1111]', true], ]; for (const [endpoint, expected] of ENDPOINTS) { test(`isPublicHttpsUrl(${JSON.stringify(endpoint)}) is ${expected}`, () => { assert.equal(isPublicHttpsUrl(endpoint), expected); }); } const DID_DOCUMENTS: [string, string | undefined][] = [ ['did:plc:z72i7hdynmk6r22z27h6tvur', 'https://plc.directory/did:plc:z72i7hdynmk6r22z27h6tvur'], ['did:plc:../../etc', undefined], ['did:web:example.com', 'https://example.com/.well-known/did.json'], ['did:web:example.com:user:alice', 'https://example.com/user/alice/did.json'], ['did:web:example.com%3A8443', 'https://example.com:8443/.well-known/did.json'], ['did:web:localhost%3A8080', undefined], ['did:web:10.0.0.1', undefined], ['did:key:z6Mk', undefined], ]; for (const [did, expected] of DID_DOCUMENTS) { test(`didDocumentUrl(${did}) is ${expected}`, () => { assert.equal(didDocumentUrl(did), expected); }); } test('resolveDidPds refuses a DID document that names a private PDS', async () => { const fakeFetch = (async () => Response.json({ service: [{ id: '#atproto_pds', type: 'AtprotoPersonalDataServer', serviceEndpoint: 'http://169.254.169.254' }] })) as typeof fetch; await assert.rejects(resolveDidPds('did:web:example.com', upstream(fakeFetch)), /no usable PDS/); }); test('resolveDidPds returns a public https PDS', async () => { const fakeFetch = (async () => Response.json({ service: [{ id: '#atproto_pds', type: 'AtprotoPersonalDataServer', serviceEndpoint: 'https://pds.example.com' }] })) as typeof fetch; assert.equal(await resolveDidPds('did:web:example.com', upstream(fakeFetch)), 'https://pds.example.com'); });