main
1// Hostile-server fuzz: compose 1–3 random mutations on a random exchange, run the edit, check the safety
2// invariants against git. Reports which check caught what, so blind spots in detection show up as gaps.
3// node test/fuzz/hostile.mjs [--n 300] [--seed 1] [--case <seed>]
4import { join } from 'node:path';
5import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
6import { git, gitBuf, tmpRoot, makeBareRepo, startGitServer, commitFiles, fsckProblems, initRepo } from '../server.js';
7import { startProxy } from '../proxy.js';
8import { MUTATORS, MUTATOR_NAMES } from './mutate.js';
9import { Rng } from './prng.js';
10import { editFile } from '../../src/edit.js';
11import { GitHttp } from '../../src/client.js';
12import { TYPE } from '../../src/objects.js';
13import { Tracer, arrayExporter, flattenLine } from '../../src/otel.js';
14import { withLimits } from '../../src/limits.js';
15import { unwrapAsync, unwrap } from '../unwrap.js';
17const args = Object.fromEntries(process.argv.slice(2).map((a, i, arr) => a.startsWith('--') ? [a.slice(2), arr[i + 1] ?? true] : []).filter(x => x.length));
18const N = parseInt(args.n ?? '300', 10), SEED = parseInt(args.seed ?? String(Date.now() % 1e9), 10), ONLY = args.case ? parseInt(args.case, 10) : null;
19// The run seed, before any case: a clock-seeded run is replayable only if it says which seed the clock gave (#308).
20console.log(`hostile: seed ${SEED}, n ${N} replay: node --import ./test/ts-resolve.mjs test/fuzz/hostile.mjs --n ${N} --seed ${SEED}`);
21const CODES = new Set(['pack_corrupt', 'protocol', 'network', 'timeout', 'aborted', 'not_git', 'auth', 'not_found', 'rate_limited', 'upstream_5xx', 'http', 'limit_exceeded', 'remote_error', 'match_count', 'cas_conflict', 'push_rejected', 'bad_request', 'unsupported', 'ambiguous']);
22// 'push-sb' is a direct sideband push (side-band-64k + report-status-v2), the response path the edit pipeline does not use
23const STAGES = [['ls-refs', 0], ['fetch', 0], ['fetch', 1], ['fetch', 2], ['fetch', 3], ['fetch', 4], ['receive-pack', 0], ['push-sb', 0]];
24// which check caught it, from the message. 'bounds' is take()'s typed short read raised inside a frame parser, e.g. a
25// pkt-line cut to 0004 in the packfile section has no band byte (git: "missing sideband designator"); case 2366521731.
26const SITE = [['trailer', /trailer/], ['adler', /adler/], ['zlib_bound', /zlib stream longer/], ['size', /size|exceeds expected|streamed more|inflated \d+ !=/], ['pkt', /pkt-line/], ['huffman', /huffman|block type|invalid|distance|stored|repeat|end-of-block|unexpected end/], ['zlib_hdr', /zlib:/], ['delta', /delta/], ['limit', /exceeds limit/], ['v2', /v2:|no packfile|sideband/], ['remote', /remote:/], ['ls_refs', /ls-refs:/], ['unsupported', /unsupported/], ['pack_hdr', /signature|version|expected 1 object|object type|not in pack|truncated before object header|missing trailer|single-object pack is a delta|trailing bytes after object|served for want/], ['tree', /tree:|not found/], ['content_type', /content-type/], ['http', /HTTP \d/], ['report', /push rejected|unpack/], ['net', /network|aborted|timeout/], ['bounds', /^short read:/], ['ambiguous', /outcome unknown/]];
28const { root, cleanup } = tmpRoot();
29const srv = await startGitServer(root);
30const FILE = 'a/b/c/target.txt', ORIG = 'before MARK after\n', EXPECTED = 'before XX after\n';
31const codes = {}, sites = {}, byStage = {}, muts = {};
32const fails = [];
33let silentOk = 0;
34try {
35 const seeds = ONLY !== null ? [ONLY] : Array.from({ length: N }, (_, i) => (SEED * 7919 + i * 104729) >>> 0);
36 for (let i = 0; i < seeds.length; i++) {
37 const rng = new Rng(seeds[i]);
38 const name = `h${i}`;
39 const bare = makeBareRepo(root, name);
40 const work = join(root, `w${i}`); mkdirSync(work); initRepo(work);
41 commitFiles(work, bare, { [FILE]: ORIG, 'a/s.txt': 'x'.repeat(rng.int(0, 70000)), 'z.txt': 'z' }, 'one');
42 const head = commitFiles(work, bare, { 'z.txt': 'z2' }, 'two');
43 const [stage, k] = rng.pick(STAGES);
44 const chain = Array.from({ length: rng.int(1, 3) }, () => rng.pick(MUTATOR_NAMES));
45 const mseed = rng.u32();
46 const proxyStage = stage === 'push-sb' ? 'receive-pack' : stage;
47 const proxy = await startProxy(srv.url, ctx => {
48 if (ctx.stage !== proxyStage || ctx.n !== k) return { type: 'pass' };
49 const r = new Rng(mseed); let b = ctx.bytes;
50 for (const m of chain) b = MUTATORS[m](r, b);
51 return { type: 'bytes', bytes: b, name: chain.join('+'), chunks: rng.bool(0.3) ? Array.from({ length: 50 }, () => rng.int(1, 300)) : undefined };
52 });
53 const exporter = arrayExporter();
54 const t0 = performance.now();
55 let result = null, error = null;
56 try {
57 if (stage === 'push-sb') {
58 // a commit prepared with git, pushed by us with sideband + report-status-v2 through the mutating proxy
59 const w2 = join(root, `p${i}`); mkdirSync(w2); git(w2, ['clone', '-q', bare, '.']); writeFileSync(join(w2, FILE), EXPECTED); git(w2, ['add', '-A']); git(w2, ['-c', 'user.name=h', '-c', 'user.email=h@x', 'commit', '-q', '-m', 'sb']);
60 const nxt = git(w2, ['rev-parse', 'HEAD']).trim();
61 const objs = git(w2, ['rev-list', '--objects', '--no-object-names', nxt, '-n1', `^${head}`]).trim().split('\n').map(o => { const t = git(w2, ['cat-file', '-t', o]).trim(); return { type: TYPE[t.toUpperCase()], data: new Uint8Array(gitBuf(w2, ['cat-file', t, o])) }; });
62 const c = unwrap(GitHttp.create(`${proxy.url}/${name}.git`, { tracer: new Tracer({ exporter }), limits: withLimits({ requestTimeoutMs: 3000 }) }));
63 const r = await unwrapAsync(c.push({ ref: 'refs/heads/main', oldOid: head, newOid: nxt, objects: objs, sideband: true, reportV2: true }));
64 result = { newCommit: nxt, report: r.report };
65 } else result = await unwrapAsync(editFile(`${proxy.url}/${name}.git`, { path: FILE, find: 'MARK', replace: 'XX', author: { name: 'h', email: 'h@x' }, message: 'h' }, { tracer: new Tracer({ exporter }), limits: withLimits({ requestTimeoutMs: 3000 }) }));
66 } catch (e) { error = e; }
67 const ms = performance.now() - t0;
68 await proxy.close();
69 const problems = [];
70 const nowHead = git(bare, ['rev-parse', 'main']).trim();
71 const content = gitBuf(bare, ['cat-file', 'blob', `main:${FILE}`]).toString();
72 if (ms > 8000) problems.push(`took ${ms | 0}ms`);
73 if (error) {
74 if (!CODES.has(error.code)) problems.push(`untyped error ${error.code}: ${error.message}`);
75 if (nowHead !== head) {
76 if (stage !== 'receive-pack' && stage !== 'push-sb') problems.push('ref moved on error');
77 else if (content !== EXPECTED) problems.push('ref moved to wrong content');
78 else if (error.code !== 'ambiguous') problems.push(`ref moved but error is ${error.code}, not ambiguous`);
79 }
80 const root = exporter.lines.flatMap(flattenLine).find(s => s.name === 'git.edit');
81 if (stage !== 'push-sb' && root?.attrs['git.edit.outcome'] !== error.code) problems.push(`outcome attr ${root?.attrs['git.edit.outcome']} != ${error.code}`);
82 const site = SITE.find(([, re]) => re.test(error.message))?.[0] ?? 'other';
83 sites[site] = (sites[site] ?? 0) + 1;
84 if (site === 'other') problems.push(`unclassified detection: ${error.message}`);
85 } else {
86 if (nowHead !== result.newCommit) problems.push('success but ref != our commit');
87 if (content !== EXPECTED) problems.push('success but wrong content');
88 const fsck = fsckProblems(bare); if (fsck.length) problems.push(`fsck ${fsck.join(' | ')}`);
89 silentOk++;
90 }
91 const code = error ? error.code : 'ok';
92 codes[code] = (codes[code] ?? 0) + 1;
93 byStage[`${stage}#${k}:${code}`] = (byStage[`${stage}#${k}:${code}`] ?? 0) + 1;
94 for (const m of chain) muts[`${m}:${code}`] = (muts[`${m}:${code}`] ?? 0) + 1;
95 process.stdout.write(`${problems.length ? 'FAIL' : 'ok '} #${String(i).padStart(3)} seed=${seeds[i]} ${stage}#${k} ${chain.join('+').padEnd(32)} → ${code.padEnd(14)} ${String(ms | 0).padStart(5)}ms\n`);
96 if (problems.length) { fails.push(seeds[i]); console.log(' ' + problems.join('\n ') + `\n repro: node test/fuzz/hostile.mjs --case ${seeds[i]}`); }
97 rmSync(bare, { recursive: true, force: true }); rmSync(work, { recursive: true, force: true });
98 }
99} finally { await srv.close(); cleanup(); }
100const show = o => Object.entries(o).sort((a, b) => b[1] - a[1]).map(([k, v]) => `${String(v).padStart(4)} ${k}`).join('\n');
101console.log(`\n${N} cases, ${fails.length} failures, ${silentOk} git-verified successes (mutation hit nothing we read)\n\noutcomes:\n${show(codes)}\n\ndetection site:\n${show(sites)}\n\nmutator → outcome:\n${show(muts)}`);
102process.exit(fails.length ? 1 : 0);
Diagnostics4 git calls · 65 ms
CallStatusSizeTime065 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 · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=55 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=6 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.6 KiB · hit56 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3737 git.response.cache=HIT git.response.duration_ms=56 git.fetch.wants=20f732fa5781ef5181426ccf6df279dc377203f3 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=3737 git.pkt.payload.max=3716 git.sideband.band1.bytes=3715 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
- a5e2de6aeab72308
- Request
- 00e48e6d…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format