main
1// Below the hash check. hostile.mjs mutates the wire, so inflate, the trailer, or the oid check rejects the
2// object before a parser runs. This stage mutates the decompressed object, re-hashes it, and serves that oid from a
3// tiny v2 server. The oracle is `git fsck --strict` on the same bytes (`hash-object --literally`). ply must reject
4// everything fsck rejects. A ply rejection of an object fsck accepts is reported; it fails the run only when the
5// object is one git itself would write.
6//
7// node test/fuzz/below-hash.mjs [--n 20] [--seed 1] [--case empty-mode|empty-name|<seed>]
8import http from 'node:http';
9import { spawnSync } from 'node:child_process';
10import { mkdtempSync, rmSync } from 'node:fs';
11import { tmpdir } from 'node:os';
12import { join } from 'node:path';
13import { concat, te } from '../../src/bytes.js';
14import { fromHex } from '../../src/sha1.js';
15import { hashObject, encodeCommit, encodeTag, encodeTree, ident, TYPE } from '../../src/objects.js';
16import { validateIdent } from '../../src/validate.js';
17import { buildPack } from '../../src/pack.js';
18import { pkt, FLUSH } from '../../src/pktline.js';
19import { listTree, readCommit } from '../../src/read.js';
20import { createTransport as createTransportR } from '../../src/http.js';
21import { fetchObject as fetchObjectR } from '../../src/fetch.js';
22import { Tracer, arrayExporter } from '../../src/otel.js';
23import { withLimits } from '../../src/limits.js';
24import { Rng } from './prng.js';
25import { OID_BYTES } from '../../src/format.js';
26import { unwrap, unwrapAsync } from '../unwrap.js';
28// Throwing adapters over the Result API (0.2.0): the assertions below are about the throw.
29const createTransport = (...a) => unwrap(createTransportR(...a));
30const fetchObject = (...a) => unwrapAsync(fetchObjectR(...a));
31const args = Object.fromEntries(process.argv.slice(2).map((a, i, arr) => a.startsWith('--') ? [a.slice(2), arr[i + 1] && !arr[i + 1].startsWith('--') ? arr[i + 1] : true] : []).filter(x => x.length));
32const N = parseInt(args.n ?? '20', 10);
33const SEED = parseInt(args.seed ?? '1', 10);
34const ONLY = args.case && args.case !== true ? String(args.case) : null;
35// The seed is printed on start and on every failing line, with the command that replays that case: a fixed case by
36// its name, a random case by its own seed, under the run's --n and --seed (they decide which random cases exist).
37console.log(`below-hash: seed ${SEED}, n ${N}`);
38const replay = c => ` seed=${SEED} replay: node --import ./test/ts-resolve.mjs test/fuzz/below-hash.mjs --n ${N} --seed ${SEED} --case ${c.seed ?? c.name}`;
39const LIMITS = withLimits({ requestTimeoutMs: 4000 });
40const AUTHOR = unwrap(ident(unwrap(validateIdent({ name: 'A', email: 'a@b.c', time: 1700000000 }))));
41const REJECT = new Set(['pack_corrupt', 'protocol', 'remote_error', 'not_found', 'unsupported']);
43const entry = (mode, name, oid) => concat([te.encode(`${mode} ${name}\0`), oid]);
44const blob = te.encode('hello\n');
46function git(dir, argv, input) {
47 const r = spawnSync('git', argv, { cwd: dir, input, encoding: 'utf8' });
48 return { code: r.status ?? 1, out: `${r.stdout ?? ''}\n${r.stderr ?? ''}` };
49}
51// Errors about the object itself. Broken links and dangling tips are not a structural rejection: a well-formed
52// tree whose target was never written still upsets fsck, and that is not the parser's question.
53function fsckErrors(dir) {
54 const r = git(dir, ['fsck', '--strict']);
55 return r.out.split('\n').map(l => l.trim()).filter(l => /^error\b/i.test(l) && !/broken link/i.test(l));
56}
58function writeChain(objects) {
59 const dir = mkdtempSync(join(tmpdir(), 'ply-fsck-'));
60 git(dir, ['init', '-q', '-b', 'main']);
61 const oids = [];
62 for (const o of objects) {
63 const r = git(dir, ['hash-object', '-t', o.type, '--literally', '-w', '--stdin'], Buffer.from(o.data));
64 const oid = r.out.trim().split('\n').filter(Boolean).pop();
65 if (r.code !== 0 || !oid) throw new Error(`hash-object ${o.type} failed: ${r.out}`);
66 const ours = hashObject(o.type, o.data);
67 if (ours !== oid) throw new Error(`hash diverged for ${o.type}: ply ${ours} git ${oid}`);
68 oids.push(oid);
69 }
70 const errors = fsckErrors(dir);
71 rmSync(dir, { recursive: true, force: true });
72 return { oids, errors, rejected: errors.length > 0 };
73}
75let current = null;
76async function packResponse(obj) {
77 const { pack } = await buildPack([{ type: TYPE[obj.type.toUpperCase()], data: obj.data }]);
78 const parts = [unwrap(pkt('packfile\n'))];
79 for (let i = 0; i < pack.length; i += 65000) parts.push(unwrap(pkt(concat([Uint8Array.of(1), pack.subarray(i, Math.min(i + 65000, pack.length))]))));
80 parts.push(FLUSH);
81 return concat(parts);
82}
83const server = http.createServer(async (req, res) => {
84 try {
85 const chunks = [];
86 for await (const c of req) chunks.push(c);
87 const body = Buffer.concat(chunks).toString('latin1');
88 let bytes;
89 if (body.includes('command=ls-refs')) {
90 bytes = concat([
91 unwrap(pkt(`${current.head} HEAD symref-target:refs/heads/main\n`)),
92 unwrap(pkt(`${current.head} refs/heads/main\n`)),
93 FLUSH,
94 ]);
95 } else if (body.includes('command=fetch')) {
96 const want = body.match(/want ([0-9a-f]{40})/);
97 const obj = want && current.byOid.get(want[1]);
98 bytes = obj ? await packResponse(obj) : concat([unwrap(pkt('ERR not our ref\n')), FLUSH]);
99 } else bytes = concat([unwrap(pkt('ERR\n')), FLUSH]);
100 res.writeHead(200, { 'content-type': 'application/x-git-upload-pack-result', 'cache-control': 'no-store' });
101 res.end(Buffer.from(bytes));
102 } catch (e) {
103 res.writeHead(500);
104 res.end(String(e?.message ?? e));
105 }
106});
107await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
108const url = `http://127.0.0.1:${server.address().port}/hostile.git`;
110async function plyAccepts(focus, head) {
111 const tracer = new Tracer({ exporter: arrayExporter() });
112 try {
113 if (focus === 'tree') await unwrapAsync(listTree(url, { ref: 'refs/heads/main' }, { tracer, limits: LIMITS }));
114 else if (focus === 'commit') await unwrapAsync(readCommit(url, { ref: 'refs/heads/main' }, { tracer, limits: LIMITS }));
115 else {
116 const t = createTransport(url, { tracer, limits: LIMITS });
117 await fetchObject(t, { oid: head, filter: 'blob:none', expectType: TYPE.TAG });
118 }
119 return { accepted: true };
120 } catch (e) {
121 return { accepted: false, code: e?.code, message: e?.message ?? String(e) };
122 }
123}
125function mutate(rng, bytes) {
126 const b = new Uint8Array(bytes);
127 const kind = rng.int(0, 4);
128 if (!b.length || kind === 4) return concat([b, Uint8Array.of(rng.int(0, 255))]);
129 const i = rng.int(0, b.length - 1);
130 if (kind === 0) { b[i] ^= 1 << rng.int(0, 7); return b; }
131 if (kind === 1) return concat([b.subarray(0, i), b.subarray(i + 1)]);
132 if (kind === 2) return concat([b.subarray(0, i), Uint8Array.of(rng.int(0, 255)), b.subarray(i)]);
133 return b.subarray(0, rng.int(0, b.length));
134}
136function validTree(blobOid) {
137 return unwrap(encodeTree([{ mode: '100644', name: 'a', oid: blobOid }]));
138}
140const cases = [];
141function add(name, spec) { cases.push({ name, ...spec }); }
143const blobOid = hashObject('blob', blob);
144const blobBytes = fromHex(blobOid);
145const treeOk = validTree(blobOid);
146const treeOid = hashObject('tree', treeOk);
147const commitOk = unwrap(encodeCommit({ tree: treeOid, author: AUTHOR, message: 'm\n' }));
148const commitOid = hashObject('commit', commitOk);
150add('valid', { objects: [{ type: 'blob', data: blob }, { type: 'tree', data: treeOk }, { type: 'commit', data: commitOk }], focus: 'tree', normal: true });
151add('empty-tree', {
152 objects: [{ type: 'blob', data: blob }, { type: 'tree', data: new Uint8Array(0) }, { type: 'commit', data: unwrap(encodeCommit({ tree: hashObject('tree', new Uint8Array(0)), author: AUTHOR, message: 'm\n' })) }],
153 focus: 'tree', normal: true,
154});
155for (const [name, tree] of [
156 ['empty-mode', concat([te.encode(' a\0'), blobBytes])],
157 ['empty-name', concat([te.encode('100644 \0'), blobBytes])],
158 ['truncated-oid', concat([te.encode('100644 a\0'), blobBytes.subarray(0, OID_BYTES - 1)])],
159 ['zero-padded-mode', concat([te.encode('0100644 a\0'), blobBytes])],
160 ['full-pathname', concat([te.encode('100644 a/b\0'), blobBytes])],
161 ['dot', concat([te.encode('100644 .\0'), blobBytes])],
162 ['dotdot', concat([te.encode('100644 ..\0'), blobBytes])],
163 ['dotgit', concat([te.encode('100644 .git\0'), blobBytes])],
164 ['unsorted', concat([entry('100644', 'b', blobBytes), entry('100644', 'a', blobBytes)])],
165 ['duplicate', concat([entry('100644', 'a', blobBytes), entry('100644', 'a', blobBytes)])],
166 ['bad-filemode', concat([te.encode('100664 a\0'), blobBytes])],
167]) {
168 const oid = hashObject('tree', tree);
169 add(name, {
170 objects: [{ type: 'blob', data: blob }, { type: 'tree', data: tree }, { type: 'commit', data: unwrap(encodeCommit({ tree: oid, author: AUTHOR, message: 'm\n' })) }],
171 focus: 'tree', normal: false,
172 });
173}
174add('missing-author', {
175 objects: [{ type: 'blob', data: blob }, { type: 'tree', data: treeOk }, { type: 'commit', data: te.encode(`tree ${treeOid}\ncommitter ${AUTHOR}\n\nm\n`) }],
176 focus: 'commit', normal: false,
177});
178add('bad-email', {
179 objects: [{ type: 'blob', data: blob }, { type: 'tree', data: treeOk }, { type: 'commit', data: te.encode(`tree ${treeOid}\nauthor Ada 1700000000 +0000\ncommitter ${AUTHOR}\n\nm\n`) }],
180 focus: 'commit', normal: false,
181});
182add('bad-date', {
183 objects: [{ type: 'blob', data: blob }, { type: 'tree', data: treeOk }, { type: 'commit', data: te.encode(`tree ${treeOid}\nauthor Ada <a@b.c> soon +0000\ncommitter ${AUTHOR}\n\nm\n`) }],
184 focus: 'commit', normal: false,
185});
186add('valid-commit', { objects: [{ type: 'blob', data: blob }, { type: 'tree', data: treeOk }, { type: 'commit', data: commitOk }], focus: 'commit', normal: true });
187{
188 const tag = unwrap(encodeTag({ object: commitOid, type: 'commit', tag: 'v1', tagger: AUTHOR, message: 'rel\n' }));
189 add('valid-tag', { objects: [{ type: 'blob', data: blob }, { type: 'tree', data: treeOk }, { type: 'commit', data: commitOk }, { type: 'tag', data: tag }], focus: 'tag', normal: true });
190 // encodeTag refuses an empty tag name (git hash-object's badTagName), so the malformed object is spelled out
191 const emptyName = te.encode(`object ${commitOid}\ntype commit\ntag \ntagger ${AUTHOR}\n\nrel\n`);
192 add('empty-tag-name', { objects: [{ type: 'blob', data: blob }, { type: 'tree', data: treeOk }, { type: 'commit', data: commitOk }, { type: 'tag', data: emptyName }], focus: 'tag', normal: false });
193}
195const randoms = [];
196for (let i = 0; i < N; i++) {
197 const seed = (SEED * 1000003 + i) >>> 0;
198 const rng = new Rng(seed);
199 if (rng.bool()) {
200 const tree = mutate(rng, treeOk);
201 const oid = hashObject('tree', tree);
202 randoms.push({ name: `tree#${seed}`, seed, objects: [{ type: 'blob', data: blob }, { type: 'tree', data: tree }, { type: 'commit', data: unwrap(encodeCommit({ tree: oid, author: AUTHOR, message: 'm\n' })) }], focus: 'tree', normal: false });
203 } else {
204 const commit = mutate(rng, commitOk);
205 randoms.push({ name: `commit#${seed}`, seed, objects: [{ type: 'blob', data: blob }, { type: 'tree', data: treeOk }, { type: 'commit', data: commit }], focus: 'commit', normal: false });
206 }
207}
209let selected = [...cases, ...randoms];
210if (ONLY !== null) {
211 const asSeed = Number(ONLY);
212 selected = Number.isInteger(asSeed) && String(asSeed) === ONLY
213 ? randoms.filter(c => c.seed === asSeed)
214 : cases.filter(c => c.name === ONLY);
215 if (!selected.length) { console.error(`no case ${ONLY}`); process.exit(2); }
216}
218const fails = [];
219const stricter = [];
220const rows = [];
221try {
222 for (const c of selected) {
223 const judged = writeChain(c.objects);
224 const head = judged.oids[c.focus === 'tag' ? judged.oids.length - 1 : judged.oids.length - 1];
225 const byOid = new Map(judged.oids.map((oid, i) => [oid, c.objects[i]]));
226 current = { head, byOid };
227 const ply = await plyAccepts(c.focus, head);
228 if (!ply.accepted && !REJECT.has(ply.code)) {
229 fails.push(c.name);
230 console.log(`FAIL ${c.name}: untyped ${ply.code}: ${ply.message}${replay(c)}`);
231 continue;
232 }
233 const fsckNo = judged.rejected;
234 const plyNo = !ply.accepted;
235 let verdict = 'ok';
236 if (fsckNo && !plyNo) { verdict = 'DIVERGE'; fails.push(c.name); }
237 else if (!fsckNo && plyNo && c.normal) { verdict = 'STRICT-NORMAL'; fails.push(c.name); }
238 else if (!fsckNo && plyNo) { verdict = 'stricter'; stricter.push({ name: c.name, code: ply.code, message: ply.message, fsck: judged.errors }); }
239 rows.push(verdict);
240 const detail = verdict === 'ok' ? '' : ` fsck=${judged.errors[0] ?? 'accept'} ply=${ply.accepted ? 'accept' : ply.code + ' ' + String(ply.message).slice(0, 80)}`;
241 console.log(`${verdict.padEnd(13)} ${c.name.padEnd(22)} ${c.focus.padEnd(6)}${detail}${verdict === 'DIVERGE' || verdict === 'STRICT-NORMAL' ? replay(c) : ''}`);
242 }
243} finally {
244 await new Promise(resolve => server.close(resolve));
245}
247console.log(`\n${selected.length} cases, ${fails.length} failures, ${stricter.length} stricter (ply rejects, fsck accepts)`);
248for (const s of stricter) console.log(` stricter ${s.name}: ${s.code}: ${String(s.message).slice(0, 120)}`);
249process.exit(fails.length ? 1 : 0);
Diagnostics4 git calls · 647 ms
CallStatusSizeTime0647 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 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 · hit7 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=638 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=7 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
fetch2004.4 KiB638 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=4531 git.response.cache=MISS git.response.duration_ms=638 git.fetch.wants=9b373e9f391cd952dec103af5ef6f73c69c8ad68 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=4531 git.pkt.payload.max=4510 git.sideband.band1.bytes=4509 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
- a5e2deec8b772308
- Request
- 4cb61182…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format