main
ply4 git calls35 ms50 KiB3 cachedprotocol v2
472 lines30 KiBRaw
1// The public namespace end to end: the cf-built Worker under workerd, its outbound fetches routed to a local router that
2// plays the account's Artifacts host (the real PUBLIC_GIT_HOST constant, no override hook) in front of git http-backend,
3// and an RPC service standing in for the Artifacts binding. The router records every request that leaves the Worker,
4// so each test can say exactly which requests carried the Worker's read token and which did not.
5import { test, before, after } from 'node:test';
6import assert from 'node:assert/strict';
7import http from 'node:http';
8import { spawn } from 'node:child_process';
9import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
10import { join } from 'node:path';
11import { fileURLToPath } from 'node:url';
12import { copyCfBundle } from './cf-bundle.js';
13import { git, gitBuf, tmpRoot, makeBareRepo, startGitServer, commitFiles } from './server.js';
15const WORKERD = [process.env.WORKERD, fileURLToPath(new URL('../node_modules/.bin/workerd', import.meta.url))].filter(Boolean).find(p => existsSync(p));
16const skip = !WORKERD && 'workerd not installed';
17const HOST = '5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net';
18const NS = 'public-v0';
19const ORIGIN = `https://${HOST}`;
20const PORT = 19200 + (process.pid % 40), PORT_TIGHT = PORT + 40, PORT_FAKE = PORT + 80;
21const PLY = 'test-ply-token-' + 'p'.repeat(20);
22// What the fake binding mints, and what the router demands for /git/public-v0/*. Never sent by any test client.
23const TOKEN = `art_v1_${'5eed'.repeat(10)}?expires=4102444800`;
24const TOKEN_FORMS = [TOKEN, TOKEN.split('?')[0], Buffer.from(`x-access-token:${TOKEN}`).toString('base64'), encodeURIComponent(TOKEN), '5eed'.repeat(10)];
25// A caller's own credential for another namespace.
26const OTHER = `art_v1_${'0the'.repeat(10)}`;
27const KNOWN = ['ply', 'moved', 'away', 'hop', 'echo', 'wide'];
29let root, cleanup, srv, router, proc, logs = '';
30let plyRepo, otherRepo;
31const hits = [];
32const seen = []; // every response status line, header and body a test client received
34function startRouter(upstream) {
35 const up = new URL(upstream);
36 const s = http.createServer((req, res) => {
37 const host = String(req.headers.host ?? '');
38 const auth = req.headers.authorization ?? null;
39 hits.push({ host, method: req.method, path: req.url, auth, via: req.headers['x-ply-via'] ?? null });
40 if (host !== HOST) { req.resume(); res.writeHead(404); return void res.end('no such host'); }
41 const m = /^\/git\/public-v0\/(moved|away|hop|echo)\.git(\/.*)$/.exec(req.url);
42 if (m) {
43 req.resume();
44 if (m[1] === 'echo') { res.writeHead(500, { 'content-type': 'text/plain' }); return void res.end(`echo ${auth} ${Buffer.from(String(auth).split(' ')[1] ?? '', 'base64').toString()}`); }
45 const location = m[1] === 'moved' ? `/git/other/secret.git${m[2]}` : m[1] === 'away' ? `https://github.com/x/y.git${m[2]}` : `/git/${NS}/ply.git${m[2]}`;
46 res.writeHead(307, { location }); return void res.end();
47 }
48 const need = req.url.startsWith(`/git/${NS}/`) ? `Bearer ${TOKEN}` : req.url.startsWith('/git/other/') ? `Bearer ${OTHER}` : null;
49 if (!need) { req.resume(); res.writeHead(404); return void res.end('not found'); }
50 if (!auth) { req.resume(); res.writeHead(401, { 'www-authenticate': 'Basic realm="git"' }); return void res.end('Authentication required'); }
51 if (auth !== need) { req.resume(); res.writeHead(403); return void res.end('Forbidden'); }
52 const chunks = [];
53 req.on('data', c => chunks.push(c));
54 req.on('end', () => {
55 const body = Buffer.concat(chunks);
56 const filter = /filter ([^\n]+)\n/.exec(body.toString('latin1'))?.[1] ?? null;
57 hits.at(-1).filter = filter;
58 // Artifacts' answer to every partial-clone filter but blob:none (lab_notebook/ply-artifacts-eval-2026-10-23.md).
59 if (filter && filter !== 'blob:none') { res.writeHead(400, { 'content-type': 'text/plain' }); return void res.end('git-upload-pack: Unsupported Git fetch filter; only blob:none is supported.'); }
60 const u = http.request({ host: up.hostname, port: up.port, path: req.url, method: req.method, headers: { ...req.headers, host: up.host } }, r => { res.writeHead(r.statusCode, r.headers); r.pipe(res); });
61 u.end(body);
62 });
63 });
64 return new Promise(ok => s.listen(0, '127.0.0.1', () => ok({ port: s.address().port, close: () => new Promise(r => { s.closeAllConnections?.(); s.close(r); }) })));
67const FAKE_ARTIFACTS = `import { WorkerEntrypoint, RpcTarget } from 'cloudflare:workers';
68const mints = [];
69const KNOWN = ${JSON.stringify(KNOWN)};
70class Repo extends RpcTarget {
71 constructor(name) { super(); this.name = name; }
72 async createToken(scope, ttl) { mints.push({ repo: this.name, scope, ttl }); return { plaintext: ${JSON.stringify(TOKEN)}, expiresAt: '2100-01-01T00:00:00.000Z' }; }
74export class Ns extends WorkerEntrypoint {
75 async get(name) { if (!KNOWN.includes(name)) throw new Error('repo not found: ' + name); return new Repo(name); }
76 async list() { return { repos: KNOWN.map(name => ({ name, status: 'ready' })) }; }
78export default { async fetch() { return Response.json(mints); } };
79`;
81before(async () => {
82 if (!WORKERD) return;
83 ({ root, cleanup } = tmpRoot());
84 srv = await startGitServer(root);
85 router = await startRouter(srv.url);
86 mkdirSync(join(root, 'git', NS), { recursive: true });
87 mkdirSync(join(root, 'git', 'other'), { recursive: true });
88 const seed = (name, files) => {
89 const bare = makeBareRepo(root, name);
90 const work = join(root, `w-${name.replace(/\//g, '-')}`); mkdirSync(work); git(work, ['init', '-q', '-b', 'main']);
91 return { bare, work, head: commitFiles(work, bare, files, `seed ${name}`) };
92 };
93 plyRepo = seed(`git/${NS}/ply`, { 'README.md': '# ply\n\npublic mirror\n', 'src/a.js': 'export const a = 1;\n' });
94 otherRepo = seed('git/other/secret', { 'SECRET.md': 'other namespace\n' });
95 // curl's shape: one directory over the viewer's old 2,000-entry cap, next to a small one three levels down.
96 const wideFiles = { 'README.md': 'wide\n', 'docs/lib/opts/A.md': 'a\n', 'docs/lib/opts/B.md': 'b\n' };
97 for (let k = 0; k < 2135; k++) wideFiles[`tests/data/test${k}`] = `t${k}\n`;
98 const wide = seed(`git/${NS}/wide`, wideFiles);
99 // More tags than the viewer's ls-refs cap (maxRefs 2,000): the tag picker must not cost the page.
100 git(wide.bare, ['update-ref', '--stdin'], { input: Array.from({ length: 2100 }, (_, k) => `create refs/tags/t${k} ${wide.head}\n`).join('') });
101 const out = join(root, 'wk'); mkdirSync(out, { recursive: true });
102 copyCfBundle(join(out, 'worker.mjs'));
103 writeFileSync(join(out, 'fake-artifacts.mjs'), FAKE_ARTIFACTS);
104 const worker = (key, rate) => `(modules = [ (name = "worker.mjs", esModule = embed "worker.mjs") ], compatibilityDate = "2026-10-18", globalOutbound = "router",
105 durableObjectNamespaces = [ (className = "ViewerRateLimit", uniqueKey = "${key}", enableSql = true) ], durableObjectStorage = (inMemory = void),
106 bindings = [ (name = "PLY_TOKEN", text = "${PLY}"), (name = "WRITES", text = "on"), (name = "PACK_CACHE", text = "on"), (name = "VIEWER_RATE_PER_MIN", text = "${rate}"),
107 (name = "VIEWER_RATE", durableObjectNamespace = "ViewerRateLimit"), (name = "PUBLIC_REPOS", service = (name = "artifacts", entrypoint = "Ns")) ])`;
108 writeFileSync(join(out, 'config.capnp'), `using Workerd = import "/workerd/workerd.capnp";
109const config :Workerd.Config = (
110 services = [ (name = "main", worker = ${worker('public-main', 1000)}), (name = "tight", worker = ${worker('public-tight', 3)}),
111 (name = "artifacts", worker = (modules = [ (name = "fake-artifacts.mjs", esModule = embed "fake-artifacts.mjs") ], compatibilityDate = "2026-10-18")),
112 (name = "router", external = (address = "127.0.0.1:${router.port}", http = ())) ],
113 sockets = [ (name = "http", address = "127.0.0.1:${PORT}", http = (), service = "main"), (name = "tight", address = "127.0.0.1:${PORT_TIGHT}", http = (), service = "tight"),
114 (name = "fake", address = "127.0.0.1:${PORT_FAKE}", http = (), service = "artifacts") ],
115);
116`);
117 proc = spawn(WORKERD, ['serve', join(out, 'config.capnp'), '--verbose'], { stdio: ['ignore', 'pipe', 'pipe'] });
118 proc.stdout.on('data', d => { logs += d; }); proc.stderr.on('data', d => { logs += d; });
119 for (let i = 0; i < 80; i++) { try { if ((await fetch(`http://127.0.0.1:${PORT}/`)).ok) break; } catch { } await new Promise(r => setTimeout(r, 100)); }
120});
121// Each step on its own: a before() that failed half way must still release every server, or node --test never exits.
122after(async () => {
123 if (!WORKERD) return;
124 try { proc?.kill('SIGKILL'); } catch { /* not started */ }
125 await Promise.allSettled([srv?.close(), router?.close()]);
126 try { cleanup?.(); } catch { /* nothing to remove */ }
127});
129async function W(path, { method = 'GET', body, headers = {}, port = PORT, ip = '198.51.100.1' } = {}) {
130 const res = await fetch(`http://127.0.0.1:${port}${path}`, { method, redirect: 'manual', headers: { 'cf-connecting-ip': ip, ...(body ? { 'content-type': 'application/json' } : {}), ...headers }, body: body ? JSON.stringify(body) : undefined });
131 const buf = Buffer.from(await res.arrayBuffer());
132 seen.push(`${res.status} ${path}\n${[...res.headers].map(([k, v]) => `${k}: ${v}`).join('\n')}\n${buf.toString('latin1')}`);
133 return { status: res.status, headers: res.headers, buf, text: buf.toString('utf8'), json: () => JSON.parse(buf.toString('utf8')) };
135const bearer = { authorization: `Bearer ${PLY}` };
136const remote = name => `${ORIGIN}/git/${NS}/${name}.git`;
137const q = o => new URLSearchParams(o).toString();
138const mark = () => hits.length;
139const since = i => hits.slice(i);
140const publicView = (name, query = '') => `/r/${name}${query}`;
141const longView = (name, query = '') => `/view/${HOST}/git/${NS}/${name}.git${query}`;
143test('viewer: public-v0/ply renders (tree, file, raw) with no token; every upstream call is upload-pack and carries the read token', { skip }, async () => {
144 const i = mark();
145 const tree = await W(publicView('ply'));
146 assert.equal(tree.status, 200, tree.text.slice(0, 600));
147 assert.match(tree.text, /README\.md/);
148 assert.match(tree.text, /public mirror/);
149 assert.match(tree.text, /upload-pack/, 'the page shows its git round trips');
150 const file = await W(publicView('ply', '?ref=main&path=README.md'));
151 assert.equal(file.status, 200);
152 assert.match(file.text, /public mirror/);
153 const raw = await W(publicView('ply', '?path=README.md&raw=1'));
154 assert.equal(raw.status, 200);
155 assert.deepEqual(raw.buf, gitBuf(plyRepo.bare, ['show', 'main:README.md']));
156 const calls = since(i);
157 assert.ok(calls.length >= 3);
158 for (const h of calls) {
159 assert.equal(h.host, HOST);
160 assert.ok(h.path.startsWith(`/git/${NS}/ply.git/`), h.path);
161 assert.ok((h.method === 'GET' && h.path.endsWith('/info/refs?service=git-upload-pack')) || (h.method === 'POST' && h.path.endsWith('/git-upload-pack')), `${h.method} ${h.path}`);
162 assert.equal(h.auth, `Bearer ${TOKEN}`);
163 }
164});
166test('more tags than the ls-refs cap: the page still opens (no tag picker), and a tag by name still resolves', { skip }, async () => {
167 const page = await W(publicView('wide'));
168 assert.equal(page.status, 200, page.text.slice(0, 400));
169 assert.doesNotMatch(page.text, /optgroup label="Tags"/);
170 const tag = await W(publicView('wide', '?ref=t2099&path=README.md'));
171 assert.equal(tag.status, 200, tag.text.slice(0, 400));
172 assert.match(tag.text, /wide/);
173});
175test('a server that refuses tree:0: the viewer learns it once per isolate, a 2,135-entry sibling tree is no 413, and its own nested links work', { skip }, async () => {
176 const i = mark();
177 const root = await W(publicView('wide'));
178 assert.equal(root.status, 200, root.text.slice(0, 400));
179 // the viewer's own link to a directory three levels down: literal slashes, and it renders
180 const dirLink = /href="([^"]*path=docs\/lib\/opts[^"]*)"/.exec((await W(publicView('wide', '?path=docs/lib'))).text)?.[1]?.replace(/&amp;/g, '&');
181 assert.ok(dirLink, 'nested link present');
182 const dir = await W(dirLink);
183 assert.equal(dir.status, 200);
184 assert.match(dir.text, /A\.md/);
185 assert.equal((await W(publicView('wide', '?path=docs%2Flib%2Fopts'))).status, 200, 'an encoded slash in path is accepted');
186 // the cap still applies to the tree that is shown: the 2,135-entry directory itself is over the viewer's 2,000
187 assert.equal((await W(publicView('wide', '?path=tests/data'))).status, 413);
188 assert.equal((await W(publicView('wide', '?path=docs%2F%2E%2E%2Fsecret'))).status, 400, 'an encoded dot is not');
189 const refused = since(i).filter(h => h.filter === 'tree:0');
190 assert.ok(refused.length <= 1, `tree:0 was refused ${refused.length} times; the isolate should learn it once`);
191 assert.match(dir.text, /learned earlier in this isolate|was rejected/);
192});
194test('Workers Cache routing: anonymous public reads use GitCache or CAS; a credentialed read never does', { skip }, async () => {
195 let i = mark();
196 assert.equal((await W(`/v1/read?${q({ remote: remote('ply'), path: 'README.md' })}`)).status, 200);
197 assert.equal((await W(publicView('ply', '?path=README.md'))).status, 200);
198 const anon = since(i).filter(h => h.path.startsWith(`/git/${NS}/ply.git/`));
199 assert.ok(anon.length > 0);
200 // Viewer pages still loop through GitCache. /v1/read|/tree|/commit use the CAS entrypoint graph (no x-ply-via).
201 for (const h of anon) assert.ok(h.via === 'gitcache' || h.via === null, `${h.method} ${h.path} via=${h.via}`);
202 // the caller's credential (for public-v0 itself, and for another namespace): straight upstream, never through GitCache
203 i = mark();
204 await W(`/v1/read?${q({ remote: remote('ply'), path: 'README.md' })}`, { headers: { ...bearer, 'git-token': OTHER } });
205 await W(`/v1/read?${q({ remote: `${ORIGIN}/git/other/secret.git`, path: 'SECRET.md' })}`, { headers: { ...bearer, 'git-token': OTHER } });
206 const cred = since(i);
207 assert.ok(cred.length > 0);
208 for (const h of cred) { assert.equal(h.via, null, `${h.method} ${h.path}`); assert.equal(h.auth, `Bearer ${OTHER}`); }
209 // /v1/refs is not on the CAS graph: still GitCache + Worker public token when no caller git credential
210 i = mark();
211 assert.equal((await W(`/v1/refs?${q({ remote: remote('ply') })}`, { headers: bearer })).status, 200);
212 for (const h of since(i)) { assert.equal(h.via, 'gitcache'); assert.equal(h.auth, `Bearer ${TOKEN}`); }
213});
215test('pinned to a commit: viewer raw files go through Pinned (cached whole); /v1 never does and is always no-store', { skip }, async () => {
216 const head = plyRepo.head;
217 // /v1 is a credentialed API: a public-v0 read at a full oid, with no credential, is still answered live and no-store
218 for (const path of [`/v1/read?${q({ remote: remote('ply'), ref: head, path: 'README.md' })}`, `/v1/tree?${q({ path: 'src', ref: head, remote: remote('ply') })}`, `/v1/commit?${q({ ref: head, remote: remote('ply') })}`]) {
219 const r = await W(path, { ip: '198.51.100.40' });
220 assert.equal(r.status, 200, r.text);
221 assert.equal(r.headers.get('x-ply-pinned-cache'), null, `${path} is not served by Pinned`);
222 assert.equal(r.headers.get('cache-control'), 'no-store', path);
223 assert.equal(r.headers.get('vary'), null, path);
224 }
225 const read = await W(`/v1/read?${q({ remote: remote('ply'), ref: head, path: 'README.md' })}`, { ip: '198.51.100.40' });
226 assert.deepEqual(read.buf, gitBuf(plyRepo.bare, ['show', `${head}:README.md`]));
227 assert.equal(read.headers.get('x-ply-commit'), head);
228 const raw = await W(publicView('ply', `?raw=1&ref=${head}&path=README.md`), { ip: '198.51.100.40' });
229 assert.equal(raw.status, 200);
230 assert.ok(raw.headers.get('x-ply-pinned-cache'));
231 assert.deepEqual(raw.buf, gitBuf(plyRepo.bare, ['show', `${head}:README.md`]));
232 for (const [path, headers] of [
233 [`/v1/read?${q({ remote: remote('ply'), ref: 'main', path: 'README.md' })}`, {}],
234 [`/v1/read?${q({ remote: remote('ply'), ref: head, path: 'README.md', trace: '1' })}`, {}],
235 [`/v1/read?${q({ remote: remote('ply'), ref: head, path: 'README.md' })}`, bearer],
236 [publicView('ply', `?ref=${head}&path=README.md`), {}],
237 ]) {
238 const r = await W(path, { headers, ip: '198.51.100.41' });
239 assert.equal(r.status, 200, path);
240 assert.equal(r.headers.get('x-ply-pinned-cache'), null, path);
241 }
242 // the front door rate-limits public reads, pinned or not (tight service: 3 per minute)
243 const ip = '203.0.113.90';
244 for (let i = 0; i < 3; i++) assert.equal((await W(`/v1/read?${q({ remote: remote('ply'), ref: head, path: 'README.md' })}`, { port: PORT_TIGHT, ip })).status, 200);
245 assert.equal((await W(`/v1/read?${q({ remote: remote('ply'), ref: head, path: 'README.md' })}`, { port: PORT_TIGHT, ip })).status, 429);
246 assert.equal((await W(publicView('ply', `?raw=1&ref=${head}&path=README.md`), { port: PORT_TIGHT, ip })).status, 429);
247});
249test('/r is the public namespace index; /r/<repo> is canonical; a missing repo is a styled 404 with no fetch', { skip }, async () => {
250 const i = mark();
251 const index = await W('/r/');
252 assert.equal(index.status, 200, index.text.slice(0, 400));
253 assert.match(index.text, /public-v0/);
254 assert.match(index.text, /href="\/r\/ply"/);
255 assert.equal(since(i).length, 0, 'the index fetches no git');
256 const missing = await W('/r/no-such-repo');
257 assert.equal(missing.status, 404);
258 assert.match(missing.text, /<h1 class="display">No repository no-such-repo in public-v0<\/h1>/);
259 assert.match(missing.text, /<a href="\/r">Repository index<\/a>/);
260 assert.equal(missing.text.includes('Repository root'), false);
261 assert.match(missing.text, /<html/);
262 const page = await W('/r/ply');
263 assert.equal(page.status, 200, page.text.slice(0, 400));
264 assert.match(page.text, /README\.md/);
265 const long = await W(longView('ply'));
266 assert.equal(long.status, 302);
267 assert.equal(long.headers.get('location'), '/r/ply');
268 const alias = await W('/r/ply?at=main&path=README.md');
269 assert.equal(alias.status, 302);
270 assert.equal(alias.headers.get('location'), '/r/ply?ref=main&path=README.md');
271 for (const bad of ['/r/a/b', '/r/.git', '/r/ply.git', '/r/-x']) assert.equal((await W(bad)).status, 404, bad);
272 for (const rejected of ['/r/a%2fb', '/r/a%00b']) assert.equal((await W(rejected)).status, 400, rejected);
273 // `..` and `%2e%2e` are dot segments to the URL parser: the request becomes `/` (usage text), never a redirect
274 for (const dots of ['/r/%2e%2e', '/r/%2E%2E/ply']) { const r = await W(dots); assert.equal(r.headers.get('location'), null, dots); assert.notEqual(r.status, 302, dots); }
275 assert.equal((await W('/r/ply', { method: 'POST' })).status, 405);
276 const home = await W('/view');
277 assert.equal(home.status, 200);
278 assert.match(home.text, /Public repositories/);
279 assert.match(home.text, /href="\/r\/ply"/);
280});
282test('viewer refuses other namespaces, nested paths, traversal and encoded variants on the Artifacts host before any fetch', { skip }, async () => {
283 const i = mark();
284 const bad = [
285 `/view/${HOST}/git/other/secret.git`,
286 `/view/${HOST}/git/default/ply.git`,
287 `/view/${HOST}/git/${NS}x/ply.git`,
288 `/view/${HOST}/git/${NS}/a/ply.git`,
289 `/view/${HOST}/git/${NS}/../other/secret.git`,
290 `/view/${HOST}/git/${NS}/%2e%2e/other/secret.git`,
291 `/view/${HOST}/git/${NS}/%2E%2E%2Fother%2Fsecret.git`,
292 `/view/${HOST}/git/${NS}%2f..%2fother/secret.git`,
293 `/view/${HOST}/git/${NS}/ply.git%2f..%2f..%2fother%2fsecret.git`,
294 `/view/${HOST}/git/${NS}/..%252fother/secret.git`,
295 `/view/${HOST}./git/${NS}/ply.git`,
296 `/view/x@${HOST}/git/${NS}/ply.git`,
297 `/view?remote=${encodeURIComponent(`${ORIGIN}/git/other/secret.git`)}`,
298 `/view?remote=${encodeURIComponent(`${ORIGIN}/git/${NS}/../other/secret.git`)}`,
299 ];
300 for (const path of bad) {
301 const r = await W(path);
302 assert.equal(r.status, 400, `${path} → ${r.status}`);
303 }
304 assert.deepEqual(since(i), [], 'nothing was fetched');
305 // the paste form accepts the public URL and redirects to the canonical path
306 const pasted = await W(`/view?remote=${encodeURIComponent(remote('ply'))}`);
307 assert.equal(pasted.status, 302);
308 assert.equal(pasted.headers.get('location'), publicView('ply'));
309});
311test('viewer: a redirect out of the repo (another namespace, another host) is not followed, so no token goes there', { skip }, async () => {
312 for (const name of ['moved', 'away']) {
313 const i = mark();
314 const r = await W(publicView(name));
315 assert.ok(r.status >= 400, `${name} → ${r.status}`);
316 const calls = since(i);
317 assert.equal(calls.some(h => h.host !== HOST || !h.path.startsWith(`/git/${NS}/${name}.git/`)), false, JSON.stringify(calls));
318 }
319});
321test('/v1 reads of public-v0 need no bearer and equal git: refs, discover, tree, read, commit', { skip }, async () => {
322 const i = mark();
323 const refs = await W(`/v1/refs?${q({ remote: remote('ply') })}`);
324 assert.equal(refs.status, 200, refs.text);
325 assert.equal(refs.json().refs['refs/heads/main'], git(plyRepo.bare, ['rev-parse', 'main']).trim());
326 assert.equal(refs.json().head, 'refs/heads/main');
327 const disc = await W(`/v1/discover?${q({ remote: remote('ply') })}`);
328 assert.equal(disc.status, 200);
329 assert.equal(disc.json().version, 2);
330 const tree = await W(`/v1/tree?${q({ remote: remote('ply'), path: 'src' })}`);
331 assert.equal(tree.status, 200);
332 assert.deepEqual(tree.json().entries.map(e => e.name), ['a.js']);
333 const read = await W(`/v1/read?${q({ remote: remote('ply'), path: 'README.md', trace: '1' })}`);
334 assert.equal(read.status, 200);
335 assert.deepEqual(read.buf, gitBuf(plyRepo.bare, ['show', 'main:README.md']));
336 assert.ok(read.headers.get('x-ply-trace'));
337 const commit = await W(`/v1/commit?${q({ remote: `${ORIGIN}/git/${NS}/ply`, trace: '1' })}`);
338 assert.equal(commit.status, 200);
339 assert.equal(commit.json().oid, plyRepo.head);
340 assert.ok(Array.isArray(commit.json().trace) && commit.json().trace.length > 0);
341 const calls = since(i);
342 assert.ok(calls.length >= 5);
343 for (const h of calls) assert.equal(h.auth, `Bearer ${TOKEN}`, `${h.method} ${h.path}`);
344 // a wrong bearer is still a 401: the public path is only for requests with no Authorization at all
345 const wrong = await W(`/v1/refs?${q({ remote: remote('ply') })}`, { headers: { authorization: `Bearer ${PLY}x` } });
346 assert.equal(wrong.status, 401);
347 // a valid bearer works too, and the Worker's token is still the one used
348 const j = mark();
349 assert.equal((await W(`/v1/refs?${q({ remote: remote('ply') })}`, { headers: bearer })).status, 200);
350 assert.ok(since(j).every(h => h.auth === `Bearer ${TOKEN}`));
351});
353test('/v1 without a bearer: another namespace, another host, traversal, unknown repo, or a caller credential are refused', { skip }, async () => {
354 const i = mark();
355 for (const r of [`${ORIGIN}/git/other/secret.git`, `${ORIGIN}/git/default/ply.git`, `${ORIGIN}/git/${NS}/a/ply.git`, `${ORIGIN}/git/${NS}/../other/secret.git`, `${ORIGIN}/git/${NS}/%2e%2e/other/secret.git`, `https://github.com/git/${NS}/ply.git`]) {
356 const res = await W(`/v1/refs?${q({ remote: r })}`);
357 assert.equal(res.status, 401, `${r} → ${res.status}`);
358 assert.equal(res.json().code, 'unauthorized');
359 }
360 for (const [m, p] of [['POST', '/v1/edit'], ['POST', '/v1/branch'], ['DELETE', '/v1/branch'], ['POST', '/v1/merge'], ['POST', '/v1/rebase'], ['POST', '/v1/cherry-pick'], ['POST', '/v1/revert'], ['POST', '/v1/tag'], ['DELETE', '/v1/tag']]) {
361 assert.equal((await W(p, { method: m, body: { remote: remote('ply'), ref: 'main', path: 'README.md', find: 'ply', replace: 'x', from: 'main', commit: plyRepo.head, author: { name: 'a', email: 'a@x' }, committer: { name: 'a', email: 'a@x' }, message: 'm' } })).status, 401, `${m} ${p}`);
362 }
363 assert.deepEqual(since(i), [], 'nothing was fetched');
364 const withCred = await W(`/v1/refs?${q({ remote: remote('ply') })}`, { headers: { 'git-token': OTHER } });
365 assert.equal(withCred.status, 400);
366 assert.deepEqual(since(i), []);
367 const missing = await W(`/v1/refs?${q({ remote: remote('nope') })}`);
368 assert.equal(missing.status, 404, missing.text);
369 assert.equal(missing.json().code, 'not_found');
370});
372test('/v1 with a bearer: another namespace gets no Worker token; a caller Git-Token is used as given (caller wins), never the Worker token', { skip }, async () => {
373 let i = mark();
374 const bare = await W(`/v1/refs?${q({ remote: `${ORIGIN}/git/other/secret.git` })}`, { headers: bearer });
375 assert.equal(bare.status, 401);
376 assert.equal(bare.json().code, 'auth');
377 assert.ok(since(i).length > 0);
378 assert.ok(since(i).every(h => h.auth === null), JSON.stringify(since(i)));
379 i = mark();
380 const other = await W(`/v1/read?${q({ remote: `${ORIGIN}/git/other/secret.git`, path: 'SECRET.md' })}`, { headers: { ...bearer, 'git-token': OTHER } });
381 assert.equal(other.status, 200, other.text);
382 assert.deepEqual(other.buf, gitBuf(otherRepo.bare, ['show', 'main:SECRET.md']));
383 assert.ok(since(i).every(h => h.auth === `Bearer ${OTHER}`));
384 i = mark();
385 // the caller's credential for public-v0 replaces the Worker's: the router demands TOKEN, so this is a 403
386 const mine = await W(`/v1/refs?${q({ remote: remote('ply') })}`, { headers: { ...bearer, 'git-token': OTHER } });
387 assert.equal(mine.status, 403);
388 assert.ok(since(i).length > 0 && since(i).every(h => h.auth === `Bearer ${OTHER}`));
389});
391test('/v1 redirects: the token is re-decided on every hop (another namespace: none; same namespace: yes; another host: not followed)', { skip }, async () => {
392 let i = mark();
393 const moved = await W(`/v1/refs?${q({ remote: remote('moved') })}`);
394 assert.equal(moved.status, 401, moved.text);
395 const movedHits = since(i);
396 assert.ok(movedHits.some(h => h.path.startsWith('/git/other/secret.git/')), JSON.stringify(movedHits));
397 for (const h of movedHits) assert.equal(h.auth, h.path.startsWith(`/git/${NS}/`) ? `Bearer ${TOKEN}` : null, `${h.method} ${h.path}`);
398 i = mark();
399 const away = await W(`/v1/refs?${q({ remote: remote('away') })}`);
400 assert.equal(away.status, 400);
401 assert.equal(since(i).some(h => h.host !== HOST), false, 'a cross-host hop is never dialled');
402 i = mark();
403 const hop = await W(`/v1/refs?${q({ remote: remote('hop') })}`);
404 assert.equal(hop.status, 200, hop.text);
405 assert.ok(since(i).some(h => h.path.startsWith(`/git/${NS}/ply.git/`) && h.auth === `Bearer ${TOKEN}`));
406});
408test('writes to public-v0 are 403 read_only with a bearer, with a caller git credential, and in any case variant; nothing is pushed', { skip }, async () => {
409 const before = git(plyRepo.bare, ['for-each-ref']);
410 const i = mark();
411 const body = { ref: 'main', path: 'README.md', find: 'ply', replace: 'x', from: 'main', commit: plyRepo.head, author: { name: 'a', email: 'a@x' }, committer: { name: 'a', email: 'a@x' }, message: 'm' };
412 for (const r of [remote('ply'), `${ORIGIN}/git/${NS}/ply`, `${ORIGIN}/git/${NS.toUpperCase()}/ply.git`, `${ORIGIN}/git/${NS}/new.git`, `${ORIGIN}/git/${NS}/a/b.git`]) {
413 for (const [m, p] of [['POST', '/v1/edit'], ['POST', '/v1/branch'], ['DELETE', '/v1/branch'], ['POST', '/v1/merge'], ['POST', '/v1/rebase'], ['POST', '/v1/cherry-pick'], ['POST', '/v1/revert'], ['POST', '/v1/tag'], ['DELETE', '/v1/tag']]) {
414 for (const headers of [bearer, { ...bearer, 'git-token': OTHER }, { ...bearer, 'git-authorization': `Bearer ${OTHER}` }]) {
415 const res = await W(p, { method: m, body: { ...body, remote: r }, headers });
416 assert.equal(res.status, 403, `${m} ${p} ${r}`);
417 assert.equal(res.json().code, 'read_only');
418 }
419 }
420 }
421 assert.deepEqual(since(i), [], 'no write reached the host');
422 assert.equal(git(plyRepo.bare, ['for-each-ref']), before);
423 // another namespace is still writable with the caller's own credential (the policy is only for public-v0)
424 const ok = await W('/v1/branch', { method: 'POST', body: { remote: `${ORIGIN}/git/other/secret.git`, ref: 'tmp' }, headers: { ...bearer, 'git-token': OTHER } });
425 assert.equal(ok.status, 201, ok.text);
426 assert.equal(hits.some(h => /receive-pack/.test(h.path) && h.auth === `Bearer ${TOKEN}`), false, 'receive-pack never carries the Worker token');
427});
429test('rate limit: public /v1 reads and viewer pages share the per-client page bucket', { skip }, async () => {
430 const ip = '203.0.113.77';
431 assert.equal((await W(`/v1/refs?${q({ remote: remote('ply') })}`, { port: PORT_TIGHT, ip })).status, 200);
432 assert.equal((await W(publicView('ply'), { port: PORT_TIGHT, ip })).status, 200);
433 assert.equal((await W(`/v1/refs?${q({ remote: remote('ply') })}`, { port: PORT_TIGHT, ip })).status, 200);
434 const limited = await W(`/v1/refs?${q({ remote: remote('ply') })}`, { port: PORT_TIGHT, ip });
435 assert.equal(limited.status, 429);
436 assert.equal(limited.json().code, 'rate_limited');
437 assert.ok(Number(limited.headers.get('retry-after')) >= 1);
438 assert.equal((await W(publicView('ply'), { port: PORT_TIGHT, ip })).status, 429);
439 assert.equal((await W(`/v1/refs?${q({ remote: remote('ply') })}`, { port: PORT_TIGHT, ip: '203.0.113.78' })).status, 200, 'another client is unaffected');
440 // a bearer request is not limited
441 assert.equal((await W(`/v1/refs?${q({ remote: remote('ply') })}`, { port: PORT_TIGHT, ip, headers: bearer })).status, 200);
442});
444test('a hostile upstream echoing the token is scrubbed from /v1 errors, viewer pages, and logs', { skip }, async () => {
445 const v1 = await W(`/v1/refs?${q({ remote: remote('echo'), trace: '1' })}`);
446 assert.ok(v1.status >= 500, v1.text);
447 const page = await W(publicView('echo'));
448 assert.ok(page.status >= 500);
449 assert.ok(hits.some(h => h.path.startsWith(`/git/${NS}/echo.git/`) && h.auth === `Bearer ${TOKEN}`), 'the echo host did receive the token, so there was something to leak');
450});
452test('the binding: only read tokens are minted, and one per repo per isolate', { skip }, async () => {
453 const mints = await (await fetch(`http://127.0.0.1:${PORT_FAKE}/`)).json();
454 assert.ok(mints.length > 0);
455 for (const m of mints) { assert.equal(m.scope, 'read'); assert.equal(m.ttl, 3600); }
456 const perRepo = {};
457 for (const m of mints) perRepo[m.repo] = (perRepo[m.repo] ?? 0) + 1;
458 // two isolates (main, tight) at most one mint each per repo; the fake's token never expires
459 for (const [repo, n] of Object.entries(perRepo)) assert.ok(n <= 2, `${repo} minted ${n} times`);
460});
462test('leak grep: the Worker token, in every form, is in no response, header, or log line', { skip }, async () => {
463 await new Promise(r => setTimeout(r, 300));
464 assert.ok(seen.length > 100, `${seen.length} responses checked`);
465 for (const form of TOKEN_FORMS) {
466 const at = seen.findIndex(s => s.includes(form));
467 assert.equal(at, -1, `response leaked ${form.slice(0, 12)}…: ${seen[at]?.slice(0, 300)}`);
468 assert.equal(logs.includes(form), false, `logs leaked ${form.slice(0, 12)}…`);
469 }
470 assert.match(logs, /ply\.request/);
471 assert.match(logs, /"public":1/);
472});
Diagnostics4 git calls · 35 ms
CallStatusSizeTime035 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200145 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=145
git.pkt.payload.max=76
git.ls_refs.count=2
git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=6
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=41349
git.pkt.payload.max=41254
git.sideband.band1.bytes=41253
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=41253
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c
git.pack.full=61
Open pack map
fetch2009.0 KiB · hit31 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=30
git.response.bytes=9236
git.response.cache=HIT
git.response.duration_ms=31
git.fetch.wants=6d47637856aa4736a234252db89e22bcd18f4abd
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=9236
git.pkt.payload.max=9215
git.sideband.band1.bytes=9214
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
Ref
refs/heads/main
Commit
526b305930b8ef382f2d7a1f9c0238730a585921
cf-ray
a5e2f1e2be58fda9
Request
afea3b08…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format