GitHubivarvong

ply

main
ply6 git calls260 ms50 KiB3 cachedprotocol v2
1315 lines96 KiBRaw
1// Safety invariants under a hostile/broken server and hostile caller input. Real git behind a fault proxy.
2// I1 Every run terminates (bounded by limits.requestTimeoutMs) with either a correct result or a typed error.
3// I2 On any error, the ref is unchanged — except a fault on the receive-pack *response*, where the ref is
4// either unchanged or exactly our commit (server applied it before the fault); never anything else.
5// I3 A successful result is always git-verified: ref == our commit, content == expected splice, fsck clean.
6// I4 Nothing an untrusted size field says is allocated before it is checked against limits.
7// I5 Caller strings that reach git objects or command lines are validated before any I/O.
8// I6 Secrets never appear in telemetry.
9// I6b A credential a server echoes back reaches no error and no span.
10// I6c A 3xx is never followed: no request (and no Authorization) reaches another origin.
11// I6d URL userinfo is the Basic credential git sends for that URL, byte for byte, and reaches no error, span or URL.
12// I6e Every credential cell (source × user × password × transport) has one stated outcome, git's where git has the input.
13import { test, beforeAll as before, afterAll as after } from 'vitest';
14import assert from 'node:assert/strict';
15import { join } from 'node:path';
16import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
17import { git, gitBuf, gitWithCred, gitUserinfoCred, tmpRoot, makeBareRepo, startGitServer, startAuthGate, commitFiles, fsckProblems, initRepo } from './server.js';
18import { startProxy } from './proxy.js';
19import { MUTATORS, MUTATOR_NAMES } from './fuzz/mutate.js';
20import { Rng } from './fuzz/prng.js';
21import { editFile } from '../src/edit.js';
22import { readFile, listTree } from '../src/read.js';
23import { pushTree } from '../src/tree.js';
24import { checkoutTree } from '../src/checkout.js';
25import { mergeBranch } from '../src/merge.js';
26import { rebaseBranch, cherryPick, revertCommit } from '../src/rebase.js';
27import { GitHttp } from '../src/client.js';
28import { createTransport as createTransportR } from '../src/http.js';
29import { fetchObject as fetchObjectR } from '../src/fetch.js';
30import { fetchPack as fetchPackR, fetchPackTree0 as fetchPackTree0R } from '../src/fetchpack.js';
31import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
32import { withLimits, DEFAULT_LIMITS, LIMIT_OBJECT_OVERHEAD, parseLimits, lowered } from '../src/limits.js';
33import { applyDelta, parsePack, buildPack, encodeObjHeader, packParseOpts, resolvePack, makeDelta } from '../src/pack.js';
34import { hashObject, TYPE } from '../src/objects.js';
35import { Sha1 } from '../src/sha1.js';
36import { concat, te } from '../src/bytes.js';
37import { spawn } from 'node:child_process';
38import http from 'node:http';
39import { inspect } from 'node:util';
40import zlib from 'node:zlib';
41import { adler32 } from '../src/inflate.js';
42import { zlibBound } from '../src/format.js';
43import { pkt, FLUSH } from '../src/pktline.js';
44import { unwrap, unwrapAsync } from './unwrap.js';
46// Throwing adapters over the Result API (0.2.0): the assertions below are about the throw.
47const createTransport = (...a) => unwrap(createTransportR(...a));
48const fetchObject = (...a) => unwrapAsync(fetchObjectR(...a));
49const fetchPack = (...a) => unwrapAsync(fetchPackR(...a));
50const fetchPackTree0 = (...a) => unwrapAsync(fetchPackTree0R(...a));
51const 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']);
52const AUTHOR = { name: 'adv', email: 'adv@x' };
53const FILE = 'a/b/target.txt';
54const ORIG = 'before MARK after\n', FIND = 'MARK', REPL = 'XX', EXPECTED = 'before XX after\n';
56let root, cleanup, srv, n = 0;
57before(async () => { ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root); });
58after(async () => { await srv.close(); cleanup(); });
60function freshRepo() {
61 const name = `adv${n++}`;
62 const bare = makeBareRepo(root, name);
63 const work = join(root, `w${name}`); mkdirSync(work); initRepo(work);
64 commitFiles(work, bare, { [FILE]: ORIG, 'a/sibling.txt': 'x'.repeat(3000), 'c.txt': 'c' }, 'one');
65 const head = commitFiles(work, bare, { 'c.txt': 'c2' }, 'two');
66 return { name, bare, work, head };
69// Run one edit through a proxy with the given fault; return everything needed to check invariants.
70async function run(fault, { limits = withLimits({ requestTimeoutMs: 1500 }), find = FIND, replace = REPL, opts = {} } = {}) {
71 const repo = freshRepo();
72 const proxy = await startProxy(srv.url, fault);
73 const exporter = arrayExporter();
74 const tracer = new Tracer({ exporter });
75 const t0 = performance.now();
76 let result = null, error = null;
77 try { result = await unwrapAsync(editFile(`${proxy.url}/${repo.name}.git`, { path: FILE, find, replace, author: AUTHOR, message: 'adv' }, { tracer, limits, ...opts })); }
78 catch (e) { error = e; }
79 const ms = performance.now() - t0;
80 await proxy.close();
81 const head = git(repo.bare, ['rev-parse', 'main']).trim();
82 const content = gitBuf(repo.bare, ['cat-file', 'blob', `main:${FILE}`]).toString();
83 const spans = exporter.lines.flatMap(flattenLine);
84 rmSync(repo.work, { recursive: true, force: true });
85 return { repo, result, error, ms, head, content, spans, log: proxy.log, exporter };
88function checkInvariants(r, { stage, allowMoved = false, requireError = false }) {
89 assert.ok(r.ms < 10000, `I1: took ${r.ms}ms`);
90 if (r.error) {
91 assert.ok(CODES.has(r.error.code), `I1: untyped error ${r.error.code}: ${r.error.message}\n${r.error.stack}`);
92 const root = r.spans.find(s => s.name === 'git.edit');
93 assert.equal(root.attrs['git.edit.outcome'], r.error.code, 'outcome attr matches error code');
94 if (allowMoved && r.head !== r.repo.head) {
95 assert.equal(r.error.code, 'ambiguous', `I2: ref moved on error → the error must say the outcome is unknown (got ${r.error.code})`);
96 assert.equal(r.content, EXPECTED, 'I2: ref moved → must be exactly our edit');
97 assert.equal(git(r.repo.bare, ['log', '-1', '--format=%P', r.head]).trim(), r.repo.head);
98 } else assert.equal(r.head, r.repo.head, `I2: ref moved on error (${stage}: ${r.error.code})`);
99 } else {
100 assert.ok(!requireError, `expected an error for ${stage}, got success`);
101 assert.equal(r.head, r.result.newCommit, 'I3: ref == our commit');
102 assert.equal(r.content, EXPECTED, 'I3: content');
103 assert.deepEqual(fsckProblems(r.repo.bare), []);
104 }
107const at = (stage, k, spec) => ctx => (ctx.stage === stage && ctx.n === k ? spec : { type: 'pass' });
108const STAGES = [['ls-refs', 0], ['fetch', 0], ['fetch', 1], ['fetch', 3], ['fetch', 4], ['receive-pack', 0]]; // commit, root tree, leaf tree, blob
110test('HTTP-level faults at every stage: statuses, non-git bodies, stall, drop, empty', async () => {
111 const faults = [
112 ['500 html', { type: 'status', status: 500 }, 'upstream_5xx'],
113 ['401', { type: 'status', status: 401 }, 'auth'],
114 ['403', { type: 'status', status: 403 }, 'auth'],
115 ['404', { type: 'status', status: 404 }, 'not_found'],
116 ['429', { type: 'status', status: 429 }, 'rate_limited'],
117 ['200 html login page', { type: 'status', status: 200, body: '<html>sign in</html>' }, 'not_git'],
118 ['200 empty body', { type: 'bytes', bytes: new Uint8Array(0) }, null],
119 ['stall', { type: 'stall' }, 'timeout'],
120 ['stall after partial', { type: 'stall', partial: 10 }, 'timeout'],
121 ['drop', { type: 'drop' }, 'network'],
122 ['drop after partial', { type: 'drop', after: 30 }, null],
123 ];
124 for (const [stage, k] of STAGES) for (const [name, spec, code] of faults) {
125 const r = await run(at(stage, k, spec));
126 const label = `${stage}#${k} ${name}`;
127 checkInvariants(r, { stage: label, requireError: true, allowMoved: stage === 'receive-pack' });
128 if (code) assert.equal(r.error.code, stage === 'receive-pack' ? 'ambiguous' : code, `${label}: ${r.error.message}`);
129 }
130});
132test('receive-pack refused for this caller (403 on its POST and on its advertisement) is auth, and the ref did not move (#144)', async () => {
133 // the proxy answers both receive-pack requests 403 without forwarding: the service is not there for this caller
134 const refuse = ctx => (ctx.stage === 'receive-pack' ? { type: 'bytes', noForward: true, status: 403, contentType: 'text/plain', bytes: new TextEncoder().encode('Forbidden') } : { type: 'pass' });
135 const r = await run(refuse);
136 checkInvariants(r, { stage: 'receive-pack 403', requireError: true, allowMoved: false });
137 assert.equal(r.error.code, 'auth', r.error.message);
138 assert.deepEqual(r.log.filter(l => l.stage === 'receive-pack').map(l => l.n), [0, 1], 'the POST, then the advertisement');
139});
141test('byte-level chunking stress: 1-byte and 7-byte chunks at every stage must succeed', async () => {
142 for (const [stage, k] of STAGES) for (const chunk of [1, 7]) {
143 const r = await run(at(stage, k, { type: 'slow', chunk, delayMs: 0 }), { limits: withLimits({ requestTimeoutMs: 30000 }) });
144 checkInvariants(r, { stage: `${stage}#${k} chunk=${chunk}` });
145 assert.ok(!r.error, `${stage}#${k} chunk=${chunk}: ${r.error?.message}`);
146 }
147});
149test('every protocol-aware mutator × every stage: typed error or git-verified success, never silent corruption', async () => {
150 const rng = new Rng(42);
151 const hist = {};
152 // real pass: mutator receives upstream bytes
153 for (const [stage, k] of STAGES) for (const name of MUTATOR_NAMES) for (let rep = 0; rep < 3; rep++) {
154 const seed = rng.u32();
155 const r = await run(ctx => (ctx.stage === stage && ctx.n === k ? { type: 'bytes', bytes: MUTATORS[name](new Rng(seed), ctx.bytes), name } : { type: 'pass' }));
156 const label = `${stage}#${k} ${name} seed=${seed}`;
157 try { checkInvariants(r, { stage: label, allowMoved: stage === 'receive-pack' }); } catch (e) { e.message = `${label}: ${e.message} | report=${JSON.stringify(r.error?.report?.refs ?? null)}`; throw e; }
158 const key = `${stage}:${r.error ? r.error.code : 'ok'}`;
159 hist[key] = (hist[key] ?? 0) + 1;
160 }
161 console.log(' mutation outcomes:', JSON.stringify(hist));
162 // the silent-success case must only ever be a mutation that touched nothing we read (band 2, trailing flush, etc.)
163});
165test('I4 limits: hostile sizes are rejected before allocation; memory does not grow', async () => {
166 const hdr = n => { const h = new Uint8Array(12); h.set([0x50, 0x41, 0x43, 0x4b]); new DataView(h.buffer).setUint32(4, 2); new DataView(h.buffer).setUint32(8, n); return h; };
167 const sign = body => concat([body, new Sha1().update(body).digest()]);
168 const rssBefore = process.memoryUsage().rss;
169 // object claims 2^40 bytes
170 const p1 = sign(concat([hdr(1), encodeObjHeader(3, 2 ** 40), Uint8Array.from([0x78, 0x9c, 3, 0, 0, 0, 0, 1])]));
171 assert.throws(() => unwrap(parsePack(p1)), e => e.code === 'limit_exceeded');
172 // delta claims 2^40 result
173 assert.throws(() => unwrap(applyDelta(new Uint8Array(1), Uint8Array.from([1, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00]))), e => e.code === 'limit_exceeded');
174 // honest-looking object whose declared size is small but zlib expands past it (bomb): must stop at the declared size
175 const zlib = await import('node:zlib');
176 const bomb = new Uint8Array(zlib.deflateSync(Buffer.alloc(8 << 20)));
177 const p2 = sign(concat([hdr(1), encodeObjHeader(3, 100), bomb]));
178 assert.throws(() => unwrap(parsePack(p2)), e => e.code === 'pack_corrupt' && /exceeds expected/.test(e.message));
179 assert.ok(process.memoryUsage().rss - rssBefore < 64 * 1024 * 1024, 'rss grew');
181 // over the wire: tree fetch answered with the bomb → streaming path stops early; metadata path rejects
182 const r = await run(ctx => (ctx.stage === 'fetch' && ctx.n === 4 ? { type: 'bytes', bytes: wrapPack(sign(concat([hdr(1), encodeObjHeader(3, 100), bomb]))), name: 'blob-bomb' } : { type: 'pass' }));
183 checkInvariants(r, { stage: 'blob bomb', requireError: true });
184 assert.equal(r.error.code, 'pack_corrupt');
185 const blobSpan = r.spans.find(s => s.name === 'git.edit.blob');
186 assert.ok(blobSpan.attrs['git.edit.blob.inflated'] <= 100 + 65536, `stopped early: inflated=${blobSpan.attrs['git.edit.blob.inflated']}`);
188 // flood of refs / shallow lines / pack bytes
189 const refsFlood = concat([...Array.from({ length: 20000 }, (_, i) => pktLine(`${'0'.repeat(39)}${i % 10} refs/heads/b${i}\n`)), te.encode('0000')]);
190 const r2 = await run(at('ls-refs', 0, { type: 'bytes', bytes: refsFlood, name: 'refs-flood' }));
191 checkInvariants(r2, { stage: 'refs flood', requireError: true }); assert.equal(r2.error.code, 'limit_exceeded');
192 const r3 = await run(ctx => (ctx.stage === 'fetch' && ctx.n === 0 ? { type: 'bytes', bytes: MUTATORS.pkt_shallow_flood(new Rng(1), ctx.bytes), name: 'shallow-flood' } : { type: 'pass' }), { limits: withLimits({ requestTimeoutMs: 1500, maxSectionLines: 100 }) });
193 checkInvariants(r3, { stage: 'shallow flood', requireError: true }); assert.equal(r3.error.code, 'limit_exceeded');
194 const big = wrapPack(sign(concat([hdr(1), encodeObjHeader(3, 2 << 20), new Uint8Array(zlib.deflateSync(Buffer.alloc(2 << 20, 7), { level: 0 }))])));
195 const r4 = await run(at('fetch', 1, { type: 'bytes', bytes: big, name: 'pack-flood' }), { limits: withLimits({ requestTimeoutMs: 3000, maxPackBytes: 1 << 20 }) });
196 checkInvariants(r4, { stage: 'pack flood', requireError: true }); assert.equal(r4.error.code, 'limit_exceeded');
197 // server streams more than declared on the blob path
198 const over = wrapPack(sign(concat([hdr(1), encodeObjHeader(3, 10), new Uint8Array(zlib.deflateSync(Buffer.alloc(300000, 65)))])));
199 const r5 = await run(at('fetch', 4, { type: 'bytes', bytes: over, name: 'over-declared' }));
200 checkInvariants(r5, { stage: 'over-declared blob', requireError: true }); assert.equal(r5.error.code, 'pack_corrupt');
201});
203test('I5 caller input validation happens before any I/O', async () => {
204 const cases = [
205 [{ author: { name: 'a\nparent 0000000000000000000000000000000000000000', email: 'a@x' } }, 'author name'],
206 [{ author: { name: 'a <b>', email: 'a@x' } }, 'author name'],
207 [{ author: { name: 'a', email: 'a\nb@x' } }, 'author email'],
208 [{ author: { name: 'a', email: 'a>b@x' } }, 'author email'],
209 [{ ref: 'refs/heads/main\0report-status' }, 'ref'],
210 [{ ref: 'refs/heads/a b' }, 'ref'],
211 [{ ref: 'main' }, 'ref'],
212 [{ ref: 'refs/heads/..' }, 'ref'],
213 [{ ref: 'refs/heads/x.lock' }, 'ref'],
214 [{ path: '../etc/passwd' }, 'path'],
215 [{ path: 'a//b' }, 'path'],
216 [{ path: '/a' }, 'path'],
217 [{ path: 'a/.git/config' }, 'path'],
218 [{ path: '' }, 'path'],
219 [{ message: 'x\0y' }, 'message'],
220 [{ find: '' }, undefined],
221 [{ time: -1 }, undefined],
222 ];
223 for (const [over, what] of cases) {
224 const exporter = arrayExporter();
225 let calls = 0;
226 const fetchImpl = async () => { calls++; throw new Error('should not be called'); };
227 await assert.rejects(unwrapAsync(editFile('http://127.0.0.1:1/x.git', { path: FILE, find: FIND, replace: REPL, author: AUTHOR, message: 'm', ...over }, { tracer: new Tracer({ exporter }), fetchImpl })),
228 e => e.code === 'bad_request' && (what === undefined || e.what === what), JSON.stringify(over));
229 assert.equal(calls, 0, 'no I/O before validation');
230 }
231 // encodeCommit refuses the injected header itself. The same bytes, written without that helper, are still
232 // rejected by receive.fsckObjects: defense in depth if a caller pushes raw objects.
233 const { encodeCommit } = await import('../src/objects.js');
234 assert.throws(() => unwrap(encodeCommit({ tree: 'a'.repeat(40), parents: ['b'.repeat(40)], author: 'x <a@x> 1 +0000', committer: 'y\nparent 0000000000000000000000000000000000000000 <a@x> 1 +0000', message: 'm' })), e => e.code === 'bad_request');
235 const repo = freshRepo();
236 const c = unwrap(GitHttp.create(`${srv.url}/${repo.name}.git`, { tracer: new Tracer() }));
237 const tree = git(repo.bare, ['rev-parse', 'main^{tree}']).trim();
238 const bad = te.encode(`tree ${tree}\nparent ${repo.head}\nauthor x <a@x> 1 +0000\ncommitter y\nparent 0000000000000000000000000000000000000000 <a@x> 1 +0000\n\nm\n`);
239 await assert.rejects(unwrapAsync(c.push({ ref: 'refs/heads/main', oldOid: repo.head, newOid: hashObject('commit', bad), objects: [{ type: TYPE.COMMIT, data: bad }] })), e => e.code === 'push_rejected');
240 assert.equal(git(repo.bare, ['rev-parse', 'main']).trim(), repo.head);
241});
243test('I5 every operation rejects a bad ident field before any fetch', async () => {
244 const good = { name: 'a', email: 'a@x' };
245 const fields = [
246 ['name', { name: 'a\nparent 0000000000000000000000000000000000000000', email: 'a@x' }, 'author name', 1700000000],
247 ['email', { name: 'a', email: 'a\nb@x' }, 'author email', 1700000000],
248 ['time', good, 'time', -1],
249 ['time-huge', good, 'time', Number.MAX_SAFE_INTEGER + 1],
250 ['tz', { ...good, tz: '+0000\nparent 0000000000000000000000000000000000000000' }, 'tz', 1700000000],
251 ['tz-range', { ...good, tz: '+9999' }, 'tz', 1700000000],
252 ];
253 const once = async (label, what, fn) => {
254 let calls = 0;
255 const fetchImpl = async () => { calls++; throw new Error('should not be called'); };
256 await assert.rejects(fn(fetchImpl), e => e.code === 'bad_request' && e.what === what, label);
257 assert.equal(calls, 0, label);
258 };
259 for (const [field, person, what, time] of fields) {
260 await once(`editFile ${field}`, what, fetchImpl => unwrapAsync(editFile('http://127.0.0.1:1/x.git', { path: FILE, find: FIND, replace: REPL, author: person, message: 'm', time }, { fetchImpl })));
261 await once(`mergeBranch ${field}`, what, fetchImpl => unwrapAsync(mergeBranch('http://127.0.0.1:1/x.git', { ref: 'refs/heads/main', from: 'refs/heads/other', author: person, message: 'm', time }, { fetchImpl })));
262 await once(`rebaseBranch ${field}`, what, fetchImpl => unwrapAsync(rebaseBranch('http://127.0.0.1:1/x.git', { ref: 'refs/heads/topic', onto: 'refs/heads/main', committer: person, author: person, time }, { fetchImpl })));
263 await once(`cherryPick ${field}`, what, fetchImpl => unwrapAsync(cherryPick('http://127.0.0.1:1/x.git', { ref: 'refs/heads/main', commit: 'a'.repeat(40), committer: person, time }, { fetchImpl })));
264 await once(`revertCommit ${field}`, what, fetchImpl => unwrapAsync(revertCommit('http://127.0.0.1:1/x.git', { ref: 'refs/heads/main', commit: 'b'.repeat(40), committer: person, time }, { fetchImpl })));
265 }
266 await once('edit message', 'message', fetchImpl => unwrapAsync(editFile('http://127.0.0.1:1/x.git', { path: FILE, find: FIND, replace: REPL, author: good, message: 'x\0y' }, { fetchImpl })));
267 await once('edit ref', 'ref', fetchImpl => unwrapAsync(editFile('http://127.0.0.1:1/x.git', { path: FILE, find: FIND, replace: REPL, author: good, message: 'm', ref: 'refs/heads/main\n' }, { fetchImpl })));
268 await once('merge ref', 'ref', fetchImpl => unwrapAsync(mergeBranch('http://127.0.0.1:1/x.git', { ref: 'refs/heads/main\n', from: 'refs/heads/other', author: good, message: 'm' }, { fetchImpl })));
269 await once('merge message', 'message', fetchImpl => unwrapAsync(mergeBranch('http://127.0.0.1:1/x.git', { ref: 'refs/heads/main', from: 'refs/heads/other', author: good, message: 'x\0y' }, { fetchImpl })));
270 await once('rebase ref', 'ref', fetchImpl => unwrapAsync(rebaseBranch('http://127.0.0.1:1/x.git', { ref: 'refs/heads/topic\n', committer: good }, { fetchImpl })));
271 await once('rebase message', 'message', fetchImpl => unwrapAsync(rebaseBranch('http://127.0.0.1:1/x.git', { ref: 'refs/heads/topic', committer: good, message: 'x\0y' }, { fetchImpl })));
272 await once('cherryPick message', 'message', fetchImpl => unwrapAsync(cherryPick('http://127.0.0.1:1/x.git', { ref: 'refs/heads/main', commit: 'a'.repeat(40), committer: good, message: 'x\0y' }, { fetchImpl })));
273 await once('revertCommit ref', 'ref', fetchImpl => unwrapAsync(revertCommit('http://127.0.0.1:1/x.git', { ref: 'refs/heads/main\n', commit: 'b'.repeat(40), committer: good }, { fetchImpl })));
274});
276test('I6 secrets never reach telemetry: token in auth, token in URL userinfo', async () => {
277 const repo = freshRepo();
278 const exporter = arrayExporter();
279 const tracer = new Tracer({ exporter });
280 const TOKEN = 'ghp_SECRET_TOKEN_1234567890';
281 const r = await unwrapAsync(editFile(`http://user:${TOKEN}@127.0.0.1:${new URL(srv.url).port}/${repo.name}.git`, { path: FILE, find: FIND, replace: REPL, author: AUTHOR, message: 'm' }, { tracer, auth: { token: TOKEN } }));
282 assert.equal(git(repo.bare, ['rev-parse', 'main']).trim(), r.newCommit);
283 const dump = JSON.stringify(exporter.lines);
284 assert.ok(!dump.includes(TOKEN), 'token leaked into spans');
285 assert.ok(!dump.includes(btoa(`x-access-token:${TOKEN}`)), 'basic auth leaked');
286 assert.ok(!dump.includes('user:'), 'userinfo leaked');
287});
289// ---- I6b/I6c: echoed credentials are scrubbed everywhere; redirects are never followed ----
291// Every string reachable from `root` (own keys, symbols, non-enumerable message/stack/cause, nested objects, functions),
292// so a check sees what JSON.stringify(err) would miss.
293function reachableText(root) {
294 const out = []; const seen = new Set();
295 const walk = v => {
296 if (typeof v === 'string') { out.push(v); return; }
297 if (v === null || (typeof v !== 'object' && typeof v !== 'function') || seen.has(v)) return;
298 seen.add(v);
299 if (ArrayBuffer.isView(v)) { out.push(Buffer.from(v.buffer, v.byteOffset, v.byteLength).toString('latin1')); return; }
300 if (v instanceof Map) { for (const [k, x] of v) { walk(k); walk(x); } return; }
301 for (const k of Reflect.ownKeys(v)) { let x; try { x = v[k]; } catch { continue; } walk(x); }
302 };
303 walk(root);
304 return out.join('\n');
307const TOK = 'ghp_EchoedToken0123456789abcdefXYZ';
308const PASS = 'glpat-EchoedPassword9876543210';
309const USER = 'deploy-user-4711-secretname';
310const AUTHS = [
311 { name: 'token', auth: { token: TOK }, secrets: [TOK, btoa(`x-access-token:${TOK}`)] },
312 { name: 'user+token', auth: { user: USER, token: TOK }, secrets: [TOK, USER, btoa(`${USER}:${TOK}`)] },
313 { name: 'header Basic', auth: { header: `Basic ${btoa(`${USER}:${PASS}`)}` }, secrets: [PASS, USER, btoa(`${USER}:${PASS}`)] },
314 { name: 'bearer', auth: { bearer: TOK }, secrets: [TOK] },
315];
316const band = (n, text) => unwrap(pkt(concat([new Uint8Array([n]), typeof text === 'string' ? te.encode(text) : text])));
317const once = (stage, k, make) => ctx => (ctx.stage === stage && ctx.n === k && ctx.phase === 'request' ? { type: 'bytes', noForward: true, name: 'echo', bytes: make() } : null);
318// A v2 fetch answer whose band-2 progress carries `text` split across two frames, spliced after the packfile header.
319const withProgress = text => ctx => {
320 if (ctx.stage !== 'fetch' || ctx.n !== 0 || ctx.phase === 'request') return { type: 'pass' };
321 const hdr = Buffer.from(unwrap(pkt('packfile\n'))); const b = Buffer.from(ctx.bytes); const at = b.indexOf(hdr) + hdr.length;
322 const half = text.length >> 1;
323 return { type: 'bytes', name: 'progress-echo', bytes: concat([b.subarray(0, at), band(2, text.slice(0, half)), band(2, text.slice(half)), b.subarray(at)]) };
324};
325const report = (...lines) => concat([band(1, concat([...lines.map(l => unwrap(pkt(`${l}\n`))), FLUSH]))]);
326const ECHOES = [
327 { name: 'ls-refs ERR', code: 'remote_error', fault: s => once('ls-refs', 0, () => concat([unwrap(pkt(`ERR access denied for ${s}\n`)), FLUSH])) },
328 { name: 'ls-refs malformed line (quoted, cut at 60)', code: 'protocol', fault: s => once('ls-refs', 0, () => concat([unwrap(pkt(`bogus-${s}\n`)), FLUSH])) },
329 { name: 'fetch ERR', code: 'remote_error', fault: s => once('fetch', 0, () => concat([unwrap(pkt(`ERR upload-pack: ${s}\n`)), FLUSH])) },
330 { name: 'fetch band 3 (commit)', code: 'remote_error', fault: s => once('fetch', 0, () => concat([unwrap(pkt('packfile\n')), band(3, `fatal: ${s}\n`), FLUSH])) },
331 { name: 'fetch band 3 (streamed blob)', code: 'remote_error', fault: s => once('fetch', 4, () => concat([unwrap(pkt('packfile\n')), band(3, `fatal: ${s}\n`), FLUSH])) },
332 { name: 'push ng reason + band-2 hook output', code: 'push_rejected', fault: s => once('receive-pack', 0, () => concat([band(2, `remote: hook saw ${s}\n`), report('unpack ok', `ng refs/heads/main pre-receive hook declined: ${s}`), FLUSH])) },
333 { name: 'push band-2 tail cut through the secret', code: 'push_rejected', fault: s => once('receive-pack', 0, () => concat([band(2, `remote: ${s}`), band(2, `${'x'.repeat(4096 - 20)}\n`), report('unpack ok', 'ng refs/heads/main pre-receive hook declined'), FLUSH])) },
334 { name: 'push band 3', code: 'ambiguous', fault: s => once('receive-pack', 0, () => concat([band(3, `error: ${s}\n`), FLUSH])) },
335 { name: 'push malformed report + hook output', code: 'ambiguous', fault: s => once('receive-pack', 0, () => concat([band(2, `remote: ${s}\n`), report('unpack ok', 'ok refs/heads/elsewhere'), FLUSH])) },
336 { name: 'fetch band-2 progress (success)', code: null, fault: withProgress },
337];
339test('I6b a credential echoed by the server reaches no error (any field) and no span: ERR, band 2/3, ng, hook output × 4 auth forms', async () => {
340 let cases = 0;
341 for (const { name: form, auth, secrets } of AUTHS) for (const echo of ECHOES) {
342 const said = `${secrets.join(' | ')} :: ${secrets.join('')}`;
343 const r = await run(echo.fault(said), { opts: { auth } });
344 const where = `${echo.name} / ${form}`;
345 let text = '';
346 if (echo.code === null) { assert.equal(r.error, null, `${where}: ${r.error?.message}`); assert.match(JSON.stringify(r.exporter.lines), /\[redacted\]/, `${where}: the echo reached a span`); }
347 else {
348 assert.equal(r.error?.code, echo.code, `${where}: ${r.error?.code} ${r.error?.message}`);
349 assert.match(reachableText(r.error), /\[redacted\]/, `${where}: the echo reached the error`);
350 text = `${reachableText(r.error)}\n${String(r.error)}\n${inspect(r.error, { depth: 20, showHidden: true })}`;
351 }
352 const dump = JSON.stringify(r.exporter.lines);
353 // whole, and as any 12-character run (a secret cut by a tail or a quote)
354 for (const s of secrets) for (const frag of [s, ...Array.from({ length: s.length - 11 }, (_, i) => s.slice(i, i + 12))]) {
355 assert.ok(!text.includes(frag), `${where}: ${JSON.stringify(frag)} in the error: ${r.error?.message}`);
356 assert.ok(!dump.includes(frag), `${where}: ${JSON.stringify(frag)} in a span`);
357 }
358 cases++;
359 }
360 assert.equal(cases, AUTHS.length * ECHOES.length);
361});
363test('I6b auth.user is scrubbed only when secret-shaped: a conventional user name in server text survives', async () => {
364 const r = await run(once('ls-refs', 0, () => concat([unwrap(pkt(`ERR denied to x-access-token and git (token ${TOK})\n`)), FLUSH])), { opts: { auth: { user: 'x-access-token', token: TOK } } });
365 assert.equal(r.error.code, 'remote_error');
366 assert.match(r.error.message, /denied to x-access-token and git \(token \[redacted\]\)/);
367 assert.ok(!JSON.stringify(r.exporter.lines).includes(TOK));
368});
370// A server that answers every request with `status` + Location: `to`, and one that records what reaches it.
371async function listen(handler) {
372 const s = http.createServer(handler); await new Promise(ok => s.listen(0, '127.0.0.1', ok));
373 return { url: `http://127.0.0.1:${s.address().port}`, close: () => new Promise(ok => { s.closeAllConnections?.(); s.close(ok); }) };
376test('I6c a 3xx is never followed: cross-origin on the GET and the POST → typed http error; the other origin sees nothing', async () => {
377 const seen = [];
378 const other = await listen((req, res) => { seen.push({ method: req.method, url: req.url, authorization: req.headers.authorization ?? null }); res.writeHead(200, { 'content-type': 'application/x-git-upload-pack-advertisement' }); res.end('0000'); });
379 let status = 302;
380 const redirector = await listen((req, res) => { res.writeHead(status, { location: `${other.url}${req.url}`, 'content-type': 'text/html' }); res.end(`moved; token ${TOK}`); });
381 try {
382 for (const s of [301, 302, 303, 307, 308]) {
383 status = s;
384 const ex = arrayExporter();
385 const c = unwrap(GitHttp.create(`${redirector.url}/r.git`, { tracer: new Tracer({ exporter: ex }), auth: { token: TOK } }));
386 const get = await unwrapAsync(c.discover()).catch(e => e);
387 assert.equal(get.code, 'http', `GET ${s}: ${get.code} ${get.message}`); assert.equal(get.status, s);
388 assert.match(get.message, new RegExp(`^HTTP ${s} git-upload-pack advertisement: redirected to ${other.url}/r.git/info/refs; ply does not follow redirects`));
389 const post = await unwrapAsync(c.lsRefs(['refs/heads/'])).catch(e => e);
390 assert.equal(post.code, 'http', `POST ${s}: ${post.code} ${post.message}`); assert.equal(post.status, s);
391 assert.match(post.message, /redirected to .*\/r\.git\/git-upload-pack; ply does not follow redirects/);
392 const edit = await unwrapAsync(editFile(`${redirector.url}/r.git`, { path: FILE, find: FIND, replace: REPL, author: AUTHOR, message: 'm' }, { tracer: new Tracer({ exporter: ex }), auth: { token: TOK } })).catch(e => e);
393 assert.equal(edit.code, 'http', `edit ${s}: ${edit.code} ${edit.message}`);
394 assert.ok(!JSON.stringify(ex.lines).includes(TOK));
395 }
396 assert.deepEqual(seen, [], 'the redirect target received requests');
397 } finally { await redirector.close(); await other.close(); }
398});
400test('I6c a same-origin 301 on the POST (a moved repository) is a clear http error, not a content-type error', async () => {
401 const hits = [];
402 const srv2 = await listen((req, res) => {
403 hits.push(`${req.method} ${req.url}`);
404 if (req.url.startsWith('/old.git/')) { res.writeHead(301, { location: req.url.replace('/old.git/', '/new.git/') }); res.end(); return; }
405 res.writeHead(200, { 'content-type': 'text/html' }); res.end('<html>repository page</html>');
406 });
407 try {
408 const err = await unwrapAsync(readFile(`${srv2.url}/old.git`, { path: 'README.md' }, { tracer: new Tracer(), auth: { token: TOK } })).catch(e => e);
409 assert.equal(err.code, 'http', `${err.code} ${err.message}`); assert.equal(err.status, 301);
410 assert.equal(err.message, `HTTP 301 git-upload-pack: redirected to ${srv2.url}/new.git/git-upload-pack; ply does not follow redirects (update the remote URL)`);
411 assert.equal(err.location, `${srv2.url}/new.git/git-upload-pack`);
412 assert.deepEqual(hits, ['POST /old.git/git-upload-pack']);
413 } finally { await srv2.close(); }
414});
416test('I6c a Location carrying a query or the credential is quoted as origin + path, scrubbed', async () => {
417 const srv2 = await listen((req, res) => { res.writeHead(302, { location: `https://elsewhere.example/${TOK}/x?sig=${TOK}` }); res.end(); });
418 try {
419 const err = await unwrapAsync(unwrap(GitHttp.create(`${srv2.url}/r.git`, { tracer: new Tracer(), auth: { token: TOK } })).lsRefs()).catch(e => e);
420 assert.equal(err.code, 'http');
421 assert.equal(err.location, 'https://elsewhere.example/[redacted]/x');
422 assert.ok(!reachableText(err).includes(TOK));
423 } finally { await srv2.close(); }
424});
426// Every ref line of an ls-refs response with `extra` appended: attributes a hostile server adds after the real ones.
427const appendLsRefAttrs = (bytes, extra) => {
428 const out = []; let o = 0;
429 while (o < bytes.length) {
430 const len = parseInt(new TextDecoder().decode(bytes.subarray(o, o + 4)), 16);
431 if (len < 4) { out.push(bytes.subarray(o, o + 4)); o += 4; continue; }
432 out.push(unwrap(pkt(`${new TextDecoder().decode(bytes.subarray(o + 4, o + len)).replace(/\n$/, '')} ${extra}\n`))); o += len;
433 }
434 return concat(out);
435};
437// ---- I6d (#32): URL userinfo is git's credential, byte for byte; it is scrubbed like any other ----
439// Tokens of the shapes hosts issue (random parts are fixed here): a classic GitHub PAT, a fine-grained one, a GitLab PAT
440// with no digit (the ≥16-with-a-digit user rule alone would leave it readable when it is the user).
441const GHP = 'ghp_R4nd0mClassicTokenAbcdefGhijklmnop12';
442const GHFG = 'github_pat_11ABCDEFG0abcdefghijkl_MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJ';
443const GLPAT = 'glpat-AbcdEfghIjklMnopQrst';
444const USERINFO = [ // [name, userinfo as written in the URL, the secrets in it]
445 ['user only (ghp_)', `${GHP}@`, [GHP]],
446 ['user only (fine-grained)', `${GHFG}@`, [GHFG]],
447 ['user only (glpat, no digit)', `${GLPAT}@`, [GLPAT]],
448 ['user: (empty password)', `${GHP}:@`, [GHP]],
449 ['user:password', `alice:${encodeURIComponent('s3cret/Pass:word@0123456789')}@`, ['s3cret/Pass:word@0123456789']],
450 [':password', `:${GHP}@`, [GHP]],
451 ['percent-encoded user only', 'to%40ken%3Aabcdefghij0123456789@', ['to@ken:abcdefghij0123456789']],
452 ['UTF-8 user only', 't%C3%A9k%E2%82%ACabcdefghij0123456789@', ['ték€abcdefghij0123456789']],
453 ['invalid escape kept (%ZZ)', 'to%ZZkenabcdefghij0123456789@', ['to%ZZkenabcdefghij0123456789']],
454 ['raw byte (%FF)', 'tok%FFabcdefghij0123456789@', ['tok\uFFFDabcdefghij0123456789']],
455];
457test('I6d URL userinfo authenticates fetch and push with the Authorization git sends for the same URL, byte for byte', async () => {
458 const gate = await startAuthGate(srv.url);
459 try {
460 for (const [name, ui, secrets] of USERINFO) {
461 const repo = freshRepo();
462 const url = `http://${ui}${new URL(gate.url).host}/${repo.name}.git`;
463 // git: the userinfo decoded by git, then ls-remote and push through the gate; its credentialed requests carry
464 // one Authorization value
465 const { cred, config, refused } = await gitUserinfoCred(repo.work, url);
466 assert.equal(refused, undefined, `${name}: git credential fill: ${refused}`);
467 gate.seen.length = 0;
468 const ls = await gitWithCred(repo.work, ['ls-remote', `${gate.url}/${repo.name}.git`], cred, config);
469 assert.equal(ls.status, 0, `${name}: git ls-remote: ${ls.stderr}`);
470 const push = await gitWithCred(repo.work, ['push', '-q', `${gate.url}/${repo.name}.git`, 'HEAD:refs/heads/by-git'], cred, config);
471 assert.equal(push.status, 0, `${name}: git push: ${push.stderr}`);
472 const gitAuth = [...new Set(gate.seen.map(r => r.authorization).filter(Boolean))];
473 assert.equal(gitAuth.length, 1, `${name}: git sent ${JSON.stringify(gitAuth)}`);
474 // ply: a read (fetch) and an edit (fetch + push) through the same gate; every request carries git's value
475 gate.seen.length = 0;
476 const exporter = arrayExporter(); const tracer = new Tracer({ exporter });
477 const read = await unwrapAsync(readFile(url, { path: FILE }, { tracer }));
478 const parts = []; for await (const c of read.stream) parts.push(c);
479 assert.equal(new TextDecoder().decode(concat(parts)), ORIG, `${name}: read`);
480 const r = await unwrapAsync(editFile(url, { path: FILE, find: FIND, replace: REPL, author: AUTHOR, message: 'm' }, { tracer }));
481 assert.equal(git(repo.bare, ['rev-parse', 'main']).trim(), r.newCommit, `${name}: push landed`);
482 assert.ok(gate.seen.some(x => x.path.endsWith('/git-receive-pack')), `${name}: ply pushed through the gate`);
483 assert.deepEqual([...new Set(gate.seen.map(x => x.authorization))], gitAuth, `${name}: ply's Authorization`);
484 // the userinfo is nowhere in the export, and every URL attribute is the credential-free URL
485 const dump = JSON.stringify(exporter.lines);
486 for (const s of [...secrets, ui, gitAuth[0], gitAuth[0].slice(6)]) assert.ok(!dump.includes(s), `${name}: ${JSON.stringify(s)} in a span`);
487 const urls = exporter.lines.flatMap(flattenLine).flatMap(sp => Object.entries(sp.attrs).filter(([k]) => /url/.test(k)).map(([, v]) => v));
488 assert.ok(urls.length >= 3, `${name}: url attributes recorded`);
489 for (const v of urls) assert.ok(v.startsWith(`${gate.url}/${repo.name}.git`) && !v.includes('@'), `${name}: url attribute ${v}`);
490 }
491 } finally { await gate.close(); }
492});
494test('I6d a refused URL credential is `auth`; the server echoing it reaches no error and no span (a short user name stays readable)', async () => {
495 const gate = await startAuthGate(srv.url, () => false); // refuses everything, quoting the header and its decoded form
496 try {
497 for (const [name, ui, secrets] of USERINFO) {
498 const exporter = arrayExporter(); const tracer = new Tracer({ exporter });
499 const url = `http://${ui}${new URL(gate.url).host}/r.git`;
500 const errs = [
501 await unwrapAsync(readFile(url, { path: FILE }, { tracer })).catch(e => e),
502 await unwrapAsync(editFile(url, { path: FILE, find: FIND, replace: REPL, author: AUTHOR, message: 'm' }, { tracer })).catch(e => e),
503 await unwrapAsync(unwrap(GitHttp.create(url, { tracer })).discover()).catch(e => e),
504 ];
505 const said = gate.seen.at(-1).authorization;
506 assert.ok(said, `${name}: no Authorization sent`);
507 for (const e of errs) {
508 assert.equal(e.code, 'auth', `${name}: ${e.code} ${e.message}`);
509 const text = `${reachableText(e)}\n${String(e)}\n${inspect(e, { depth: 20, showHidden: true })}`;
510 for (const s of [...secrets, said, said.slice(6), ui]) for (const frag of [s, ...Array.from({ length: Math.max(0, s.length - 11) }, (_, i) => s.slice(i, i + 12))]) {
511 assert.ok(!text.includes(frag), `${name}: ${JSON.stringify(frag)} in the error: ${e.message}`);
512 }
513 }
514 assert.match(errs[0].message, /\[redacted\]/, `${name}: the echo was scrubbed`);
515 const dump = JSON.stringify(exporter.lines);
516 for (const s of [...secrets, said.slice(6), ui]) assert.ok(!dump.includes(s), `${name}: ${JSON.stringify(s)} in a span`);
517 }
518 // `git@` and `ivar@` alone are names: sent as `git:` / `ivar:` like git does, and not scrubbed from server text
519 const e = await unwrapAsync(readFile(`http://ivar@${new URL(gate.url).host}/r.git`, { path: FILE }, { tracer: new Tracer() })).catch(x => x);
520 assert.equal(gate.seen.at(-1).authorization, `Basic ${btoa('ivar:')}`);
521 assert.equal(e.code, 'auth'); assert.match(e.message, /\(ivar:\)/);
522 } finally { await gate.close(); }
523});
525test('I6d unusable userinfo or a malformed URL is bad_request before any request, and the error carries no credential', async () => {
526 const cases = [['LF', `${GHP}%0A@`, 'url userinfo'], ['CR', `${GHP}%0D@`, 'url userinfo'], ['NUL', `x:${GHP}%00@`, 'url userinfo']];
527 for (const [name, ui, what] of cases) {
528 let calls = 0;
529 const fetchImpl = async () => { calls++; throw new Error('should not be called'); };
530 const e = await unwrapAsync(readFile(`http://${ui}127.0.0.1:1/r.git`, { path: FILE }, { tracer: new Tracer(), fetchImpl })).catch(x => x);
531 assert.equal(e.code, 'bad_request', `${name}: ${e.code} ${e.message}`); assert.equal(e.what, what);
532 assert.equal(calls, 0, name);
533 assert.ok(!reachableText(e).includes(GHP.slice(0, 12)), `${name}: credential in the error`);
534 }
535 // git refuses LF and NUL here and sends CR; ply refuses all three. A URL that does not parse: Node's TypeError
536 // carries what it was given (`input`), credential and all.
537 const e = (() => { try { createTransport(`http://${GHP}@127.0.0.1:99999/r.git`); } catch (x) { return x; } })();
538 assert.equal(e?.code, 'bad_request'); assert.equal(e.what, 'url');
539 assert.ok(!reachableText(e).includes(GHP.slice(0, 12)) && !inspect(e, { showHidden: true }).includes(GHP.slice(0, 12)), 'credential in the error');
540});
542// ---- I6e (#40): the credential product space ----
543// Every (source × user × password) cell has one expected outcome: the exact Authorization, anonymous, or `bad_request`
544// before any request. git is the oracle wherever git has the same input: URL userinfo as written; explicit fields as a
545// credential helper's `username=`/`password=`; an explicit header as `http.extraHeader`. In no cell does a secret, or
546// any 12 characters of one, reach an error, a span, a URL attribute or `t.url`. (The ssh:// cells left with the SSH
547// transport, 2026-10-31; history on branch archive/ssh.)
548const CRED_USER = { absent: undefined, short: 'ivar', secret: GHP, utf8: 'tëst', 'ctl-lf': 'iv\nar', 'ctl-soh': 'iv\x01ar' };
549const CRED_PASS = { absent: undefined, empty: '', ascii: 's3cret/Pass:word@0123456789', utf8: 'pässwörd€-0123456789' };
550const basicOf = (user, pass) => `Basic ${Buffer.from(`${user}:${pass}`, 'utf8').toString('base64')}`;
551const userinfoOf = (user, pass) => user === undefined && pass === undefined ? '' : `${encodeURIComponent(user ?? '')}${pass === undefined ? '' : `:${encodeURIComponent(pass)}`}@`;
552const fragments = s => s.length < 12 ? [s] : [s, ...Array.from({ length: s.length - 11 }, (_, i) => s.slice(i, i + 12))];
554// The expected outcome of a cell, stated without running anything.
555// { header } every request carries exactly this Authorization
556// { anonymous } no Authorization (and so the gate's 401: `auth`)
557// { refused } `bad_request`, zero requests
558function expectCell(source, uk, pk) {
559 const user = CRED_USER[uk], pass = CRED_PASS[pk];
560 if (source === 'url') {
561 if (user === undefined && pass === undefined) return { anonymous: true };
562 if (uk === 'ctl-lf') return { refused: 'url userinfo' }; // git: "credential url cannot be parsed"
563 return { header: basicOf(user ?? '', pass ?? '') }; // as written, percent-decoded to UTF-8 bytes (#39)
564 }
565 if (source === 'fields') {
566 if (uk === 'ctl-lf') return { refused: 'auth user' };
567 if (pass === undefined) return { refused: 'auth' }; // { user } alone, or {}: git's helper API asks for the password
568 if (pass === '' && user === undefined) return { refused: 'auth' }; // { token: '' }: nothing to send
569 return { header: basicOf(user ?? 'x-access-token', pass) }; // UTF-8 bytes, as git sends a helper's
570 }
571 if (source === 'header') {
572 if (user === undefined && pass === undefined) return { refused: 'auth' }; // header: ''
573 return { header: basicOf(user ?? '', pass ?? '') }; // verbatim
574 }
575 throw new Error(`unknown source ${source}`);
577const authFor = (source, user, pass) => {
578 if (source === 'fields') return { ...(user !== undefined && { user }), ...(pass !== undefined && { token: pass }) };
579 if (source === 'header') return { header: user === undefined && pass === undefined ? '' : basicOf(user ?? '', pass ?? '') };
580 return undefined;
581};
582// git with the same input: the Authorization values it sent (null for none) and its exit status. No credential is in
583// the URL git dials (see gitWithCred): URL userinfo is decoded by git (`git credential fill`) and handed to its helper;
584// explicit fields are the helper's `username=`/`password=`; an explicit header is `http.extraHeader`.
585async function gitHttpOracle(gate, work, source, user, pass, repoPath) {
586 const plain = `${gate.url}${repoPath}`;
587 let cred = null; const config = [];
588 if (source === 'url') {
589 if (userinfoOf(user, pass)) {
590 const d = await gitUserinfoCred(work, `http://${userinfoOf(user, pass)}${new URL(gate.url).host}${repoPath}`);
591 if (d.refused) return { status: null, sent: [], stderr: d.refused };
592 cred = d.cred; config.push(...d.config);
593 }
594 } else if (source === 'fields') {
595 const u = user ?? (pass ? 'x-access-token' : undefined); // ply's documented default user
596 cred = Buffer.from(`${u === undefined ? '' : `username=${u}\n`}${pass === undefined ? '' : `password=${pass}\n`}`, 'utf8');
597 } else config.push(['http.extraHeader', `Authorization: ${authFor('header', user, pass).header}`]);
598 gate.seen.length = 0;
599 const r = await gitWithCred(work, ['ls-remote', plain], cred, config);
600 return { status: r.status, sent: [...new Set(gate.seen.map(x => x.authorization).filter(Boolean))], stderr: r.stderr };
603test('I6e the credential product: every source × user × password cell gets its stated header or typed refusal, equal to git where git has the input, and leaks nothing', async () => {
604 const accept = await startAuthGate(srv.url);
605 const refuse = await startAuthGate(srv.url, () => false); // 401 echoing the header and its decoded form
606 const repo = freshRepo();
607 const repoPath = `/${repo.name}.git`;
608 const tally = { header: 0, anonymous: 0, refused: 0, oracle: 0 };
609 try {
610 for (const source of ['url', 'fields', 'header']) for (const uk of Object.keys(CRED_USER)) for (const pk of Object.keys(CRED_PASS)) {
611 const cell = `${source} × user ${uk} × password ${pk}`;
612 const user = CRED_USER[uk], pass = CRED_PASS[pk];
613 const want = expectCell(source, uk, pk);
614 const secrets = [...(pass && pass.length >= 4 ? [pass] : []), ...(uk === 'secret' ? [GHP] : []), ...(want.header ? [want.header, want.header.slice(6)] : [])];
615 const noLeak = (text, where) => { for (const s of secrets) for (const f of fragments(s)) assert.ok(!text.includes(f), `${cell}: ${JSON.stringify(f)} in ${where}`); };
616 const errText = e => `${reachableText(e)}\n${String(e)}\n${inspect(e, { depth: 20, showHidden: true })}`;
618 const auth = authFor(source, user, pass);
619 const urlOf = gate => `http://${source === 'url' ? userinfoOf(user, pass) : ''}${new URL(gate.url).host}${repoPath}`;
620 // 1. ply through the accepting gate
621 accept.seen.length = 0;
622 const exporter = arrayExporter(); const tracer = new Tracer({ exporter });
623 const got = await unwrapAsync(readFile(urlOf(accept), { path: FILE }, { tracer, auth })).then(async r => { const parts = []; for await (const c of r.stream) parts.push(c); return new TextDecoder().decode(concat(parts)); }, e => e);
624 const spans = exporter.lines.flatMap(flattenLine);
625 if (want.refused) {
626 assert.equal(got?.code, 'bad_request', `${cell}: ${got?.code} ${got?.message}`); assert.equal(got.what, want.refused, cell);
627 assert.equal(accept.seen.length, 0, `${cell}: requests were sent`);
628 // #40.3: the root span is ended with the error recorded, and the caller can link to it
629 const rootSpan = spans.find(sp => sp.name === 'git.read');
630 assert.ok(rootSpan, `${cell}: root span not exported`);
631 assert.equal(rootSpan.attrs['git.read.outcome'], 'bad_request', cell); assert.equal(rootSpan.status.code, 2, cell);
632 assert.equal(got.span?.name, 'git.read', cell);
633 noLeak(errText(got), 'the error');
634 tally.refused++;
635 } else if (want.anonymous) {
636 assert.equal(got?.code, 'auth', `${cell}: ${got?.code}`);
637 assert.deepEqual([...new Set(accept.seen.map(x => x.authorization))], [null], cell);
638 tally.anonymous++;
639 } else {
640 assert.equal(got, ORIG, `${cell}: read failed: ${got?.code} ${got?.message}`);
641 assert.deepEqual([...new Set(accept.seen.map(x => x.authorization))], [want.header], `${cell}: ply's Authorization`);
642 tally.header++;
643 }
644 noLeak(JSON.stringify(exporter.lines), 'a span');
645 for (const sp of spans) for (const [k, v] of Object.entries(sp.attrs)) if (/url/.test(k)) assert.ok(String(v).startsWith(`${accept.url}${repoPath}`) && !String(v).includes('@'), `${cell}: ${k} = ${v}`);
646 if (!want.refused) { const t = createTransport(urlOf(accept), { auth }); assert.equal(t.url, `${accept.url}${repoPath}`, cell); noLeak(t.url, 't.url'); }
647 // 2. git with the same input, through the same gate
648 if (!(source === 'fields' && uk === 'ctl-lf') && !(source === 'header' && want.refused)) {
649 const g = await gitHttpOracle(accept, repo.work, source, user, pass, repoPath);
650 if (want.header) { assert.equal(g.status, 0, `${cell}: git: ${g.stderr}`); assert.deepEqual(g.sent, [want.header], `${cell}: git's Authorization`); }
651 else assert.deepEqual(g.sent, [], `${cell}: git sent ${JSON.stringify(g.sent)} where ply sends nothing`);
652 if (want.refused && source === 'url') assert.match(g.stderr, /credential url cannot be parsed/, `${cell}: git refuses the userinfo: ${g.stderr}`);
653 tally.oracle++;
654 }
655 // 3. a refusing server that echoes the credential: `auth`, and the echo is scrubbed everywhere
656 if (want.header) {
657 const ex2 = arrayExporter();
658 const e = await unwrapAsync(readFile(urlOf(refuse), { path: FILE }, { tracer: new Tracer({ exporter: ex2 }), auth })).catch(x => x);
659 assert.equal(e.code, 'auth', `${cell}: ${e.code} ${e.message}`);
660 assert.equal(refuse.seen.at(-1).authorization, want.header, cell);
661 if (secrets.length > 2) assert.match(e.message, /\[redacted\]/, `${cell}: the echo was not scrubbed: ${e.message}`);
662 noLeak(errText(e), 'the echoed error'); noLeak(JSON.stringify(ex2.lines), 'an echoed span');
663 }
664 }
665 // 3 sources × 6 users × 4 passwords = 72 cells: url 19 headers + 1 anonymous + 4 refused (LF); fields 14 headers +
666 // 10 refused (LF ×4, no token ×5, an empty token alone); header 23 + 1 refused (empty). git ran on 67: every cell but
667 // the 4 a helper cannot carry (LF) and the refused header.
668 assert.deepEqual(tally, { header: 56, anonymous: 1, refused: 15, oracle: 67 }, JSON.stringify(tally));
669 } finally { await accept.close(); await refuse.close(); }
670});
672test('I6e an Authorization header or a Bearer token that fetch could not send is bad_request before any request', async () => {
673 for (const [name, auth] of [['header, non-ASCII', { header: 'Bearer tök€n-0123456789' }], ['header, control', { header: 'Bearer t\x01k-0123456789' }], ['header, LF', { header: 'Bearer t\nk-0123456789' }],
674 ['bearer, non-ASCII', { bearer: 'tök€n-0123456789' }], ['bearer, control', { bearer: 't\x7fk-0123456789' }], ['bearer, empty', { bearer: '' }]]) {
675 let calls = 0;
676 const exporter = arrayExporter();
677 const e = await unwrapAsync(readFile('http://127.0.0.1:1/r.git', { path: FILE }, { tracer: new Tracer({ exporter }), auth, fetchImpl: async () => { calls++; throw new Error('sent'); } })).catch(x => x);
678 assert.equal(e.code, 'bad_request', `${name}: ${e.code} ${e.message}`); assert.equal(calls, 0, name);
679 assert.ok(!reachableText(e).includes('0123456789') && !JSON.stringify(exporter.lines).includes('0123456789'), `${name}: echoed`);
680 }
681});
683test('#16 ls-refs attributes never override the parsed oid or name: oid:/name: extras leave the edit on the real head', async () => {
684 for (const extra of ['oid:junk name:refs/heads/other', `oid:${'1'.repeat(40)}`, 'name:refs/heads/other', 'name:HEAD oid:junk __proto__:x constructor:y']) {
685 const r = await run(ctx => (ctx.stage === 'ls-refs' && ctx.phase !== 'request' ? { type: 'bytes', bytes: appendLsRefAttrs(ctx.bytes, extra) } : { type: 'pass' }));
686 assert.equal(r.error, null, `${extra}: ${r.error?.code} ${r.error?.message}`);
687 assert.ok(r.log.some(l => l.stage === 'ls-refs' && l.spec === 'bytes'), `${extra}: the ls-refs response was rewritten`);
688 checkInvariants(r, { stage: `ls-refs + ${extra}` });
689 assert.equal(git(r.repo.bare, ['log', '-1', '--format=%P', r.head]).trim(), r.repo.head, `${extra}: parent is the real head`);
690 }
691});
693// #18: the merge-base walk's commit-only packs, lied about. Commits are hash-verified, so ancestry can only be withheld.
694// P ← X ← M is main; feat = merge(Q, y10) with Q ← P and y10 ← … ← y1 ← X. git's base is X (via y1); with y1..y3 cut
695// the only common ancestor in view is P. X sets g=2 and y1 sets it back to 1: base X → g=1 (git), base P → g=2.
696function cutHistoryRepo() {
697 const name = `cut${n++}`; const bare = makeBareRepo(root, name); const work = join(root, `w${name}`); mkdirSync(work); initRepo(work);
698 const g = args => git(work, ['-c', 'user.name=c', '-c', 'user.email=c@x', ...args]).trim();
699 const put = (files, msg) => { for (const [p, v] of Object.entries(files)) writeFileSync(join(work, p), v); g(['add', '-A']); g(['commit', '-q', '--allow-empty', '-m', msg]); return g(['rev-parse', 'HEAD']); };
700 const P = put({ g: '1\n', f: 'f\n' }, 'P'), X = put({ g: '2\n' }, 'X');
701 const y = [null, put({ g: '1\n' }, 'y1')]; for (let i = 2; i <= 10; i++) y.push(put({ [`y${i}`]: `${i}\n` }, `y${i}`));
702 g(['checkout', '-q', '--detach', X]); const M = put({ m: 'm\n' }, 'M');
703 g(['checkout', '-q', '--detach', P]); const Q = put({ q: 'q\n' }, 'Q'); g(['merge', '-q', '--no-edit', y[10]]); const S = g(['rev-parse', 'HEAD']);
704 g(['push', '-q', bare, `${M}:refs/heads/main`, `${S}:refs/heads/feat`, `${y[10]}:refs/heads/lin`]);
705 rmSync(work, { recursive: true, force: true });
706 return { name, bare, P, X, M, Q, S, y };
708// a fetch the merge-base walk makes (commit-only, deepen > 1), rewritten by `lie(objects, shallow)` → { objects, shallow }
709const td = new TextDecoder();
710const walkFetch = ctx => ctx.stage === 'fetch' && ctx.phase !== 'request' && /filter tree:0/.test(ctx.reqBody.toString('latin1')) && Number(/deepen (\d+)/.exec(ctx.reqBody.toString('latin1'))?.[1]) > 1;
711const wantOf = ctx => /want ([0-9a-f]{40})/.exec(ctx.reqBody.toString('latin1'))[1];
712function splitFetch(bytes) { // v2 fetch response → { shallow, objects: Map oid → { type, data } }
713 const shallow = [], parts = []; let o = 0, inPack = false;
714 while (o < bytes.length) {
715 const len = parseInt(td.decode(bytes.subarray(o, o + 4)), 16); if (len < 4) { o += 4; continue; }
716 const body = bytes.subarray(o + 4, o + len); o += len;
717 if (inPack) { if (body[0] === 1) parts.push(body.subarray(1)); continue; }
718 const line = td.decode(body); if (line.startsWith('shallow ')) shallow.push(line.slice(8).trim()); if (line === 'packfile\n') inPack = true;
719 }
720 return { shallow, objects: unwrap(resolvePack(unwrap(parsePack(concat(parts), unwrap(packParseOpts()))))).objects };
722async function fetchReply(objects, shallow) {
723 const { pack } = await buildPack([...objects.values()].map(o => ({ type: o.type, data: o.data })));
724 const head = shallow.length ? [pktLine('shallow-info\n'), ...shallow.map(x => pktLine(`shallow ${x}\n`)), te.encode('0001')] : [];
725 return concat([...head, wrapPack(pack)]);
727const walkLie = lie => ctx => { if (!walkFetch(ctx)) return { type: 'pass' }; const r = splitFetch(ctx.bytes); const l = lie(r.objects, r.shallow, wantOf(ctx)); return { type: 'bytes', bytes: fetchReply(l.objects, l.shallow), name: 'walk lie' }; };
728const without = (objects, drop) => new Map([...objects].filter(([k]) => !drop.includes(k)));
729const objOf = (bare, oid) => { const ty = git(bare, ['cat-file', '-t', oid]).trim(); return { type: TYPE[ty.toUpperCase()], data: new Uint8Array(gitBuf(bare, ['cat-file', ty, oid])) }; };
730async function mergeVia(R, fault, op = 'merge') {
731 const proxy = await startProxy(srv.url, fault);
732 const url = `${proxy.url}/${R.name}.git`, opts = { tracer: new Tracer(), limits: withLimits({ requestTimeoutMs: 3000 }) };
733 let result = null, error = null;
734 try { result = op === 'merge' ? await unwrapAsync(mergeBranch(url, { ref: 'refs/heads/main', from: 'refs/heads/feat', author: AUTHOR, message: 'm', time: 1 }, opts)) : await unwrapAsync(rebaseBranch(url, { ref: 'refs/heads/lin', onto: 'refs/heads/main', committer: AUTHOR, time: 1 }, opts)); }
735 catch (e) { error = e; }
736 await proxy.close();
737 return { result, error, counts: { ...proxy.counts }, main: git(R.bare, ['rev-parse', 'main']).trim(), lin: git(R.bare, ['rev-parse', 'lin']).trim() };
740test('#18 merge base over a cut graph: a server that withholds ancestors without a shallow line fails closed (pack_corrupt), nothing pushed', async t => {
741 // honest control: git's base, git's tree, g=1
742 {
743 const R = cutHistoryRepo();
744 assert.equal(git(R.bare, ['merge-base', '--all', 'main', 'feat']).trim(), R.X);
745 const r = await mergeVia(R, () => ({ type: 'pass' }));
746 assert.equal(r.error, null, r.error?.message);
747 assert.equal(r.result.base, R.X); assert.equal(r.result.tree, git(R.bare, ['merge-tree', '--write-tree', R.M, R.S]).trim());
748 assert.equal(gitBuf(R.bare, ['cat-file', 'blob', 'main:g']).toString(), '1\n');
749 }
750 const lies = {
751 // the depth boundary is real, its shallow lines are stripped: at deepen 8, y4's parent y3 is missing unannounced
752 'strip shallow-info': () => (objects) => ({ objects, shallow: [] }),
753 // y1 never sent (honest shallow lines otherwise): at deepen 16 the server reports the history whole, y2's parent missing
754 'omit y1': R => (objects, shallow) => ({ objects: without(objects, [R.y[1]]), shallow }),
755 };
756 const wrong = [];
757 for (const [name, lie] of Object.entries(lies)) {
758 const R = cutHistoryRepo();
759 const r = await mergeVia(R, walkLie(lie(R)));
760 console.log(`${name}: ${r.error ? r.error.code : r.result.outcome}, requests ${JSON.stringify(r.counts)}`);
761 const ok = r.error?.code === 'pack_corrupt' && /withheld and [0-9a-f]{40} not reported shallow/.test(r.error.message) && r.main === R.M && !r.counts['receive-pack'];
762 if (!ok) wrong.push(`${name}: ${r.result ? `${r.result.outcome} with base ${r.result.base} (git: X ${R.X}; P ${R.P}), main:g=${JSON.stringify(gitBuf(R.bare, ['cat-file', 'blob', 'main:g']).toString())}` : `${r.error.code}: ${r.error.message}`}; requests ${JSON.stringify(r.counts)}`);
763 // rebase walks the same way: a linear branch y10 ← … ← X onto main fails closed the same, the branch untouched
764 const R2 = cutHistoryRepo(), rb = await mergeVia(R2, walkLie(lie(R2)), 'rebase');
765 if (rb.error?.code !== 'pack_corrupt' || rb.lin !== R2.y[10]) wrong.push(`${name} rebase: ${rb.error ? `${rb.error.code}: ${rb.error.message}` : rb.result.outcome}, lin ${rb.lin === R2.y[10] ? 'untouched' : 'moved'}`);
766 }
767 assert.deepEqual(wrong, []);
768});
770test('#18 the walk\'s pack holds only what was asked for, well-formed: an extra commit, a malformed commit, the want answered by a tree → pack_corrupt, nothing pushed', async t => {
771 const cases = {
772 // feat's head in main's pack: a real commit, but not an ancestor of the want
773 'unrequested commit': R => (objects, shallow, want) => ({ objects: want === R.M ? new Map([...objects, [R.S, objOf(R.bare, R.S)]]) : objects, shallow }),
774 // a commit parseCommit reads but fsck --strict refuses (no email, no date), riding along unasked
775 'malformed commit': R => (objects, shallow) => ({ objects: new Map([...objects, ['x', { type: TYPE.COMMIT, data: te.encode(`tree ${git(R.bare, ['rev-parse', 'main^{tree}']).trim()}\nauthor nobody\ncommitter nobody\n\nbad\n`) }]]), shallow }),
776 // the wanted commit's place taken by its root tree
777 'want answered by a tree': R => (objects, shallow, want) => ({ objects: want === R.S ? new Map([...without(objects, [R.S]), ['t', objOf(R.bare, `${R.S}^{tree}`)]]) : objects, shallow }),
778 };
779 const wrong = [];
780 for (const [name, lie] of Object.entries(cases)) {
781 const R = cutHistoryRepo();
782 const r = await mergeVia(R, walkLie(lie(R)));
783 console.log(`${name}: ${r.error ? `${r.error.code} (${r.error.message})` : r.result.outcome}, requests ${JSON.stringify(r.counts)}`);
784 if (r.error?.code !== 'pack_corrupt' || r.main !== R.M || r.counts['receive-pack']) wrong.push(`${name}: ${r.result ? `${r.result.outcome} (base ${r.result.base})` : `${r.error.code}: ${r.error.message}`}; requests ${JSON.stringify(r.counts)}`);
785 }
786 assert.deepEqual(wrong, []);
787});
789test('#18 the blob:none walk harvests only checked objects: a malformed commit in the wide pack fails the fetch and never reaches fetchObject', async () => {
790 const R = cutHistoryRepo();
791 const bad = te.encode(`tree ${git(R.bare, ['rev-parse', 'main^{tree}']).trim()}\nauthor nobody\ncommitter nobody\n\nbad\n`), badOid = hashObject('commit', bad);
792 const proxy = await startProxy(srv.url, ctx => {
793 if (ctx.stage !== 'fetch' || ctx.phase === 'request' || !/filter blob:none/.test(ctx.reqBody.toString('latin1'))) return { type: 'pass' };
794 const r = splitFetch(ctx.bytes); return { type: 'bytes', bytes: fetchReply(new Map([...r.objects, [badOid, { type: TYPE.COMMIT, data: bad }]]), r.shallow) };
795 });
796 try {
797 const t = createTransport(`${proxy.url}/${R.name}.git`, { tracer: new Tracer(), quirks: { treeFilter: 'blob:none' } });
798 let res = null, err = null;
799 try { res = await fetchPackTree0(t, { wants: [R.M], filter: 'tree:0', deepen: 8 }); } catch (e) { err = e; }
800 const served = t.objects.has(badOid) ? await fetchObject(t, { oid: badOid, expectType: TYPE.COMMIT }) : null;
801 assert.equal(err?.code, 'pack_corrupt', res ? `fetched; harvested ${t.objects.size}, malformed commit ${served?.harvested ? 'served by fetchObject from the harvest' : 'not kept'}` : String(err?.message));
802 assert.equal(t.objects.has(badOid), false, 'nothing harvested from a refused pack');
803 } finally { await proxy.close(); }
804});
806// #34: a server holding a tree that names `d` twice, a file and a directory, apart (`d` < `d.x` < `d/`). Before the fix
807// readFile d returned the blob, listTree listed both, editFile built a commit receive-pack's fsck rejected, and
808// mergeBranch pushed a merge re-encoded with one `d`. Every read path now refuses the tree as pack_corrupt and no ref moves.
809function dupTreeRepo() {
810 const name = `dup34-${n++}`;
811 const bare = makeBareRepo(root, name);
812 const hash = (type, bytes) => gitBuf(bare, ['hash-object', '--literally', '-w', '-t', type, '--stdin'], bytes).toString().trim();
813 const entry = (mode, nm, oid) => Buffer.concat([Buffer.from(`${mode} ${nm}\0`), Buffer.from(oid, 'hex')]);
814 const blob = text => hash('blob', Buffer.from(text));
815 const sub = hash('tree', entry('100644', 'f', blob('in dir\n')));
816 const top = (ok, extra = []) => hash('tree', Buffer.concat([entry('100644', 'd', blob('file d\n')), entry('100644', 'd.x', blob('dx\n')), entry('40000', 'd', sub), entry('100644', 'ok.txt', blob(ok)), ...extra]));
817 const commit = (tree, parents, msg) => git(bare, ['-c', 'user.name=x', '-c', 'user.email=x@x', 'commit-tree', tree, ...parents.flatMap(p => ['-p', p]), '-m', msg]).trim();
818 const c0 = commit(top('ok MARK 0\n'), [], 'base');
819 const c1 = commit(top('ok MARK 1\n'), [c0], 'main');
820 const c2 = commit(top('ok MARK 0\n', [entry('100644', 'z.txt', blob('z\n'))]), [c0], 'other');
821 git(bare, ['update-ref', 'refs/heads/main', c1]);
822 git(bare, ['update-ref', 'refs/heads/other', c2]);
823 return { name, bare, c0, c1, c2, tree: git(bare, ['rev-parse', `${c1}^{tree}`]).trim() };
826test('#34 a tree naming `d` twice (file and directory, apart) is pack_corrupt on every read path; no ref moves', async () => {
827 const R = dupTreeRepo();
828 const url = `${srv.url}/${R.name}.git`;
829 const opts = () => ({ tracer: new Tracer() });
830 const who = { ...AUTHOR, time: 1700000000 };
831 const drain = async r => { if (r?.stream) for await (const _ of r.stream); if (r?.files) for await (const g of r.files) if (g.stream) for await (const _ of g.stream); return r; };
832 const ops = {
833 'readFile d': () => unwrapAsync(readFile(url, { path: 'd' }, opts())).then(drain),
834 'readFile d/f': () => unwrapAsync(readFile(url, { path: 'd/f' }, opts())).then(drain),
835 'readFile d.x': () => unwrapAsync(readFile(url, { path: 'd.x' }, opts())).then(drain),
836 'readFile ok.txt': () => unwrapAsync(readFile(url, { path: 'ok.txt' }, opts())).then(drain),
837 'listTree /': () => unwrapAsync(listTree(url, {}, opts())),
838 'listTree d': () => unwrapAsync(listTree(url, { path: 'd' }, opts())),
839 'checkoutTree': () => unwrapAsync(checkoutTree(url, {}, opts())).then(drain),
840 'checkoutTree d': () => unwrapAsync(checkoutTree(url, { path: 'd' }, opts())).then(drain),
841 ...Object.fromEntries(['d', 'd/f', 'd.x', 'ok.txt', 'nope'].map(path => [`editFile ${path}`, () => unwrapAsync(editFile(url, { path, find: 'MARK', replace: 'XX', author: AUTHOR, message: 'e' }, opts()))])),
842 'pushTree': () => unwrapAsync(pushTree(url, { files: { 'new.txt': 'n\n' }, author: AUTHOR, message: 'p' }, opts())),
843 'mergeBranch main<-other': () => unwrapAsync(mergeBranch(url, { ref: 'refs/heads/main', from: 'refs/heads/other', author: AUTHOR, message: 'm', time: 1 }, opts())),
844 'mergeBranch other<-main': () => unwrapAsync(mergeBranch(url, { ref: 'refs/heads/other', from: 'refs/heads/main', author: AUTHOR, message: 'm', time: 1 }, opts())),
845 'rebaseBranch other onto main': () => unwrapAsync(rebaseBranch(url, { ref: 'refs/heads/other', onto: 'refs/heads/main', committer: AUTHOR, time: 1 }, opts())),
846 'cherryPick other onto main': () => unwrapAsync(cherryPick(url, { ref: 'refs/heads/main', commit: R.c2, committer: who, time: 1 }, opts())),
847 'revertCommit main': () => unwrapAsync(revertCommit(url, { ref: 'refs/heads/main', commit: R.c1, committer: who, time: 1 }, opts())),
848 'fetchObject tree': () => fetchObject(createTransport(url, opts()), { oid: R.tree, expectType: TYPE.TREE }),
849 'fetchPackTree0 harvest': () => fetchPackTree0(createTransport(url, { ...opts(), quirks: { treeFilter: 'blob:none' } }), { wants: [R.c1], filter: 'tree:0', deepen: 1 }),
850 };
851 const got = {};
852 for (const [label, op] of Object.entries(ops)) {
853 try { await op(); got[label] = 'ok'; } catch (e) { got[label] = `${e?.code}: ${e?.message}`; }
854 }
855 const want = Object.fromEntries(Object.keys(ops).map(k => [k, 'pack_corrupt: tree: duplicate entry "d"']));
856 assert.deepEqual(got, want);
857 assert.equal(git(R.bare, ['rev-parse', 'main']).trim(), R.c1);
858 assert.equal(git(R.bare, ['rev-parse', 'other']).trim(), R.c2);
859 // the raw pack path holds the trees unparsed; the checked path is fetchPackTree0's assertFetched (above)
860 const raw = await fetchPack(createTransport(url, opts()), { wants: [R.c1], filter: 'blob:none', deepen: 1 });
861 assert.ok(raw.objects.has(R.tree), 'the server really serves the tree');
862});
864test('ambiguous push (response lost after server applied it): ref == our commit; a naive retry is match_count, content correct', async () => {
865 const r = await run(at('receive-pack', 0, { type: 'drop', after: 0 }));
866 assert.equal(r.error.code, 'ambiguous');
867 assert.notEqual(r.head, r.repo.head, 'server applied the push');
868 assert.equal(r.content, EXPECTED);
869 // retry against the real server
870 await assert.rejects(unwrapAsync(editFile(`${srv.url}/${r.repo.name}.git`, { path: FILE, find: FIND, replace: REPL, author: AUTHOR, message: 'adv' }, { tracer: new Tracer() })), e => e.code === 'match_count');
871 assert.equal(gitBuf(r.repo.bare, ['cat-file', 'blob', `main:${FILE}`]).toString(), EXPECTED);
872 assert.equal(git(r.repo.bare, ['rev-list', '--count', `${r.repo.head}..main`]).trim(), '1', 'no duplicate commit');
873});
875test('external AbortSignal mid-stream → aborted, ref unchanged', async () => {
876 const ac = new AbortController();
877 const r = await run(at('fetch', 4, { type: 'slow', chunk: 4, delayMs: 20 }), { opts: { signal: ac.signal } , limits: withLimits({ requestTimeoutMs: 30000 }) });
878 void r; // control: completes
879 const ac2 = new AbortController();
880 setTimeout(() => ac2.abort(), 150);
881 const r2 = await run(at('fetch', 4, { type: 'slow', chunk: 2, delayMs: 30 }), { opts: { signal: ac2.signal }, limits: withLimits({ requestTimeoutMs: 30000 }) });
882 checkInvariants(r2, { stage: 'abort', requireError: true });
883 assert.equal(r2.error.code, 'aborted');
884});
886test('I4 six objects at the per-object cap exceed the inflated aggregate', { timeout: 120_000 }, async () => {
887 const zlib = await import('node:zlib');
888 const per = DEFAULT_LIMITS.maxObjectSize;
889 const z = zlib.deflateSync(Buffer.alloc(per));
890 const entry = concat([encodeObjHeader(3, per), z]);
891 const hdr = new Uint8Array(12);
892 hdr.set([0x50, 0x41, 0x43, 0x4b]);
893 new DataView(hdr.buffer).setUint32(4, 2);
894 new DataView(hdr.buffer).setUint32(8, 6);
895 const body = concat([hdr, entry, entry, entry, entry, entry]);
896 const pack = concat([body, new Sha1().update(body).digest()]);
897 assert.throws(() => unwrap(parsePack(pack, unwrap(packParseOpts(DEFAULT_LIMITS)))), e => {
898 assert.equal(e.code, 'limit_exceeded');
899 assert.equal(e.what, 'pack inflated bytes');
900 // each object charges LIMIT_OBJECT_OVERHEAD beyond its bytes (#142): three maximum objects and a small one fit, a fourth maximum one does not
901 assert.equal(e.got, (per + LIMIT_OBJECT_OVERHEAD) * 4);
902 assert.equal(e.max, DEFAULT_LIMITS.maxInflatedTotal);
903 return true;
904 });
905});
907test('I4 many ref-deltas resolve in linear lookups', { timeout: 30_000 }, async () => {
908 const base = te.encode('a');
909 const delta = makeDelta(base, te.encode('b')).delta;
910 const baseOid = hashObject('blob', base);
911 const n = 8_000;
912 const objects = [{ type: TYPE.BLOB, data: base }];
913 for (let i = 0; i < n; i++) objects.push({ type: TYPE.BLOB, data: delta, base: { tag: 'ref', oid: baseOid } });
914 const { pack } = await buildPack(objects);
915 const parsed = unwrap(parsePack(pack, unwrap(packParseOpts())));
916 const t0 = Date.now();
917 const resolved = unwrap(resolvePack(parsed));
918 const dt = Date.now() - t0;
919 assert.equal(resolved.stats.baseLookups, n);
920 assert.ok(dt < 3_000, `resolvePack took ${dt}ms`);
921});
923test('I4 parsePack keeps inflated bytes within the cap', async () => {
924 for (let n = 1; n <= 4; n++) {
925 const objects = Array.from({ length: n }, (_, i) => ({ type: TYPE.BLOB, data: te.encode(String(i + 1)) }));
926 const { pack } = await buildPack(objects);
927 const parsed = unwrap(parsePack(pack, unwrap(packParseOpts())));
928 assert.ok(parsed.stats.inflated + n * LIMIT_OBJECT_OVERHEAD <= DEFAULT_LIMITS.maxInflatedTotal);
929 assert.ok(parsed.stats.inflated <= parsed.objects.reduce((s, o) => s + o.size, 0));
930 }
931 const declared = 1_000;
932 const hdr = new Uint8Array(12);
933 hdr.set([0x50, 0x41, 0x43, 0x4b]);
934 new DataView(hdr.buffer).setUint32(4, 2);
935 new DataView(hdr.buffer).setUint32(8, 1);
936 const body = concat([hdr, encodeObjHeader(3, declared), Uint8Array.of(0)]);
937 const pack = concat([body, new Sha1().update(body).digest()]);
938 assert.throws(() => unwrap(parsePack(pack, { limits: withLimits({ maxInflatedTotal: declared - 1, maxObjectSize: declared }), maxInflatedTotal: declared - 1 })), e => e.code === 'limit_exceeded' && e.what === 'pack inflated bytes' && e.got === declared + LIMIT_OBJECT_OVERHEAD);
939});
941const pktLine = s => { const b = te.encode(s); return concat([te.encode((b.length + 4).toString(16).padStart(4, '0')), b]); };
942const pktRaw = b => concat([te.encode((b.length + 4).toString(16).padStart(4, '0')), b]);
944// #13: a server answering a blob fetch with PACK + version + count=1 and then object-header continuation bytes forever.
945// The streamed paths (edit's streamBlobEdit, readFile's openBlob, checkoutTree's single-blob stream) used to keep every
946// byte in the header buffer, re-concatenated per chunk, until the request timer fired between chunks: linear memory,
947// quadratic CPU. The header phase now ends at its 22nd byte, so the first band-1 frame is refused.
948function endlessHeader() {
949 const served = { bytes: 0, requests: 0 };
950 const frame = pktRaw(concat([Uint8Array.of(1), new Uint8Array(65515).fill(0x80)]));
951 const f = async (url, init) => {
952 const body = init?.body instanceof Uint8Array ? Buffer.from(init.body).toString('latin1') : '';
953 if (!body.includes('command=fetch\n') || body.includes('filter ')) return fetch(url, init);
954 served.requests++;
955 let first = true;
956 const stream = new ReadableStream({
957 pull(c) {
958 const chunk = first ? concat([pktLine('packfile\n'), pktRaw(Uint8Array.of(1, 0x50, 0x41, 0x43, 0x4b, 0, 0, 0, 2, 0, 0, 0, 1, 0xb0))]) : frame;
959 first = false; served.bytes += chunk.length; c.enqueue(chunk);
960 },
961 });
962 return new Response(stream, { status: 200, headers: { 'content-type': 'application/x-git-upload-pack-result' } });
963 };
964 return { served, f };
967test('I4 a blob fetch whose object header never ends: pack_corrupt within the first frame on every streamed path', { timeout: 60_000 }, async () => {
968 const repo = freshRepo();
969 const url = `${srv.url}/${repo.name}.git`;
970 const limits = withLimits({ requestTimeoutMs: 5000 });
971 const refused = e => e.code === 'pack_corrupt' && e.message === 'pack: size varint longer than 64 bits';
972 const paths = {
973 edit: f => unwrapAsync(editFile(url, { path: FILE, find: FIND, replace: REPL, author: AUTHOR, message: 'adv' }, { tracer: new Tracer(), limits, fetchImpl: f })),
974 read: f => unwrapAsync(readFile(url, { path: FILE }, { tracer: new Tracer(), limits, fetchImpl: f })),
975 checkout: async f => { const r = await unwrapAsync(checkoutTree(url, {}, { tracer: new Tracer(), limits, fetchImpl: f, batch: 1 })); for await (const g of r.files) if (g.stream) for await (const _ of g.stream); },
976 };
977 const seen = {};
978 for (const [name, op] of Object.entries(paths)) {
979 const { served, f } = endlessHeader();
980 const rss0 = process.memoryUsage().rss, t0 = performance.now();
981 const err = await op(f).then(() => null, e => e);
982 const ms = performance.now() - t0;
983 assert.ok(err && refused(err), `${name}: ${err?.code}: ${err?.message}`);
984 assert.equal(served.requests, 1, `${name}: one blob request`);
985 assert.ok(ms < 1000, `${name}: ${ms.toFixed(0)} ms`);
986 assert.ok(served.bytes < 1 << 20, `${name}: the client read ${served.bytes} bytes of header`);
987 assert.ok(process.memoryUsage().rss - rss0 < 64 << 20, `${name}: rss grew`);
988 seen[name] = `${served.bytes} B served, ${ms.toFixed(0)} ms`;
989 }
990 console.log(' endless object header:', JSON.stringify(seen));
991 assert.equal(git(repo.bare, ['rev-parse', 'main']).trim(), repo.head, 'I2: ref unchanged');
992 rmSync(repo.work, { recursive: true, force: true });
993});
995// A server that answers a blob fetch (or, for `wants`, every unfiltered fetch) with `first` and then `frame` forever,
996// yielding to the event loop between chunks as a socket does and erroring the body when the request signal aborts (as a
997// real fetch does), so the test depends on the client's caps and not on the request timer. Counts every byte it served.
998function endlessBody({ first, frame }) {
999 const served = { bytes: 0, requests: 0, perRequest: [] };
1000 const f = async (url, init) => {
1001 const body = init?.body instanceof Uint8Array ? Buffer.from(init.body).toString('latin1') : '';
1002 if (!body.includes('command=fetch\n') || body.includes('filter ')) return fetch(url, init);
1003 const req = { wants: body.split('want ').length - 1, bytes: 0 };
1004 served.requests++; served.perRequest.push(req);
1005 let more = true, ctrl;
1006 init.signal?.addEventListener('abort', () => { more = false; ctrl?.error(init.signal.reason); });
1007 const stream = new ReadableStream({
1008 start(c) { ctrl = c; },
1009 async pull(c) {
1010 await new Promise(r => setImmediate(r));
1011 if (!more) return;
1012 const chunk = req.bytes === 0 ? first : frame;
1013 req.bytes += chunk.length; served.bytes += chunk.length; c.enqueue(chunk);
1014 },
1015 });
1016 return new Response(stream, { status: 200, headers: { 'content-type': 'application/x-git-upload-pack-result' } });
1017 };
1018 return { served, f };
1020const PACK_ONE = Uint8Array.of(0x50, 0x41, 0x43, 0x4b, 0, 0, 0, 2, 0, 0, 0, 1); // PACK, v2, one object
1021// 13103 non-final empty stored blocks (`00 00 00 ff ff`): infinite compressed input, zero inflated output
1022const EMPTY_STORED = (() => { const b = new Uint8Array(65515); for (let i = 0; i + 5 <= b.length; i += 5) b.set([0x00, 0x00, 0x00, 0xff, 0xff], i); return b; })();
1023const STORED_FRAME = pktRaw(concat([Uint8Array.of(1), EMPTY_STORED]));
1024// a single-object pack whose object header declares `size` bytes of blob and whose zlib stream never ends
1025const endlessStored = size => endlessBody({ first: concat([pktLine('packfile\n'), pktRaw(concat([Uint8Array.of(1), PACK_ONE, encodeObjHeader(3, size), Uint8Array.of(0x78, 0x01), EMPTY_STORED.subarray(0, 65000)]))]), frame: STORED_FRAME });
1027const streamedPaths = (url, limits) => ({
1028 edit: f => unwrapAsync(editFile(url, { path: FILE, find: FIND, replace: REPL, author: AUTHOR, message: 'adv' }, { tracer: new Tracer(), limits, fetchImpl: f })),
1029 read: async f => { const r = await unwrapAsync(readFile(url, { path: FILE }, { tracer: new Tracer(), limits, fetchImpl: f })); for await (const _ of r.stream) void _; },
1030 'checkout single': async f => { const r = await unwrapAsync(checkoutTree(url, {}, { tracer: new Tracer(), limits, fetchImpl: f, batch: 1 })); for await (const g of r.files) if (g.stream) for await (const _ of g.stream) void _; },
1031 'checkout batched': async f => { const r = await unwrapAsync(checkoutTree(url, {}, { tracer: new Tracer(), limits, fetchImpl: f })); for await (const g of r.files) if (g.stream) for await (const _ of g.stream) void _; },
1032});
1034// The probe behind this test: on the previous source, readFile accepted 793.6 MiB and editFile 632.7 MiB of this stream in
1035// 8 s (~90 MiB/s) and failed `timeout` — at the default 60 s timer that is ~5 GiB per request — because no streamed path
1036// applied limits.maxPackBytes to the body, and the declared-size check only fires on inflated bytes, of which there are none.
1037test('I4 a blob whose zlib stream never ends (empty stored blocks): refused at the per-object bound, never the timer, on every streamed path', { timeout: 60_000 }, async () => {
1038 const repo = freshRepo();
1039 const url = `${srv.url}/${repo.name}.git`;
1040 const limits = withLimits({ requestTimeoutMs: 5000, maxPackBytes: 2 << 20 });
1041 const seen = {};
1042 for (const [name, op] of Object.entries(streamedPaths(url, limits))) {
1043 const { served, f } = endlessStored(ORIG.length);
1044 const rss0 = process.memoryUsage().rss, t0 = performance.now();
1045 const err = await op(f).then(() => null, e => e);
1046 const ms = performance.now() - t0;
1047 assert.ok(err, `${name}: succeeded`);
1048 assert.notEqual(err.code, 'timeout', `${name}: the request timer, not a cap, ended it (${served.bytes} B served)`);
1049 assert.ok(['pack_corrupt', 'limit_exceeded'].includes(err.code), `${name}: ${err.code}: ${err.message}`);
1050 assert.ok(ms < 1000, `${name}: ${ms.toFixed(0)} ms`);
1051 for (const r of served.perRequest) {
1052 // one blob: the first frame already carries more zlib bytes than an 18-byte blob allows → refused on that frame.
1053 // many blobs (a buffered batch): bounded by maxPackBytes, plus at most one frame past it, then split down to single blobs.
1054 const cap = r.wants === 1 ? 0 : limits.maxPackBytes;
1055 assert.ok(r.bytes <= cap + 2 * STORED_FRAME.length, `${name}: a ${r.wants}-want request read ${r.bytes} B (cap ${cap})`);
1057 assert.ok(process.memoryUsage().rss - rss0 < 64 << 20, `${name}: rss grew by ${process.memoryUsage().rss - rss0}`);
1058 seen[name] = `${err.code} (${err.message.slice(0, 70)}) after ${served.bytes} B in ${served.requests} request(s), ${ms.toFixed(0)} ms`;
1059 // the single-blob paths are refused inside the first frame
1060 if (name !== 'checkout batched') { assert.equal(err.code, 'pack_corrupt'); assert.match(err.message, /zlib stream longer than its declared size allows/); assert.equal(served.requests, 1); }
1062 console.log(' endless zlib stream:', JSON.stringify(seen, null, 1));
1063 assert.equal(git(repo.bare, ['rev-parse', 'main']).trim(), repo.head, 'I2: ref unchanged');
1064 rmSync(repo.work, { recursive: true, force: true });
1065});
1067// The same stream against a blob declared large enough that its zlib bound is past maxPackBytes: the pack-byte cap is
1068// what stops it, on every consumer of packStream, with limit_exceeded's `got` within one frame of the cap.
1069test('I4 maxPackBytes bounds the streamed body on every path: limit_exceeded within one frame of the cap, not timeout', { timeout: 60_000 }, async () => {
1070 const repo = freshRepo();
1071 const url = `${srv.url}/${repo.name}.git`;
1072 const limits = withLimits({ requestTimeoutMs: 5000, maxPackBytes: 1 << 20 });
1073 for (const [name, op] of Object.entries(streamedPaths(url, limits))) {
1074 const { served, f } = endlessStored(16 << 20);
1075 const t0 = performance.now();
1076 const err = await op(f).then(() => null, e => e);
1077 const ms = performance.now() - t0;
1078 assert.ok(err && err.code === 'limit_exceeded' && err.what === 'pack bytes', `${name}: ${err?.code}: ${err?.message}`);
1079 assert.equal(err.max, limits.maxPackBytes);
1080 assert.ok(err.got > limits.maxPackBytes && err.got <= limits.maxPackBytes + STORED_FRAME.length, `${name}: got ${err.got}`);
1081 assert.ok(ms < 2000, `${name}: ${ms.toFixed(0)} ms`);
1082 for (const r of served.perRequest) assert.ok(r.bytes <= limits.maxPackBytes + 2 * STORED_FRAME.length, `${name}: a ${r.wants}-want request read ${r.bytes} B`);
1084 // band 2 (progress) is the other stream a fetch response carries; it is held to the same cap
1085 const PROGRESS_FRAME = pktRaw(concat([Uint8Array.of(2), new Uint8Array(65515).fill(0x2e)]));
1086 const { served, f } = endlessBody({ first: concat([pktLine('packfile\n'), PROGRESS_FRAME]), frame: PROGRESS_FRAME });
1087 const err = await unwrapAsync(editFile(url, { path: FILE, find: FIND, replace: REPL, author: AUTHOR, message: 'adv' }, { tracer: new Tracer(), limits, fetchImpl: f })).then(() => null, e => e);
1088 assert.ok(err && err.code === 'limit_exceeded' && err.what === 'progress bytes', `progress flood: ${err?.code}: ${err?.message}`);
1089 assert.ok(served.bytes <= limits.maxPackBytes + 2 * PROGRESS_FRAME.length, `progress flood: ${served.bytes} B served`);
1090 assert.equal(git(repo.bare, ['rev-parse', 'main']).trim(), repo.head, 'I2: ref unchanged');
1091 rmSync(repo.work, { recursive: true, force: true });
1092});
1094// ---- issue #36: the streamed-body bound, every cell ----
1095// The space is a product of tagged unions, and every cell runs (no sampling):
1096// path edit | read | checkout single | checkout batched | fetchObject (buffered single) | fetchPack (buffered pack)
1097// shape valid | endless (empty stored blocks forever) | oversized (one stored block of S+1 bytes, declared S) |
1098// trailing (a valid stream, then 7 junk bytes) | truncated (half the stream, then the response ends) |
1099// padded_to_bound (empty stored blocks up to zlibBound(S), then the data) | padded_past_bound (one block more)
1100// size 0 | 1 | 65535 | 65536 | 1 MiB (the declared, and for the honest shapes the real, blob size)
1101// cap under | at | over (maxPackBytes against R, defined below)
1102// 6 × 7 × 5 × 3 = 630 cells. The hostile body is band 1 of the blob request, framed as git frames it (65515-byte band-1
1103// payloads; a finite body is split into equal frames so its last frame holds its last bytes). Every other request (ls-refs,
1104// commit, trees, push, checkout's companion blob) goes to git http-backend. The fake honours init.signal and enqueues a
1105// frame only when the client reads (highWaterMark 0), so `served` is exactly the band-1 bytes the client asked for.
1107// R is the band-1 byte count at the end of the frame in which the client must decide: the last frame for a shape decided
1108// at the end, the frame holding the first zlib byte past zlibBound(S) for a stream spStep refuses on the way (endless and
1109// padded_past_bound on every path but fetchPack, which buffers and has no "on the way"). Caps: under R + 1, at R (the cap
1110// is inclusive), over R − 1. The expectation, stated before anything runs:
1111// over → limit_exceeded 'pack bytes', got in (cap, cap + one frame]
1112// under/at, endless, fetchPack → limit_exceeded as well: a buffered pack that never ends has only the cap
1113// under/at, valid/padded_to_bound → ok (bytes equal the blob's; edit: the new commit, then the ref is reset), except
1114// edit at S = 0, which is match_count (find is 1 byte)
1115// under/at, any other shape → pack_corrupt
1116// checkout batched → the 2-want batch is limit_exceeded (its pack is R + the companion > cap), splits once,
1117// and the hostile blob then fares exactly as in checkout single
1118// Asserted in every cell: the code; never `timeout`; served ≤ cap + one frame and ≤ R for each hostile request; for
1119// endless on an spStep path served ≤ the pack bound (12 + header + zlibBound(S) + 20) + one frame, whatever the cap; a
1120// limit_exceeded carries what/got/max; I2 for edit (ref unchanged unless ok).
1121const MX = {
1122 frame: 65515, find: 'M', repl: 'N',
1123 paths: ['edit', 'read', 'checkout single', 'checkout batched', 'fetchObject', 'fetchPack'],
1124 shapes: ['valid', 'endless', 'oversized', 'trailing', 'truncated', 'padded_to_bound', 'padded_past_bound'],
1125 sizes: [0, 1, 65535, 65536, 1 << 20],
1126 rels: ['under', 'at', 'over'],
1128const EMPTY_BLOCK = [0x00, 0x00, 0x00, 0xff, 0xff];
1129const ZHDR = Uint8Array.of(0x78, 0x01);
1130const be32 = n => Uint8Array.of(n >>> 24, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff);
1131const storedBlocks = (data, final = true) => {
1132 const out = [];
1133 for (let i = 0; i === 0 || i < data.length; i += 65535) {
1134 const d = data.subarray(i, Math.min(i + 65535, data.length)), last = final && i + 65535 >= data.length;
1135 out.push(Uint8Array.of(last ? 1 : 0, d.length & 0xff, d.length >> 8, ~d.length & 0xff, (~d.length >> 8) & 0xff), d);
1137 return concat(out);
1139const emptyBlocks = k => { const b = new Uint8Array(5 * k); for (let i = 0; i < k; i++) b.set(EMPTY_BLOCK, 5 * i); return b; };
1140// the blob of size S: deterministic bytes, never the find byte except at offset 0 (exactly one match for edit)
1141const mxContent = S => { const b = new Uint8Array(S); let x = 0x2545f491 ^ S; for (let i = 0; i < S; i++) { x ^= x << 13; x ^= x >>> 17; x ^= x << 5; b[i] = x & 0xff; if (b[i] === 0x4d) b[i] = 0x4e; } if (S) b[0] = 0x4d; return b; };
1142// { zlib, endless, cut } for a shape: the zlib bytes of the object (the prefix, for endless), and whether the pack is cut
1143function mxZlib(shape, data) {
1144 const S = data.length, bound = zlibBound(S);
1145 const padded = k => concat([ZHDR, emptyBlocks(k), storedBlocks(data), be32(adler32(data))]);
1146 const k0 = Math.floor((bound - padded(0).length) / 5);
1147 const deflated = new Uint8Array(zlib.deflateSync(data)); // git's encoder: zlib at Z_DEFAULT_COMPRESSION
1148 switch (shape) {
1149 case 'valid': return { zlib: deflated };
1150 case 'endless': return { zlib: ZHDR, endless: true };
1151 case 'oversized': { const more = concat([data, Uint8Array.of(0x4e)]); return { zlib: concat([ZHDR, storedBlocks(more), be32(adler32(more))]) }; }
1152 case 'trailing': return { zlib: concat([deflated, new Uint8Array(7).fill(0xee)]) };
1153 case 'truncated': return { zlib: deflated.subarray(0, deflated.length >> 1), cut: true };
1154 case 'padded_to_bound': { const z = padded(k0); assert.ok(z.length <= bound && z.length + 5 > bound); return { zlib: z }; }
1155 case 'padded_past_bound': { const z = padded(k0 + 1); assert.ok(z.length > bound); return { zlib: z }; }
1157 throw new Error(shape);
1159// band 1 of a response: { prefix, endless }. `companion` (checkout batched) goes first, as a second object.
1160function mxPack(shape, data, companion = null) {
1161 const { zlib: z, endless, cut } = mxZlib(shape, data);
1162 const objs = [...(companion ? [encodeObjHeader(TYPE.BLOB, companion.length), new Uint8Array(zlib.deflateSync(companion))] : []), encodeObjHeader(TYPE.BLOB, data.length), z];
1163 const body = concat([Uint8Array.of(0x50, 0x41, 0x43, 0x4b, 0, 0, 0, 2, 0, 0, 0, companion ? 2 : 1), ...objs]);
1164 return { prefix: endless || cut ? body : concat([body, new Sha1().update(body).digest()]), endless: !!endless };
1166// the frames of a band-1 body, as a lazy list of byte ranges: [start, end) for frame i
1167function mxFrames({ prefix, endless }) {
1168 if (endless) return { count: Infinity, range: i => [i * MX.frame, (i + 1) * MX.frame], bytes: (a, b) => { const out = new Uint8Array(b - a); for (let p = a; p < b; p++) out[p - a] = p < prefix.length ? prefix[p] : EMPTY_BLOCK[(p - prefix.length) % 5]; return out; } };
1169 const n = Math.max(1, Math.ceil(prefix.length / MX.frame)), step = Math.ceil(prefix.length / n);
1170 return { count: n, range: i => [i * step, Math.min(prefix.length, (i + 1) * step)], bytes: (a, b) => prefix.subarray(a, b) };
1172// the end of the frame holding band-1 byte number d (1-based)
1173const frameEnd = (fr, d) => { for (let i = 0; ; i++) { const [, e] = fr.range(i); if (e >= d) return e; } };
1174// the first band-1 byte count at which spStep's zlib bytes (everything after the object header, less the 20-byte trailer it
1175// withholds) exceed zlibBound(S)
1176const boundByte = S => PACK_ONE.length + encodeObjHeader(TYPE.BLOB, S).length + zlibBound(S) + 1 + 20;
1178function mxExpect(path, shape, S) {
1179 if (path === 'checkout batched') return { ...mxExpect('checkout single', shape, S), split: true };
1180 const spStepPath = path !== 'fetchPack';
1181 const okCode = path === 'edit' && S < MX.find.length ? 'match_count' : 'ok';
1182 if (shape === 'valid' || shape === 'padded_to_bound') return { decide: 'end', code: okCode };
1183 if (shape === 'endless') return spStepPath ? { decide: 'bound', code: 'pack_corrupt' } : { decide: 'never', code: 'limit_exceeded' };
1184 if (shape === 'padded_past_bound' && spStepPath) return { decide: 'bound', code: 'pack_corrupt' };
1185 return { decide: 'end', code: 'pack_corrupt' };
1188// a fetch that serves `body(wants)` for the blob request and forwards everything else to the real server
1189// The hostile request: an unfiltered fetch that wants the target.
1190function mxHostile(target, body) {
1191 const req = body instanceof Uint8Array ? Buffer.from(body).toString('latin1') : '';
1192 return req.includes('command=fetch\n') && !req.includes('filter ') && req.includes(`want ${target}\n`);
1194function mxFetch(target, bodyFor, setCap) {
1195 const served = [];
1196 const f = async (url, init) => {
1197 const req = init?.body instanceof Uint8Array ? Buffer.from(init.body).toString('latin1') : '';
1198 const hostile = mxHostile(target, init?.body);
1199 setCap(hostile);
1200 if (!hostile) return fetch(url, init);
1201 const wants = req.split('want ').length - 1;
1202 const fr = mxFrames(bodyFor(wants));
1203 const rec = { wants, bytes: 0, frames: 0 }; served.push(rec);
1204 let i = -1, ctrl, gone = false;
1205 init.signal?.addEventListener('abort', () => { gone = true; try { ctrl?.error(init.signal.reason); } catch { /* already closed */ } });
1206 const stream = new ReadableStream({
1207 start(c) { ctrl = c; },
1208 async pull(c) {
1209 await new Promise(r => setImmediate(r));
1210 if (gone) return;
1211 if (i === -1) { i++; return c.enqueue(pktLine('packfile\n')); }
1212 // asked for more after the last frame: the client has taken the whole body, so the cap under test is done
1213 // (edit's push checks its own pack against maxPackBytes before the next request)
1214 if (i >= fr.count) { setCap(false); c.enqueue(te.encode('0000')); return c.close(); }
1215 const [a, b] = fr.range(i++);
1216 rec.bytes += b - a; rec.frames++;
1217 c.enqueue(pktRaw(concat([Uint8Array.of(1), fr.bytes(a, b)])));
1218 },
1219 }, { highWaterMark: 0 });
1220 return new Response(stream, { status: 200, headers: { 'content-type': 'application/x-git-upload-pack-result' } });
1221 };
1222 return { f, served };
1225test('I4 #36 matrix: path × body shape × declared size × cap, every cell, never the timer', { timeout: 600_000 }, async () => {
1226 const COMPANION = te.encode('companion\n');
1227 const repos = new Map();
1228 for (const S of MX.sizes) {
1229 const name = `mx${S}`, bare = makeBareRepo(root, name), work = join(root, `w${name}`);
1230 mkdirSync(work); initRepo(work);
1231 const data = mxContent(S);
1232 const head = commitFiles(work, bare, { 'a.bin': Buffer.from(data), 'z.txt': Buffer.from(COMPANION) }, 'mx');
1233 repos.set(S, { url: `${srv.url}/${name}.git`, bare, head, data, oid: hashObject('blob', data) });
1235 const cells = [], tally = {};
1236 for (const path of MX.paths) for (const shape of MX.shapes) for (const S of MX.sizes) for (const rel of MX.rels) cells.push({ path, shape, S, rel });
1237 assert.equal(cells.length, 630);
1238 // one lane per size (each size has its own repo, so an edit's push and ref reset touch no other lane)
1239 await Promise.all(MX.sizes.map(async lane => { for (const cell of cells.filter(c => c.S === lane)) {
1240 const { path, shape, S, rel } = cell, repo = repos.get(S), exp = mxExpect(path, shape, S);
1241 const single = mxPack(shape, repo.data), fr = mxFrames(single);
1242 const L = single.endless ? Infinity : single.prefix.length;
1243 const R = exp.decide === 'end' ? L : frameEnd(fr, boundByte(S));
1244 const cap = rel === 'under' ? R + 1 : rel === 'at' ? R : R - 1;
1245 const code = rel === 'over' ? 'limit_exceeded' : exp.code;
1246 // the cap under test applies to the hostile request; every other request runs under the default. A limits record is
1247 // parsed once where it enters (#156), so the switch is the transport's: the caller's transport (complete, its limits
1248 // checked) is kept as it is, and each exchange reads `limits` from it as the exchange starts.
1249 const base = unwrap(parseLimits({ requestTimeoutMs: 30_000 }));
1250 const capped = lowered(base, 'maxPackBytes', cap);
1251 let hostileNow = false;
1252 const { f, served } = mxFetch(repo.oid, wants => (wants > 1 ? mxPack(shape, repo.data, COMPANION) : single), hostile => { hostileNow = hostile; });
1253 const made = unwrap(createTransportR(repo.url, { tracer: new Tracer(), limits: base, fetchImpl: f }));
1254 const tt = Object.create(made, {
1255 limits: { get: () => (hostileNow ? capped : base), enumerable: true },
1256 // an exchange is hostile by its request, known when the transport is handed the body (before its limits are read)
1257 frames: { value(service, body, span, hints) { hostileNow = mxHostile(repo.oid, body); return made.frames.call(this, service, body, span, hints); }, enumerable: true },
1258 });
1259 const opts = { tracer: new Tracer(), limits: base, transport: tt };
1260 const drain = async s => { const parts = []; for await (const c of s) parts.push(c); return concat(parts); };
1261 const files = async r => { const got = new Map(); for await (const g of r.files) got.set(g.path, g.stream ? await drain(g.stream) : null); return got; };
1262 const ops = {
1263 edit: () => unwrapAsync(editFile(repo.url, { path: 'a.bin', find: MX.find, replace: MX.repl, author: AUTHOR, message: 'mx' }, opts)),
1264 read: async () => drain((await unwrapAsync(readFile(repo.url, { path: 'a.bin' }, opts))).stream),
1265 'checkout single': async () => (await files(await unwrapAsync(checkoutTree(repo.url, {}, { ...opts, batch: 1 })))).get('a.bin'),
1266 'checkout batched': async () => (await files(await unwrapAsync(checkoutTree(repo.url, {}, opts)))).get('a.bin'),
1267 fetchObject: async () => (await unwrapAsync(fetchObjectR(tt, { oid: repo.oid, filter: null, expectType: TYPE.BLOB }))).data,
1268 fetchPack: async () => (await unwrapAsync(fetchPackR(tt, { wants: [repo.oid] }))).objects.get(repo.oid).data,
1269 };
1270 const t0 = performance.now();
1271 const res = await ops[path]().then(v => ({ v }), e => ({ e }));
1272 const ms = performance.now() - t0;
1273 const got = res.e ? res.e.code : 'ok';
1274 const where = `${path} / ${shape} / S=${S} / ${rel} (R=${R}, cap=${cap})`;
1275 // the edit's ref, read and put back before any assertion, so a failing cell cannot change the blob the next one serves
1276 const ref = path === 'edit' ? git(repo.bare, ['rev-parse', 'main']).trim() : repo.head;
1277 if (ref !== repo.head) git(repo.bare, ['update-ref', 'refs/heads/main', repo.head]);
1278 assert.notEqual(got, 'timeout', `${where}: the timer ended it`);
1279 assert.equal(got, code, `${where}: ${res.e?.message ?? 'ok'} after ${served.map(r => r.bytes).join('+')} B`);
1280 if (got === 'limit_exceeded') { assert.equal(res.e.what, 'pack bytes', where); assert.equal(res.e.max, cap, where); assert.ok(res.e.got > cap && res.e.got <= cap + MX.frame, `${where}: got ${res.e.got}`); }
1281 if (path === 'edit') assert.equal(ref !== repo.head, got === 'ok', `${where}: the ref moved iff the edit succeeded (I2)`);
1282 if (got === 'ok' && path !== 'edit') assert.ok(Buffer.from(res.v).equals(Buffer.from(repo.data)), `${where}: bytes`);
1283 // the hostile requests: one, or for checkout batched the 2-want batch (refused at the cap) and then the blob alone
1284 assert.deepEqual(served.map(r => r.wants), exp.split ? [2, 1] : [1], `${where}: requests`);
1285 for (const r of served) {
1286 assert.ok(r.bytes <= cap + MX.frame, `${where}: a ${r.wants}-want request served ${r.bytes} B past cap ${cap} + one frame`);
1287 if (r.wants === 1 && exp.decide !== 'never') assert.ok(r.bytes <= R, `${where}: served ${r.bytes} B > R ${R}`);
1288 if (r.wants === 1 && shape === 'endless' && path !== 'fetchPack') assert.ok(r.bytes <= boundByte(S) - 1 + MX.frame, `${where}: served ${r.bytes} B for a ${S}-byte blob`);
1290 if (exp.split) assert.ok(served[0].bytes > cap, `${where}: the batch was refused under the cap`);
1291 const k = `${path}: ${code}`; tally[k] = (tally[k] ?? 0) + 1;
1292 cell.ms = ms; cell.served = served.reduce((n, r) => n + r.bytes, 0);
1293 } }));
1294 const slowest = cells.reduce((a, b) => (b.ms > a.ms ? b : a));
1295 const endless = cells.filter(c => c.shape === 'endless' && c.rel === 'under');
1296 console.log(' #36 matrix: 630 cells', JSON.stringify(tally), `slowest ${slowest.path}/${slowest.shape}/${slowest.S}/${slowest.rel} ${slowest.ms.toFixed(0)} ms`);
1297 console.log(' endless, cap under R:', endless.map(c => `${c.path} S=${c.S}: ${c.served} B`).join('; '));
1298});
1300test('request timeout survives a GC during a stalled body', { timeout: 8_000 }, async () => {
1301 const child = spawn(process.execPath, ['--expose-gc', 'test/timeout-gc.mjs'], { stdio: ['ignore', 'pipe', 'pipe'] });
1302 let out = '', err = '';
1303 child.stdout.on('data', d => { out += d; });
1304 child.stderr.on('data', d => { err += d; });
1305 const code = await new Promise(resolve => child.on('close', resolve));
1306 assert.equal(code, 0, err + out);
1307});
1309// helpers
1310function wrapPack(pack) { // v2 fetch response: packfile section + sideband band 1 frames + flush
1311 const parts = [pktLine('packfile\n')];
1312 for (let i = 0; i < pack.length; i += 65000) parts.push(pktRaw(concat([Uint8Array.of(1), pack.subarray(i, Math.min(i + 65000, pack.length))])));
1313 parts.push(te.encode('0000'));
1314 return concat(parts);
Diagnostics6 git calls · 260 ms
CallStatusSizeTime0260 ms
discover200191 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=191
git.server.agent=git/github-9922ec520325-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=191
git.pkt.payload.max=36
ls-refs20012 KiB3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=11990
git.response.cache=UPDATING
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=152
git.pkt.data=151
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=11990
git.pkt.payload.max=122
git.ls_refs.count=151
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4347
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
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=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4347
git.pkt.payload.max=4247
git.sideband.band1.bytes=4247
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=4247
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400
git.pack.full=10
Open pack map
fetchtree:0200919 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=919
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=919
git.pkt.payload.max=892
git.sideband.band1.bytes=892
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=892
git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02005.6 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=5724
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=5724
git.pkt.payload.max=5697
git.sideband.band1.bytes=5697
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=5697
git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetch20028 KiB248 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=248
git.response.bytes=28258
git.response.cache=MISS
git.response.duration_ms=248
git.fetch.wants=673007995a0dba4e9bdcc8e42943a87807634a84
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=5
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=28258
git.pkt.payload.max=16384
git.sideband.band1.bytes=28226
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://github.com/ivarvong/ply
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e149d8ee81a140
Request
f7be9249…
Protocol
v2 · sha1
Agent
git/github-9922ec520325-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format