// The public namespace's credential rules as pure functions: which URLs get the Worker's read token, how the token // source caches and refreshes, that a caller's credential always wins, and the scrubber. The end-to-end checks against // real git under workerd are in test/public-worker.test.js. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { PUBLIC_GIT_HOST, PUBLIC_GIT_ORIGIN, PUBLIC_NAMESPACE, READ_TOKEN_REFRESH_S, READ_TOKEN_TTL_S, createTokenSource, EMPTY_PUBLIC_REPOS, isPublicNamespaceRemote, isPublicRepoName, listPublicRepos, publicRemote, publicRepoOfRemote, publicUploadPackRepo, scrubSecrets, withPublicReadCredential, type ArtifactsNamespace, EXISTS_TTL_MS, EXISTS_REFRESH_AHEAD_MS, publicRepoExistsMemo, } from '../src/public.ts'; import { PUBLIC_NAMESPACE as CONFIG_NAMESPACE } from '../src/public-namespace.ts'; /** Null and a missing value take the fallback. An empty string and 0 stay. */ function fill(value: unknown, fallback: T): T { return value === null || value === void 0 ? fallback : value as T; } /** One property. A null receiver and a missing key are holes. A stored null stays null. */ function slot(receiver: unknown, key: string): unknown { if (receiver === null || receiver === void 0) return void 0; if (typeof receiver !== 'object' && typeof receiver !== 'function') return void 0; return (receiver as Record)[key]; } const O = PUBLIC_GIT_ORIGIN; const NS = PUBLIC_NAMESPACE; const TOKEN = `art_v1_${'ab12'.repeat(10)}?expires=4102444800`; test('listPublicRepos is the list /view and /r/ share; unavailable only when list() throws', async () => { const get = async () => { throw new Error('unused'); }; assert.deepEqual(await listPublicRepos(null, 0), EMPTY_PUBLIC_REPOS); assert.deepEqual(await listPublicRepos({ get }, 0), EMPTY_PUBLIC_REPOS); const down = { get, list: async () => { throw new Error('upstream'); } }; assert.deepEqual(await listPublicRepos(down, 0), { names: [], unavailable: true }); const names = ['ply', 'curl', 'ply-web', 'deepfile']; const up = { get, list: async () => ({ repos: names.map(name => ({ name, status: 'ready' as const })) }) }; const page = await listPublicRepos(up, 0); assert.deepEqual(page, { names: ['curl', 'deepfile', 'ply', 'ply-web'], unavailable: false }); assert.deepEqual(await listPublicRepos(down, 1_000), page); assert.deepEqual(await listPublicRepos({ get, list: async () => ({ repos: [{ name: 'only' }] }) }, 120_000), { names: ['only'], unavailable: false }); }); test('the namespace is one constant, shared by the binding config and the URL rules', () => { assert.equal(NS, 'public-v0'); assert.equal(CONFIG_NAMESPACE, NS); assert.equal(publicRemote('ply'), `${O}/git/${NS}/ply.git`); assert.equal(PUBLIC_GIT_ORIGIN, `https://${PUBLIC_GIT_HOST}`); }); test('the read token is attached only to upload-pack on exactly /git//', () => { const ok: [string, string, string][] = [ ['GET', `${O}/git/${NS}/ply.git/info/refs?service=git-upload-pack`, 'ply'], ['GET', `${O}/git/${NS}/ply/info/refs?service=git-upload-pack`, 'ply'], ['POST', `${O}/git/${NS}/ply.git/git-upload-pack`, 'ply'], ['post', `${O}/git/${NS}/my_repo-1.x.git/git-upload-pack`, 'my_repo-1.x'], ]; for (const [m, u, repo] of ok) assert.equal(publicUploadPackRepo(m, u), repo, `${m} ${u}`); const denied: [string, string][] = [ // receive-pack, in every form ['GET', `${O}/git/${NS}/ply.git/info/refs?service=git-receive-pack`], ['POST', `${O}/git/${NS}/ply.git/git-receive-pack`], ['GET', `${O}/git/${NS}/ply.git/info/refs?service=git-upload-pack&service=git-receive-pack`], // wrong verb, query, or endpoint ['POST', `${O}/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/ply.git/git-upload-pack`], ['POST', `${O}/git/${NS}/ply.git/git-upload-pack?x=1`], ['GET', `${O}/git/${NS}/ply.git/info/refs`], ['GET', `${O}/git/${NS}/ply.git/info/refs?service=git-upload-pack#f`], ['GET', `${O}/git/${NS}/ply.git/HEAD`], ['GET', `${O}/git/${NS}/ply.git/objects/info/packs`], // another namespace, a prefix or suffix of the namespace, a nested path, the namespace itself ['GET', `${O}/git/default/ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}x/ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/x${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS.toUpperCase()}/ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/a/ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/info/refs?service=git-upload-pack`], ['GET', `${O}/${NS}/ply.git/info/refs?service=git-upload-pack`], // traversal and encodings ['GET', `${O}/git/${NS}/../default/ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/./ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/%2e%2e/default/ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/%2E%2E/default/ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}%2fply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/ply.git%2finfo%2frefs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/ply%252egit/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/ply.git\\..\\x/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}//ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/..ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/.ply.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/ply.git.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/p ly.git/info/refs?service=git-upload-pack`], ['GET', `${O}/git/${NS}/ply\u0000.git/info/refs?service=git-upload-pack`], // another host, scheme, port, userinfo, case, trailing dot, lookalikes ['GET', `https://github.com/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `http://${PUBLIC_GIT_HOST}/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `https://${PUBLIC_GIT_HOST}:443/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `https://${PUBLIC_GIT_HOST}:8443/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `https://${PUBLIC_GIT_HOST}./git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `https://${PUBLIC_GIT_HOST.toUpperCase()}/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `https://${PUBLIC_GIT_HOST}.evil.example/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `https://evil.example/${PUBLIC_GIT_HOST}/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `https://x@${PUBLIC_GIT_HOST}/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `https://other.artifacts.cloudflare.net/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', `/git/${NS}/ply.git/info/refs?service=git-upload-pack`], ['GET', ''], ]; for (const [m, u] of denied) assert.equal(publicUploadPackRepo(m, u), null, `${m} ${u}`); }); test('repo names and remotes: the viewer and /v1 accept one repo directly under the namespace', () => { for (const n of ['ply', 'a', 'A9', 'x.y_z-1']) assert.equal(isPublicRepoName(n), true, n); for (const n of ['', '.x', '-x', '_x', 'a..b', 'a/b', 'a.git', 'a%2f', 'a b', 'x'.repeat(101)]) assert.equal(isPublicRepoName(n), false, n); assert.equal(publicRepoOfRemote(`${O}/git/${NS}/ply.git`), 'ply'); assert.equal(publicRepoOfRemote(`${O}/git/${NS}/ply`), 'ply'); for (const u of [`${O}/git/${NS}/a/ply.git`, `${O}/git/other/ply.git`, `${O}/git/${NS}/ply.git/info/refs`, `${O}/git/${NS}/..%2fx.git`, `https://github.com/git/${NS}/ply.git`]) { assert.equal(publicRepoOfRemote(u), null, u); } // write refusal is broad on purpose: any case, any depth under the namespace for (const u of [`${O}/git/${NS}/ply.git`, `${O}/git/${NS.toUpperCase()}/ply.git`, `${O}/git/${NS}/a/b.git`, `https://${PUBLIC_GIT_HOST.toUpperCase()}/git/${NS}/ply`]) { assert.equal(isPublicNamespaceRemote(u), true, u); } for (const u of [`${O}/git/default/ply.git`, `${O}/git/${NS}x/ply.git`, `https://github.com/git/${NS}/ply.git`, 'not a url']) { assert.equal(isPublicNamespaceRemote(u), false, u); } }); type Mint = { repo: string; scope: string; ttl: number }; function fakeBinding(opts: { now: () => number } & Partial<{ known: string[]; ttlS: number; fail: Error }> = { now: Date.now }) { const mints: Mint[] = []; let disposed = 0; let n = 0; const binding: ArtifactsNamespace = { async get(name) { if (opts.fail) throw opts.fail; if (!fill(opts.known, ['ply']).includes(name)) throw new Error(`repo not found: ${name}`); return { async createToken(scope, ttl) { mints.push({ repo: name, scope, ttl }); n += 1; const exp = Math.floor(opts.now() / 1000) + fill(opts.ttlS, ttl); return { plaintext: `art_v1_${String(n).padStart(40, '0')}?expires=${exp}`, expiresAt: new Date(exp * 1000).toISOString() }; }, [Symbol.dispose]() { disposed += 1; }, }; }, }; return { binding, mints, disposed: () => disposed }; } test('token source: read scope only, one mint per repo while fresh, shared by concurrent callers, refreshed near expiry', async () => { let clock = 1_800_000_000_000; const now = () => clock; const fake = fakeBinding({ known: ['ply', 'b'], now }); const src = createTokenSource(fake.binding, now); const [a, b, c] = await Promise.all([src.readToken('ply'), src.readToken('ply'), src.readToken('ply')]); assert.equal(a, b); assert.equal(b, c); assert.equal(fake.mints.length, 1, 'concurrent callers share one mint'); assert.deepEqual(fake.mints[0], { repo: 'ply', scope: 'read', ttl: READ_TOKEN_TTL_S }); assert.equal(fake.disposed(), 1, 'the repo handle is released'); clock += (READ_TOKEN_TTL_S - READ_TOKEN_REFRESH_S - 1) * 1000; assert.equal(await src.readToken('ply'), a, 'still fresh: cached'); assert.equal(fake.mints.length, 1); clock += 2000; const fresh = await src.readToken('ply'); assert.notEqual(fresh, a, 'inside the refresh window: re-minted'); assert.equal(fake.mints.length, 2); await src.readToken('b'); assert.equal(fake.mints.length, 3); assert.ok(fake.mints.every(m => m.scope === 'read')); // the retired token is still scrubbed const secrets = src.secrets(); for (const t of [a, fresh]) { assert.ok(secrets.includes(t)); assert.ok(secrets.includes(t.split('?')[0] as string)); } await assert.rejects(src.readToken('../x'), { code: 'bad_request' }); await assert.rejects(src.readToken('nope'), { code: 'not_found' }); await assert.rejects(createTokenSource(fakeBinding({ now, fail: new Error('boom') }).binding, now).readToken('ply'), { code: 'upstream_5xx' }); }); // The Workers binding's get() returns an RpcPromise: a method called on it is pipelined into the same round trip. function pipelinedBinding(opts: { known: string[] }) { const log: string[] = []; let disposed = 0; const binding = { get(name: string) { log.push(`get ${name}`); const known = opts.known.includes(name); const handle = { async createToken() { throw new Error('the handle was awaited: not pipelined'); }, [Symbol.dispose]() { disposed += 1; } }; // get answers only after a tick, so a pipelined createToken must not wait for it const p = new Promise((resolve, reject) => { setTimeout(() => (known ? resolve(handle) : reject(new Error(`repo not found: ${name}`))), 5); }); return Object.assign(p, { async createToken(scope: 'read' | 'write', ttl: number) { log.push(`createToken ${scope} ${ttl} (pipelined)`); if (!known) throw new Error(`repo not found: ${name}`); return { plaintext: `art_v1_${'1'.repeat(40)}?expires=${Math.floor(Date.now() / 1000) + ttl}` }; }, }); }, } as unknown as ArtifactsNamespace; return { binding, log, disposed: () => disposed }; } test('token source: on a pipelining binding, createToken rides get\'s round trip; the handle is still released', async () => { const fake = pipelinedBinding({ known: ['ply'] }); const src = createTokenSource(fake.binding); const t = await src.readToken('ply'); assert.match(t, /^art_v1_1{40}\?expires=\d+$/); assert.deepEqual(fake.log, ['get ply', `createToken read ${READ_TOKEN_TTL_S} (pipelined)`]); assert.equal(fake.disposed(), 1, 'the repo handle is released'); await assert.rejects(src.readToken('nope'), { code: 'not_found' }); }); test('withPublicReadCredential: adds Bearer only on a public upload-pack URL with no Authorization; a caller credential wins', async () => { const fake = fakeBinding({ now: Date.now }); const src = createTokenSource(fake.binding); const seen: { url: string; method: string; auth: string | null }[] = []; const inner: typeof globalThis.fetch = async (input, init) => { seen.push({ url: String(input), method: fill(slot(init, 'method'), 'GET'), auth: new Headers(slot(init, 'headers') as HeadersInit).get('authorization') }); return new Response('ok'); }; const f = withPublicReadCredential(inner, src); await f(`${O}/git/${NS}/ply.git/info/refs?service=git-upload-pack`, { headers: { 'git-protocol': 'version=2' } }); await f(`${O}/git/${NS}/ply.git/git-upload-pack`, { method: 'POST', body: new Uint8Array([1]) }); await f(`${O}/git/${NS}/ply.git/git-receive-pack`, { method: 'POST', body: new Uint8Array([1]) }); await f(`${O}/git/default/ply.git/info/refs?service=git-upload-pack`); await f(`https://github.com/git/${NS}/ply.git/info/refs?service=git-upload-pack`); await f(`${O}/git/${NS}/ply.git/info/refs?service=git-upload-pack`, { headers: { Authorization: 'Bearer caller' } }); const token = await src.readToken('ply'); assert.deepEqual(seen.map(s => s.auth), [`Bearer ${token}`, `Bearer ${token}`, null, null, null, 'Bearer caller']); assert.equal(new Headers({ 'git-protocol': 'version=2' }).get('git-protocol'), 'version=2'); // no token source (no binding): pass-through const plain = withPublicReadCredential(inner, null); await plain(`${O}/git/${NS}/ply.git/info/refs?service=git-upload-pack`); assert.equal(slot(seen.at(-1), 'auth'), null); }); test('scrubSecrets removes every form, longest first', () => { const scrub = scrubSecrets([TOKEN, TOKEN.split('?')[0] as string, btoa(`x-access-token:${TOKEN}`), 'short']); const text = `a ${TOKEN} b ${TOKEN.split('?')[0]} c ${btoa(`x-access-token:${TOKEN}`)} short`; const out = scrub(text); assert.equal(out.includes('art_v1_'), false); assert.equal(out.includes(btoa(`x-access-token:${TOKEN}`)), false); assert.match(out, /short$/, 'strings under 8 chars are not treated as secrets'); }); test('publicRepoExistsMemo remembers a yes for a minute and never remembers a no', async () => { const known = new Set(['ply']); let calls = 0; const binding = { async get(name: string) { calls++; if (!known.has(name)) throw new Error(`repo not found: ${name}`); return {}; } }; assert.equal(await publicRepoExistsMemo(binding, 'ply', 1_000), true); assert.equal(await publicRepoExistsMemo(binding, 'ply', 1_000 + EXISTS_TTL_MS - 1), true); assert.equal(calls, 1, 'the second ask within the minute is the memo'); known.delete('ply'); assert.equal(await publicRepoExistsMemo(binding, 'ply', 1_000 + EXISTS_TTL_MS), false, 'past the minute the binding is asked again'); assert.equal(calls, 2); assert.equal(await publicRepoExistsMemo(binding, 'ply', 1_000 + EXISTS_TTL_MS + 1), false); assert.equal(calls, 3, 'a no is not remembered'); known.add('ply'); assert.equal(await publicRepoExistsMemo(binding, 'ply', 1_000 + EXISTS_TTL_MS + 2), true, 'a repo that appears is seen at once'); const other = { async get() { calls++; return {}; } }; assert.equal(await publicRepoExistsMemo(other, 'ply', 1_000 + EXISTS_TTL_MS + 2), true); assert.equal(calls, 5, 'the memo is per binding object'); assert.equal(await publicRepoExistsMemo(null, 'ply'), 'unknown'); assert.equal(await publicRepoExistsMemo(binding, '../x'), false); }); test('publicRepoExistsMemo refreshes ahead in the background: never blocks on the binding while the memo is live, never trusts a yes past its minute', async () => { let present = true; let calls = 0; const binding = { async get(name: string) { calls += 1; if (!present) throw new Error(`repo not found: ${name}`); return {}; } }; const deferred: Promise[] = []; const defer = (p: Promise) => { deferred.push(p); }; assert.equal(await publicRepoExistsMemo(binding, 'ply', 1_000, defer), true); assert.equal(calls, 1, 'a cold memo asks the binding on the request path'); assert.equal(await publicRepoExistsMemo(binding, 'ply', 1_000 + EXISTS_TTL_MS - EXISTS_REFRESH_AHEAD_MS - 1, defer), true); assert.equal(deferred.length, 0, 'before the refresh window: no refresh'); const late = 1_000 + EXISTS_TTL_MS - 5; assert.equal(await publicRepoExistsMemo(binding, 'ply', late, defer), true, 'inside the window: answered from the memo'); assert.equal(await publicRepoExistsMemo(binding, 'ply', late, defer), true); assert.equal(deferred.length, 1, 'one refresh in flight at a time'); await Promise.all(deferred); assert.equal(calls, 2); assert.equal(await publicRepoExistsMemo(binding, 'ply', 1_000 + EXISTS_TTL_MS + 10, null), true, 'the refresh extended the memo from when it started'); assert.equal(calls, 2); // a refresh that finds the repo gone drops the memo at once: the next request asks again and gets the 404 present = false; const late2 = late + EXISTS_TTL_MS - 5; assert.equal(await publicRepoExistsMemo(binding, 'ply', late2, defer), true); await Promise.all(deferred); assert.equal(await publicRepoExistsMemo(binding, 'ply', late2 + 1, defer), false); // without a waitUntil the memo just expires, as before present = true; const other = { async get() { calls += 1; return {}; } }; await publicRepoExistsMemo(other, 'ply', 0, null); const before = calls; assert.equal(await publicRepoExistsMemo(other, 'ply', EXISTS_TTL_MS - 1, null), true); assert.equal(calls, before); });