main
1// A source that lies in its fetch answer: ply and real git against the SAME server (a proxy in front of
2// `git http-backend` that replaces one upload-pack response). The expectation in every test is git's verdict on that
3// server, read from git's own exit status and stderr in the test; ply must reach the same verdict (a failed fetch),
4// never ok.
5import { test, beforeAll as before, afterAll as after } from 'vitest';
6import assert from 'node:assert/strict';
7import http from 'node:http';
8import { mkdirSync } from 'node:fs';
9import { join } from 'node:path';
10import { git, gitAsync, tmpRoot, makeBareRepo, startGitServer, commitFiles } from './server.js';
11import { GitHttp } from '../src/client.js';
12import { v2Init, v2Step, v2End, fetchBody } from '../src/fetch.js';
13import { Tracer } from '../src/otel.js';
15// a pkt-line by hand (not ply's writer)
16const pkt = s => { const b = Buffer.from(s); return Buffer.concat([Buffer.from((b.length + 4).toString(16).padStart(4, '0')), b]); };
17const FLUSH = Buffer.from('0000');
19// mode(body) => a Buffer to answer an upload-pack POST with, or null to pass the request to the real server;
20// tamper(body, answer) => the bytes to send in place of the real server's answer to an upload-pack POST
21function lyingProxy(upstream, mode, tamper = (_body, answer) => answer) {
22 const seen = [];
23 const server = http.createServer(async (req, res) => {
24 const chunks = []; for await (const c of req) chunks.push(c);
25 const body = Buffer.concat(chunks);
26 const isUpload = req.method === 'POST' && req.url.endsWith('/git-upload-pack');
27 const lie = isUpload ? mode(body.toString('latin1')) : null;
28 if (isUpload) seen.push(body.toString('latin1'));
29 if (lie) { res.writeHead(200, { 'content-type': 'application/x-git-upload-pack-result' }); res.end(lie); return; }
30 const headers = { ...req.headers }; delete headers.host; delete headers['content-length']; delete headers['transfer-encoding'];
31 const up = await fetch(upstream + req.url, { method: req.method, headers, body: req.method === 'POST' ? body : undefined });
32 const real = Buffer.from(await up.arrayBuffer());
33 const out = isUpload ? tamper(body.toString('latin1'), real) : real;
34 const h = {}; for (const [k, v] of up.headers) if (!['content-length', 'transfer-encoding', 'content-encoding', 'connection'].includes(k)) h[k] = v;
35 res.writeHead(up.status, h); res.end(out);
36 });
37 return new Promise(r => server.listen(0, '127.0.0.1', () => r({ url: `http://127.0.0.1:${server.address().port}`, seen, close: () => new Promise(x => { server.close(x); server.closeAllConnections?.(); }) })));
38}
39// real git's fetch of main from `url` into the repository at `cwd`: its exit status and stderr
40const gitFetch = async (cwd, url) => { const r = await gitAsync(cwd, ['-c', 'protocol.version=2', 'fetch', url, 'main']); return { failed: r.status !== 0, stderr: r.stderr }; };
41const client = url => GitHttp.create(url, { tracer: new Tracer() }).value;
43let tmp, srv, A, C, empty, haveA;
44before(async () => {
45 tmp = tmpRoot();
46 srv = await startGitServer(tmp.root);
47 const src = makeBareRepo(tmp.root, 'src');
48 const work = join(tmp.root, 'work'); mkdirSync(work); git(work, ['init', '-q', '-b', 'main']);
49 A = commitFiles(work, src, { 'a.txt': 'one\n' }, 'A');
50 commitFiles(work, src, { 'a.txt': 'one\ntwo\n' }, 'B');
51 C = commitFiles(work, src, { 'b.txt': 'three\n' }, 'C');
52 empty = join(tmp.root, 'empty'); mkdirSync(empty); git(empty, ['init', '-q', '-b', 'main']);
53 // a client repository that holds A only: what real git negotiates with
54 haveA = join(tmp.root, 'haveA'); mkdirSync(haveA); git(haveA, ['init', '-q', '-b', 'main']); git(haveA, ['fetch', '-q', src, `${A}:refs/heads/old`]);
55});
56after(async () => { await srv.close(); tmp.cleanup(); });
58test('a fetch sent with done and answered with acknowledgments only is a protocol error, as it is for git', async () => {
59 const p = await lyingProxy(srv.url, body => (body.includes('command=fetch') && body.includes('done\n') ? Buffer.concat([pkt('acknowledgments\n'), pkt('NAK\n'), FLUSH]) : null));
60 try {
61 // the oracle: git (fetch-pack.c do_fetch_pack_v2, FETCH_GET_PACK) dies on the same answer
62 const g = await gitFetch(empty, `${p.url}/src.git`);
63 assert.equal(g.failed, true, 'git fails the fetch');
64 assert.match(g.stderr, /expected 'packfile', received 'acknowledgments'/);
65 const r = await client(`${p.url}/src.git`).fetchPack({ wants: [C] });
66 assert.deepEqual([r.ok, r.ok ? null : r.error.code], [false, 'protocol']);
67 } finally { await p.close(); }
68});
70test('v2 end: without done, acknowledgments alone end a round; ready with no packfile, and a line that is no acknowledgment, do not', () => {
71 // The step machine driven by hand-written frames. Expected values are git's (fetch-pack.c): process_ack reads only
72 // "NAK", "ACK <oid>" and "ready" and dies "unexpected acknowledgment line: '%s'" on anything else; after `ready` it
73 // dies "expected packfile to be sent after 'ready'" when the next section is not the packfile; and with `done` sent
74 // it never reads an acknowledgments section (previous test).
75 const te = new TextEncoder();
76 const run = lines => { let st = v2Init(); for (const l of [...lines, null]) { const r = v2Step(st, l === null ? { kind: 'flush' } : { kind: 'data', payload: te.encode(l) }); if (!r.ok) return r; st = r.value.state; } return { ok: true, value: st }; };
77 const end = (lines, done) => { const r = run(lines); return r.ok ? v2End(r.value, done) : r; };
78 const code = r => (r.ok ? 'ok' : r.error.code);
79 assert.equal(code(end(['acknowledgments\n', 'NAK\n'], false)), 'ok', 'a negotiation round');
80 assert.equal(code(end(['acknowledgments\n', `ACK ${A}\n`], false)), 'ok', 'a negotiation round with a common commit');
81 assert.equal(code(end(['acknowledgments\n', 'NAK\n'], true)), 'protocol', 'done was sent: the answer must be a pack');
82 assert.equal(code(end(['acknowledgments\n', `ACK ${A}\n`, 'ready\n'], false)), 'protocol', 'ready, then no packfile');
83 assert.equal(code(end(['acknowledgments\n', 'hello\n'], false)), 'protocol', 'not an acknowledgment line');
84});
86test('done is read once: what is not a boolean is refused before any request; false is a round, left out is done', async () => {
87 // A server that answers every fetch sent WITHOUT done with acknowledgments only (a legitimate negotiation round) and
88 // passes a fetch sent with done to real git. Expected values are the owner's rule (done is true, false or left out;
89 // anything else is bad_request) and the protocol: the request's own bytes say whether `done` was sent.
90 const p = await lyingProxy(srv.url, body => (body.includes('command=fetch') && !body.includes('done\n') ? Buffer.concat([pkt('acknowledgments\n'), pkt('NAK\n'), FLUSH]) : null));
91 try {
92 const c = client(`${p.url}/src.git`);
93 const fetches = () => p.seen.filter(b => b.includes('command=fetch'));
94 for (const done of [null, 0, '', 1, 'true']) {
95 const before = fetches().length;
96 // oxlint-disable-next-line no-await-in-loop -- one request at a time: the count is per call
97 const r = await c.fetchPack({ wants: [C], haves: [A], done });
98 assert.deepEqual([r.ok, r.ok ? null : r.error.code, fetches().length - before], [false, 'bad_request', 0], `done: ${JSON.stringify(done)}`);
99 const b = fetchBody({ wants: [C], done });
100 assert.deepEqual([b.ok, b.ok ? null : b.error.code], [false, 'bad_request'], `fetchBody done: ${JSON.stringify(done)}`);
101 }
102 const round = await c.fetchPack({ wants: [C], haves: [A], done: false });
103 assert.deepEqual([round.ok, round.ok && Boolean(round.value.parsed), fetches().at(-1).includes('done\n')], [true, false, false], 'done: false is a round without a pack');
104 const full = await c.fetchPack({ wants: [C], haves: [A] });
105 assert.deepEqual([full.ok, full.ok && Boolean(full.value.parsed), fetches().at(-1).includes('0009done\n')], [true, true, true], 'done left out is sent, and answered with a pack');
106 } finally { await p.close(); }
107});
109test('a fetch sent with done and answered with acknowledgments before its packfile is a protocol error, as it is for git', async () => {
110 // real git's answer (a real pack) with an acknowledgments section put in front of it
111 const DELIM = Buffer.from('0001');
112 const p = await lyingProxy(srv.url, () => null, (body, answer) => (body.includes('command=fetch') && body.includes('done\n') ? Buffer.concat([pkt('acknowledgments\n'), pkt('NAK\n'), DELIM, answer]) : answer));
113 try {
114 // the oracle: after a request with done, git reads the packfile section next and dies on any other
115 const g = await gitFetch(empty, `${p.url}/src.git`);
116 assert.equal(g.failed, true, 'git fails the fetch');
117 assert.match(g.stderr, /expected 'packfile', received 'acknowledgments'/);
118 const r = await client(`${p.url}/src.git`).fetchPack({ wants: [C] });
119 assert.deepEqual([r.ok, r.ok ? null : r.error.code], [false, 'protocol']);
120 assert.match(r.error.message, /expected packfile, received acknowledgments/);
121 } finally { await p.close(); }
122 // the same shape through the step machine: refused with done; without done it is git's `ready` then the pack
123 const te = new TextEncoder();
124 const data = l => ({ kind: 'data', payload: te.encode(l) });
125 const end = (frames, done) => { let st = v2Init(); for (const f of frames) { const s = v2Step(st, f); if (!s.ok) return s; st = s.value.state; } return v2End(st, done); };
126 const code = x => (x.ok ? 'ok' : x.error.code);
127 const acksThenPack = ready => [data('acknowledgments\n'), data(`ACK ${A}\n`), ...(ready ? [data('ready\n')] : []), { kind: 'delim' }, data('packfile\n'), { kind: 'flush' }];
128 assert.equal(code(end(acksThenPack(false), true)), 'protocol', 'done was sent: no acknowledgments section');
129 assert.equal(code(end(acksThenPack(true), true)), 'protocol', 'done was sent: not even with ready');
130 assert.equal(code(end(acksThenPack(true), false)), 'ok', 'no done: ready, then the pack');
131 assert.equal(code(end([data('shallow-info\n'), { kind: 'delim' }, data('packfile\n'), { kind: 'flush' }], true)), 'ok', 'done: shallow-info, then the pack');
132});
Diagnostics4 git calls · 325 ms
CallStatusSizeTime0325 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 1020038 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=320 git.response.bytes=38434 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=19912563f540228593df0c489082e5e2508e21e4 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=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2003.4 KiB320 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3511 git.response.cache=MISS git.response.duration_ms=320 git.fetch.wants=0650bb280e812ea175f47a0b965b8e5b3200da75 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=3511 git.pkt.payload.max=3490 git.sideband.band1.bytes=3489 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.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e224a6581ca87d
- Request
- d66823c6…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format