GitHubivarvong

ply

main
ply6 git calls229 ms68 KiB3 cachedprotocol v2
2486 lines193 KiBRaw
1// Branch and message assertions for the mutation gate. Prefix and round-trip tests kill the happy path;
2// these kill a mutant that deletes one check, flips one comparison, or empties one error string.
3import { test, vi } from 'vitest';
4import assert from 'node:assert/strict';
5import zlib from 'node:zlib';
6import { readdirSync, readFileSync } from 'node:fs';
7import { join } from 'node:path';
8import { take, indexOfBytes, bytesOf, te, td, concat, decodeBytes, encodeBytes, decodeChunk, bufferSource } from '../src/bytes.js';
9import {
10 parseTree, parseCommit, parseTag, parseIdent, assertFetched, canonMode, encodeTree, encodeCommit, encodeTag, TYPE, hashObject, ident,
11} from '../src/objects.js';
12import { pkt, pktInit, pktStep, pktFinish, FLUSH, DELIM } from '../src/pktline.js';
13import {
14 parsePack, applyDelta, makeDelta, readObjHeader, readOfsDelta, readUint32BE, packParseOpts, resolvePack,
15} from '../src/packresolve.js';
16import { encodeObjHeader, encodeVarint, buildPack, zlibDeflate, copyOp } from '../src/pack.js';
17import { inflate, huffBuild, adler32 } from '../src/inflate.js';
18import { v2Init, v2Step as v2StepR, parseSideband as parseSidebandR, spInit, spStep as spStepR, spFinish as spFinishR, fetchBody as fetchBodyR, isShortRead } from '../src/fetch.js';
19import { attributeTree, bestBases, mergeBase, mergeBranch } from '../src/merge.js';
20import { rebaseBranch } from '../src/rebase.js';
21import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
22import { parseLsRefLine as parseLsRefLineR, parseUnbornLine, lsRefs as lsRefsR, lsRefsBody as lsRefsBodyR } from '../src/lsrefs.js';
23import { parseCapabilitiesV2 } from '../src/discover.js';
24import { lsRefs as lsRefsAsked } from '../src/client.js';
25import { classifyReport, reportInit, reportStep, reportFinish, decideOutcome, outcomeResult, ngClass, planPush, refusedBeforeReceivePack, reportLineCap, requestCaps, streamInit, streamStep, GIT_UNPACK_ERRORS, push, ZERO_OID } from '../src/push.js';
26import { checkPushCaps, capsProblem, parseReceiveAdvert, pushWithOptions as pushWithOptionsR } from '../src/receivecaps.js';
27import { parseLine, parseFile, UNSET } from '../src/attributes.js';
28import { withGpgsig } from '../src/sign.js';
29import { withLimits, DEFAULT_LIMITS, LIMIT_OBJECT_OVERHEAD as OV, parseLimits, checkLimit } from '../src/limits.js';
30import { validateIdent, isValidatedIdent, validatePushRef } from '../src/validate.js';
31import { createTransport as createTransportR } from '../src/http.js';
32import { verifyRef } from '../src/operation.js';
33import { syncRef } from '../src/sync.js';
34import { negotiate as negotiateR } from '../src/negotiate.js';
35import { parseOid, parseCommitId } from '../src/oid.js';
36import {
37 OID_BYTES, OID_HEX_CHARS, HEX_DIGIT_MIN, HEX_DIGIT_MAX, HEX_LOWER_MIN, HEX_LOWER_MAX, PACK_SIGNATURE, PACK_HEADER_BYTES, PACK_VERSION,
38 DELTA_COPY, DELTA_COPY_OFF1, DELTA_COPY_OFF2, DELTA_COPY_OFF3, DELTA_COPY_OFF4,
39 DELTA_COPY_LEN1, DELTA_COPY_LEN2, DELTA_COPY_LEN3, DELTA_SIZE_MIN, SIDEBAND_PACK, SIDEBAND_PROGRESS, zlibBound,
40} from '../src/format.js';
41import { errOf, unwrap, unwrapAsync } from './unwrap.js';
43// Throwing adapters over the Result API (0.2.0): the assertions below are about the throw.
44const v2Step = (...a) => unwrap(v2StepR(...a));
45const parseSideband = (...a) => unwrap(parseSidebandR(...a));
46const spStep = (...a) => unwrap(spStepR(...a));
47const spFinish = (...a) => unwrap(spFinishR(...a));
48const fetchBody = (...a) => unwrap(fetchBodyR(...a));
49const parseLsRefLine = (...a) => unwrap(parseLsRefLineR(...a));
50const lsRefs = (...a) => unwrapAsync(lsRefsR(...a));
51const lsRefsBody = (...a) => unwrap(lsRefsBodyR(...a));
52const pushWithOptions = (...a) => unwrapAsync(pushWithOptionsR(...a));
53const createTransport = (...a) => unwrap(createTransportR(...a));
54const negotiate = (...a) => unwrapAsync(negotiateR(...a));
55const oid = n => (n.toString(16).padStart(2, '0').repeat(OID_BYTES));
56const oidBytes = n => Uint8Array.from({ length: OID_BYTES }, () => n);
57const throws = (fn, code, message) => assert.throws(fn, e => {
58 assert.equal(e.code, code, e.message);
59 assert.equal(e.message, message);
60 return true;
61});
62const data = (...parts) => concat(parts.map(p => typeof p === 'string' ? te.encode(p) : p));
64test('take, indexOfBytes, and bytesOf reject every out-of-range shape', () => {
65 const buf = Uint8Array.of(1, 2, 3, 4);
66 assert.deepEqual([...unwrap(take(buf, 1, 2))], [2, 3]);
67 assert.equal(unwrap(take(buf, 4, 0)).length, 0);
68 assert.equal(unwrap(take(buf, 0, 1, 'protocol'))[0], 1);
69 for (const [off, n] of [[1.5, 1], [0, 1.5], [NaN, 1], [0, NaN], [-1, 1], [0, -1], [3, 2], [5, 0]]) {
70 throws(() => unwrap(take(buf, off, n)), 'pack_corrupt', `short read: need ${n} bytes at ${off}, have 4`);
71 }
72 throws(() => unwrap(take(undefined, 0, 1)), 'pack_corrupt', 'short read: need 1 bytes at 0, have 0');
73 throws(() => unwrap(take(null, 0, 1, 'protocol')), 'protocol', 'short read: need 1 bytes at 0, have 0');
75 const hay = Uint8Array.of(1, 2, 1, 2, 3);
76 assert.equal(indexOfBytes(hay, new Uint8Array(), 3), 3);
77 assert.equal(indexOfBytes(hay, Uint8Array.of(9)), -1);
78 assert.equal(indexOfBytes(hay, Uint8Array.of(1)), 0);
79 assert.equal(indexOfBytes(hay, Uint8Array.of(2, 3)), 3);
80 assert.equal(indexOfBytes(hay, Uint8Array.of(1, 2, 3), 1), 2);
81 assert.equal(indexOfBytes(hay, Uint8Array.of(1, 9)), -1);
83 const text = te.encode('ab');
84 assert.deepEqual(bytesOf('ab'), text);
85 assert.equal(bytesOf(text), text);
86 assert.equal(bytesOf('').length, 0);
87});
89test('parseTree error strings and the boundaries next to them', () => {
90 const id = oidBytes(4);
91 const raw = (text, extra = id) => data(text, extra);
92 const bad = [
93 ['100644a\0', 'tree: malformed entry'],
94 [' a\0', 'tree: empty mode'],
95 ['100644 \0', 'tree: empty name'],
96 ['8 a\0', 'tree: malformed mode'],
97 ['19 a\0', 'tree: malformed mode'],
98 ['0 a\0', 'tree: zero-padded mode'],
99 ['0100644 a\0', 'tree: zero-padded mode'],
100 ['100644 /x\0', 'tree: full pathname'],
101 ['100644 a/b\0', 'tree: full pathname'],
102 ['100644 .\0', 'tree: bad name'],
103 ['100644 ..\0', 'tree: bad name'],
104 ['100644 .git\0', 'tree: bad name'],
105 ['100644 .GIT\0', 'tree: bad name'],
106 ['100644 .git:x\0', 'tree: bad name'],
107 ['100644 a\\.git\0', 'tree: bad name'],
108 [`100644 ${'l'.repeat(4097)}\0`, 'tree: large pathname'],
109 ['120000 .gitmodules\0', 'tree: .gitmodules is a symbolic link'],
110 ['40000 GITMOD~1\0', 'tree: .gitmodules is not a blob'],
111 ['160000 .gitmodules\0', 'tree: .gitmodules is not a blob'],
112 ['160000 .gitattributes\0', 'tree: .gitattributes is not a blob'],
113 ['40000 .gitattributes\0', 'tree: .gitattributes is not a blob'],
114 ];
115 for (const [text, message] of bad) throws(() => unwrap(parseTree(raw(text))), 'pack_corrupt', message);
116 throws(() => unwrap(parseTree(raw('100644 a\0', id.subarray(0, OID_BYTES - 1)))), 'pack_corrupt', `short read: need ${OID_BYTES} bytes at ${'100644 a\0'.length}, have ${'100644 a\0'.length + OID_BYTES - 1}`);
117 throws(() => unwrap(parseTree(data(raw('100644 b\0'), raw('100644 a\0')))), 'pack_corrupt', 'tree: not strictly sorted');
118 throws(() => unwrap(parseTree(data(raw('100644 a\0'), raw('100644 a\0', oidBytes(5))))), 'pack_corrupt', 'tree: duplicate entry "a"');
119 assert.equal(unwrap(parseTree(new Uint8Array(0))).length, 0);
120 // canon_mode, as git ls-tree prints it; a 4096-byte name is the longest fsck allows
121 assert.equal(unwrap(parseTree(raw('100664 a\0')))[0].mode, '100644');
122 assert.equal(unwrap(parseTree(raw(`100644 ${'l'.repeat(4096)}\0`)))[0].name.length, 4096);
123 throws(() => unwrap(parseTree(raw('100644 a\0', new Uint8Array(OID_BYTES)))), 'pack_corrupt', 'tree: null sha1');
124 assert.equal(unwrap(parseTree(raw('100644 a\0', Uint8Array.of(...new Uint8Array(OID_BYTES - 1), 1))))[0].name, 'a');
125 for (const ok of ['120000 .gitattributes', '120000 .gitignore', '120000 .mailmap', '100644 .gitmodules', '100644 git~2', '40000 a\\.gitattributes']) assert.equal(unwrap(parseTree(raw(`${ok}\0`))).length, 1, ok);
126 // issue #34: one name twice is git fsck duplicateEntries whatever the modes, next to each other or not. A file and a
127 // directory of one name sort apart ('d' < 'd.x' < 'd/'), so the old neighbour-only check passed them. encodeTree
128 // refuses to write such a tree, so the bytes are built by hand.
129 const e = (mode, name, n = 1) => raw(`${mode} ${name}\0`, oidBytes(n));
130 throws(() => unwrap(parseTree(data(e('100644', 'd', 2), e('40000', 'd')))), 'pack_corrupt', 'tree: duplicate entry "d"');
131 throws(() => unwrap(parseTree(data(e('100644', 'd', 2), e('100644', 'd.x'), e('40000', 'd')))), 'pack_corrupt', 'tree: duplicate entry "d"');
132 throws(() => unwrap(parseTree(data(e('120000', 'd', 2), e('100644', 'd-'), e('100644', 'd.x'), e('40000', 'd')))), 'pack_corrupt', 'tree: duplicate entry "d"');
133 throws(() => unwrap(parseTree(data(e('160000', 'd', 2), e('40000', 'd')))), 'pack_corrupt', 'tree: duplicate entry "d"');
134 throws(() => unwrap(parseTree(data(e('40000', 'd', 2), e('40000', 'd')))), 'pack_corrupt', 'tree: duplicate entry "d"');
135 // the same name in a later, unrelated position is still the duplicate, reported before the order it also breaks
136 throws(() => unwrap(parseTree(data(e('100644', 'a'), e('100644', 'b'), e('100644', 'a')))), 'pack_corrupt', 'tree: duplicate entry "a"');
137 // order: a directory sorts as name + '/'. S_ISDIR of the mode decides, as in fsck: `40755` is a directory, a gitlink
138 // is not. Either way round, one of the two lists is out of order.
139 assert.deepEqual(unwrap(parseTree(data(e('100644', 'd.x'), e('40000', 'd')))).map(x => x.name), ['d.x', 'd']);
140 throws(() => unwrap(parseTree(data(e('40000', 'd'), e('100644', 'd.x')))), 'pack_corrupt', 'tree: not strictly sorted');
141 assert.deepEqual(unwrap(parseTree(data(e('100644', 'd.x'), e('40755', 'd')))).map(x => x.mode), ['100644', '40000'], 'canon_mode: 40755 reads as a tree');
142 throws(() => unwrap(parseTree(data(e('40755', 'd'), e('100644', 'd.x')))), 'pack_corrupt', 'tree: not strictly sorted');
143 // fsck keeps the mode in an unsigned short: `1040000` is a directory there, `140000` (S_IFSOCK) is not
144 assert.deepEqual(unwrap(parseTree(data(e('100644', 'd.x'), e('1040000', 'd')))).map(x => x.name), ['d.x', 'd'], 'only the low 16 bits of a mode count');
145 throws(() => unwrap(parseTree(data(e('1040000', 'd'), e('100644', 'd.x')))), 'pack_corrupt', 'tree: not strictly sorted');
146 assert.deepEqual(unwrap(parseTree(data(e('140000', 'd'), e('100644', 'd.x')))).map(x => x.name), ['d', 'd.x']);
147 // past 2^53 a whole-string parse loses the low bits; the last six digits keep them
148 assert.deepEqual(unwrap(parseTree(data(e('100644', 'd.x'), e('777777777777777777777777040000', 'd')))).map(x => x.name), ['d.x', 'd'], 'a long mode is read from its last six digits');
149 // canonMode (the string form checkout reads) and parseTree's byte read agree on a long mode: its last six digits
150 for (const m of ['777777777777777777777777040000', '777777777777777777777777100755', '1040000', '7100644']) assert.equal(canonMode(m), unwrap(parseTree(data(e(m, 'd'))))[0].mode, m);
151 assert.deepEqual(unwrap(parseTree(data(e('100644', 'd'), e('160000', 'd.x')))).map(x => x.mode), ['100644', '160000']);
152 throws(() => unwrap(parseTree(data(e('100644', 'd.x'), e('160000', 'd')))), 'pack_corrupt', 'tree: not strictly sorted');
153 assert.deepEqual(unwrap(parseTree(data(e('40000', 'd'), e('100644', 'd0')))).map(x => x.name), ['d', 'd0'], "'/' sorts below '0'");
154 throws(() => unwrap(parseTree(data(e('100644', 'd0'), e('40000', 'd')))), 'pack_corrupt', 'tree: not strictly sorted');
155 // case-only and NFC/NFD twins are two names to git and to ply
156 assert.deepEqual(unwrap(parseTree(data(e('100644', 'D'), e('100644', 'd')))).map(x => x.name), ['D', 'd']);
157 assert.deepEqual(unwrap(parseTree(data(e('100644', 'e\u0301'), e('100644', '\u00e9')))).map(x => x.name), ['e\u0301', '\u00e9']);
158 // order is on the raw bytes; byte-distinct names stay distinct (decodeBytes is lossless, #43), so the decode-twin refusal
159 // #34 had (U+FFFD twins, a dropped leading BOM) cannot arise and is gone: these parse as two names, as in git
160 const rawE = (mode, bytes) => data(te.encode(`${mode} `), Uint8Array.from(bytes), Uint8Array.of(0), oidBytes(1));
161 throws(() => unwrap(parseTree(data(rawE('100644', [0x64, 0xff]), rawE('100644', [0x64, 0xfe])))), 'pack_corrupt', 'tree: not strictly sorted');
162 assert.deepEqual(unwrap(parseTree(data(rawE('100644', [0x64, 0xfe]), rawE('100644', [0x64, 0xff])))).map(x => x.name), ['d\udcfe', 'd\udcff']);
163 assert.deepEqual(unwrap(parseTree(data(rawE('100644', [0x61]), rawE('100644', [0xef, 0xbb, 0xbf, 0x61])))).map(x => x.name), ['a', '\ufeffa']);
164 assert.deepEqual(unwrap(parseTree(data(rawE('100644', [0x64, 0xc3, 0xa9]), rawE('100644', [0x64, 0xff])))).map(x => x.name), ['d\u00e9', 'd\udcff'], 'raw bytes: 0xc3 < 0xff (0xff decodes to its escape U+DCFF, #43)');
165 assert.deepEqual(unwrap(parseTree(data(rawE('100644', [0x64, 0xef, 0xbf, 0xbe]), rawE('100644', [0x64, 0xf0])))).map(x => x.name.length), [2, 2]);
166 throws(() => unwrap(parseTree(data(rawE('100644', [0x64, 0xf0]), rawE('100644', [0x64, 0xef, 0xbf, 0xbe])))), 'pack_corrupt', 'tree: not strictly sorted');
167 // a leading U+FEFF is a byte of the name to git; the default TextDecoder would drop it and `README` would be found (#43)
168 assert.equal(unwrap(parseTree(raw('100644 \ufeffREADME\0')))[0].name, '\ufeffREADME');
169 assert.equal(td.decode(te.encode('\ufeffX')), '\ufeffX');
170 // and a mode is not a name: `\ufeff100644` is malformed, as git's ls-tree says
171 throws(() => unwrap(parseTree(raw('\ufeff100644 a\0'))), 'pack_corrupt', 'tree: malformed mode');
172 // ill-formed UTF-8 names are escaped byte by byte, sort and re-encode as their bytes
173 const latin = data(Uint8Array.from([...te.encode('100644 caf'), 0xe9, 0]), oidBytes(1), Uint8Array.from([...te.encode('100644 caf'), 0xe9, 0x41, 0]), oidBytes(2));
174 assert.deepEqual(unwrap(parseTree(latin)).map(e => e.name), ['caf\udce9', 'caf\udce9A']);
175 assert.deepEqual(unwrap(encodeTree(unwrap(parseTree(latin)))), latin);
176 // a directory sorts as name + '/' on the raw bytes: `a\xff` (file) after `a/`-keyed dir `a`
177 assert.deepEqual(unwrap(parseTree(data(raw('40000 a\0', oidBytes(1)), Uint8Array.from([...te.encode('100644 a'), 0xff, 0]), oidBytes(2)))).map(e => e.name), ['a', 'a\udcff']);
178 throws(() => unwrap(parseTree(data(Uint8Array.from([...te.encode('100644 a'), 0xff, 0]), oidBytes(2), raw('40000 a\0', oidBytes(1))))), 'pack_corrupt', 'tree: not strictly sorted');
179});
181test('decodeBytes / encodeBytes: a bijection on bytes; decodeChunk holds a cut sequence (#43)', () => {
182 const cases = [[0xef, 0xbb, 0xbf, 0x52], [0x63, 0xe9], [0xc0, 0xae], [0xe0, 0x80, 0xae], [0xed, 0xa0, 0x80], [0xed, 0x9f, 0xbf], [0xf0, 0x8f, 0xbf, 0xbf], [0xf0, 0x90, 0x80, 0x80], [0xf4, 0x8f, 0xbf, 0xbf], [0xf4, 0x90, 0x80, 0x80], [0xf5, 0x80], [0xc1, 0xbf], [0xc2, 0x80], [0xdf, 0xbf], [0xe2, 0x82], [0x80], [0xff], [0x41, 0, 0x0d], [0xc3, 0xa9, 0xff]];
183 const want = ['\ufeffR', 'c\udce9', '\udcc0\udcae', '\udce0\udc80\udcae', '\udced\udca0\udc80', '\ud7ff', '\udcf0\udc8f\udcbf\udcbf', '\u{10000}', '\u{10ffff}', '\udcf4\udc90\udc80\udc80', '\udcf5\udc80', '\udcc1\udcbf', '\u0080', '\u07ff', '\udce2\udc82', '\udc80', '\udcff', 'A\0\r', '\u00e9\udcff'];
184 cases.forEach((c, i) => {
185 const b = Uint8Array.from(c);
186 assert.equal(decodeBytes(b), want[i], c.join(' '));
187 assert.deepEqual(unwrap(encodeBytes(want[i])), b, c.join(' '));
188 });
189 for (const [s, msg] of [['\ud800', 'x: lone surrogate U+D800 is no byte sequence'], ['a\udfff', 'x: lone surrogate U+DFFF is no byte sequence'], ['\udc7f', 'x: lone surrogate U+DC7F is no byte sequence'], ['\udcc3\udca9', 'x: escaped bytes spell well-formed UTF-8; pass the characters']]) throws(() => unwrap(encodeBytes(s, 'x')), 'bad_request', msg);
190 throws(() => unwrap(encodeBytes(1, 'x')), 'bad_request', 'x must be a string');
191 assert.deepEqual(unwrap(encodeBytes('\u{1f680}\udcff')), Uint8Array.from([0xf0, 0x9f, 0x9a, 0x80, 0xff]));
192 // a well-formed string takes the TextEncoder path; a lone escape at either end is still found
193 assert.deepEqual(unwrap(encodeBytes('\udcffa')), Uint8Array.from([0xff, 0x61]));
194 let d = decodeChunk(new Uint8Array(0), Uint8Array.from([0x61, 0xf0, 0x9f]));
195 assert.deepEqual([d.text, [...d.carry]], ['a', [0xf0, 0x9f]]);
196 d = decodeChunk(d.carry, Uint8Array.from([0x9a, 0x80, 0xe2]));
197 assert.deepEqual([d.text, [...d.carry]], ['\u{1f680}', [0xe2]]);
198 d = decodeChunk(d.carry, Uint8Array.from([0x82, 0xac]));
199 assert.deepEqual([d.text, [...d.carry]], ['\u20ac', []]);
200 // a complete sequence at the end is not held; nor a stray continuation byte
201 assert.deepEqual([...decodeChunk(new Uint8Array(0), Uint8Array.from([0xc3, 0xa9])).carry], []);
202 assert.equal(decodeChunk(new Uint8Array(0), Uint8Array.from([0x80])).text, '\ufffd');
203 assert.deepEqual([...decodeChunk(new Uint8Array(0), Uint8Array.from([0xe2, 0x82, 0x80, 0x80])).carry], []);
204});
206// bufferSource is the BufferSource boundary (bytes.ts): an ArrayBuffer view is returned as itself, and a
207// SharedArrayBuffer view is copied into a fresh ArrayBuffer. Both arms have to be observed or the copy is uncovered.
208test('bufferSource keeps an ArrayBuffer view and copies a SharedArrayBuffer view', () => {
209 const own = Uint8Array.of(1, 2, 3, 4);
210 assert.equal(bufferSource(own), own);
211 assert.ok(bufferSource(own).buffer instanceof ArrayBuffer);
212 const wide = new Uint8Array([9, 8, 7, 6, 5]);
213 const view = wide.subarray(1, 4);
214 assert.equal(bufferSource(view), view);
215 assert.deepEqual([...bufferSource(view)], [8, 7, 6]);
216 const empty = new Uint8Array(0);
217 assert.equal(bufferSource(empty), empty);
218 const shared = new Uint8Array(new SharedArrayBuffer(4));
219 shared.set([9, 8, 7, 6]);
220 const copy = bufferSource(shared);
221 assert.notEqual(copy, shared);
222 assert.ok(copy.buffer instanceof ArrayBuffer);
223 assert.ok(!(copy.buffer instanceof SharedArrayBuffer));
224 assert.deepEqual([...copy], [9, 8, 7, 6]);
225 copy[0] = 1;
226 assert.equal(shared[0], 9);
227 const sharedEmpty = new Uint8Array(new SharedArrayBuffer(0));
228 const emptyCopy = bufferSource(sharedEmpty);
229 assert.notEqual(emptyCopy, sharedEmpty);
230 assert.equal(emptyCopy.byteLength, 0);
231 assert.ok(emptyCopy.buffer instanceof ArrayBuffer);
232});
234test('commit and tag headers, idents, and assertFetched', () => {
235 const author = 'Ada <ada@ex> 1700000000 +0000';
236 const tree = oid(1);
237 const C = 'commit: ', T = 'tag: ';
238 // verify_headers: a NUL before the blank line, and a header that does not end in a newline
239 throws(() => unwrap(parseCommit(te.encode('tree'))), 'pack_corrupt', `${C}unterminatedHeader: unterminated header`);
240 throws(() => unwrap(parseCommit(new Uint8Array(0))), 'pack_corrupt', `${C}unterminatedHeader: unterminated header`);
241 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\0\n\n`))), 'pack_corrupt', `${C}nulInHeader: unterminated header: NUL at offset 45`);
242 throws(() => unwrap(parseCommit(te.encode('\0'))), 'pack_corrupt', `${C}nulInHeader: unterminated header: NUL at offset 0`);
243 throws(() => unwrap(parseCommit(te.encode('nospace\n'))), 'pack_corrupt', `${C}missingTree: invalid format - expected 'tree' line`);
244 throws(() => unwrap(parseCommit(te.encode('\n\nbody'))), 'pack_corrupt', `${C}missingTree: invalid format - expected 'tree' line`);
245 throws(() => unwrap(parseCommit(te.encode(`tree\n${tree}\n`))), 'pack_corrupt', `${C}missingTree: invalid format - expected 'tree' line`);
246 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\n`))), 'pack_corrupt', `${C}missingAuthor: invalid format - expected 'author' line`);
247 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\n parent-sig\n\n`))), 'pack_corrupt', `${C}missingAuthor: invalid format - expected 'author' line`);
248 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\nauthor ${author}\n`))), 'pack_corrupt', `${C}missingCommitter: invalid format - expected 'committer' line`);
249 // the tree and parent ids: 40 hex digits of either case, then the newline; git prints the id lowercase
250 throws(() => unwrap(parseCommit(te.encode(`tree zz\nauthor ${author}\ncommitter ${author}\n\n`))), 'pack_corrupt', `${C}badTreeSha1: invalid 'tree' line format - bad sha1`);
251 throws(() => unwrap(parseCommit(te.encode(`tree ${tree.slice(1)}g\nauthor ${author}\ncommitter ${author}\n\n`))), 'pack_corrupt', `${C}badTreeSha1: invalid 'tree' line format - bad sha1`);
252 throws(() => unwrap(parseCommit(te.encode(`tree ${tree} \nauthor ${author}\ncommitter ${author}\n\n`))), 'pack_corrupt', `${C}badTreeSha1: invalid 'tree' line format - bad sha1`);
253 throws(() => unwrap(parseCommit(te.encode(`tree ${tree.slice(1)}\nauthor ${author}\ncommitter ${author}\n\n`))), 'pack_corrupt', `${C}badTreeSha1: invalid 'tree' line format - bad sha1`);
254 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\nparent zz\nauthor ${author}\ncommitter ${author}\n\n`))), 'pack_corrupt', `${C}badParentSha1: invalid 'parent' line format - bad sha1`);
255 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\nparent ${oid(2)}x\nauthor ${author}\ncommitter ${author}\n\n`))), 'pack_corrupt', `${C}badParentSha1: invalid 'parent' line format - bad sha1`);
256 const upper = unwrap(parseCommit(te.encode(`tree ${'AB'.repeat(20)}\nparent ${'Cd'.repeat(20)}\nauthor ${author}\ncommitter ${author}\n\n`)));
257 assert.equal(upper.tree, 'ab'.repeat(20));
258 assert.deepEqual(upper.parents, ['cd'.repeat(20)]);
259 assert.equal(unwrap(parseCommit(te.encode(`tree ${'9f'.repeat(20)}\nauthor ${author}\ncommitter ${author}\n`))).tree, '9f'.repeat(20));
260 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\nauthor ${author}\nauthor ${author}\ncommitter ${author}\n\n`))), 'pack_corrupt', `${C}multipleAuthors: invalid format - multiple 'author' lines`);
261 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\nauthor Ada\ncommitter ${author}\n\n`))), 'pack_corrupt', `${C}missingEmail: invalid author line - missing email`);
262 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\nauthor ${author}\ncommitter Ada\n\n`))), 'pack_corrupt', `${C}missingEmail: invalid committer line - missing email`);
263 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\nauthor Ada <ada@ex>\ncommitter ${author}\n\n`))), 'pack_corrupt', `${C}missingSpaceBeforeDate: invalid author line - missing space before date`);
264 // the body: no NUL anywhere in a commit (nulInCommit is WARN, an error under --strict)
265 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\nauthor ${author}\ncommitter ${author}\n\nsub\0ject\n`))), 'pack_corrupt', `${C}nulInCommit: NUL byte in the commit object body`);
266 throws(() => unwrap(parseCommit(te.encode(`tree ${tree}\nauthor ${author}\ncommitter ${author}\n\n\0`))), 'pack_corrupt', `${C}nulInCommit: NUL byte in the commit object body`);
267 const cont = unwrap(parseCommit(te.encode(`tree ${tree}\nauthor ${author}\ncommitter ${author}\n parent-sig\n\n`)));
268 assert.deepEqual(cont.extra, [' parent-sig']);
269 assert.equal(cont.encoding, null);
270 assert.equal(cont.message, '');
271 const noBody = unwrap(parseCommit(te.encode(`tree ${tree}\nauthor ${author}\ncommitter ${author}\nsubject\n`)));
272 assert.deepEqual(noBody.extra, ['subject']);
273 assert.equal(noBody.message, null);
274 assert.deepEqual(unwrap(parseCommit(te.encode(`tree ${tree}\nauthor ${author}\ncommitter ${author}\n`))).extra, []);
275 assert.equal(unwrap(parseCommit(te.encode(`tree ${tree}\nauthor ${author}\ncommitter ${author}\n\n\nm`))).message, '\nm');
276 // a leading BOM is a byte before `tree`, as git reads it
277 throws(() => unwrap(parseCommit(concat([Uint8Array.of(0xef, 0xbb, 0xbf), te.encode(`tree ${tree}\nauthor ${author}\ncommitter ${author}\n\n`)]))), 'pack_corrupt', `${C}missingTree: invalid format - expected 'tree' line`);
279 const commit = unwrap(encodeCommit({ tree, parents: [oid(2)], author, message: 'hi\n' }));
280 assert.equal(unwrap(parseCommit(commit)).tree, tree);
281 assert.deepEqual(unwrap(parseCommit(commit)).parents, [oid(2)]);
282 unwrap(assertFetched('commit', commit));
283 throws(() => unwrap(assertFetched('commit', te.encode(`author ${author}\ncommitter ${author}\n\n`))), 'pack_corrupt', `${C}missingTree: invalid format - expected 'tree' line`);
284 assert.deepEqual(assertFetched('blob', te.encode('x')), { ok: true, value: { tag: 'none' } });
285 assert.deepEqual(assertFetched('ofs_delta', te.encode('x')), { ok: true, value: { tag: 'none' } });
286 assert.deepEqual(assertFetched('ref_delta', te.encode('x')), { ok: true, value: { tag: 'none' } });
287 throws(() => unwrap(assertFetched('submodule', te.encode('x'))), 'pack_corrupt', 'object: bad type submodule');
288 throws(() => unwrap(assertFetched('tree', data(' a\0', oidBytes(1)))), 'pack_corrupt', 'tree: empty mode');
289 throws(() => unwrap(assertFetched('tag', te.encode('x'))), 'pack_corrupt', `${T}unterminatedHeader: unterminated header`);
291 const tagger = author;
292 const tag = unwrap(encodeTag({ object: oid(3), type: 'commit', tag: 'v1', tagger, message: 'rel\n' }));
293 assert.equal(unwrap(parseTag(tag)).tag, 'v1');
294 unwrap(assertFetched('tag', tag));
295 throws(() => unwrap(parseTag(te.encode('tag'))), 'pack_corrupt', `${T}unterminatedHeader: unterminated header`);
296 throws(() => unwrap(parseTag(te.encode(`object ${oid(3)}\0\n`))), 'pack_corrupt', `${T}nulInHeader: unterminated header: NUL at offset 47`);
297 throws(() => unwrap(parseTag(te.encode('\n\n'))), 'pack_corrupt', `${T}missingObject: invalid format - expected 'object' line`);
298 throws(() => unwrap(parseTag(te.encode(`type commit\ntag v1\ntagger ${tagger}\n\n`))), 'pack_corrupt', `${T}missingObject: invalid format - expected 'object' line`);
299 throws(() => unwrap(parseTag(te.encode(`object ${oid(3).slice(2)}\ntype commit\n\n`))), 'pack_corrupt', `${T}badObjectSha1: invalid 'object' line format - bad sha1`);
300 throws(() => unwrap(parseTag(te.encode(`object ${oid(3)}\n\n`))), 'pack_corrupt', `${T}missingTypeEntry: invalid format - expected 'type' line`);
301 throws(() => unwrap(parseTag(te.encode(`object ${oid(3)}\ntag v1\ntagger ${tagger}\n\n`))), 'pack_corrupt', `${T}missingTypeEntry: invalid format - expected 'type' line`);
302 for (const bad of ['commi', '', 'commit ', 'Commit', 'ofs_delta', 'constructor', 'toString'])
303 throws(() => unwrap(parseTag(te.encode(`object ${oid(3)}\ntype ${bad}\ntag v1\n\n`))), 'pack_corrupt', `${T}badType: invalid 'type' value`);
304 for (const type of ['commit', 'tree', 'blob', 'tag']) assert.equal(unwrap(parseTag(te.encode(`object ${oid(3)}\ntype ${type}\ntag v1\n`))).type, type);
305 throws(() => unwrap(parseTag(te.encode(`object ${oid(3)}\ntype commit\n\n`))), 'pack_corrupt', `${T}missingTagEntry: invalid format - expected 'tag' line`);
306 throws(() => unwrap(parseTag(te.encode(`object ${oid(3)}\ntype commit\ntagger ${tagger}\n\n`))), 'pack_corrupt', `${T}missingTagEntry: invalid format - expected 'tag' line`);
307 throws(() => unwrap(parseTag(te.encode(`object ${oid(3)}\ntype commit\ntag v1\ntagger Ada\n\n`))), 'pack_corrupt', `${T}missingEmail: invalid tagger line - missing email`);
308 // INFO under --strict, so accepted: no tagger (missingTaggerEntry) and a name that is not a refname (badTagName)
309 const old = unwrap(parseTag(te.encode(`object ${oid(3)}\ntype commit\ntag v0.99\n\nold\n`)));
310 assert.equal(old.tagger, null);
311 assert.equal(old.message, 'old\n');
312 assert.deepEqual(old.extra, []);
313 assert.deepEqual(unwrap(parseTag(te.encode(`object ${oid(3)}\ntype commit\ntag v1\nfoo bar\n\n`))).extra, ['foo bar']);
314 assert.equal(unwrap(parseTag(te.encode(`object ${oid(3)}\ntype commit\ntag \ntagger ${tagger}\n\n`))).tag, '');
315 assert.equal(unwrap(parseTag(te.encode(`object ${oid(3)}\ntype commit\ntag v1..2\ntagger ${tagger}\n\n`))).tag, 'v1..2');
316 // fsck never reads a tag's body: a NUL there is accepted, unlike in a commit
317 assert.equal(unwrap(parseTag(te.encode(`object ${oid(3)}\ntype commit\ntag v1\ntagger ${tagger}\n\nr\0l\n`))).message, 'r\0l\n');
318 assert.equal(unwrap(parseTag(te.encode(`object ${'AB'.repeat(20)}\ntype commit\ntag v1\n`))).object, 'ab'.repeat(20));
320 assert.equal(parseIdent(1), null);
321 assert.deepEqual(parseIdent(' bare name '), { name: 'bare name', email: '', time: null, tz: '' });
322 assert.deepEqual(parseIdent('foo >bar'), { name: 'foo >bar', email: '', time: null, tz: '' });
323 assert.deepEqual(parseIdent('<ada@ex> 1700000000 +0000'), { name: '', email: 'ada@ex', time: '1700000000', tz: '+0000' });
324 assert.deepEqual(parseIdent(' Ada <ada@ex> 1700000000 +0000'), { name: ' Ada', email: 'ada@ex', time: '1700000000', tz: '+0000' });
325 assert.deepEqual(parseIdent('Ada <ada@ex> nope +0100'), { name: 'Ada', email: 'ada@ex', time: null, tz: '+0100' });
326});
328test('headers after the committer and the tagger are kept, not judged, except a second copy (PR #7)', () => {
329 const author = 'Ada <ada@ex> 1700000000 +0000';
330 const other = 'Bea <bea@ex> 1700000001 +0000';
331 const tree = oid(1);
332 const tree2 = oid(2);
333 const committer = author;
334 const body = (lines) => te.encode(`${lines.join('\n')}\n\nmsg\n`);
335 const ok = unwrap(parseCommit(body([`tree ${tree}`, `parent ${oid(3)}`, `parent ${oid(4)}`, `author ${author}`, `committer ${committer}`, 'encoding UTF-8', 'gpgsig -----BEGIN', ' signature', 'mergetag object x'])));
336 assert.equal(ok.tree, tree);
337 assert.deepEqual(ok.parents, [oid(3), oid(4)]);
338 assert.equal(ok.author, author);
339 assert.equal(ok.committer, committer);
340 assert.equal(ok.encoding, 'UTF-8');
341 assert.deepEqual(ok.extra, ['gpgsig -----BEGIN', ' signature', 'mergetag object x']);
342 assert.equal(ok.message, 'msg\n');
343 const again = unwrap(parseCommit(unwrap(encodeCommit(ok))));
344 assert.equal(again.encoding, 'UTF-8');
345 assert.deepEqual(again.extra, ok.extra);
346 const bare = unwrap(encodeCommit({ tree, parents: [oid(2)], author, message: 'hi\n' }));
347 assert.equal(td.decode(bare).includes('\nencoding '), false);
348 assert.equal(unwrap(parseCommit(bare)).encoding, null);
349 const enc = unwrap(encodeCommit({ tree, author, encoding: 'ISO-8859-1', message: 'hi\n' }));
350 assert.equal(unwrap(parseCommit(enc)).encoding, 'ISO-8859-1');
351 assert.deepEqual([...unwrap(encodeCommit(unwrap(parseCommit(enc))))], [...enc]);
352 const signed = unwrap(withGpgsig(enc, '-----BEGIN SSH SIGNATURE-----\nAAA\n-----END SSH SIGNATURE-----\n'));
353 const parsedSig = unwrap(parseCommit(signed));
354 assert.equal(parsedSig.encoding, 'ISO-8859-1');
355 assert.equal(parsedSig.extra[0].startsWith('gpgsig '), true);
356 assert.equal(unwrap(parseCommit(body([`tree ${tree}`, `author ${author}`, `committer ${committer}`, 'gpgsig x']))).extra[0], 'gpgsig x');
357 const plain = unwrap(encodeCommit({ tree, author, message: 'hi\n', extra: ['x-custom v'] }));
358 const signedPlain = td.decode(unwrap(withGpgsig(plain, 'SIG\n')));
359 const plainHeaders = signedPlain.split('\n\n')[0].split('\n');
360 const ci = plainHeaders.findIndex(l => l.startsWith('committer '));
361 assert.equal(plainHeaders[ci + 1].startsWith('gpgsig '), true);
362 assert.equal(plainHeaders[ci + 2], 'x-custom v');
363 assert.equal(unwrap(parseCommit(unwrap(withGpgsig(bare, 'SIG\n')))).extra[0].startsWith('gpgsig '), true);
364 // after the committer fsck reads nothing, but a second tree, parent, author, committer or encoding is a copy git's
365 // views disagree on (pretty's %an/%cn read the last, %T and the date the first): refused, typed (PR #7)
366 const after = (...lines) => unwrap(parseCommit(body([`tree ${tree}`, `author ${author}`, `committer ${committer}`, ...lines])));
367 const dup = (label, lines) => assert.throws(() => after(...lines), e => e.code === 'pack_corrupt' && e.message === label && e.msgId === 'duplicateHeader');
368 dup('commit: duplicate tree', [`tree ${tree2}`]);
369 dup('commit: duplicate tree', ['gpgsig x', `tree ${tree2}`]);
370 dup('commit: bad header order', [`parent ${oid(3)}`]);
371 dup('commit: duplicate author', [`author ${other}`]);
372 dup('commit: duplicate author', ['encoding UTF-8', `author ${other}`]);
373 dup('commit: duplicate committer', [`committer ${other}`]);
374 dup('commit: duplicate committer', ['committer garbage']);
375 dup('commit: duplicate encoding', ['encoding ISO-8859-1', 'encoding UTF-8']);
376 dup('commit: duplicate encoding', ['encoding ISO-8859-1', 'gpgsig x', 'encoding UTF-8']);
377 assert.deepEqual(after('foo').extra, ['foo']);
378 assert.deepEqual(after('treehouse x', 'authors y', ' tree z').extra, ['treehouse x', 'authors y', ' tree z']);
379 assert.equal(after('gpgsig x', ' y', 'encoding ISO-8859-1').encoding, 'ISO-8859-1');
380 assert.equal(after(' encoding ISO-8859-1').encoding, null);
381 assert.equal(after('encodingISO').encoding, null);
382 // before the committer, the order is fsck's: tree, parents, one author, committer
383 const bad = (lines, msgId, why) => throws(() => unwrap(parseCommit(body(lines))), 'pack_corrupt', `commit: ${msgId}: ${why}`);
384 const noAuthor = ['missingAuthor', "invalid format - expected 'author' line"];
385 const noCommitter = ['missingCommitter', "invalid format - expected 'committer' line"];
386 bad([`parent ${oid(3)}`, `tree ${tree}`, `author ${author}`, `committer ${committer}`], 'missingTree', "invalid format - expected 'tree' line");
387 bad([`author ${author}`, `tree ${tree}`, `committer ${committer}`], 'missingTree', "invalid format - expected 'tree' line");
388 bad([`tree ${tree}`, `tree ${tree2}`, `author ${author}`, `committer ${committer}`], ...noAuthor);
389 bad([`tree ${tree}`, `committer ${committer}`], ...noAuthor);
390 bad([`tree ${tree}`, 'encoding UTF-8', `author ${author}`, `committer ${committer}`], ...noAuthor);
391 bad([`tree ${tree}`, ' parent-sig', `author ${author}`, `committer ${committer}`], ...noAuthor);
392 bad([`tree ${tree}`, 'x-custom v', `author ${author}`, `committer ${committer}`], ...noAuthor);
393 bad([`tree ${tree}`, `committer ${committer}`, `author ${author}`], ...noAuthor);
394 bad([`tree ${tree}`, `author ${author}`, 'encoding UTF-8', `committer ${committer}`], ...noCommitter);
395 bad([`tree ${tree}`, `author ${author}`, `parent ${oid(3)}`, `committer ${committer}`], ...noCommitter);
396 const twoParents = unwrap(parseCommit(body([`tree ${tree}`, `parent ${oid(3)}`, `parent ${oid(3)}`, `author ${author}`, `committer ${committer}`])));
397 assert.deepEqual(twoParents.parents, [oid(3), oid(3)]);
399 const tags = unwrap(parseTag(body([`object ${oid(5)}`, 'type commit', 'tag v1', `tagger ${author}`, 'gpgsig abc'])));
400 assert.equal(tags.object, oid(5));
401 assert.equal(tags.type, 'commit');
402 assert.equal(tags.tag, 'v1');
403 assert.equal(tags.tagger, author);
404 assert.deepEqual(tags.extra, ['gpgsig abc']);
405 assert.equal(tags.message, 'msg\n');
406 assert.deepEqual(unwrap(parseTag(body([`object ${oid(5)}`, 'type commit', 'tag v1', `tagger ${author}`, 'gpgsig abc', ' more']))).extra, ['gpgsig abc', ' more']);
407 // after the tagger fsck reads nothing (extraHeaderEntry is IGNORE), but a second object, type, tag or tagger is
408 // refused like a second author (PR #7)
409 const tagAfter = (...lines) => unwrap(parseTag(body([`object ${oid(5)}`, 'type commit', 'tag v1', `tagger ${author}`, ...lines])));
410 assert.throws(() => tagAfter(`tagger ${other}`), e => e.code === 'pack_corrupt' && e.message === 'tag: duplicate tagger' && e.msgId === 'duplicateHeader');
411 assert.throws(() => tagAfter('gpgsig x', `tagger ${other}`), e => e.message === 'tag: duplicate tagger');
412 assert.throws(() => tagAfter(`object ${oid(6)}`), e => e.message === 'tag: duplicate object' && e.msgId === 'duplicateHeader');
413 assert.throws(() => tagAfter('type blob'), e => e.message === 'tag: duplicate type');
414 assert.throws(() => tagAfter('gpgsig x', 'tag v2'), e => e.message === 'tag: duplicate tag');
415 assert.deepEqual(tagAfter('objection x', 'typed y', 'tagline z').extra, ['objection x', 'typed y', 'tagline z']);
416 const tagBad = (lines, msgId, why) => throws(() => unwrap(parseTag(body(lines))), 'pack_corrupt', `tag: ${msgId}: ${why}`);
417 tagBad([`object ${oid(5)}`, `object ${oid(6)}`, 'type commit', 'tag v1', `tagger ${author}`], 'missingTypeEntry', "invalid format - expected 'type' line");
418 tagBad([`object ${oid(5)}`, 'type commit', 'type blob', 'tag v1', `tagger ${author}`], 'missingTagEntry', "invalid format - expected 'tag' line");
419 tagBad(['type commit', `object ${oid(5)}`, 'tag v1', `tagger ${author}`], 'missingObject', "invalid format - expected 'object' line");
420 tagBad(['gpgsig x', `object ${oid(5)}`, 'type commit', 'tag v1', `tagger ${author}`], 'missingObject', "invalid format - expected 'object' line");
421 // a tag line followed by anything but a tagger: missingTaggerEntry (INFO), so the rest is extra, but a second copy
422 // of a tag header further down (a tagger git's %(tagger) would show while fsck saw none) is refused
423 assert.deepEqual(unwrap(parseTag(body([`object ${oid(5)}`, 'type commit', 'tag v1', 'x y']))).extra, ['x y']);
424 assert.throws(() => unwrap(parseTag(body([`object ${oid(5)}`, 'type commit', 'tag v1', 'tag v2', `tagger ${author}`]))), e => e.message === 'tag: duplicate tag');
425 assert.throws(() => unwrap(parseTag(body([`object ${oid(5)}`, 'type commit', 'tag v1', 'x y', `tagger ${author}`]))), e => e.message === 'tag: duplicate tagger');
426 assert.equal(unwrap(parseTag(te.encode(`object ${oid(5)}\ntype commit\ntag \n`))).tag, '');
427});
429test('fsck_ident: every rule, in git\'s order, on every role', () => {
430 const tree = oid(1);
431 const who = tz => `A <a@b.c> 1700000000 ${tz}`;
432 const commit = (author, committer = who('+0000')) => te.encode(`tree ${tree}\nauthor ${author}\ncommitter ${committer}\n\nm\n`);
433 const tag = tagger => te.encode(`object ${tree}\ntype commit\ntag v1\ntagger ${tagger}\n\n`);
434 const all = (ident, msgId, why) => {
435 throws(() => unwrap(assertFetched('commit', commit(ident))), 'pack_corrupt', `commit: ${msgId}: invalid author line - ${why}`);
436 throws(() => unwrap(assertFetched('commit', commit(who('+0000'), ident))), 'pack_corrupt', `commit: ${msgId}: invalid committer line - ${why}`);
437 throws(() => unwrap(assertFetched('tag', tag(ident))), 'pack_corrupt', `tag: ${msgId}: invalid tagger line - ${why}`);
438 };
439 const fine = ident => { unwrap(assertFetched('commit', commit(ident))); unwrap(assertFetched('commit', commit(who('+0000'), ident))); unwrap(assertFetched('tag', tag(ident))); };
440 for (const tz of ['+0000', '-0000', '+0530', '-1200', '+1400', '+9999', '+1560', '+2400', '+0059']) fine(who(tz));
441 // fsck skips spaces and tabs before the timestamp; a timestamp of 0 is not zero-padded; any name and email bytes
442 fine('A <a@b.c> 1700000000 +0000');
443 fine('A <a@b.c> \t1700000000 +0530');
444 fine('A <a@b.c> 0 -1200');
445 fine('A <a@b.c> 1700000009 +0000');
446 fine('A <a@b.c> 9223372036854775807 +0000');
447 fine(' <a@b.c> 1700000000 +0000');
448 fine('A <> 1700000000 +0000');
449 fine('A <a b> 1700000000 +0000');
450 fine('A\t <a@b.c> 1700000000 +0000');
451 const badTz = ['+000', '+00000', '+00', '+', '0000', '+0a00', '+00:00', 'Z', '+0000x', '+0000 ', '+0000\t', ' +0000', '+00 00', '++000', '+-000', '+0000 extra', '+000a', 'a0000'];
452 for (const tz of badTz) all(who(tz), 'badTimezone', 'bad time zone');
453 all('A <a@b.c> 1700000000', 'badDate', 'bad date');
454 all('A <a@b.c> 12:34 +0000', 'badDate', 'bad date');
455 all('Ada <ada@ex> soon +0000', 'badDate', 'bad date');
456 all('A <a@b.c> +0000', 'badDate', 'bad date');
457 all('A <a@b.c> 1700000000\t+0000', 'badDate', 'bad date');
458 all('A <a@b.c> \t', 'badDate', 'bad date');
459 all('A <a@b.c> 01700000000 +0000', 'zeroPaddedDate', 'zero-padded date');
460 all('A <a@b.c> 00 +0000', 'zeroPaddedDate', 'zero-padded date');
461 all('A <a@b.c> 0x +0000', 'zeroPaddedDate', 'zero-padded date');
462 all('A <a@b.c> 9223372036854775808 +0000', 'badDateOverflow', 'date causes integer overflow');
463 all('A <a@b.c> 99999999999999999999999 +0000', 'badDateOverflow', 'date causes integer overflow');
464 all('A <a@b.c> 99999999999999999999999x +0000', 'badDateOverflow', 'date causes integer overflow');
465 all('A <a@b.c>1700000000 +0000', 'missingSpaceBeforeDate', 'missing space before date');
466 all('A <a@b.c>', 'missingSpaceBeforeDate', 'missing space before date');
467 all('A <a@b.c 1700000000 +0000', 'badEmail', 'bad email');
468 all('A <a<b@c> 1700000000 +0000', 'badEmail', 'bad email');
469 all('A <a@b.c', 'badEmail', 'bad email');
470 // a space is required immediately before '<'. commit#23000087 replaced it with '!'; commit#19000059 with 0x9E.
471 all('A<a@b.c> 1700000000 +0000', 'missingSpaceBeforeEmail', 'missing space before email');
472 all('A!<a@b.c> 1700000000 +0000', 'missingSpaceBeforeEmail', 'missing space before email');
473 all('A\t<a@b.c> 1700000000 +0000', 'missingSpaceBeforeEmail', 'missing space before email');
474 all('<a@b.c> 1700000000 +0000', 'missingNameBeforeEmail', 'missing space before email');
475 all('A > <a@b.c> 1700000000 +0000', 'badName', 'bad name');
476 all('A a@b.c> 1700000000 +0000', 'badName', 'bad name');
477 all('A U Thor 1700000000 +0000', 'missingEmail', 'missing email');
478 all('', 'missingEmail', 'missing email');
479 const glued = concat([
480 te.encode(`tree ${tree}\nauthor A <a@b.c> 1700000000 +0000\ncommitter A `),
481 Uint8Array.of(0x9e),
482 te.encode('<a@b.c> 1700000000 +0000\n\nm\n'),
483 ]);
484 throws(() => unwrap(assertFetched('commit', glued)), 'pack_corrupt', 'commit: missingSpaceBeforeEmail: invalid committer line - missing space before email');
485 // commit#1000022: 0xC1 inserted into the author zone, Latin-1 "+00Á00"
486 const poison = concat([
487 te.encode(`tree ${tree}\nauthor A <a@b.c> 1700000000 +00`),
488 Uint8Array.of(0xc1),
489 te.encode('00\ncommitter A <a@b.c> 1700000000 +0000\n\nm\n'),
490 ]);
491 throws(() => unwrap(assertFetched('commit', poison)), 'pack_corrupt', 'commit: badTimezone: invalid author line - bad time zone');
492 // a non-UTF-8 name is bytes git accepts
493 unwrap(assertFetched('commit', concat([te.encode(`tree ${tree}\nauthor J`), Uint8Array.of(0xf6), te.encode(`rg <j@b.c> 1 +0000\ncommitter ${who('+0000')}\n\n`)])));
494});
496test('pkt-line kinds, counters, and length errors', () => {
497 const step = chunk => unwrap(pktStep(pktInit(), chunk));
498 throws(() => step(te.encode('zzzz')), 'protocol', 'bad pkt-line length at byte 0');
499 throws(() => step(te.encode('0003')), 'protocol', 'invalid pkt-line length 3');
500 const one = step(concat([te.encode('0002'), FLUSH, DELIM, unwrap(pkt('ab'))]));
501 assert.deepEqual(one.frames.map(f => f.kind), ['end', 'flush', 'delim', 'data']);
502 assert.equal(td.decode(one.frames[3].payload), 'ab');
503 assert.equal(one.state.end, 1);
504 assert.equal(one.state.flush, 1);
505 assert.equal(one.state.delim, 1);
506 assert.equal(one.state.data, 1);
507 assert.equal(one.state.maxPayload, 2);
508 const partial = unwrap(pktStep(pktInit(), te.encode('000')));
509 assert.equal(partial.frames.length, 0);
510 throws(() => unwrap(pktFinish(partial.state)), 'protocol', 'trailing 3 bytes after last pkt-line');
511 assert.equal(unwrap(pktFinish(one.state)).frames, 4);
512 const longer = unwrap(pktStep(one.state, unwrap(pkt('abcd'))));
513 assert.equal(longer.state.maxPayload, 4);
514 assert.equal(longer.state.data, 2);
515});
517test('pack header, ofs base, delta opcodes, and trailer', async () => {
518 const blob = te.encode('hello');
519 const { pack } = await buildPack([{ type: TYPE.BLOB, data: blob }]);
520 const parsed = unwrap(parsePack(pack));
521 assert.equal(parsed.stats.version, PACK_VERSION);
522 assert.equal(parsed.stats.count, 1);
523 assert.equal(parsed.stats.full, 1);
524 assert.equal(parsed.stats.ofsDelta, 0);
525 assert.equal(parsed.stats.byType.blob, 1);
526 assert.equal(parsed.stats.inflated, blob.length);
527 assert.ok(parsed.stats.inflate.blocks >= 1);
528 assert.deepEqual(parsed.objects[0].data, blob);
530 const badSig = pack.slice();
531 badSig.set(PACK_SIGNATURE.map((b, i) => (i === 0 ? b : 0)));
532 throws(() => unwrap(parsePack(badSig)), 'pack_corrupt', 'pack: bad signature');
533 const badVer = pack.slice();
534 badVer[7] = 3;
535 throws(() => unwrap(parsePack(badVer)), 'pack_corrupt', 'pack: unsupported version 3');
536 throws(() => unwrap(parsePack(pack.subarray(0, 4))), 'pack_corrupt', 'pack: bad signature');
538 const za = await zlibDeflate(blob);
539 const ha = encodeObjHeader(TYPE.BLOB, blob.length);
540 const other = te.encode('hello!');
541 const delta = makeDelta(blob, other).delta;
542 const zd = await zlibDeflate(delta);
543 const hd = encodeObjHeader(TYPE.OFS_DELTA, delta.length);
544 const second = PACK_HEADER_BYTES + ha.length + za.length;
545 const ofs = second - PACK_HEADER_BYTES;
546 assert.ok(ofs < 128);
547 const body = concat([
548 Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 2),
549 ha, za, hd, Uint8Array.of(ofs), zd,
550 ]);
551 const { Sha1 } = await import('../src/sha1.js');
552 const ofsPack = concat([body, new Sha1().update(body).digest()]);
553 const ofsParsed = unwrap(parsePack(ofsPack));
554 assert.equal(ofsParsed.stats.full, 1);
555 assert.equal(ofsParsed.stats.ofsDelta, 1);
556 assert.deepEqual(ofsParsed.objects[1].base, { tag: 'ofs', offset: PACK_HEADER_BYTES });
558 const early = concat([
559 Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1),
560 hd, Uint8Array.of(1), zd,
561 ]);
562 throws(() => unwrap(parsePack(concat([early, new Sha1().update(early).digest()]))), 'pack_corrupt', `pack: ofs delta base before pack start at ${PACK_HEADER_BYTES}`);
564 const t5 = encodeObjHeader(5, blob.length);
565 const badTypeBody = concat([Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1), t5, za]);
566 throws(() => unwrap(parsePack(concat([badTypeBody, new Sha1().update(badTypeBody).digest()]))), 'pack_corrupt', `pack: bad object type 5 at ${PACK_HEADER_BYTES}`);
567 throws(() => unwrap(parsePack(pack, { limits: withLimits({ maxObjectSize: blob.length - 1 }) })), 'limit_exceeded', `pack object size ${blob.length} exceeds limit ${blob.length - 1}`);
568 const flipped = ofsPack.slice();
569 flipped[flipped.length - 1] ^= 1;
570 assert.throws(() => unwrap(parsePack(flipped)), e => e.code === 'pack_corrupt' && /^pack: trailer sha mismatch [0-9a-f]{40} != [0-9a-f]{40}$/.test(e.message));
571 assert.equal(unwrap(parsePack(flipped, { verify: false })).objects.length, 2);
572 const extra = concat([pack, Uint8Array.of(0)]);
573 assert.throws(() => unwrap(parsePack(extra)), /expected trailer at/);
575 assert.equal(unwrap(readUint32BE(Uint8Array.of(0, 0, 1, 2), 0)), 258);
576 const hdr = unwrap(readObjHeader(encodeObjHeader(TYPE.BLOB, 300), 0));
577 assert.equal(hdr.type, TYPE.BLOB);
578 assert.equal(hdr.size, 300);
579 const ofsHdr = unwrap(readOfsDelta(Uint8Array.of(0x81, 2), 0));
580 assert.equal(ofsHdr.off, 258);
582 throws(() => unwrap(applyDelta(blob, new Uint8Array(DELTA_SIZE_MIN - 1))), 'pack_corrupt', 'delta: shorter than DELTA_SIZE_MIN');
583 const four = new Uint8Array(DELTA_SIZE_MIN);
584 throws(() => unwrap(applyDelta(new Uint8Array(0), four)), 'pack_corrupt', 'delta: reserved opcode 0');
585 throws(() => unwrap(applyDelta(te.encode('no'), delta)), 'pack_corrupt', `delta: base size ${blob.length} != 2`);
586 const applied = unwrap(applyDelta(blob, makeDelta(blob, other).delta));
587 assert.deepEqual(applied.out, other);
588 assert.equal(applied.ops.insert + applied.ops.copy >= 1, true);
590 const base = Uint8Array.of(9, 8, 7);
591 const op = Uint8Array.of(
592 DELTA_COPY | DELTA_COPY_OFF1 | DELTA_COPY_OFF2 | DELTA_COPY_OFF3 | DELTA_COPY_OFF4 | DELTA_COPY_LEN1 | DELTA_COPY_LEN2 | DELTA_COPY_LEN3,
593 1, 0, 0, 0, 2, 0, 0,
594 );
595 const copyDelta = concat([encodeVarint(base.length), encodeVarint(2), op]);
596 const copied = unwrap(applyDelta(base, copyDelta));
597 assert.deepEqual([...copied.out], [8, 7]);
598 assert.equal(copied.ops.copy, 1);
599 assert.equal(copied.ops.copyBytes, 2);
600 throws(() => unwrap(applyDelta(base, concat([encodeVarint(base.length), encodeVarint(1), Uint8Array.of(DELTA_COPY | DELTA_COPY_OFF1 | DELTA_COPY_LEN1, 5, 1)]))), 'pack_corrupt', 'delta: copy out of range');
601 throws(() => unwrap(applyDelta(base, concat([encodeVarint(base.length), encodeVarint(1), Uint8Array.of(2, 9)]))), 'pack_corrupt', 'delta: insert out of range');
602 throws(() => unwrap(applyDelta(base, concat([encodeVarint(base.length), encodeVarint(2), Uint8Array.of(1, 1)]))), 'pack_corrupt', 'delta: produced 1 != 2');
603 throws(() => unwrap(applyDelta(base, makeDelta(base, base).delta, { maxResult: 0 })), 'limit_exceeded', `delta result size ${base.length} exceeds limit 0`);
604 assert.ok(copyOp(1, 1)[0] & DELTA_COPY);
605});
607test('inflate stored, fixed, dynamic, and the header checks', () => {
608 const h = huffBuild(Uint8Array.of(1, 2, 0, 2));
609 assert.deepEqual([...h.count].slice(0, 4), [0, 1, 2, 0]);
610 assert.deepEqual([...h.symbol].slice(0, 3), [0, 1, 3]);
611 assert.equal(adler32(Uint8Array.of(1, 2, 3)), 851975);
613 const stored = (bytes, final) => {
614 const out = new Uint8Array(1 + 4 + bytes.length);
615 out[0] = final ? 1 : 0;
616 out[1] = bytes.length & 255;
617 out[2] = bytes.length >> 8;
618 out[3] = ~out[1] & 255;
619 out[4] = ~out[2] & 255;
620 out.set(bytes, 5);
621 return out;
622 };
623 const two = unwrap(inflate(concat([stored(Uint8Array.of(1, 2), false), stored(Uint8Array.of(3), true)]), 0, { raw: true }));
624 assert.deepEqual([...two.out], [1, 2, 3]);
625 assert.equal(two.stats.blocks, 2);
626 assert.equal(two.stats.stored, 2);
627 throws(() => unwrap(inflate(Uint8Array.of(1), 0, { raw: true })), 'pack_corrupt', 'inflate: truncated stored header');
628 const badLen = stored(Uint8Array.of(1), true);
629 badLen[3] ^= 1;
630 throws(() => unwrap(inflate(badLen, 0, { raw: true })), 'pack_corrupt', 'inflate: stored length check failed');
631 throws(() => unwrap(inflate(Uint8Array.of(7), 0, { raw: true })), 'pack_corrupt', 'inflate: invalid block type');
632 throws(() => unwrap(inflate(stored(Uint8Array.of(1, 2, 3), true).subarray(0, 6), 0, { raw: true })), 'pack_corrupt', 'inflate: truncated stored block');
634 const hello = te.encode('hello hello hello hello');
635 const fixed = unwrap(inflate(new Uint8Array(zlib.deflateSync(hello))));
636 assert.equal(fixed.stats.fixed, 1);
637 assert.equal(fixed.stats.dynamic, 0);
638 assert.equal(td.decode(fixed.out), td.decode(hello));
639 assert.equal(fixed.stats.literals, 7);
640 assert.equal(fixed.stats.matches, 1);
641 assert.equal(fixed.stats.maxDist, 6);
642 assert.equal(fixed.stats.maxLen, 16);
643 const level0 = unwrap(inflate(new Uint8Array(zlib.deflateSync(hello, { level: 0 }))));
644 assert.equal(level0.stats.stored, 1);
645 let varied = '';
646 for (let i = 0; i < 4000; i++) varied += `word${i % 700} `;
647 const dynamic = unwrap(inflate(new Uint8Array(zlib.deflateSync(Buffer.from(varied)))));
648 assert.equal(dynamic.stats.dynamic, 1);
649 assert.equal(td.decode(dynamic.out), varied);
651 const grown = unwrap(inflate(new Uint8Array(zlib.deflateSync(Buffer.from('a'.repeat(10000))))));
652 assert.equal(grown.out.length, 10000);
653 throws(() => unwrap(inflate(new Uint8Array(zlib.deflateSync(hello)), 0, { expectedSize: 1 })), 'pack_corrupt', 'inflate: output exceeds expected 1');
654 assert.throws(() => unwrap(inflate(new Uint8Array(zlib.deflateSync(hello)), 0, { expectedSize: hello.length + 10, maxOutput: 4 })), e => e.code === 'limit_exceeded' && e.message === `inflate: expected size ${hello.length + 10} exceeds limit 4`);
655 throws(() => unwrap(inflate(new Uint8Array(zlib.deflateSync(Buffer.from('a'.repeat(1000)))), 0, { maxOutput: 100 })), 'limit_exceeded', 'inflate: output exceeds limit 100');
657 throws(() => unwrap(inflate(Uint8Array.of(0x01, 0x00))), 'pack_corrupt', 'zlib: unsupported method 1');
658 throws(() => unwrap(inflate(Uint8Array.of(0x78, 0x00))), 'pack_corrupt', 'zlib: bad header check');
659 throws(() => unwrap(inflate(Uint8Array.of(0x78, 0x20))), 'pack_corrupt', 'zlib: preset dictionary not supported');
660 const chopped = new Uint8Array(zlib.deflateSync(hello)).subarray(0, 8);
661 throws(() => unwrap(inflate(chopped)), 'pack_corrupt', 'inflate: unexpected end of input');
662 const mismatch = new Uint8Array(zlib.deflateSync(hello));
663 mismatch[mismatch.length - 1] ^= 1;
664 throws(() => unwrap(inflate(mismatch)), 'pack_corrupt', 'inflate: adler32 mismatch');
665 assert.equal(unwrap(inflate(mismatch, 0, { verifyAdler: false })).out.length, hello.length);
666 const rawCut = new Uint8Array(zlib.deflateRawSync(hello)).subarray(0, 1);
667 throws(() => unwrap(inflate(rawCut, 0, { raw: true })), 'pack_corrupt', 'inflate: unexpected end of input');
668});
670// The acknowledgments section (src/fetch.ts v2Step), in the suite the mutation gate runs. Expected values are git's
671// (fetch-pack.c process_ack): it reads "NAK", "ACK <oid>" (the prefix "ACK " with its space) and "ready", and dies
672// "unexpected acknowledgment line" on anything else. The message is ply's fixed text with the line in JSON quotes,
673// written out here by hand.
674test('v2 acknowledgments: NAK and ACK <oid> are kept; any other line is a protocol error that quotes it', () => {
675 const data = s => ({ kind: 'data', payload: te.encode(s) });
676 const inAcks = () => unwrap(v2StepR(v2Init(), data('acknowledgments\n'))).state;
677 const id = 'ab'.repeat(20);
678 const nak = v2StepR(inAcks(), data('NAK\n'));
679 assert.deepEqual([nak.ok, nak.value?.state.acks], [true, ['NAK']]);
680 const ack = v2StepR(inAcks(), data(`ACK ${id}\n`));
681 assert.deepEqual([ack.ok, ack.value?.state.acks], [true, [`ACK ${id}`]]);
682 for (const [sent, quoted] of [
683 ['hello\n', '"hello"'], ['\n', '""'], ['ACK\n', '"ACK"'], [`ACK${id}\n`, `"ACK${id}"`], ['NAKed\n', '"NAKed"'], ['nak\n', '"nak"'], [` ACK ${id}\n`, `" ACK ${id}"`],
684 ]) {
685 const r = v2StepR(inAcks(), data(sent));
686 assert.deepEqual([r.ok, r.error?.code, r.error?.message], [false, 'protocol', `v2: unexpected acknowledgment line ${quoted}`], JSON.stringify(sent));
687 }
688});
690test('v2 sections, sideband, and the single-object pack stream', async () => {
691 const limits = withLimits({ maxSectionLines: 2 });
692 let st = v2Init(limits);
693 const feed = frame => { const r = v2Step(st, frame); st = r.state; return r; };
694 const line = s => ({ kind: 'data', payload: te.encode(s) });
695 const errOf = (text) => {
696 st = v2Init(limits);
697 try { feed(line(`ERR ${text}\n`)); } catch (e) { return e; }
698 assert.fail(`ERR ${text} did not throw`);
699 };
700 const boom = errOf('boom');
701 assert.equal(boom.code, 'remote_error');
702 assert.equal(boom.message, 'remote: boom');
703 assert.equal(boom.reason, undefined);
704 for (const text of ['not our ref', 'not our ref abc', 'upload-pack: not our ref', 'upload-pack: not our ref abc']) {
705 const e = errOf(text);
706 assert.equal(e.code, 'remote_error', text);
707 assert.equal(e.message, `remote: ${text}`, text);
708 assert.equal(e.reason, 'not_our_ref', text);
709 }
710 for (const text of ['not our refresh', 'upload-pack: not our refresh', 'upload-pack: not our refX']) {
711 const e = errOf(text);
712 assert.equal(e.code, 'remote_error', text);
713 assert.equal(e.message, `remote: ${text}`, text);
714 assert.equal(e.reason, undefined, text);
715 }
716 st = v2Init(limits);
717 assert.equal(feed(line('packfile')).state.phase, 'pack');
718 assert.deepEqual(feed({ kind: 'flush' }).state.done, true);
719 throws(() => feed(line('x')), 'protocol', 'v2: frame after flush');
721 st = v2Init(limits);
722 feed(line('acknowledgments'));
723 assert.equal(feed(line('NAK')).state.acks[0], 'NAK');
724 assert.equal(feed(line('ready')).state.ready, true);
725 assert.equal(st.acks.length, 1);
726 assert.equal(feed(line('NAK')).state.acks.length, 2);
727 throws(() => feed(line('NAK')), 'limit_exceeded', 'acknowledgment lines 3 exceeds limit 2');
729 st = v2Init();
730 feed(line('shallow-info'));
731 const id = oid(7);
732 assert.deepEqual(feed(line(`shallow ${id}`)).state.shallow, [id]);
733 assert.deepEqual(feed(line(`shallow ${id}`)).state.shallow, [id]);
734 assert.deepEqual(feed(line(`unshallow ${oid(8)}`)).state.unshallow, [oid(8)]);
735 assert.deepEqual(feed(line(`unshallow ${oid(8)}`)).state.unshallow, [oid(8)]);
736 assert.equal(feed(line('other')).state.shallow.length, 1);
737 st = v2Init(withLimits({ maxSectionLines: 1 }));
738 feed(line('shallow-info'));
739 feed(line(`shallow ${id}`));
740 throws(() => feed(line('other')), 'limit_exceeded', 'shallow-info lines 2 exceeds limit 1');
742 st = v2Init(limits);
743 feed(line('wanted-refs'));
744 const kept = feed(line('abc'));
745 assert.equal(kept.state.section, 'wanted-refs');
746 assert.equal(kept.pack, null);
747 assert.equal(feed({ kind: 'delim' }).state.section, null);
748 assert.equal(feed({ kind: 'end' }).state.done, false);
750 st = v2Init(limits);
751 feed(line('packfile'));
752 // a delim or response-end inside the packfile section ends git's demultiplexer: refused, not skipped
753 throws(() => feed({ kind: 'delim' }), 'protocol', 'v2: delim inside the packfile section');
754 throws(() => feed({ kind: 'end' }), 'protocol', 'v2: end inside the packfile section');
755 const body = te.encode('xyz');
756 const band = n => ({ kind: 'data', payload: concat([Uint8Array.of(n), body]) });
757 const pack = feed(band(SIDEBAND_PACK));
758 assert.deepEqual(pack.pack, body);
759 assert.equal(pack.state.band1, 3);
760 assert.equal(pack.state.packFrames, 1);
761 const progress = te.encode('p'.repeat(600));
762 const prog = feed({ kind: 'data', payload: concat([Uint8Array.of(SIDEBAND_PROGRESS), progress]) });
763 assert.equal(prog.state.band2, 600);
764 assert.equal(prog.state.band2Frames, 1);
765 assert.equal(prog.state.progress.length, 512);
766 const bandErr = (body) => {
767 try { feed({ kind: 'data', payload: concat([Uint8Array.of(3), te.encode(body)]) }); } catch (e) { return e; }
768 assert.fail(body);
769 };
770 const bandBoom = bandErr(' boom \n');
771 assert.equal(bandBoom.code, 'remote_error');
772 assert.equal(bandBoom.message, 'remote: boom');
773 assert.equal(bandBoom.reason, undefined);
774 const bandAbsent = bandErr('upload-pack: not our ref abc');
775 assert.equal(bandAbsent.code, 'remote_error');
776 assert.equal(bandAbsent.message, 'remote: upload-pack: not our ref abc');
777 assert.equal(bandAbsent.reason, 'not_our_ref');
778 assert.equal(bandErr('not our refresh').reason, undefined);
779 throws(() => feed({ kind: 'data', payload: Uint8Array.of(9, 1) }), 'protocol', 'v2: bad sideband 9');
780 throws(() => parseSideband(new Uint8Array(0)), 'protocol', 'short read: need 1 bytes at 0, have 0');
781 // an empty data frame (0004) in the packfile section has no band byte: parseSideband's short read is the step's Err
782 throws(() => feed({ kind: 'data', payload: new Uint8Array(0) }), 'protocol', 'short read: need 1 bytes at 0, have 0');
783 assert.equal(parseSideband(Uint8Array.of(SIDEBAND_PACK)).body.length, 0);
784 assert.deepEqual(parseSideband(concat([Uint8Array.of(SIDEBAND_PROGRESS), te.encode('z')])).body, te.encode('z'));
786 const blob = te.encode('pack-obj');
787 const { pack: bytes } = await buildPack([{ type: TYPE.BLOB, data: blob }]);
788 let sp = spInit();
789 const early = spStep(sp, bytes.subarray(0, PACK_HEADER_BYTES));
790 assert.equal(early.state.phase, 'header');
791 const mid = spStep(early.state, bytes.subarray(PACK_HEADER_BYTES));
792 sp = mid.state;
793 const finished = spFinish(sp);
794 assert.equal(finished.phase, 'body');
795 assert.equal(finished.trailer.length, 40);
796 throws(() => spFinish(spInit()), 'pack_corrupt', 'pack: truncated before object header');
797 const sig = bytes.slice();
798 sig[0] ^= 0xff;
799 throws(() => spStep(spInit(), sig), 'pack_corrupt', 'pack: bad signature');
800 const ver = bytes.slice();
801 ver[7] = 9;
802 throws(() => spStep(spInit(), ver), 'pack_corrupt', 'pack: bad version');
803 const { pack: two } = await buildPack([{ type: TYPE.BLOB, data: blob }, { type: TYPE.BLOB, data: blob }]);
804 throws(() => spStep(spInit(), two), 'pack_corrupt', 'pack: expected 1 object, got 2');
805 const { pack: ref } = await buildPack([{ type: TYPE.BLOB, data: blob, base: { tag: 'ref', oid: oid(1) } }]);
806 throws(() => spStep(spInit(), ref), 'pack_corrupt', 'pack: single-object pack is a delta');
807 const torn = bytes.slice();
808 torn[torn.length - 1] ^= 1;
809 let bad = spInit();
810 bad = spStep(bad, torn).state;
811 assert.throws(() => spFinish(bad), e => e.code === 'pack_corrupt' && e.message.startsWith('pack: trailer ') && e.message.includes(' != '));
812});
814test('ls-refs lines and report-status', () => {
815 const id = oid(4);
816 assert.deepEqual(parseLsRefLine(`${id} refs/heads/main`), { oid: id, name: 'refs/heads/main', peeled: null, 'symref-target': null });
817 assert.deepEqual(parseLsRefLine(`${id} HEAD symref-target:refs/heads/main peeled:${id}`), {
818 oid: id, name: 'HEAD', 'symref-target': 'refs/heads/main', peeled: id,
819 });
820 assert.deepEqual(parseLsRefLine(`${id} refs/tags/v1 bare`), { oid: id, name: 'refs/tags/v1', peeled: null, 'symref-target': null });
821 // #16: only symref-target and peeled are kept; a server attribute never overrides the parsed oid or name
822 assert.deepEqual(parseLsRefLine(`${id} refs/heads/main oid:junk name:refs/heads/other`), { oid: id, name: 'refs/heads/main', peeled: null, 'symref-target': null });
823 assert.deepEqual(parseLsRefLine(`${id} refs/heads/main oid:${oid(5)} name:HEAD __proto__:x constructor:y`), { oid: id, name: 'refs/heads/main', peeled: null, 'symref-target': null });
824 assert.deepEqual(parseLsRefLine(`${id} refs/heads/main peeled symref-target`), { oid: id, name: 'refs/heads/main', peeled: null, 'symref-target': null }, 'no colon: not the attribute (git skip_prefix)');
825 assert.deepEqual(parseLsRefLine(`${id} refs/heads/main peeled_ symref-target_`), { oid: id, name: 'refs/heads/main', peeled: null, 'symref-target': null }, 'no colon: never a key cut one char short');
826 assert.deepEqual(parseLsRefLine(`${id} refs/heads/main peeled:${id} `), { oid: id, name: 'refs/heads/main', peeled: id, 'symref-target': null }, 'doubled spaces');
827 // the name is never read as an attribute, with or without attributes after it
828 assert.deepEqual(parseLsRefLine(`${id} symref-target:refs/heads/x`), { oid: id, name: 'symref-target:refs/heads/x', peeled: null, 'symref-target': null });
829 assert.deepEqual(parseLsRefLine(`${id} peeled:junk symref-target:refs/heads/main`), { oid: id, name: 'peeled:junk', 'symref-target': 'refs/heads/main', peeled: null });
830 // git reads an oid in either case (parse_oid_hex_algop: hexval); ply keeps it lower-case (the advertisement space, record=upperPeeled)
831 assert.deepEqual(parseLsRefLine(`${id} refs/tags/v1 peeled:${'AB'.repeat(20)}`), { oid: id, name: 'refs/tags/v1', peeled: 'ab'.repeat(20), 'symref-target': null });
832 for (const bad of ['junk', 'GB'.repeat(20), `${oid(5)}0`, '']) {
833 const line = `${id} refs/tags/v1 peeled:${bad}`;
834 throws(() => parseLsRefLine(line), 'protocol', `ls-refs: malformed line ${JSON.stringify(line.slice(0, 60))}`);
835 }
836 throws(() => parseLsRefLine('ERR no such ref'), 'remote_error', 'remote: no such ref');
837 throws(() => parseLsRefLine('nope'), 'protocol', 'ls-refs: malformed line "nope"');
838 // an empty second field is a ref named "" (process_ref_v2), and an `unborn` record is no ref (record=emptyName, unbornOther)
839 assert.deepEqual(parseLsRefLine(`${id} `), { oid: id, name: '', peeled: null, 'symref-target': null });
840 assert.equal(parseLsRefLine('unborn HEAD symref-target:refs/heads/main'), null);
841 assert.equal(parseLsRefLine('unborn '), null);
842 throws(() => parseLsRefLine('unborn'), 'protocol', 'ls-refs: malformed line "unborn"');
843 throws(() => parseLsRefLine(`${'zz'.repeat(20)} refs/heads/main`), 'protocol', `ls-refs: malformed line ${JSON.stringify(`${'zz'.repeat(20)} refs/heads/main`)}`);
844 throws(() => parseLsRefLine(`${'ab'.repeat(32)} refs/heads/main`), 'unsupported', 'unsupported object-format: only sha1 repositories are supported');
845 const upper = 'ab'.repeat(20).toUpperCase();
846 assert.deepEqual(parseLsRefLine(`${upper} refs/heads/main`), { oid: 'ab'.repeat(20), name: 'refs/heads/main', peeled: null, 'symref-target': null });
847 throws(() => parseLsRefLine(`${'AB'.repeat(32)} refs/heads/main`), 'unsupported', 'unsupported object-format: only sha1 repositories are supported');
848 const long = 'not a ref line '.repeat(8);
849 throws(() => parseLsRefLine(long), 'protocol', `ls-refs: malformed line ${JSON.stringify(long.slice(0, 60))}`);
851 // one report parser (classifyReport): the lines parseReportStatus read, judged by git's grammar
852 const rs = (lines, refs = ['refs/heads/main', 'refs/heads/other']) => classifyReport(lines.map(l => te.encode(`${l}\n`)), refs.map(ref => ({ ref })));
853 assert.deepEqual(rs(['unpack ok', 'ok refs/heads/main', 'ok refs/heads/other']), { tag: 'statuses', refs: [{ ref: 'refs/heads/main', ok: true, reason: '' }, { ref: 'refs/heads/other', ok: true, reason: '' }] });
854 assert.deepEqual(rs(['unpack ok', 'ok refs/heads/main', 'ng refs/heads/other failed to lock']).refs[1], { ref: 'refs/heads/other', ok: false, reason: 'failed to lock' });
855 // report-status-v2 option lines come only from a proc-receive hook (not modelled): the report is not git's plain one
856 assert.deepEqual(rs(['unpack ok', 'ok refs/heads/main', 'ng refs/heads/other failed to lock', 'option old-oid abc']), { tag: 'malformed', why: 'a line is not a ref status' });
857 assert.deepEqual(rs(['unpack error', 'ok refs/heads/main', 'ok refs/heads/other']), { tag: 'malformed', why: 'no git unpack line first' });
858 assert.deepEqual(rs(['ok refs/heads/main']), { tag: 'malformed', why: 'no git unpack line first' });
859 assert.deepEqual(rs(['unpackok', 'ok refs/heads/main']), { tag: 'malformed', why: 'no git unpack line first' });
860 // send-pack.c receive_unpack_status reads one unpack line; a second is an invalid ref status
861 assert.deepEqual(rs(['unpack index-pack abnormal exit', 'unpack ok', 'ok refs/heads/main'], ['refs/heads/main']), { tag: 'malformed', why: 'a line is not a ref status' });
862 assert.deepEqual(rs(['unpack ok', 'ok refs/heads/main', 'unpack ok'], ['refs/heads/main']), { tag: 'malformed', why: 'a line is not a ref status' });
863});
865test('gitattributes lines', () => {
866 const no = { macrosOk: false };
867 const yes = { macrosOk: true };
868 assert.deepEqual(parseLine('*.txt text', no), { pat: { pattern: '*.txt', negative: false, mustBeDir: false, nodir: true, prefix: 0, endsWith: true }, states: [['text', true]] });
869 assert.deepEqual(parseLine(' *.txt text', no).pat.pattern, '*.txt');
870 assert.equal(parseLine('# comment', no), null);
871 assert.equal(parseLine('', no), null);
872 assert.equal(parseLine('!*.txt text', no), null);
873 assert.equal(parseLine('docs/ text', no).pat.mustBeDir, true);
874 assert.equal(parseLine('docs/ text', no).pat.pattern, 'docs');
875 assert.equal(parseLine('dir/file text', no).pat.nodir, false);
876 assert.deepEqual(parseLine('*.c diff=cpp', no).states, [['diff', 'cpp']]);
877 assert.deepEqual(parseLine('*.c -text', no).states, [['text', false]]);
878 assert.deepEqual(parseLine('*.c !text', no).states, [['text', UNSET]]);
879 assert.equal(parseLine('"*.txt" text', no).pat.pattern, '*.txt');
880 assert.deepEqual(parseLine('[attr]binary -diff -merge -text', yes), { macro: 'binary', states: [['diff', false], ['merge', false], ['text', false]] });
881 assert.equal(parseLine('[attr]binary -diff', no), null);
882 assert.equal(parseLine('[attr] text', yes).pat.pattern, '[attr]');
883 assert.equal(parseLine('*.c -', no), null);
884 assert.equal(parseLine('*.c =novalue', no), null);
885 assert.equal(parseLine('*.c builtin_x', no), null);
886 assert.deepEqual(parseLine('*.c builtin_x', { macrosOk: false, reserveBuiltin: false }).states, [['builtin_x', true]]);
887 assert.equal(parseLine('x'.repeat(2048), no), null);
888 assert.equal(parseLine('x'.repeat(2047), no).pat.pattern.length, 2047);
889 const file = parseFile('*.txt text\n*.c -text\n# c\n\n*.bin binary\0junk\n');
890 assert.deepEqual(file.map(l => l.pat.pattern), ['*.txt', '*.c', '*.bin']);
891 assert.deepEqual(parseFile(''), []);
892 assert.equal(parseFile('*.txt text').length, 1);
893 assert.equal(hashObject('blob', te.encode('x')).length, OID_HEX_CHARS);
894});
896test('boundaries between a thrown error and its neighbour', async () => {
897 const author = 'Ada <ada@ex> 1700000000 +0000';
898 const tree = oid(1);
899 throws(() => unwrap(assertFetched('commit', te.encode(`tree ${tree}\nauthor Ada >x 1 +0000\ncommitter ${author}\n\n`))), 'pack_corrupt', 'commit: badName: invalid author line - bad name');
900 throws(() => unwrap(assertFetched('commit', te.encode(`tree ${tree}\nauthor Ada <ada@ex 1 +0000\ncommitter ${author}\n\n`))), 'pack_corrupt', 'commit: badEmail: invalid author line - bad email');
901 throws(() => unwrap(assertFetched('commit', te.encode(`tree ${tree}\nauthor <ada@ex> 1700000000 +0000\ncommitter ${author}\n\n`))), 'pack_corrupt', 'commit: missingNameBeforeEmail: invalid author line - missing space before email');
902 unwrap(assertFetched('commit', te.encode(`tree ${tree}\nauthor <ada@ex> 1700000000 +0000\ncommitter ${author}\n\n`)));
903 throws(() => unwrap(parseTree(data('100644 a', oidBytes(4)))), 'pack_corrupt', 'tree: malformed entry');
904 throws(() => unwrap(parseTree(data('g7 a\0', oidBytes(4)))), 'pack_corrupt', 'tree: malformed mode');
905 throws(() => unwrap(parseTag(te.encode('tag'))), 'pack_corrupt', 'tag: unterminatedHeader: unterminated header');
906 const extra = unwrap(parseTag(te.encode(`object ${oid(3)}\ntype commit\ntag v1\ntagger ${author}\nfoo bar\n\n`)));
907 assert.equal(extra.tagger, author);
908 assert.deepEqual(extra.extra, ['foo bar']);
909 throws(() => unwrap(parseTag(te.encode(`object ${oid(3)}\ntype commit\ntag v1\ntagger ${author}\ntagger second <s@ex> 1 +0000\n\n`))), 'pack_corrupt', 'tag: duplicate tagger');
910 assert.deepEqual(parseIdent('>a <b@c> 1 +0000'), { name: '>a', email: 'b@c', time: '1', tz: '+0000' });
911 assert.deepEqual(parseIdent('Ada <a@b>'), { name: 'Ada', email: 'a@b', time: null, tz: '' });
912 assert.deepEqual(parseIdent('Ada <a@b> 1 +0100'), { name: 'Ada', email: 'a@b', time: '1', tz: '+0100' });
913 assert.equal(parseIdent('Ada <a@b> 12abc +0000').time, null);
914 assert.equal(parseIdent('Ada <a@b> x12 +0000').time, null);
915 assert.deepEqual(parseIdent('Ada <ada@ex'), { name: 'Ada <ada@ex', email: '', time: null, tz: '' });
917 const blob = te.encode('hello');
918 const { pack } = await buildPack([{ type: TYPE.BLOB, data: blob }]);
919 assert.equal(unwrap(parsePack(pack, { limits: withLimits({ maxObjectSize: blob.length }) })).objects.length, 1);
920 const emptyBody = Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 0);
921 const { Sha1 } = await import('../src/sha1.js');
922 const emptyPack = concat([emptyBody, new Sha1().update(emptyBody).digest()]);
923 assert.equal(emptyPack.length, PACK_HEADER_BYTES + OID_BYTES);
924 assert.equal(unwrap(parsePack(emptyPack)).stats.count, 0);
925 const commitBytes = te.encode(`tree ${tree}\nauthor ${author}\ncommitter ${author}\n\n`);
926 const tagBytes = te.encode(`object ${oid(3)}\ntype commit\ntag v\ntagger ${author}\n\n`);
927 for (const [type, bytes] of [[TYPE.COMMIT, commitBytes], [TYPE.TAG, tagBytes], [0, blob]]) {
928 const z = await zlibDeflate(bytes);
929 const h = encodeObjHeader(type, bytes.length);
930 const body = concat([Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1), h, z]);
931 const built = concat([body, new Sha1().update(body).digest()]);
932 if (type === 0) throws(() => unwrap(parsePack(built)), 'pack_corrupt', `pack: bad object type 0 at ${PACK_HEADER_BYTES}`);
933 else assert.equal(unwrap(parsePack(built)).stats.full, 1);
934 }
935 const parsed = unwrap(parsePack(pack));
936 assert.ok(parsed.objects[0].headerBytes > 0 && parsed.objects[0].headerBytes < 16);
937 assert.equal(parsed.stats.compressed > 0, true);
938 assert.equal(parsed.stats.inflate.blocks > 0, true);
940 const base = Uint8Array.of(1, 2, 3, 4);
941 const exact = makeDelta(base, base).delta;
942 assert.deepEqual(unwrap(applyDelta(base, exact, { maxResult: base.length })).out, base);
943 const inserted = unwrap(applyDelta(base, concat([encodeVarint(base.length), encodeVarint(1), Uint8Array.of(1, 9)])));
944 assert.equal(inserted.ops.insertBytes, 1);
946 let sp = spInit();
947 const opened = spStep(sp, pack.subarray(0, PACK_HEADER_BYTES + 1));
948 assert.equal(opened.state.phase, 'body');
949 assert.equal(spStep(spInit(), pack).state.bytes, pack.length);
950 throws(() => spFinish(opened.state), 'pack_corrupt', 'pack: missing trailer');
951 const streamed = spStep(opened.state, pack.subarray(PACK_HEADER_BYTES + 1));
952 assert.equal(spFinish(streamed.state).zlibBytes, streamed.state.zlibBytes);
953 assert.ok(streamed.state.zlibBytes > 0);
955 const no = { macrosOk: false };
956 const yes = { macrosOk: true };
957 assert.equal(parseLine('*.txt text=v', no).states[0][1], 'v');
958 assert.equal(parseLine('*.txt eq=at=mid', no).states[0][1], 'at=mid');
959 assert.equal(parseLine('"*.c" diff', no).pat.pattern, '*.c');
960 assert.equal(parseLine('[attr]-no -text', yes), null);
961 assert.equal(parseLine('[attr]ok -text', yes).macro, 'ok');
962 assert.equal(parseFile('*.c text\0ignored').length, 1);
963 assert.equal(parseFile('*.c text').length, 1);
964 assert.deepEqual(parseFile('*.c text', { maxFile: 8 }), []);
965 assert.equal(parseFile('*.c text', { maxFile: 9 }).length, 1);
967 const id = oid(9);
968 throws(() => parseLsRefLine(` ${id}`), 'protocol', `ls-refs: malformed line ${JSON.stringify(` ${id}`)}`);
969 assert.equal(parseLsRefLine(`${id} refs/heads/a`).name, 'refs/heads/a');
970 assert.equal(parseLsRefLine(`${id} refs/heads/a symref-target:val`)['symref-target'], 'val');
971 assert.equal(parseLsRefLine(`${id} refs/heads/a sym:val`).sym, undefined);
972 const main = [{ ref: 'refs/heads/main' }];
973 assert.deepEqual(classifyReport(['unpack ok\n', 'ok refs/heads/main\n'].map(l => te.encode(l)), main), { tag: 'statuses', refs: [{ ref: 'refs/heads/main', ok: true, reason: '' }] });
974 assert.equal(classifyReport(['unpack ok\n', 'ok refs/heads/main\n', 'option alone\n'].map(l => te.encode(l)), main).tag, 'malformed');
975 assert.equal(classifyReport([te.encode('option alone\n')], main).tag, 'malformed');
977 let st = v2Init();
978 const line = s => ({ kind: 'data', payload: te.encode(s) });
979 const end = v2Step(st, { kind: 'end' });
980 assert.equal(end.state.done, false);
981 st = v2Step(st, line('shallow-info')).state;
982 st = v2Step(st, line(`unshallow ${id}`)).state;
983 assert.deepEqual(st.unshallow, [id]);
984 const nl = v2Step(st, line('shallow x\n')).state;
985 assert.equal(nl.shallow[0], 'x');
986});
988test('asymmetric boundaries a symmetric fixture hides', async () => {
989 const no = { macrosOk: false };
990 assert.equal(parseLine('*.txt text', no).pat.endsWith, true);
991 assert.equal(parseLine('*.txt text', no).pat.nodir, true);
992 assert.equal(parseLine('/file text', no).pat.nodir, false);
993 assert.equal(parseLine('docs/ text', no).pat.nodir, true);
994 assert.equal(parseLine('*a* text', no).pat.endsWith, false);
995 assert.equal(parseLine('a* text', no).pat.endsWith, false);
996 assert.deepEqual(parseLine('*.c plain later=v', no).states[0], ['plain', true]);
997 assert.equal(parseLine('*.c plain later=v', no).states[1][0], 'later');
998 assert.deepEqual(parseFile('[attr]binary -text'), []);
999 assert.deepEqual(parseFile('*.c builtin_x'), []);
1000 assert.equal(parseFile('*.c builtin_x', { reserveBuiltin: false }).length, 1);
1001 assert.deepEqual(parseFile('\0*.c text'), []);
1002 assert.equal(parseFile('*.c text').length, 1);
1003 assert.equal(parseFile('\n*.c text')[0].pat.pattern, '*.c');
1005 throws(() => unwrap(parseTree(Uint8Array.of(0x20))), 'pack_corrupt', 'tree: malformed entry');
1006 assert.deepEqual(parseIdent('a><b@c> 1 +0000'), { name: 'a>', email: 'b@c', time: '1', tz: '+0000' });
1007 const author = 'Ada <ada@ex> 1700000000 +0000';
1008 const tree = oid(1);
1009 throws(() => unwrap(assertFetched('commit', te.encode(`tree ${tree}\nauthor ><ada@ex> 1700000000 +0000\ncommitter ${author}\n\n`))), 'pack_corrupt', 'commit: badName: invalid author line - bad name');
1010 throws(() => unwrap(assertFetched('commit', te.encode(`tree ${tree}\nauthor >a <ada@ex> 1700000000 +0000\ncommitter ${author}\n\n`))), 'pack_corrupt', 'commit: badName: invalid author line - bad name');
1011 throws(() => unwrap(assertFetched('commit', te.encode(`tree ${tree}\nauthor ${author}\ncommitter Ada\n\n`))), 'pack_corrupt', 'commit: missingEmail: invalid committer line - missing email');
1012 const id = oid(3);
1013 const tagger = author;
1014 throws(() => unwrap(parseTag(te.encode(`type commit\nobject ${id}\ntag v1\ntagger ${tagger}\n\n`))), 'pack_corrupt', "tag: missingObject: invalid format - expected 'object' line");
1015 throws(() => unwrap(parseTag(te.encode(`object ${id}\ntag v1\ntype blob\ntagger ${tagger}\n\n`))), 'pack_corrupt', "tag: missingTypeEntry: invalid format - expected 'type' line");
1017 const onlyDelim = unwrap(pktStep(pktInit(), DELIM));
1018 assert.equal(onlyDelim.state.delim, 1);
1019 assert.equal(onlyDelim.state.end, 0);
1020 assert.equal(onlyDelim.state.flush, 0);
1021 const onlyEnd = unwrap(pktStep(pktInit(), te.encode('0002')));
1022 assert.equal(onlyEnd.state.end, 1);
1023 assert.equal(onlyEnd.state.delim, 0);
1024 const first = unwrap(pkt('ab'));
1025 throws(() => unwrap(pktStep(unwrap(pktStep(pktInit(), first)).state, te.encode('zzzz'))), 'protocol', `bad pkt-line length at byte ${first.length}`);
1026 const long = unwrap(pkt('abcd'));
1027 const afterLong = unwrap(pktStep(pktInit(), long)).state;
1028 const shorter = unwrap(pktStep(afterLong, unwrap(pkt('z'))));
1029 assert.equal(shorter.state.maxPayload, 4);
1030 assert.equal(unwrap(pktStep(pktInit(), unwrap(pkt('a')))).state.carry, null);
1031 const partial = te.encode('000');
1032 const carried = unwrap(pktStep(pktInit(), partial));
1033 partial[0] = 0x31;
1034 assert.equal(carried.state.carry[0], 0x30);
1035 assert.equal(carried.state.carryMax, 3);
1036 const rest = te.encode('6abZ');
1037 const finished = unwrap(pktStep(carried.state, rest));
1038 assert.equal(finished.state.carry.length, 1);
1039 assert.equal(finished.state.carryMax, 3);
1041 const embedded = v2Step(v2Init(), { kind: 'data', payload: te.encode('foo\nbar\n') });
1042 assert.equal(embedded.state.sections[0], 'foo\nbar');
1043 const eleven = new Uint8Array(PACK_HEADER_BYTES - 1);
1044 eleven.set(PACK_SIGNATURE);
1045 eleven[0] ^= 0xff;
1046 assert.equal(spStep(spInit(), eleven).state.phase, 'header');
1047 const continued = encodeObjHeader(TYPE.BLOB, 300);
1048 const waitHead = concat([
1049 Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1),
1050 continued.subarray(0, 1),
1051 ]);
1052 const waited = spStep(spInit(), waitHead);
1053 assert.equal(waited.state.phase, 'header');
1054 assert.deepEqual(waited.zlib, []);
1056 const base = Uint8Array.of(1, 2, 3);
1057 const off4 = Uint8Array.of(
1058 DELTA_COPY | DELTA_COPY_OFF1 | DELTA_COPY_OFF4 | DELTA_COPY_LEN1,
1059 1, 1, 1,
1060 );
1061 throws(() => unwrap(applyDelta(base, concat([encodeVarint(base.length), encodeVarint(1), off4]))), 'pack_corrupt', 'delta: copy out of range');
1062 throws(() => unwrap(applyDelta(base, concat([encodeVarint(base.length), encodeVarint(2), Uint8Array.of(DELTA_COPY | DELTA_COPY_OFF1 | DELTA_COPY_LEN1, 2, 2)]))), 'pack_corrupt', 'delta: copy out of range');
1063 throws(() => unwrap(applyDelta(base, concat([encodeVarint(base.length), encodeVarint(1), Uint8Array.of(2, 9, 9)]))), 'pack_corrupt', 'delta: insert out of range');
1065 const one = [{ ref: 'refs/heads/a' }];
1066 assert.deepEqual(classifyReport([], one), { tag: 'malformed', why: 'no git unpack line first' });
1067 assert.deepEqual(classifyReport([te.encode('unpack ok\n')], one), { tag: 'malformed', why: 'no status for refs/heads/a' });
1068 assert.deepEqual(decideOutcome(classifyReport(['unpack ok\n', 'ng refs/heads/a no\n'].map(l => te.encode(l)), one)), { tag: 'rejected', code: 'push_rejected', refs: [{ ref: 'refs/heads/a', reason: 'no', cls: 'rejected' }] });
1070 const ref = `${oid(4)} refs/heads/a :val`;
1071 assert.deepEqual(parseLsRefLine(ref), { oid: oid(4), name: 'refs/heads/a', peeled: null, 'symref-target': null });
1072 // a doubled space after the oid: an empty name, then `refs/heads/a` as an attribute token (process_ref_v2 splits on every space)
1073 assert.deepEqual(parseLsRefLine(`${oid(4)} refs/heads/a`), { oid: oid(4), name: '', peeled: null, 'symref-target': null });
1074 const spaced = parseLsRefLine(`${oid(4)} refs/heads/a symref-target:b peeled:${oid(5)}`);
1075 assert.deepEqual(spaced, { oid: oid(4), name: 'refs/heads/a', 'symref-target': 'b', peeled: oid(5) });
1077 const hello = te.encode('hello hello hello hello');
1078 const inflated = unwrap(inflate(new Uint8Array(zlib.deflateSync(hello))));
1079 assert.ok(inflated.stats.matchBytes > 0);
1080 assert.equal(inflated.stats.matchBytes, 16);
1081 const repeated = te.encode('abcdefghijabcdefghijab');
1082 const twice = unwrap(inflate(new Uint8Array(zlib.deflateSync(repeated))));
1083 assert.ok(twice.stats.maxLen >= 10);
1084 assert.ok(twice.stats.maxDist >= 10);
1086 const blob = te.encode('hello');
1087 const { pack } = await buildPack([{ type: TYPE.BLOB, data: blob }]);
1088 const shrunk = pack.slice();
1089 shrunk[PACK_HEADER_BYTES] = (shrunk[PACK_HEADER_BYTES] & 0xf0) | 1;
1090 throws(() => unwrap(parsePack(shrunk)), 'pack_corrupt', 'inflate: output exceeds expected 1');
1091 const stored = new Uint8Array(zlib.deflateSync(blob, { level: 0 }));
1092 const header = encodeObjHeader(TYPE.BLOB, blob.length);
1093 const body = concat([Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1), header, stored]);
1094 const { Sha1 } = await import('../src/sha1.js');
1095 const storedPack = concat([body, new Sha1().update(body).digest()]);
1096 const parsed = unwrap(parsePack(storedPack));
1097 assert.equal(parsed.stats.inflate.stored, 1);
1098 assert.equal(parsed.stats.inflate.fixed, 0);
1099 assert.equal(parsed.stats.inflate.dynamic, 0);
1100 const fixed = unwrap(parsePack(pack));
1101 assert.equal(fixed.stats.inflate.stored + fixed.stats.inflate.fixed + fixed.stats.inflate.dynamic, fixed.stats.inflate.blocks);
1102 let words = '';
1103 for (let i = 0; i < 400; i++) words += `word${i} `;
1104 const wordBytes = te.encode(words);
1105 const wordInf = unwrap(inflate(new Uint8Array(zlib.deflateSync(wordBytes))));
1106 assert.equal(wordInf.stats.dynamic, 1);
1107 assert.equal(wordInf.stats.maxDist, 2819);
1108 assert.equal(wordInf.stats.maxLen, 9);
1109 assert.equal(wordInf.stats.matchBytes, 2745);
1110 const variedZ = new Uint8Array(zlib.deflateSync(wordBytes));
1111 const variedHeader = encodeObjHeader(TYPE.BLOB, wordBytes.length);
1112 const variedBody = concat([Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1), variedHeader, variedZ]);
1113 const variedPack = concat([variedBody, new Sha1().update(variedBody).digest()]);
1114 assert.equal(unwrap(parsePack(variedPack)).stats.inflate.dynamic, 1);
1116 assert.equal(parseLine('a text', no).pat.endsWith, false);
1117 assert.deepEqual(parseLine('*.c text=', no).states, [['text', '']]);
1118 assert.deepEqual(parseIdent(' Ada <ada@ex'), { name: 'Ada <ada@ex', email: '', time: null, tz: '' });
1119 throws(() => unwrap(parseTag(te.encode(`object ${id}\ntype commit\ntype blob\ntag v1\ntagger ${tagger}\n\n`))), 'pack_corrupt', "tag: missingTagEntry: invalid format - expected 'tag' line");
1121 throws(() => unwrap(pktStep(pktInit(), te.encode('0000zzzz'))), 'protocol', 'bad pkt-line length at byte 4');
1122 const line = s => ({ kind: 'data', payload: te.encode(s) });
1123 const endOnly = v2Step(v2Init(), { kind: 'end' });
1124 assert.equal(endOnly.state.done, false);
1125 assert.deepEqual(endOnly.state.sections, []);
1126 let shallow = v2Step(v2Init(), line('shallow-info')).state;
1127 shallow = v2Step(shallow, line(`shallow ${oid(4)}`)).state;
1128 const other = v2Step(shallow, line('other')).state;
1129 assert.deepEqual(other.unshallow, []);
1130 assert.deepEqual(other.acks, []);
1132 const ofsHeader = encodeObjHeader(TYPE.OFS_DELTA, 1);
1133 const ofsZ = await zlibDeflate(Uint8Array.of(0));
1134 const ofsBody = concat([Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1), ofsHeader, Uint8Array.of(1), ofsZ]);
1135 throws(() => spStep(spInit(), concat([ofsBody, new Sha1().update(ofsBody).digest()])), 'pack_corrupt', 'pack: single-object pack is a delta');
1136 const whole = pack.slice();
1137 const opened = spStep(spInit(), whole.subarray(0, PACK_HEADER_BYTES + 1));
1138 whole[PACK_HEADER_BYTES] ^= 0xff;
1139 const streamed = spStep(opened.state, whole.subarray(PACK_HEADER_BYTES + 1));
1140 assert.equal(spFinish(streamed.state).trailer.length, 40);
1142 throws(() => unwrap(applyDelta(base, concat([encodeVarint(base.length), encodeVarint(1), Uint8Array.of(DELTA_COPY | DELTA_COPY_LEN1, 2)]))), 'pack_corrupt', 'delta: copy out of range');
1143 throws(() => unwrap(applyDelta(base, concat([encodeVarint(base.length), encodeVarint(5), Uint8Array.of(3, 1)]))), 'pack_corrupt', 'delta: insert out of range');
1145 throws(() => parseLsRefLine('x'.repeat(80)), 'protocol', `ls-refs: malformed line ${JSON.stringify('x'.repeat(60))}`);
1146 throws(() => parseLsRefLine(`${'ab'.repeat(20)}c`), 'protocol', `ls-refs: malformed line ${JSON.stringify(`${'ab'.repeat(20)}c`)}`);
1148 const bits = []; const put = (v, n) => { for (let i = 0; i < n; i++) bits.push((v >> i) & 1); };
1149 put(1, 1); put(2, 2);
1150 // the end-of-block code alone at one bit: the one incomplete literal/length set zlib accepts (inftrees.c); any other
1151 // is refused at the header (test/inflate-codesets.test.js). Code-length symbols 0 and 1 at one bit: 0 → `0`, 1 → `1`.
1152 put(0, 5); put(0, 5); put(14, 4);
1153 for (const n of [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) put(n, 3);
1154 for (let i = 0; i < 256; i++) put(0, 1);
1155 put(1, 1);
1156 put(0, 1);
1157 for (let i = 0; i < 16; i++) put(1, 1);
1158 const raw = Uint8Array.from(Array.from({ length: Math.ceil(bits.length / 8) }, (_, i) => bits.slice(i * 8, i * 8 + 8).reduce((b, v, j) => b | (v << j), 0)));
1159 throws(() => unwrap(inflate(raw, 0, { raw: true, maxOutput: 1024 })), 'pack_corrupt', 'inflate: invalid huffman code');
1161 const helloZ = new Uint8Array(zlib.deflateSync(hello));
1162 assert.equal(unwrap(inflate(helloZ, 0, { expectedSize: hello.length, maxOutput: hello.length + 10 })).out.length, hello.length);
1163 const emptyStored = Uint8Array.of(1, 0, 0, 255, 255);
1164 assert.equal(unwrap(inflate(emptyStored, 0, { raw: true })).out.length, 0);
1165 const zhead = Uint8Array.of(0x78, 0x9c);
1166 const cut = concat([zhead, Uint8Array.of(1, 0, 0, 255, 255)]);
1167 throws(() => unwrap(inflate(cut)), 'pack_corrupt', 'inflate: truncated adler32');
1168});
1170test('parser edges a symmetric or unreachable fixture left alive', async () => {
1171 const no = { macrosOk: false };
1172 assert.equal(parseLine(' "*.c" text', no).pat.pattern, '*.c');
1173 assert.equal(parseLine('a"b text', no).pat.pattern, 'a"b');
1174 assert.deepEqual(parseLine('*.c text more=v', no).states, [['text', true], ['more', 'v']]);
1175 assert.equal(parseFile('*.c text')[0].states[0][0], 'text');
1176 assert.equal(parseFile('\n*.c text\n*.h text').length, 2);
1178 const carriedHex = unwrap(pktStep(pktInit(), te.encode('00'))).state;
1179 throws(() => unwrap(pktStep(carriedHex, te.encode('00zzzz'))), 'protocol', 'bad pkt-line length at byte 4');
1181 const hello = te.encode('hello hello hello hello');
1182 const helloZ = new Uint8Array(zlib.deflateSync(hello));
1183 throws(() => unwrap(inflate(helloZ, 0, { expectedSize: hello.length + 10 })), 'pack_corrupt', `inflate: size ${hello.length} != expected ${hello.length + 10}`);
1185 const { Sha1 } = await import('../src/sha1.js');
1186 const blob = te.encode('hello-hello-hello');
1187 const storedZ = new Uint8Array(zlib.deflateSync(blob, { level: 0 }));
1188 const objHeader = encodeObjHeader(TYPE.BLOB, blob.length);
1189 const head = concat([Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1), objHeader]);
1190 const unsigned = concat([head, storedZ]);
1191 const whole = concat([unsigned, new Sha1().update(unsigned).digest()]);
1192 const opened = spStep(spInit(), whole.subarray(0, head.length));
1193 assert.equal(opened.state.phase, 'body');
1194 assert.equal(opened.state.lag.length, 0);
1195 const chunk = whole.slice(head.length);
1196 const streamed = spStep(opened.state, chunk);
1197 const lagByte = streamed.state.lag[streamed.state.lag.length - 1];
1198 chunk[chunk.length - 1] ^= 0xff;
1199 assert.equal(streamed.state.lag[streamed.state.lag.length - 1], lagByte);
1201 // RFC 1951 §3.2.6 fixed Huffman codes, built the same way inflate does, then emitted MSB first.
1202 const litTab = huffCodes(huffBuild(Array.from({ length: 288 }, (_, i) => (i < 144 ? 8 : i < 256 ? 9 : i < 280 ? 7 : 8))));
1203 const distTab = huffCodes(huffBuild(Array.from({ length: 30 }, () => 5)));
1204 const fixed = (...ops) => {
1205 const bits = [];
1206 const put = (v, n) => { for (let i = 0; i < n; i++) bits.push((v >> i) & 1); };
1207 const putCode = rec => { for (let i = rec.len - 1; i >= 0; i--) bits.push((rec.code >> i) & 1); };
1208 put(1, 1); put(1, 2);
1209 for (const op of ops) {
1210 if (op.lit !== undefined) putCode(litTab.get(op.lit));
1211 else if (op.len) { putCode(litTab.get(op.sym)); if (op.extra) put(op.extra, op.extraBits); putCode(distTab.get(op.dist)); if (op.distExtra !== undefined) put(op.distExtra, op.distExtraBits); }
1212 else putCode(litTab.get(256));
1214 return packBits(bits);
1215 };
1217 // 11 bits: final fixed block, one literal, no end-of-block. The padded high bits of the last byte are zeros.
1218 throws(() => unwrap(inflate(fixed({ lit: 97 }), 0, { raw: true })), 'pack_corrupt', 'inflate: unexpected end of input');
1219 throws(() => unwrap(inflate(fixed({ sym: 286, len: true, dist: 0 }), 0, { raw: true, maxOutput: 64 })), 'pack_corrupt', 'inflate: bad length symbol');
1220 throws(() => unwrap(inflate(fixed({ sym: 257, len: true, dist: 0 }), 0, { raw: true, maxOutput: 64 })), 'pack_corrupt', 'inflate: distance too far back');
1221 const overlapped = unwrap(inflate(fixed({ lit: 97 }, { sym: 257, len: true, dist: 0 }, {}), 0, { raw: true }));
1222 assert.deepEqual([...overlapped.out], [97, 97, 97, 97]);
1223 const distances = unwrap(inflate(fixed(
1224 ...[...'abcdef'].map(ch => ({ lit: ch.charCodeAt(0) })),
1225 { sym: 257, len: true, dist: 4, distExtra: 1, distExtraBits: 1 },
1226 { sym: 257, len: true, dist: 0 },
1227 {},
1228 ), 0, { raw: true }));
1229 assert.equal(distances.stats.maxDist, 6);
1230 assert.equal(distances.out.length, 12);
1232 // One literal plus a 256-byte match is 257 bytes. The initial buffer is 256 and the limit is the exact size,
1233 // so growing onto the limit is legal and stopping one short of it is not.
1234 const onto = unwrap(inflate(fixed({ lit: 97 }, { sym: 284, len: true, extra: 29, extraBits: 5, dist: 0 }, {}), 0, { raw: true, maxOutput: 257 }));
1235 assert.equal(onto.out.length, 257);
1236 // A second match asks for more than twice the current buffer, so the growth loop has to run.
1237 const grown = unwrap(inflate(fixed(
1238 { lit: 97 },
1239 { sym: 284, len: true, extra: 27, extraBits: 5, dist: 0 },
1240 { sym: 285, len: true, dist: 0 },
1241 {},
1242 ), 0, { raw: true }));
1243 assert.equal(grown.out.length, 513);
1245 const stored = new Uint8Array(305);
1246 stored[0] = 1; stored[1] = 300 & 255; stored[2] = 300 >> 8;
1247 const nlen = (~300) & 0xffff; stored[3] = nlen & 255; stored[4] = nlen >> 8;
1248 stored.fill(7, 5);
1249 throws(() => unwrap(inflate(stored, 0, { raw: true, maxOutput: 100 })), 'limit_exceeded', 'inflate: output exceeds limit 100');
1251 const dyn = (hlit, hdist) => {
1252 const bits = [];
1253 const put = (v, n) => { for (let i = 0; i < n; i++) bits.push((v >> i) & 1); };
1254 put(1, 1); put(2, 2); put(hlit, 5); put(hdist, 5); put(0, 4);
1255 return packBits(bits);
1256 };
1257 throws(() => unwrap(inflate(dyn(30, 0), 0, { raw: true })), 'pack_corrupt', 'inflate: bad dynamic counts');
1258 throws(() => unwrap(inflate(dyn(0, 30), 0, { raw: true })), 'pack_corrupt', 'inflate: bad dynamic counts');
1260 // Code-length symbol 16 as the first length, and symbol 18 repeated past the 258 length slots.
1261 const repeat = extras => {
1262 const bits = [];
1263 const put = (v, n) => { for (let i = 0; i < n; i++) bits.push((v >> i) & 1); };
1264 put(1, 1); put(2, 2); put(0, 5); put(0, 5); put(0, 4);
1265 for (const n of extras.head) put(n, 3);
1266 for (const [v, n] of extras.body) put(v, n);
1267 return packBits(bits);
1268 };
1269 // head: the lengths of the code-length symbols 16, 17, 18, 0. Each set is complete (two 1-bit codes: 0 → `0`, the
1270 // other → `1`); zlib refuses a one-code code-length set as `invalid code lengths set` before any symbol is read.
1271 throws(() => unwrap(inflate(repeat({ head: [1, 0, 0, 1], body: [[1, 1]] }), 0, { raw: true })), 'pack_corrupt', 'inflate: repeat with no previous');
1272 throws(() => unwrap(inflate(repeat({ head: [0, 0, 1, 1], body: [[1, 1], [127, 7], [1, 1], [127, 7]] }), 0, { raw: true })), 'pack_corrupt', 'inflate: repeat overflow');
1273 throws(() => unwrap(inflate(repeat({ head: [0, 0, 1, 1], body: [[1, 1], [127, 7], [1, 1], [120 - 11, 7]] }), 0, { raw: true })), 'pack_corrupt', 'inflate: no end-of-block code');
1274 // and the one-code set itself is refused as zlib refuses it
1275 throws(() => unwrap(inflate(repeat({ head: [1, 0, 0, 0], body: [[0, 1]] }), 0, { raw: true })), 'pack_corrupt', 'inflate: invalid code lengths set');
1276 assert.throws(() => zlib.inflateRawSync(repeat({ head: [1, 0, 0, 0], body: [[0, 1]] })), /invalid code lengths set/);
1278 // 15 data bits end on the last bit of the buffer, so one more Huffman step has nothing to read.
1279 const hole = [];
1280 const putHole = (v, n) => { for (let i = 0; i < n; i++) hole.push((v >> i) & 1); };
1281 // the end-of-block code alone at one bit (the incomplete set zlib accepts), read as the unused `1`
1282 putHole(1, 1); putHole(2, 2); putHole(0, 5); putHole(0, 5); putHole(14, 4);
1283 for (const n of [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) putHole(n, 3);
1284 for (let i = 0; i < 256; i++) putHole(0, 1);
1285 putHole(1, 1); putHole(0, 1);
1286 for (let i = 0; i < 15; i++) putHole(1, 1);
1287 assert.equal(hole.length % 8, 0);
1288 throws(() => unwrap(inflate(packBits(hole), 0, { raw: true, maxOutput: 32 })), 'pack_corrupt', 'inflate: invalid huffman code');
1289});
1291test('parsePack caps reject a hostile count and inflated total on the boundary', async () => {
1292 const blob = te.encode('hello');
1293 const other = te.encode('hello!');
1294 const { pack } = await buildPack([{ type: TYPE.BLOB, data: blob }]);
1295 const { pack: two } = await buildPack([{ type: TYPE.BLOB, data: blob }, { type: TYPE.BLOB, data: other }]);
1296 const emptyBody = Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 0);
1297 const { Sha1 } = await import('../src/sha1.js');
1298 const emptyPack = concat([emptyBody, new Sha1().update(emptyBody).digest()]);
1299 // each object charges its declared bytes plus LIMIT_OBJECT_OVERHEAD (#142)
1300 const total = blob.length + other.length + 2 * OV;
1302 assert.equal(unwrap(parsePack(pack, { maxObjects: 1, maxInflatedTotal: blob.length + OV })).objects.length, 1);
1303 assert.equal(unwrap(parsePack(emptyPack, { maxObjects: 0, maxInflatedTotal: 0 })).stats.count, 0);
1304 assert.equal(unwrap(parsePack(two, { maxObjects: 2, maxInflatedTotal: total })).objects.length, 2);
1305 throws(() => unwrap(parsePack(pack, { maxObjects: 0 })), 'limit_exceeded', 'pack objects 1 exceeds limit 0');
1306 throws(() => unwrap(parsePack(two, { maxObjects: 1 })), 'limit_exceeded', 'pack objects 2 exceeds limit 1');
1307 throws(() => unwrap(parsePack(pack, { maxInflatedTotal: blob.length + OV - 1 })), 'limit_exceeded', `pack inflated bytes ${blob.length + OV} exceeds limit ${blob.length + OV - 1}`);
1308 // the overhead alone is charged: a pack that holds its bytes but not its objects' records is refused
1309 throws(() => unwrap(parsePack(pack, { maxInflatedTotal: blob.length })), 'limit_exceeded', `pack inflated bytes ${blob.length + OV} exceeds limit ${blob.length}`);
1310 throws(() => unwrap(parsePack(two, { maxInflatedTotal: total - 1 })), 'limit_exceeded', `pack inflated bytes ${total} exceeds limit ${total - 1}`);
1312 const zero = unwrap(packParseOpts(withLimits({ maxPackObjects: 0, maxInflatedTotal: 0 })));
1313 assert.equal(zero.maxObjects, 0);
1314 assert.equal(zero.maxInflatedTotal, 0);
1315 assert.equal(zero.limits.maxObjectSize, withLimits({}).maxObjectSize);
1316 throws(() => unwrap(parsePack(pack, zero)), 'limit_exceeded', 'pack objects 1 exceeds limit 0');
1318 const positive = unwrap(packParseOpts({ maxPackObjects: 1, maxInflatedTotal: blob.length + OV, maxObjectSize: blob.length + blob.length }));
1319 assert.equal(positive.maxObjects, 1);
1320 assert.equal(positive.maxInflatedTotal, blob.length + OV);
1321 assert.equal(positive.limits.maxObjectSize, blob.length + blob.length);
1322 assert.equal(unwrap(parsePack(pack, positive)).objects.length, 1);
1324 const open = unwrap(packParseOpts());
1325 assert.equal(open.maxObjects, DEFAULT_LIMITS.maxPackObjects);
1326 assert.equal(open.maxInflatedTotal, DEFAULT_LIMITS.maxInflatedTotal);
1327 assert.equal(unwrap(parsePack(pack, open)).stats.inflated, blob.length);
1328 throws(() => unwrap(packParseOpts({ maxObjectSize: 1, maxPackObjects: -1 })), 'bad_request', 'limits.maxPackObjects must be a finite non-negative number');
1329 throws(() => unwrap(parsePack(two, { limits: withLimits({ maxPackObjects: 1 }) })), 'limit_exceeded', 'pack objects 2 exceeds limit 1');
1330 throws(() => unwrap(parsePack(pack, { maxObjects: Infinity, maxInflatedTotal: blob.length + OV })), 'bad_request', 'limits.maxPackObjects must be a finite non-negative number');
1331});
1333function huffCodes(h) {
1334 const map = new Map();
1335 let code = 0, index = 0;
1336 for (let len = 1; len < 16; len++) {
1337 for (let i = 0; i < h.count[len]; i++) { map.set(h.symbol[index + i], { len, code }); code++; }
1338 index += h.count[len];
1339 code <<= 1;
1341 return map;
1343function packBits(bits) {
1344 return Uint8Array.from(Array.from({ length: Math.ceil(bits.length / 8) }, (_, i) => bits.slice(i * 8, i * 8 + 8).reduce((b, v, j) => b | (v << j), 0)));
1347const spanStub = { set() {}, event() {}, error() {} };
1348const tracerStub = { span: async (_name, _opts, fn) => fn(spanStub) };
1350test('every DEFAULT_LIMITS key is read outside limits.js', () => {
1351 const src = readdirSync('src').filter(f => /\.[jt]s$/.test(f) && !f.startsWith('limits.')).map(f => readFileSync(join('src', f), 'utf8')).join('\n');
1352 for (const key of Object.keys(DEFAULT_LIMITS)) assert.match(src, new RegExp(key), key);
1353 assert.equal(DEFAULT_LIMITS.maxInflatedTotal, DEFAULT_LIMITS.maxObjectSize * 4);
1354 assert.equal(DEFAULT_LIMITS.maxPackObjects, 100_000);
1355 assert.equal(DEFAULT_LIMITS.maxLsRefsBytes, DEFAULT_LIMITS.maxPackBytes);
1356 assert.equal(DEFAULT_LIMITS.maxReportLines, DEFAULT_LIMITS.maxRefs);
1357 assert.equal(DEFAULT_LIMITS.maxCommitParents, DEFAULT_LIMITS.maxRefs);
1358 assert.equal(DEFAULT_LIMITS.maxRebaseCommits, DEFAULT_LIMITS.maxRefs);
1359 throws(() => unwrap(checkLimit(Infinity, 'maxObjectSize')), 'bad_request', 'limits.maxObjectSize must be a finite non-negative number');
1360 throws(() => unwrap(checkLimit(-1, 'maxObjectSize')), 'bad_request', 'limits.maxObjectSize must be a finite non-negative number');
1361 throws(() => unwrap(parseLimits({ maxInflatedTotal: null })), 'bad_request', 'limits.maxInflatedTotal must be a finite non-negative number');
1362 assert.equal(unwrap(parseLimits({ maxObjectSize: 0 })).maxObjectSize, 0);
1363 assert.equal(unwrap(parseLimits({})).maxInflatedTotal, DEFAULT_LIMITS.maxInflatedTotal);
1364});
1366test('aggregate caps fail closed before allocation', async () => {
1367 const hostile = new Uint8Array(32);
1368 hostile.set(PACK_SIGNATURE);
1369 new DataView(hostile.buffer).setUint32(4, PACK_VERSION);
1370 new DataView(hostile.buffer).setUint32(8, DEFAULT_LIMITS.maxPackObjects + 1);
1371 throws(() => unwrap(parsePack(hostile)), 'limit_exceeded', `pack objects ${DEFAULT_LIMITS.maxPackObjects + 1} exceeds limit ${DEFAULT_LIMITS.maxPackObjects}`);
1373 const hello = te.encode('hi');
1374 const zhello = new Uint8Array(zlib.deflateSync(hello));
1375 throws(() => unwrap(inflate(zhello, 0, { expectedSize: DEFAULT_LIMITS.maxObjectSize + 1 })), 'limit_exceeded', `inflate: expected size ${DEFAULT_LIMITS.maxObjectSize + 1} exceeds limit ${DEFAULT_LIMITS.maxObjectSize}`);
1376 throws(() => unwrap(inflate(zhello, 0, { maxOutput: Infinity })), 'bad_request', 'limits.maxObjectSize must be a finite non-negative number');
1378 const twoEntries = unwrap(encodeTree([{ mode: '100644', name: 'a', oid: oid(1) }, { mode: '100644', name: 'b', oid: oid(2) }]));
1379 assert.equal(unwrap(parseTree(twoEntries, { maxTreeEntries: 2 })).length, 2);
1380 throws(() => unwrap(parseTree(twoEntries, { maxTreeEntries: 1 })), 'limit_exceeded', 'tree entries 2 exceeds limit 1');
1381 // Longer than one minimum frame, one entry: the byte hint must not treat it as past the cap.
1382 const longName = 'n'.repeat(40);
1383 const wideTree = unwrap(encodeTree([{ mode: '100644', name: longName, oid: oid(1) }]));
1384 const underCap = unwrap(parseTree(wideTree, { maxTreeEntries: 1 }));
1385 assert.equal(underCap.length, 1);
1386 assert.equal(underCap[0].name, longName);
1387 throws(() => unwrap(parseTree(twoEntries, { maxTreeEntries: -1 })), 'bad_request', 'limits.maxTreeEntries must be a finite non-negative number');
1388 assert.equal(unwrap(parseTree(twoEntries, {})).length, 2);
1390 const tree = oid(1);
1391 const who = 'A <a@b.c> 1 +0000';
1392 const parents = [oid(2), oid(3)];
1393 const commit = te.encode(`tree ${tree}\n${parents.map(p => `parent ${p}`).join('\n')}\nauthor ${who}\ncommitter ${who}\n\nm\n`);
1394 assert.equal(unwrap(parseCommit(commit, { maxCommitParents: 2 })).parents.length, 2);
1395 throws(() => unwrap(parseCommit(commit, { maxCommitParents: 1 })), 'limit_exceeded', 'commit parents 2 exceeds limit 1');
1396 throws(() => unwrap(parseCommit(commit, { maxCommitParents: NaN })), 'bad_request', 'limits.maxCommitParents must be a finite non-negative number');
1397 assert.equal(unwrap(parseCommit(commit, {})).parents.length, 2);
1399 const line = s => ({ kind: 'data', payload: te.encode(s) });
1400 // v2Init takes a checked record: the caller's is refused where it is parsed, before any frame is read
1401 throws(() => unwrap(parseLimits({ maxSectionLines: Infinity })), 'bad_request', 'limits.maxSectionLines must be a finite non-negative number');
1402 let st = v2Step(v2Init(withLimits({ maxSectionLines: 1 })), line('wanted-refs')).state;
1403 st = v2Step(st, line('abc')).state;
1404 throws(() => v2Step(st, line('def')), 'limit_exceeded', 'section lines 2 exceeds limit 1');
1405 st = v2Step(v2Init(withLimits({ maxSectionLines: 1 })), line('acknowledgments')).state;
1406 st = v2Step(st, { kind: 'delim' }).state;
1407 throws(() => v2Step(st, line('shallow-info')), 'limit_exceeded', 'section headers 2 exceeds limit 1');
1408 // one wanted-refs body line under a cap of 2 is still kept
1409 st = v2Step(v2Init(withLimits({ maxSectionLines: 2 })), line('wanted-refs')).state;
1410 assert.equal(v2Step(st, line('abc')).state.section, 'wanted-refs');
1412 // the report's line bound is the read's (push: maxReportLines), and every limit push reads is checked before anything is sent
1413 const delA = { commands: [{ ref: 'refs/heads/a', oldOid: oid(1), newOid: ZERO_OID }] };
1414 for (const key of ['maxRefs', 'maxReportLines', 'maxPackBytes']) {
1415 const r = planPush(delA, withLimits({ [key]: null }));
1416 assert.equal(r.ok, false); assert.equal(r.error.code, 'bad_request'); assert.equal(r.error.message, `limits.${key} must be a finite non-negative number`);
1418 const planned = unwrap(planPush(delA, withLimits({ maxReportLines: 1, maxPackBytes: 2 })));
1419 assert.deepEqual([planned.maxReportLines, planned.maxPack, planned.needPack, planned.checked], [1, 2, false, null]);
1421 const base = te.encode('abcd');
1422 const delta = makeDelta(base, te.encode('abXY')).delta;
1423 const baseOid = hashObject('blob', base);
1424 const { pack } = await buildPack([
1425 { type: TYPE.BLOB, data: delta, base: { tag: 'ref', oid: baseOid } },
1426 { type: TYPE.BLOB, data: delta, base: { tag: 'ref', oid: baseOid } },
1427 { type: TYPE.BLOB, data: delta, base: { tag: 'ref', oid: baseOid } },
1428 { type: TYPE.BLOB, data: base },
1429 ]);
1430 const parsed = unwrap(parsePack(pack));
1431 assert.equal(unwrap(resolvePack(parsed)).stats.baseLookups, 3);
1432 assert.throws(() => unwrap(resolvePack(parsed, undefined, withLimits({ maxInflatedTotal: parsed.stats.inflated + 4 * OV + te.encode('abXY').length }))), e => e.code === 'limit_exceeded');
1433 throws(() => unwrap(applyDelta(base, makeDelta(base, base).delta, { maxResult: Infinity })), 'bad_request', 'limits.maxDeltaResult must be a finite non-negative number');
1434});
1436test('idents are branded, wire fields reject breaks, atomic split is ambiguous, ls-refs needs a flush', async () => {
1437 const person = unwrap(validateIdent({ name: 'A', email: 'a@b.c', time: 1 }));
1438 assert.equal(isValidatedIdent(person), true);
1439 assert.equal(person.tz, '+0000');
1440 assert.equal(unwrap(ident(person)), 'A <a@b.c> 1 +0000');
1441 throws(() => unwrap(ident({ name: 'A', email: 'a@b.c', time: 1, tz: '+0000' })), 'bad_request', 'ident: unvalidated identity');
1442 throws(() => unwrap(ident({ ...person })), 'bad_request', 'ident: unvalidated identity');
1443 for (const tz of ['+0000', '-0000', '+0530', '-0530', '+0059', '+1400', '-1200']) unwrap(validateIdent({ name: 'A', email: 'a@b.c', time: 0, tz }));
1444 for (const tz of ['+1401', '-1230', '+9999', '+1560', '+2400', '0000', '+000', '+00000']) {
1445 assert.throws(() => unwrap(validateIdent({ name: 'A', email: 'a@b.c', time: 0, tz })), e => e.code === 'bad_request' && e.what === 'tz', tz);
1447 throws(() => unwrap(validateIdent({ name: 'A', email: 'a@b.c', time: -1 })), 'bad_request', 'invalid time: must be a non-negative safe integer');
1448 throws(() => unwrap(validateIdent({ name: 'A', email: 'a@b.c', time: 1.5 })), 'bad_request', 'invalid time: must be a non-negative safe integer');
1449 throws(() => unwrap(validateIdent({ name: 'A', email: 'a@b.c', time: '1700000000' })), 'bad_request', 'invalid time: must be a non-negative safe integer');
1450 throws(() => unwrap(validateIdent({ name: 'A', email: 'a@b.c', time: 0, tz: '+0000\nparent x' })), 'bad_request', 'invalid tz: must be a sign and four digits');
1452 const tree = oid(1);
1453 throws(() => unwrap(encodeCommit({ tree, author: 'A <a@b.c> 1 +0000', committer: 'y\nparent x <a@b.c> 1 +0000' })), 'bad_request', 'committer must not contain NUL or newline');
1454 throws(() => unwrap(encodeCommit({ tree, author: 'A <a@b.c> 1 +0000', message: 'a\0b' })), 'bad_request', 'message must not contain NUL');
1455 assert.ok(unwrap(encodeCommit({ tree, author: 'A <a@b.c> 1 +0000', message: 'a\nb\n' })).byteLength > 0);
1456 throws(() => unwrap(encodeTag({ object: tree, type: 'commit', tag: 'v\n1', tagger: 'A <a@b.c> 1 +0000' })), 'bad_request', 'tag must not contain NUL or newline');
1457 // encodeTag refuses what git hash-object -t tag refuses (fsck_tag_standalone), as a bad request (issue #103)
1458 const tagOf = f => unwrap(encodeTag({ object: tree, type: 'commit', tag: 'v1', tagger: 'A <a@b.c> 1 +0000', message: 'm\n', ...f }));
1459 assert.equal(td.decode(tagOf({})), `object ${tree}\ntype commit\ntag v1\ntagger A <a@b.c> 1 +0000\n\nm\n`);
1460 assert.equal(td.decode(tagOf({ tag: 'release/v1', message: null, extra: ['x y'] })), `object ${tree}\ntype commit\ntag release/v1\ntagger A <a@b.c> 1 +0000\nx y\n`);
1461 for (const tag of ['', 'v 1', 'a..b', 'v1.lock', 'v1/', 'v*']) throws(() => tagOf({ tag }), 'bad_request', "tag: badTagName: invalid 'tag' name");
1462 throws(() => tagOf({ object: 'zz'.repeat(20) }), 'bad_request', "tag: badObjectSha1: invalid 'object' line format - bad sha1");
1463 throws(() => tagOf({ type: 'bogus' }), 'bad_request', "tag: badType: invalid 'type' value");
1464 throws(() => tagOf({ tagger: 'A a@b.c 1 +0000' }), 'bad_request', 'tag: missingEmail: invalid tagger line - missing email');
1465 throws(() => tagOf({ extra: ['tag v2'] }), 'bad_request', 'tag: duplicate tag');
1466 assert.throws(() => tagOf({ type: 'bogus' }), e => e.what === 'tag');
1467 assert.throws(() => tagOf({ tag: '' }), e => e.what === 'tag');
1469 throws(() => fetchBody({ wants: ['abc\n'] }), 'bad_request', 'invalid want: must be a string without NUL, CR, or LF');
1470 throws(() => fetchBody({ wants: [oid(1)], deepen: -1 }), 'bad_request', 'invalid deepen: must be a non-negative safe integer');
1471 throws(() => fetchBody({ wants: [oid(1)], deepenSince: '1\n2' }), 'bad_request', 'invalid deepen-since: must be a string without NUL, CR, or LF');
1472 throws(() => fetchBody({ wants: [oid(1)], haves: ['a\rb'] }), 'bad_request', 'invalid have: must be a string without NUL, CR, or LF');
1473 assert.ok(fetchBody({ wants: [oid(1)], deepenSince: 1, filter: 'blob:none', deepen: 1 }).byteLength > 0);
1474 await assert.rejects(() => negotiate({}, { wants: [oid(1)], haves: [oid(2)], maxRounds: Infinity }), e => e.code === 'bad_request' && e.what === 'maxNegotiateRounds');
1475 await assert.rejects(() => negotiate({}, { wants: [oid(1)], haves: [oid(2)], batch: 0 }), e => e.code === 'bad_request' && e.what === 'negotiateBatch');
1477 const commands = [{ ref: 'refs/heads/a' }, { ref: 'refs/heads/b' }];
1478 const mixed = classifyReport(['unpack ok\n', 'ok refs/heads/a\n', 'ng refs/heads/b failed to lock\n'].map(l => te.encode(l)), commands);
1479 assert.deepEqual(decideOutcome(mixed, { atomic: true }), { tag: 'ambiguous', why: 'atomic push split: some refs ok, some ng' });
1480 assert.deepEqual(decideOutcome(mixed), { tag: 'partial', landed: ['refs/heads/a'], rejected: [{ ref: 'refs/heads/b', reason: 'failed to lock', cls: 'cas' }] });
1482 const refLine = `${oid(4)} refs/heads/main\n`;
1483 const framesOf = list => ({
1484 limits: DEFAULT_LIMITS, tracer: tracerStub, parent: null,
1485 async *frames() { for (const f of list) yield f; },
1486 });
1487 await assert.rejects(lsRefs(framesOf([{ kind: 'data', payload: te.encode(refLine) }])), e => e.code === 'protocol' && e.message === 'ls-refs: response ended without the terminating flush-pkt');
1488 await assert.rejects(lsRefs(framesOf([{ kind: 'data', payload: te.encode(refLine) }, { kind: 'flush' }, { kind: 'data', payload: te.encode(refLine) }])), e => e.code === 'protocol' && e.message === 'ls-refs: data after the terminating flush-pkt');
1489 // #144: an answer with no frame at all is what a v0/v1 upload-pack sends a v2 request; the advertisement says which
1490 const advertising = (body) => ({ ...framesOf([]), advertise: async () => body });
1491 const lsAdv = (...lines) => ({ ok: true, value: concat(lines.map(l => (l === null ? FLUSH : unwrap(pkt(l))))) });
1492 const v0 = lsAdv('# service=git-upload-pack\n', null, `${oid(4)} HEAD\0multi_ack side-band-64k\n`, null);
1493 await assert.rejects(lsRefs(advertising(v0)), e => e.code === 'unsupported' && e.what === 'protocol version' && e.message === `ls-refs: the server does not speak protocol v2, which ply requires (an empty answer to command=ls-refs; advertisement: server did not answer protocol v2 (first line ${JSON.stringify(`${oid(4)} HEAD\0multi_ack side-band-64k`)}))`);
1494 await assert.rejects(lsRefs(advertising(lsAdv('version 1\n', `${oid(4)} HEAD\n`, null))), e => e.code === 'unsupported' && e.what === 'protocol version' && e.message.endsWith('(first line "version 1"))'));
1495 for (const body of [lsAdv('version 2\n', 'ls-refs=unborn\n', null), { ok: false, error: { code: 'http', message: 'down' } }, { ok: true, value: te.encode('zzzz') }]) {
1496 await assert.rejects(lsRefs(advertising(body)), e => e.code === 'protocol' && e.message === 'ls-refs: response ended without the terminating flush-pkt');
1498 await assert.rejects(lsRefs(framesOf([])), e => e.code === 'protocol' && e.message === 'ls-refs: response ended without the terminating flush-pkt');
1499 await assert.rejects(lsRefs(framesOf([{ kind: 'delim' }])), e => e.code === 'protocol' && e.message === 'ls-refs: unexpected delim in ref listing');
1500 const listed = await lsRefs(framesOf([{ kind: 'data', payload: te.encode(refLine) }, { kind: 'flush' }]));
1501 assert.equal(listed.refs.get('refs/heads/main').oid, oid(4));
1502 // ref names are bytes (#43): a trailing U+FEFF or U+00A0 stays (trim() ate both), two Latin-1 names stay two refs
1503 const odd = [te.encode(`${oid(1)} refs/heads/x\ufeff\n`), te.encode(`${oid(2)} refs/heads/y\u00a0\n`), Uint8Array.from([...te.encode(`${oid(3)} refs/heads/`), 0xe9, 0x0a]), Uint8Array.from([...te.encode(`${oid(5)} refs/heads/`), 0xe8, 0x0a]), te.encode(`${oid(6)} refs/heads/cr\r\n`)];
1504 const oddRefs = (await lsRefs(framesOf([...odd.map(payload => ({ kind: 'data', payload })), { kind: 'flush' }]))).refs;
1505 assert.deepEqual([...oddRefs.keys()], ['refs/heads/x\ufeff', 'refs/heads/y\u00a0', 'refs/heads/\udce9', 'refs/heads/\udce8', 'refs/heads/cr\r']);
1506 assert.deepEqual(unwrap(pkt('refs/heads/\udce9')), Uint8Array.from([...te.encode('0010refs/heads/'), 0xe9]));
1507 await assert.rejects(lsRefs(framesOf([]), ['refs/heads/a\nb']), e => e.code === 'bad_request' && e.what === 'ref prefix');
1508 const payload = te.encode(refLine);
1509 await assert.rejects(lsRefs({ ...framesOf([{ kind: 'data', payload }, { kind: 'flush' }]), limits: withLimits({ maxLsRefsBytes: payload.length - 1 }) }), e => e.code === 'limit_exceeded' && e.what === 'ls-refs bytes');
1511 // unborn (issue #103): the reader, the request argument, and the advertisement check that gates it
1512 const M = { name: 'HEAD', target: 'refs/heads/main' };
1513 assert.deepEqual(parseUnbornLine('unborn HEAD symref-target:refs/heads/main'), M);
1514 assert.deepEqual(parseUnbornLine('unborn HEAD peeled:x symref-target:refs/heads/a symref-target:refs/heads/b'), { name: 'HEAD', target: 'refs/heads/a' });
1515 assert.equal(parseUnbornLine('unborn HEAD', M), M, 'no target: the previous one stands');
1516 assert.equal(parseUnbornLine('unborn HEAD symref-targe:refs/heads/x', M), M, 'not the attribute');
1517 assert.equal(parseUnbornLine('unborn HEAD xsymref-target:refs/heads/x', M), M, 'the key starts the token');
1518 assert.equal(parseUnbornLine('unborn HEAD'), null, 'prev defaults to null');
1519 assert.equal(parseUnbornLine('unborn HEAD symref-target:', M), null, 'an empty target clears it');
1520 assert.deepEqual(parseUnbornLine('unborn HEAD symref-target::x'), { name: 'HEAD', target: ':x' });
1521 assert.equal(parseUnbornLine('unborn refs/heads/x symref-target:refs/heads/y', M), M, 'only HEAD');
1522 assert.equal(parseUnbornLine('unborn HEAD symref-target:refs/heads/y', M), M, 'a doubled space is an empty name');
1523 assert.equal(parseUnbornLine(`${oid(1)} HEAD symref-target:refs/heads/y`, M), M, 'not an unborn line');
1524 assert.equal(parseUnbornLine('unbornx HEAD symref-target:refs/heads/y', M), M);
1525 const bodyOf = (...a) => td.decode(lsRefsBody(...a));
1526 assert.equal(bodyOf(['HEAD'], { unborn: true }), '0014command=ls-refs\n00010009peel\n000csymrefs\n000bunborn\n0014ref-prefix HEAD\n0000');
1527 assert.equal(bodyOf(['HEAD']), '0014command=ls-refs\n00010009peel\n000csymrefs\n0014ref-prefix HEAD\n0000');
1528 assert.equal(bodyOf([], { unborn: false }), bodyOf([]));
1529 const unbornFrames = [te.encode('unborn HEAD symref-target:refs/heads/a\n'), te.encode('unborn HEAD symref-target:refs/heads/b\n'), te.encode('unborn HEAD\n'), te.encode(refLine)];
1530 const recording = (list, adv = null) => {
1531 const sent = [], ads = [];
1532 return { sent, ads, t: { ...framesOf(list), url: 'http://x/r.git', kind: 'http', async *frames(_svc, body) { sent.push(td.decode(body)); for (const f of list) yield f; }, async advertise() { ads.push(1); return { ok: true, value: adv }; } } };
1533 };
1534 const listing = [...unbornFrames.map(p => ({ kind: 'data', payload: p })), { kind: 'flush' }];
1535 const withReader = recording(listing);
1536 const got = await lsRefs(withReader.t, [], { unborn: parseUnbornLine });
1537 assert.deepEqual(got.unborn, { name: 'HEAD', target: 'refs/heads/b' }, 'the last target wins');
1538 assert.equal(got.refs.size, 1);
1539 assert.match(withReader.sent[0], /000bunborn\n/);
1540 const noReader = recording(listing);
1541 assert.equal((await lsRefs(noReader.t)).unborn, null);
1542 assert.doesNotMatch(noReader.sent[0], /unborn/);
1543 // the public lsRefs: unborn is sent only when the advertisement lists ls-refs=unborn (and only when asked)
1544 const adv = feature => concat([unwrap(pkt('version 2\n')), unwrap(pkt('agent=git/2.50.0\n')), unwrap(pkt(`ls-refs${feature}\n`)), unwrap(pkt('fetch=filter\n')), unwrap(pkt('object-format=sha1\n')), FLUSH]);
1545 for (const [feature, asked, sends] of [['=unborn', true, true], ['', true, false], ['=other', true, false], ['=unborn', false, false]]) {
1546 const r = recording(listing, adv(feature));
1547 const res = await unwrapAsync(lsRefsAsked(r.t, [], asked ? { unborn: true } : {}));
1548 assert.equal(/unborn/.test(r.sent[0]), sends, `${feature} asked=${asked}`);
1549 assert.deepEqual(res.unborn, sends ? { name: 'HEAD', target: 'refs/heads/b' } : null, `${feature} asked=${asked}`);
1550 assert.equal(r.ads.length, asked ? 1 : 0, 'the advertisement is fetched only when asked');
1552 for (const lsRefsUnborn of [true, false]) {
1553 const r = recording(listing, null);
1554 const res = await unwrapAsync(lsRefsAsked(r.t, [], { unborn: true, info: { lsRefsUnborn } }));
1555 assert.equal(r.ads.length, 0, 'info in hand: no advertisement');
1556 assert.equal(/unborn/.test(r.sent[0]), lsRefsUnborn);
1557 assert.equal(res.unborn !== null, lsRefsUnborn);
1560 const reportLines = ['unpack ok\n', 'ok refs/heads/a\n', 'ng refs/heads/b failed to lock\n'];
1561 const receipt = () => ({
1562 limits: DEFAULT_LIMITS, tracer: tracerStub, parent: null,
1563 async *frames() { for (const l of reportLines) yield { kind: 'data', payload: te.encode(l) }; yield { kind: 'flush' }; },
1564 });
1565 const deletes = [
1566 { ref: 'refs/heads/a', oldOid: ZERO_OID, newOid: ZERO_OID },
1567 { ref: 'refs/heads/b', oldOid: ZERO_OID, newOid: ZERO_OID },
1568 ];
1569 const all = new Set(['atomic', 'push-options']);
1570 await assert.rejects(unwrapAsync(push(receipt(), { commands: deletes, sideband: false, checked: unwrap(checkPushCaps(all, { atomic: true })) })), e => e.code === 'ambiguous' && /atomic push split/.test(e.message));
1571 // atomic and push options only as checked capabilities: an untyped caller's raw fields are refused before anything is sent
1572 for (const raw of [{ atomic: true }, { pushOptions: ['x'] }]) await assert.rejects(unwrapAsync(push(receipt(), { commands: deletes, sideband: false, ...raw })), e => e.code === 'bad_request' && e.message === 'push: unchecked atomic or push options');
1573 await assert.rejects(unwrapAsync(push(receipt(), { commands: deletes, sideband: false, atomic: false, pushOptions: [] })), e => e.code === 'push_partial');
1574 await assert.rejects(unwrapAsync(push(receipt(), { commands: deletes, sideband: false, checked: unwrap(checkPushCaps(all, { atomic: false })) })), e => e.code === 'push_partial' && e.message === 'push partially applied: refs/heads/a updated; refs/heads/b (failed to lock) refused' && e.landed.join() === 'refs/heads/a' && e.rejected[0].reason === 'failed to lock');
1575 await assert.rejects(unwrapAsync(push(receipt(), { commands: [{ ref: 'refs/heads/a\n', oldOid: ZERO_OID, newOid: ZERO_OID }], sideband: false })), e => e.code === 'bad_request' && e.what === 'ref');
1576 await assert.rejects(unwrapAsync(push(receipt(), { ref: 'refs/heads/a', oldOid: ZERO_OID, newOid: ZERO_OID, sideband: false, checked: unwrap(checkPushCaps(all, { pushOptions: ['a\nb'] })) })), e => e.code === 'bad_request' && e.what === 'push option');
1577 await assert.rejects(unwrapAsync(push({ ...receipt(), limits: withLimits({ maxRefs: 1 }) }, { commands: deletes, sideband: false })), e => e.code === 'limit_exceeded' && e.what === 'push commands');
1579 const fetchImpl = (_url, init) => new Promise((_resolve, reject) => {
1580 // The request timer is unref'd, so a live socket (here, an interval) has to keep the loop awake.
1581 const keep = setInterval(() => {}, 50);
1582 init.signal.addEventListener('abort', () => { clearInterval(keep); reject(init.signal.reason); });
1583 });
1584 const http = createTransport('http://127.0.0.1:9/r.git', { fetchImpl, limits: withLimits({ requestTimeoutMs: 40 }), tracer: tracerStub });
1585 const t0 = Date.now();
1586 await assert.rejects((async () => { for await (const frame of http.frames('git-upload-pack', new Uint8Array(0), spanStub)) void frame; })(), e => e.code === 'timeout');
1587 assert.ok(Date.now() - t0 < 2000);
1589 assert.throws(() => createTransport('http://127.0.0.1/r.git', { userAgent: 'ply\r\nX: 1' }), e => e.code === 'bad_request' && e.what === 'userAgent');
1590 assert.throws(() => createTransport('http://127.0.0.1/r.git', { auth: { bearer: 'tok\n' } }), e => e.code === 'bad_request' && e.what === 'bearer');
1591 let pulls = 0;
1592 const advBody = new ReadableStream({ pull(controller) { pulls++; controller.enqueue(new Uint8Array(1000)); if (pulls > 8) controller.close(); } });
1593 const advHttp = createTransport('http://127.0.0.1:9/r.git', {
1594 fetchImpl: async () => new Response(advBody, { status: 200, headers: { 'content-type': 'application/x-git-upload-pack-advertisement' } }),
1595 limits: withLimits({ maxPackBytes: 2500 }), tracer: tracerStub,
1596 });
1597 await assert.rejects(unwrapAsync(advHttp.advertise('git-upload-pack', spanStub)), e => e.code === 'limit_exceeded' && e.what === 'advertisement bytes' && e.got > 2500);
1598 assert.ok(pulls <= 4, `advertisement kept reading (${pulls} chunks)`);
1599 const declared = createTransport('http://127.0.0.1:9/r.git', {
1600 fetchImpl: async () => ({
1601 ok: true, status: 200,
1602 headers: { get: n => (n === 'content-type' ? 'application/x-git-upload-pack-advertisement' : n === 'content-length' ? '999999999' : null) },
1603 body: { async *[Symbol.asyncIterator]() { throw new Error('body should not be read'); }, async cancel() {} },
1604 }),
1605 limits: withLimits({ maxPackBytes: 2500 }), tracer: tracerStub,
1606 });
1607 await assert.rejects(unwrapAsync(declared.advertise('git-upload-pack', spanStub)), e => e.code === 'limit_exceeded' && e.what === 'advertisement bytes' && e.got === 999999999);
1608 let errPulls = 0;
1609 const errBody = new ReadableStream({ pull(controller) { errPulls++; controller.enqueue(te.encode('x'.repeat(3000))); if (errPulls > 6) controller.close(); } });
1610 const errHttp = createTransport('http://127.0.0.1:9/r.git', {
1611 fetchImpl: async () => new Response(errBody, { status: 500, headers: { 'content-type': 'text/plain' } }),
1612 limits: withLimits({ requestTimeoutMs: 2000 }), tracer: tracerStub,
1613 });
1614 await assert.rejects((async () => { for await (const frame of errHttp.frames('git-upload-pack', new Uint8Array(0), spanStub)) void frame; })(), e => e.code === 'upstream_5xx' && e.message.includes('xxx') && e.miss === undefined);
1615 assert.ok(errPulls <= 3, `error body kept reading (${errPulls} chunks)`);
1616 const page = `missing repo ${'y'.repeat(500)}`;
1617 const missingRepo = createTransport('http://127.0.0.1:9/missing.git', {
1618 fetchImpl: async () => new Response(page, { status: 404, headers: { 'content-type': 'text/plain' } }),
1619 tracer: tracerStub,
1620 });
1621 // the advertisement names the status and the miss; it does not quote the body
1622 await assert.rejects(unwrapAsync(missingRepo.advertise('git-upload-pack', spanStub)), e => e.code === 'not_found' && e.message === 'HTTP 404 git-upload-pack advertisement' && e.miss?.kind === 'repository');
1623 // the POST quotes a prefix of the page and stops: a dropped slice would keep the whole page
1624 await assert.rejects((async () => { for await (const frame of missingRepo.frames('git-upload-pack', new Uint8Array(0), spanStub)) void frame; })(), e => e.code === 'not_found' && e.miss?.kind === 'repository' && e.message.includes('yyyy') && !e.message.includes('y'.repeat(250)));
1625});
1627test('encodeTree duplicate names and the pre-send push size refusal (issue #11)', async () => {
1628 const blob = oid(1), dir = oid(2);
1629 throws(() => unwrap(encodeTree([{ mode: '100644', name: 'a', oid: blob }, { mode: '100644', name: 'a', oid: blob }])), 'bad_request', 'tree: duplicate entry "a"');
1630 throws(() => unwrap(encodeTree([{ mode: '100644', name: 'a', oid: blob }, { mode: '100644', name: 'a.b', oid: blob }, { mode: '40000', name: 'a', oid: dir }])), 'bad_request', 'tree: duplicate entry "a"');
1631 assert.equal(unwrap(encodeTree([{ mode: '100644', name: 'README', oid: blob }, { mode: '100644', name: 'readme', oid: blob }])).length, 2 * (OID_BYTES + 'README'.length + '100644 \0'.length), 'case-only twins are two names');
1632 let sent = 0;
1633 const t = { limits: withLimits({ maxPackBytes: 40 }), tracer: tracerStub, parent: null, async *frames() { sent++; for (const f of []) yield f; } };
1634 const objects = [{ type: TYPE.BLOB, data: te.encode('x'.repeat(64)) }];
1635 await assert.rejects(unwrapAsync(push(t, { ref: 'refs/heads/a', oldOid: ZERO_OID, newOid: oid(3), objects, sideband: false })), e => e.code === 'limit_exceeded' && e.what === 'push pack bytes' && e.max === 40 && e.got > 40);
1636 assert.equal(sent, 0, 'nothing reached the transport');
1637 const fits = { ...t, limits: withLimits({ maxPackBytes: 1 << 20 }) };
1638 await assert.rejects(unwrapAsync(push(fits, { ref: 'refs/heads/a', oldOid: ZERO_OID, newOid: oid(3), objects, sideband: false })), e => e.code === 'ambiguous');
1639 assert.equal(sent, 1, 'under the cap the body is sent');
1640});
1642test('git.push.object names every entry: a type id with no name is its decimal, not otel\'s null-as-empty', async () => {
1643 const ex = arrayExporter();
1644 const t = { limits: DEFAULT_LIMITS, tracer: new Tracer({ exporter: ex }), parent: null, async *frames() { for (const f of []) yield f; } };
1645 const objects = [{ type: 5, data: te.encode('x') }, { type: TYPE.BLOB, data: te.encode('y') }, { type: TYPE.BLOB, data: te.encode('z'), base: { tag: 'ref', oid: oid(4) } }];
1646 await assert.rejects(unwrapAsync(push(t, { ref: 'refs/heads/a', oldOid: ZERO_OID, newOid: oid(3), objects, sideband: false })), e => e.code === 'ambiguous');
1647 const span = ex.lines.flatMap(flattenLine).find(s => s.name === 'git.receive-pack');
1648 assert.deepEqual(span.events.filter(e => e.name === 'git.push.object').map(e => e.attrs['git.object.type']), ['5', 'blob', 'ref_delta']);
1649});
1651test('size varints stop at 64 bits: a header that never ends is pack_corrupt at its 11th byte, never a NaN size (#13)', () => {
1652 const ones = n => new Uint8Array(n).fill(0x80);
1653 // ten bytes is the longest legal object header (4 + 9 × 7 bits), and every size it can carry is finite
1654 assert.deepEqual(unwrap(readObjHeader(concat([Uint8Array.of(0xb0), ones(8), Uint8Array.of(0)]), 0)), { type: TYPE.BLOB, size: 0, next: 10 });
1655 assert.ok(Number.isFinite(unwrap(readObjHeader(concat([Uint8Array.of(0xbf), new Uint8Array(8).fill(0xff), Uint8Array.of(0x7f)]), 0)).size));
1656 throws(() => unwrap(readObjHeader(concat([Uint8Array.of(0xb0), ones(9), Uint8Array.of(0)]), 0)), 'pack_corrupt', 'pack: size varint longer than 64 bits');
1657 // the probe: 200 continuation bytes used to decode to NaN, which every `size > limit` check lets through
1658 throws(() => unwrap(readObjHeader(concat([Uint8Array.of(0xb0), ones(200), Uint8Array.of(0)]), 0)), 'pack_corrupt', 'pack: size varint longer than 64 bits');
1659 throws(() => unwrap(parsePack(concat([Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1, 0xb0), ones(200), new Uint8Array(40)]))), 'pack_corrupt', 'pack: size varint longer than 64 bits');
1661 // a delta size may take ten groups (bits 0..69); the tenth starts at bit 63, the eleventh is refused
1662 assert.deepEqual([...unwrap(applyDelta(new Uint8Array(0), concat([ones(9), Uint8Array.of(0, 1, 1, 0x41)]))).out], [0x41]);
1663 assert.throws(() => unwrap(applyDelta(new Uint8Array(0), concat([Uint8Array.of(0), new Uint8Array(9).fill(0xff), Uint8Array.of(1, 1, 0x41)]))), e => e.code === 'limit_exceeded' && /^delta result size \d+ exceeds limit/.test(e.message));
1664 throws(() => unwrap(applyDelta(new Uint8Array(0), concat([ones(10), Uint8Array.of(0, 1, 1, 0x41)]))), 'pack_corrupt', 'delta: size varint longer than 64 bits');
1665 // the probe: an over-long result size was NaN; NaN passed the limit, and the copy ended in an untyped RangeError
1666 const base = Uint8Array.of(9, 8, 7);
1667 throws(() => unwrap(applyDelta(base, concat([encodeVarint(base.length), ones(200), Uint8Array.of(0, DELTA_COPY | DELTA_COPY_OFF1 | DELTA_COPY_LEN1, 0, 3)]))), 'pack_corrupt', 'delta: size varint longer than 64 bits');
1669 // the streamed single-object parser: its header phase holds ≤ 21 bytes, for every caller (fetchSingle, edit, read, checkout)
1670 const packHead = Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1, 0xb0);
1671 let sp = spInit(), held = 0;
1672 for (const b of concat([packHead, ones(8)])) { sp = spStep(sp, Uint8Array.of(b)).state; held = Math.max(held, sp.head.length); }
1673 assert.equal(sp.phase, 'header');
1674 assert.equal(held, PACK_HEADER_BYTES + 9);
1675 throws(() => spStep(sp, Uint8Array.of(0x80)), 'pack_corrupt', 'pack: size varint longer than 64 bits');
1676 assert.equal(spStep(sp, Uint8Array.of(0)).state.phase, 'body');
1677 throws(() => spStep(spInit(), concat([packHead, ones(1 << 20)])), 'pack_corrupt', 'pack: size varint longer than 64 bits');
1678});
1682// The zlib stream of an object declared S bytes may not run past zlibBound(S) = S + ⌈S/8⌉ + ⌈S/64⌉ + 13 (format.js; the
1683// inequalities against zlib's deflateBound forms are in format-facts.test.js). Before this bound, a stream of non-final
1684// empty stored blocks (`00 00 00 ff ff`: 5 bytes in, 0 bytes out) was accepted by every path until the request timer:
1685// readFile took 793.6 MiB and editFile 632.7 MiB in 8 s, both `timeout`. Now the buffered inflate refuses it at the first
1686// block boundary past the bound (and a stream that ends past the bound, at its end), spStep refuses the streamed body on
1687// the chunk that crosses it, and the same empty stored blocks exactly at the bound still inflate.
1688test('zlib stream length is bounded by the declared size: inflate (both checks), spStep; exactly at the bound passes, one past fails', async () => {
1689 const { Sha1 } = await import('../src/sha1.js');
1690 const storedBlock = (data, final) => concat([Uint8Array.of(final ? 1 : 0, data.length & 0xff, data.length >> 8, ~data.length & 0xff, (~data.length >> 8) & 0xff), data]);
1691 const adlerBytes = n => Uint8Array.of(n >>> 24, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff);
1692 const emptyBlocks = k => Array.from({ length: k }, () => storedBlock(new Uint8Array(0), false));
1693 const streamOf = (data, k) => concat([Uint8Array.of(0x78, 0x01), ...emptyBlocks(k), storedBlock(data, true), adlerBytes(adler32(data))]);
1694 const ten = te.encode('0123456789');
1695 assert.equal(zlibBound(10), 26);
1696 const exact = streamOf(ten, 1), over = streamOf(ten, 2); // 2 + 5·k + 15 + 4: 26 at k = 1, 31 at k = 2
1697 assert.equal(exact.length, 26); assert.equal(over.length, 31);
1698 assert.deepEqual([...zlib.inflateSync(exact)], [...ten]); assert.deepEqual([...zlib.inflateSync(over)], [...ten], 'zlib itself accepts both: the bound is ours');
1700 // the empirical half of the bound: node's zlib (every level, strategy, memLevel 1/2/8/9 and windowBits 9/15 — the memLevel
1701 // 1 and 2 cases are the storelen/fixedlen regimes) never emits more than zlibBound(S) for S bytes, incompressible or not
1702 const rnd = n => { const b = new Uint8Array(n); let x = 0x9e3779b9 ^ n; for (let i = 0; i < n; i++) { x ^= x << 13; x ^= x >>> 17; x ^= x << 5; b[i] = x & 0xff; } return b; };
1703 const inputs = [...Array.from({ length: 70 }, (_, n) => n), 127, 128, 255, 256, 1000, 4096, 65535, 65536, 1 << 20].flatMap(n => [rnd(n), new Uint8Array(n).fill(0x61), te.encode('ab'.repeat(n >> 1))]);
1704 let deflates = 0;
1705 for (const d of inputs) for (const level of [-1, 0, 1, 6, 9]) for (const strategy of [0, 1, 2, 3, 4]) for (const memLevel of d.length > 5000 ? [8] : [1, 2, 8, 9]) for (const windowBits of [9, 15]) {
1706 const z = zlib.deflateSync(d, { level, strategy, memLevel, windowBits }); deflates++;
1707 assert.ok(z.length <= zlibBound(d.length), `zlib emitted ${z.length} B for ${d.length} B (bound ${zlibBound(d.length)}) at level ${level} strategy ${strategy} memLevel ${memLevel} windowBits ${windowBits}`);
1709 assert.ok(deflates > 10_000, `${deflates} deflates`);
1711 // inflate, end check: consumed > bound is pack_corrupt even though every block started inside the bound (at 2, 7, 12 ≤ 26).
1712 // `pos` is non-zero so the consumed count is measured from the stream's start, not the buffer's.
1713 const junk = new Uint8Array(7).fill(0xee);
1714 const ok = unwrap(inflate(concat([junk, exact]), junk.length, { expectedSize: 10 }));
1715 assert.equal(ok.consumed, 26); assert.deepEqual([...ok.out], [...ten]);
1716 throws(() => unwrap(inflate(concat([junk, over]), junk.length, { expectedSize: 10 })), 'pack_corrupt', 'inflate: zlib stream longer than its declared size allows (31 > 26 for 10 bytes)');
1717 // without an expected size the output cap stands in for the declared size: zlibBound(14) = 30 < 31 = zlibBound(15)
1718 throws(() => unwrap(inflate(over, 0, { maxOutput: 14 })), 'pack_corrupt', 'inflate: zlib stream longer than its declared size allows (31 > 30 for 14 bytes)');
1719 assert.equal(unwrap(inflate(over, 0, { maxOutput: 15 })).consumed, 31);
1720 // an expected size of 0 is a size, not an absent one: zlibBound(0) = 13 (zlib's own empty stream is 8 bytes; a stored one 11)
1721 const empty = new Uint8Array(0);
1722 assert.equal(unwrap(inflate(streamOf(empty, 0), 0, { expectedSize: 0 })).consumed, 11);
1723 throws(() => unwrap(inflate(streamOf(empty, 1), 0, { expectedSize: 0 })), 'pack_corrupt', 'inflate: zlib stream longer than its declared size allows (16 > 13 for 0 bytes)');
1725 // inflate, block-start check (what ends an endless stream): zlibBound(2) = 17 and block boundaries fall at 2, 7, 12, 17, 22.
1726 // A stream cut at 17 is still "unexpected end" (17 is not past the bound); one cut at 22 is refused as too long before the
1727 // reader asks for more input; 1 MiB of empty stored blocks is refused by the same check at the same byte, in microseconds.
1728 const two = te.encode('ab');
1729 assert.equal(zlibBound(2), 17);
1730 const blocks = k => concat([Uint8Array.of(0x78, 0x01), ...emptyBlocks(k)]);
1731 throws(() => unwrap(inflate(concat([junk, blocks(3)]), junk.length, { expectedSize: 2 })), 'pack_corrupt', 'inflate: unexpected end of input');
1732 throws(() => unwrap(inflate(concat([junk, blocks(4)]), junk.length, { expectedSize: 2 })), 'pack_corrupt', 'inflate: zlib stream longer than its declared size allows (22 > 17 for 2 bytes)');
1733 assert.equal(unwrap(inflate(streamOf(two, 0), 0, { expectedSize: 2 })).consumed, 13);
1734 throws(() => unwrap(inflate(streamOf(two, 1), 0, { expectedSize: 2 })), 'pack_corrupt', 'inflate: zlib stream longer than its declared size allows (18 > 17 for 2 bytes)');
1735 const mib = new Uint8Array(1 << 20); mib.set([0x78, 0x01]); for (let i = 2; i + 5 <= mib.length; i += 5) mib.set([0, 0, 0, 0xff, 0xff], i);
1736 const t0 = performance.now();
1737 throws(() => unwrap(inflate(mib, 0, { expectedSize: 2 })), 'pack_corrupt', 'inflate: zlib stream longer than its declared size allows (22 > 17 for 2 bytes)');
1738 assert.ok(performance.now() - t0 < 100, 'the endless stream was walked, not refused');
1739 // parsePack (the buffered path) takes the same refusal through inflate for the first object
1740 const hostile = concat([Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1), encodeObjHeader(TYPE.BLOB, 2), blocks(20), new Uint8Array(20)]);
1741 throws(() => unwrap(parsePack(hostile)), 'pack_corrupt', 'inflate: zlib stream longer than its declared size allows (22 > 17 for 2 bytes)');
1743 // spStep: the streamed body is refused on the byte that takes the zlib bytes (the body minus the 20-byte trailer lag) past
1744 // the bound. The message carries no byte count, so it is the same whatever the chunking (I10, props.test.js).
1745 const packOf = z => { const body = concat([Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1), encodeObjHeader(TYPE.BLOB, 10), z]); return concat([body, new Sha1().update(body).digest()]); };
1746 const atBound = spFinish(spStep(spInit(), packOf(exact)).state);
1747 assert.equal(atBound.zlibBytes, 26); assert.equal(atBound.size, 10);
1748 const longer = 'pack: zlib stream longer than its declared size allows (10-byte object, 26-byte bound)';
1749 throws(() => spStep(spInit(), packOf(over)), 'pack_corrupt', longer);
1750 assert.throws(() => spStep(spInit(), packOf(over)), e => e.got === 31 && e.max === 26 && e.size === 10, 'the fields carry the count the message leaves out');
1751 const hdrLen = PACK_HEADER_BYTES + encodeObjHeader(TYPE.BLOB, 10).length;
1752 let sp = spInit(), at = -1;
1753 const bytes = packOf(over);
1754 for (let i = 0; i < bytes.length && at < 0; i++) {
1755 try { sp = spStep(sp, bytes.subarray(i, i + 1)).state; } catch (e) { assert.equal(e.code, 'pack_corrupt'); assert.equal(e.message, longer); assert.deepEqual([e.got, e.max, e.size], [27, 26, 10]); at = i; }
1757 assert.equal(sp.zlibBytes, 26, 'the state before the refusing byte is exactly at the bound');
1758 assert.equal(at, hdrLen + 26 + 20, 'the 27th zlib byte is body byte 47 (20 of them are still the withheld trailer lag)');
1759});
1761// Every consumer of packStream (fetchSingle, fetchWide, fetchPackOnce, streamBlobEdit, openBlob, checkoutTree's streams)
1762// gets limits.maxPackBytes from v2Step, before the frame is yielded: band 1 as 'pack bytes', band 2 as 'progress bytes'.
1763// A consumer cannot forget the cap, and a missing limit is bad_request on the first frame.
1764const sidebandFrame = (n, len) => ({ kind: 'data', payload: concat([Uint8Array.of(n), new Uint8Array(len).fill(0x78)]) });
1765test('v2Step holds band 1 and band 2 to maxPackBytes, for every consumer of packStream', () => {
1766 const line = s => ({ kind: 'data', payload: te.encode(s) });
1767 const band = sidebandFrame;
1768 const limits = withLimits({ maxPackBytes: 10 });
1769 let st = v2Step(v2Init(limits), line('packfile')).state;
1770 const feed = f => { const r = v2Step(st, f); st = r.state; return r; };
1771 assert.equal(feed(band(SIDEBAND_PACK, 6)).pack.length, 6);
1772 assert.equal(feed(band(SIDEBAND_PACK, 4)).pack.length, 4);
1773 assert.equal(st.band1, 10);
1774 assert.equal(feed(band(SIDEBAND_PROGRESS, 10)).pack, null);
1775 assert.equal(st.band2, 10);
1776 assert.throws(() => feed(band(SIDEBAND_PACK, 1)), e => e.code === 'limit_exceeded' && e.message === 'pack bytes 11 exceeds limit 10' && e.what === 'pack bytes' && e.got === 11 && e.max === 10);
1777 assert.throws(() => feed(band(SIDEBAND_PROGRESS, 1)), e => e.code === 'limit_exceeded' && e.message === 'progress bytes 11 exceeds limit 10' && e.what === 'progress bytes' && e.got === 11 && e.max === 10);
1778 assert.equal(feed(band(SIDEBAND_PACK, 0)).pack.length, 0, 'an empty frame at the cap is not past it');
1779 throws(() => unwrap(parseLimits({ maxSectionLines: 1, maxPackBytes: -1 })), 'bad_request', 'limits.maxPackBytes must be a finite non-negative number');
1780});
1782// receive-pack's band 2 (hook stderr, the host's explanation) is held to maxPackBytes: a flood of it used to be read until
1783// the request timer, with the tail kept but the bytes unbounded (issue #36). Since the push outcome space (I36) every
1784// response frame, band 1, 2 or 3, counts toward one 'report bytes' cap, and a limit hit after the body is sent is
1785// `ambiguous` (original `limit_exceeded`): the ref may have moved. (Merge of #49 and #51: #49's separate 'progress bytes'
1786// counter became this shared one.)
1787test('push holds band 2 to maxPackBytes, counted with its report', async () => {
1788 const band = sidebandFrame;
1789 const del = [{ ref: 'refs/heads/a', oldOid: oid(1), newOid: ZERO_OID }];
1790 const report = { kind: 'data', payload: concat([Uint8Array.of(SIDEBAND_PACK), unwrap(pkt('unpack ok\n')), unwrap(pkt('ok refs/heads/a\n')), FLUSH]) };
1791 const fl = [band(SIDEBAND_PROGRESS, 30), band(SIDEBAND_PROGRESS, 20), report, { kind: 'flush' }];
1792 const total = fl.reduce((n, f) => n + (f.payload?.length ?? 0), 0);
1793 const pushT = (frames, max) => ({ limits: withLimits({ maxPackBytes: max }), tracer: tracerStub, parent: null, async *frames() { yield* frames; } });
1794 const fits = await unwrapAsync(push(pushT(fl, total), { commands: del, sideband: true }));
1795 assert.equal(fits.report.tag, "statuses"); assert.equal(fits.hookOutput.length, 50);
1796 await assert.rejects(unwrapAsync(push(pushT(fl, total - 1), { commands: del, sideband: true })),
1797 e => e.code === 'ambiguous' && e.original === 'limit_exceeded' && e.cause.what === 'report bytes' && e.cause.got === total && e.cause.max === total - 1);
1798 // an endless band 2 is cut at the cap, not read until the timer: reading stops on the frame that crosses it
1799 let pulled = 0;
1800 const flood = { limits: withLimits({ maxPackBytes: 10_000 }), tracer: tracerStub, parent: null, async *frames() { for (;;) { pulled++; yield band(SIDEBAND_PROGRESS, 999); } } };
1801 await assert.rejects(unwrapAsync(push(flood, { commands: del, sideband: true })), e => e.code === 'ambiguous' && e.original === 'limit_exceeded' && e.cause.what === 'report bytes' && e.cause.max === 10_000);
1802 assert.equal(pulled, 11, 'ten 1,000-byte frames fit, the eleventh crosses the cap');
1803 // The report's own caps bound what is kept, never the outcome (owner decision 5; test/push-report-bound.test.js reads
1804 // real response bodies): maxPackBytes is the one byte cap on the answer, so an endless band 2 or an endless run of
1805 // keepalives (one band byte each) ends there, by that name, under the default reason and progress limits.
1806 const endless = frame => ({ limits: withLimits({ maxPackBytes: 100_000 }), tracer: tracerStub, parent: null, async *frames() { for (;;) yield frame; } });
1807 const capped = (what, max) => e => e.code === 'ambiguous' && e.original === 'limit_exceeded' && e.cause.what === what && e.cause.max === max;
1808 await assert.rejects(unwrapAsync(push(endless(band(SIDEBAND_PROGRESS, 60_000)), { commands: del, sideband: true })), capped('report bytes', 100_000));
1809 // 100,000 keepalives fit, the next one is over
1810 await assert.rejects(unwrapAsync(push(endless(band(SIDEBAND_PACK, 0)), { commands: del, sideband: true })), e => capped('report bytes', 100_000)(e) && e.cause.got === 100_001);
1811 // an unpack status past maxReportReasonBytes (4,096) is cut and read as a line, as one under it is: neither is a git
1812 // unpack status, and the next copy of it is no ref status
1813 const unpackLine = n => ({ kind: 'data', payload: concat([Uint8Array.of(SIDEBAND_PACK), unwrap(pkt(`unpack ${'x'.repeat(n - 8)}\n`))]) });
1814 for (const n of [4137, 4138, 20_000]) await assert.rejects(unwrapAsync(push(endless(unpackLine(n)), { commands: del, sideband: true })), e => e.code === 'ambiguous' && e.original === undefined && e.message === 'push outcome unknown: a line is not a ref status');
1815});
1817test('push outcome (SAFETY.md I36): the exact report shapes, the decision, and the fields the enumerator does not compare', async () => {
1818 const two = [{ ref: 'refs/heads/a' }, { ref: 'refs/heads/b' }];
1819 const cls = (lines, commands = two) => classifyReport(lines.map(l => (typeof l === 'string' ? te.encode(l) : l)), commands);
1820 const why = (lines, commands) => cls(lines, commands).why;
1821 assert.equal(why([Uint8Array.of(0xff)]), 'a line is not one LF-terminated line');
1822 // a ref name's bytes >= 0x80 are legal to git and echoed as sent (#43): `\xff` is the ref the command spelled `\udcff`,
1823 // never one spelled with U+FFFD
1824 assert.equal(why([data('unpack ok\n'), data('ok refs/heads/', Uint8Array.of(0xff), '\n')], [{ ref: 'refs/heads/\ufffd' }]), 'status for a ref not commanded');
1825 assert.deepEqual(cls([data('unpack ok\n'), data('ok refs/heads/', Uint8Array.of(0xe9, 0xe8), '\n')], [{ ref: 'refs/heads/\udce9\udce8' }]), { tag: 'statuses', refs: [{ ref: 'refs/heads/\udce9\udce8', ok: true, reason: '' }] });
1826 assert.equal(why(['unpack ok\n', data('ng refs/heads/a caf', Uint8Array.of(0xe9), '\n'), 'ok refs/heads/b\n']), 'no readable reason for refs/heads/a');
1827 assert.equal(why(['unpack ok']), 'a line is not one LF-terminated line');
1828 assert.equal(why(['unpack ok\nx\n']), 'a line is not one LF-terminated line');
1829 for (const l of [[], ['unpack ok*\n'], ['ok refs/heads/a\n']]) assert.equal(why(l), 'no git unpack line first');
1830 assert.equal(why(['unpack ok\n', 'nf refs/heads/a hook declined\n', 'ok refs/heads/b\n']), 'a line is not a ref status');
1831 assert.equal(why(['unpack ok\n', 'ok refs/heads/zz\n', 'ok refs/heads/b\n']), 'status for a ref not commanded');
1832 assert.equal(why(['unpack ok\n', 'ok refs/heads/a\n', 'ok refs/heads/a\n']), 'two statuses for refs/heads/a');
1833 for (const r of ['', 'hook\x01declined', '\x01x', 'x\x01', 'caf\u00e9']) assert.equal(why(['unpack ok\n', `ng refs/heads/a ${r}\n`, 'ok refs/heads/b\n']), 'no readable reason for refs/heads/a', JSON.stringify(r));
1834 assert.equal(why(['unpack ok\n', 'ng refs/heads/a\n', 'ok refs/heads/b\n']), 'no readable reason for refs/heads/a');
1835 assert.equal(why(['unpack ok\n', 'ok refs/heads/a\n']), 'no status for refs/heads/b');
1836 assert.equal(why(['unpack index-pack abnormal exit\n', 'ok refs/heads/a\n', 'ng refs/heads/b unpacker error\n']), 'unpack failed, a ref is not "unpacker error"');
1837 assert.equal(why(['unpack ok\n', 'ok refs/heads/a\n', 'ng refs/heads/b unpacker error\n']), '"unpacker error" beside "unpack ok"');
1838 const unpackerRefs = [{ ref: 'refs/heads/a', ok: false, reason: 'unpacker error' }, { ref: 'refs/heads/b', ok: false, reason: 'unpacker error' }];
1839 for (const status of GIT_UNPACK_ERRORS) assert.deepEqual(cls([`unpack ${status}\n`, 'ng refs/heads/a unpacker error\n', 'ng refs/heads/b unpacker error\n']), { tag: 'unpackFailed', status, refs: unpackerRefs });
1840 assert.equal(GIT_UNPACK_ERRORS.length, 8);
1841 assert.deepEqual(cls(['unpack ok\n', 'ng refs/heads/b hook declined\n', 'ok refs/heads/a\n']), { tag: 'statuses', refs: [{ ref: 'refs/heads/b', ok: false, reason: 'hook declined' }, { ref: 'refs/heads/a', ok: true, reason: '' }] });
1843 const failed = { tag: 'unpackFailed', status: 'index-pack abnormal exit', refs: unpackerRefs };
1844 assert.deepEqual(decideOutcome(failed), { tag: 'rejected', code: 'push_rejected', refs: unpackerRefs.map(r => ({ ref: r.ref, reason: r.reason, cls: 'rejected' })) });
1845 assert.deepEqual(decideOutcome(failed, { pack: false }), { tag: 'ambiguous', why: 'unpack failed but no pack was sent' });
1846 assert.deepEqual(decideOutcome({ tag: 'malformed', why: 'w' }), { tag: 'ambiguous', why: 'w' });
1847 assert.deepEqual(decideOutcome({ tag: 'broken', why: 'b', remote: 'r' }), { tag: 'ambiguous', why: 'b' });
1848 // a shape outside the union is a programmer bug, not an outcome
1849 assert.throws(() => decideOutcome({ tag: 'weird' }), /unreachable/);
1850 // the refusal of an all-ng report, by the classes present (the old rejectionCode, now part of the outcome)
1851 const sts = (...rs) => ({ tag: 'statuses', refs: rs.map(([ref, ok, reason]) => ({ ref, ok, reason })) });
1852 const ngs = (...reasons) => sts(...reasons.map((r, i) => [`r${i}`, false, r]));
1853 const tagOf = shape => { const o = decideOutcome(shape); return o.tag === 'rejected' ? o.code : o.tag; };
1854 assert.equal(tagOf(ngs('failed to update ref')), 'cas_conflict');
1855 for (const r of ['xfailed to update ref', 'failed to update refs', 'a bad ref', 'bad refs']) assert.equal(tagOf(ngs(r)), 'push_rejected', r);
1856 assert.equal(tagOf({ tag: 'unpackFailed', status: 'unpack-objects abnormal exit', refs: [{ ref: 'r0', ok: false, reason: 'failed to update ref' }] }), 'push_rejected');
1857 assert.deepEqual(decideOutcome(ngs('stale ref')), { tag: 'rejected', code: 'cas_conflict', refs: [{ ref: 'r0', reason: 'stale ref', cls: 'cas' }] });
1858 // the class table (test/space/push-ng.space.js): an atomic CAS loss, git >= 2.51's batched texts, denyNonFastForwards,
1859 // a proc-receive hook that did not answer, and the precedence refusal > non-fast-forward > cas
1860 for (const r of ['atomic transaction failed', 'bad ref', 'incorrect old value provided', 'reference already exists', 'reference does not exist']) assert.equal(tagOf(ngs(r)), 'cas_conflict', r);
1861 for (const r of ['refname conflict', 'hook declined', 'funny refname', 'atomic push failure']) assert.equal(tagOf(ngs(r)), 'push_rejected', r);
1862 assert.equal(tagOf(ngs('non-fast-forward')), 'not_fast_forward');
1863 assert.equal(tagOf(ngs('non-fast-forward', 'atomic push failure')), 'not_fast_forward');
1864 assert.equal(tagOf(ngs('non-fast-forward', 'failed to update ref')), 'not_fast_forward');
1865 assert.equal(tagOf(ngs('non-fast-forward', 'hook declined')), 'push_rejected');
1866 assert.equal(tagOf(ngs('failed to update ref', 'atomic push failure')), 'cas_conflict');
1867 assert.equal(tagOf(ngs('fail to run proc-receive hook', 'hook declined')), 'ambiguous', 'a hook that may have acted outranks a refusal');
1868 assert.equal(tagOf(ngs('rejected: non-fast-forward')), 'cas_conflict', 'a host\'s wording keeps its heuristic');
1869 for (const [r, c] of [['proc-receive failed to report status', 'ambiguous'], ['fail to run proc-receive hook', 'ambiguous'], ['atomic push failure', 'innocent'], ['non-fast-forward', 'nff'], ['failed to lock', 'cas'], ['fetch first', 'cas'], ['x already exists', 'cas'], ['failed to lock x', 'rejected']]) assert.equal(ngClass(r), c, r);
1870 assert.deepEqual(decideOutcome(sts(['a', true, ''], ['b', false, 'fail to run proc-receive hook'])), { tag: 'ambiguous', why: 'proc-receive: fail to run proc-receive hook' });
1871 assert.deepEqual(decideOutcome(sts(['a', false, 'proc-receive failed to report status'])), { tag: 'ambiguous', why: 'proc-receive: proc-receive failed to report status' });
1872 assert.deepEqual(decideOutcome(sts(['a', false, 'non-fast-forward'])), { tag: 'rejected', code: 'not_fast_forward', refs: [{ ref: 'a', reason: 'non-fast-forward', cls: 'nff' }] });
1874 // the outcome as a Result: the error a caller matches on, per tag
1875 const ctx = { report: null, hookOutput: 'h', remote: undefined, commands: [{ ref: 'a', oldOid: oid(1), newOid: oid(2) }, { ref: 'b', oldOid: ZERO_OID, newOid: oid(2) }] };
1876 assert.deepEqual(outcomeResult({ tag: 'landed' }, ctx), { ok: true, value: null });
1877 const errOf = (o, c = ctx) => { const r = outcomeResult(o, c); assert.equal(r.ok, false); return r.error; };
1878 const ref = (name, reason, c) => ({ ref: name, reason, cls: c });
1879 let e = errOf({ tag: 'rejected', code: 'push_rejected', refs: [ref('a', 'hook declined', 'rejected')] });
1880 assert.deepEqual([e.code, e.message, e.hookOutput, e.report], ['push_rejected', 'push rejected: hook declined', 'h', null]);
1881 e = errOf({ tag: 'rejected', code: 'not_fast_forward', refs: [ref('a', 'non-fast-forward', 'nff')] });
1882 assert.deepEqual([e.code, e.message], ['not_fast_forward', 'push rejected: non-fast-forward']);
1883 // cas_conflict: expected is the culprit's own old oid (null for a create), actual unknown (receive-pack does not say)
1884 e = errOf({ tag: 'rejected', code: 'cas_conflict', refs: [ref('a', 'atomic push failure', 'innocent'), ref('b', 'reference already exists', 'cas')] });
1885 assert.deepEqual([e.code, e.message, e.expected, e.actual], ['cas_conflict', 'push rejected: atomic push failure', null, null]);
1886 e = errOf({ tag: 'rejected', code: 'cas_conflict', refs: [ref('a', 'stale ref', 'cas')] });
1887 assert.deepEqual([e.expected, e.actual], [oid(1), null]);
1888 e = errOf({ tag: 'partial', landed: ['a'], rejected: [ref('b', 'hook declined', 'rejected')] });
1889 assert.deepEqual([e.code, e.message, e.landed, e.rejected[0].ref], ['push_partial', 'push partially applied: a updated; b (hook declined) refused', ['a'], 'b']);
1890 e = errOf({ tag: 'ambiguous', why: 'w' });
1891 assert.deepEqual([e.code, e.message, e.original], ['ambiguous', 'push outcome unknown: w', undefined]);
1892 e = errOf({ tag: 'ambiguous', why: 'band 3' }, { ...ctx, remote: 'boom' });
1893 assert.deepEqual([e.code, e.message, e.original], ['ambiguous', 'push outcome unknown: remote: boom: band 3', 'remote_error']);
1894 assert.throws(() => outcomeResult({ tag: 'weird' }, ctx), /unreachable/);
1896 // the report state machine: absorbing malformed, one status per ref, the finish checks
1897 let rst = reportInit([{ ref: 'a' }]);
1898 assert.deepEqual(reportFinish(rst), { tag: 'malformed', why: 'no git unpack line first' });
1899 rst = reportStep(rst, te.encode('unpack ok\n'));
1900 assert.equal(rst.tag, 'refs');
1901 assert.deepEqual(reportFinish(rst), { tag: 'malformed', why: 'no status for a' });
1902 const bad = reportStep(rst, te.encode('xx\n'));
1903 assert.equal(reportStep(bad, te.encode('ok a\n')), bad);
1904 assert.throws(() => reportStep({ tag: 'weird' }, te.encode('x\n')), /unreachable/);
1905 assert.throws(() => reportFinish({ tag: 'weird' }), /unreachable/);
1907 // the checked capabilities: the request asks for atomic / push-options only from them
1908 const caps = (...c) => new Set(c);
1909 assert.deepEqual(requestCaps([{ ref: 'a', oldOid: oid(1), newOid: ZERO_OID }], {}), ['report-status', 'side-band-64k', 'delete-refs', 'agent=ply/0.2.0']);
1910 assert.deepEqual(requestCaps([{ ref: 'a', oldOid: oid(1), newOid: oid(2) }], { sideband: false, reportV2: true }, unwrap(checkPushCaps(caps('atomic', 'push-options'), { atomic: true, pushOptions: ['o'] }))), ['report-status-v2', 'atomic', 'push-options', 'agent=ply/0.2.0']);
1911 assert.deepEqual(requestCaps([{ ref: 'a', oldOid: oid(1), newOid: oid(2) }], {}, unwrap(checkPushCaps(caps(), {}))), ['report-status', 'side-band-64k', 'agent=ply/0.2.0']);
1912 assert.deepEqual(capsProblem(caps('atomic')), { tag: 'ok' });
1913 assert.deepEqual(capsProblem(caps('object-format=sha256', 'atomic'), { atomic: true }), { tag: 'objectFormat', format: 'sha256' });
1914 assert.deepEqual(capsProblem(caps('object-format=sha1'), { atomic: true }), { tag: 'noAtomic' });
1915 assert.deepEqual(capsProblem(caps('atomic'), { atomic: true, pushOptions: ['x'] }), { tag: 'noPushOptions' });
1916 assert.deepEqual(capsProblem(caps(), { pushOptions: [] }), { tag: 'ok' });
1917 const unsup = checkPushCaps(caps('agent=x'), { pushOptions: ['x'] });
1918 assert.deepEqual([unsup.ok, unsup.error.code, unsup.error.message, unsup.error.caps], [false, 'unsupported', 'server does not advertise push-options', ['agent=x']]);
1919 assert.equal(checkPushCaps(caps('object-format=sha256')).error.message, 'server repository is object-format sha256');
1920 assert.equal(checkPushCaps(caps(), { atomic: true }).error.message, 'server does not advertise atomic');
1921 assert.deepEqual(unwrap(checkPushCaps(caps('push-options'), { pushOptions: ['p'] })), { atomic: false, pushOptions: ['p'] });
1922 // pushWithOptions reads the advertisement only when the push asks for atomic or push options
1923 let adverts = 0;
1924 const advertised = advertCaps => ({ limits: DEFAULT_LIMITS, tracer: tracerStub, parent: null,
1925 async advertise() { adverts++; return { ok: true, value: concat([unwrap(pkt(`${oid(1)} refs/heads/a\0${advertCaps}\n`)), FLUSH]) }; },
1926 async *frames() { yield { kind: 'data', payload: te.encode('unpack ok\n') }; yield { kind: 'data', payload: te.encode('ok refs/heads/a\n') }; yield { kind: 'flush' }; } });
1927 const del1 = { commands: [{ ref: 'refs/heads/a', oldOid: oid(1), newOid: ZERO_OID }], sideband: false };
1928 assert.equal((await pushWithOptions(advertised('report-status'), del1)).report.tag, 'statuses'); assert.equal(adverts, 0);
1929 await assert.rejects(pushWithOptions(advertised('report-status'), { ...del1, atomic: true }), e2 => e2.code === 'unsupported' && e2.caps.join() === 'report-status'); assert.equal(adverts, 1);
1930 assert.equal((await pushWithOptions(advertised('report-status atomic'), { ...del1, atomic: true })).report.tag, 'statuses'); assert.equal(adverts, 2);
1931 assert.deepEqual(parseReceiveAdvert([{ kind: 'flush' }]), { ok: true, value: new Set() });
1932 // parseReceiveAdvert, line by line (connect.c get_remote_heads)
1933 const dat = l => ({ kind: 'data', payload: typeof l === 'string' ? te.encode(l) : l });
1934 const FL = { kind: 'flush' };
1935 const advErr = (frames, message) => { const r = parseReceiveAdvert(frames); assert.equal(r.ok, false, message); assert.deepEqual([r.error.code, r.error.message], ['protocol', message]); };
1936 const advOk = (frames, capsWant) => { const r = parseReceiveAdvert(frames); assert.equal(r.ok, true, JSON.stringify(r.error?.message)); assert.deepEqual([...r.value], capsWant); };
1937 advErr([dat('# service=git-receive-pack\n'), dat(`${oid(1)} refs/heads/a\n`)], 'advertisement: no flush after the service header');
1938 advOk([dat('# service=git-receive-pack\n'), FL, dat(`${oid(1)} refs/heads/a\0report-status atomic\n`), FL], ['report-status', 'atomic']);
1939 // a v2 answer names no push capabilities: refused by name (#232), never read as an empty set
1940 const V2_REFUSED = 'receive-pack advertisement: the server answered in protocol v2, which has no push capabilities';
1941 for (const frames of [[dat('version 2\n'), dat('agent=x\n'), FL], [dat('version 2'), dat('agent=x\n'), FL], [dat('version 2\n'), dat('agent=x\n')], [dat('# service=git-receive-pack\n'), FL, dat('version 2\n'), FL]]) {
1942 const r = parseReceiveAdvert(frames);
1943 assert.equal(r.ok, false);
1944 assert.deepEqual([r.error.code, r.error.message], ['unsupported', V2_REFUSED]);
1946 advErr([dat('\nversion 2'), FL], 'receive-pack advertisement: unexpected line "\\nversion 2"');
1947 advErr([dat(`${oid(1)} refs/heads/a\n`)], 'receive-pack advertisement: no flush after the refs');
1948 advErr([dat(`${oid(1)} refs/heads/a\n`), { kind: 'delim' }, FL], 'receive-pack advertisement: invalid packet (delim)');
1949 advErr([dat('bogus\n'), FL], 'receive-pack advertisement: unexpected line "bogus"');
1950 advErr([dat(`${'x'.repeat(70)}\n`), FL], `receive-pack advertisement: unexpected line "${'x'.repeat(60)}"`);
1951 // only the trailing LF is the line's; a caps value keeps an inner one
1952 advOk([dat(`${oid(1)} refs/heads/a\0agent=a\nb`), FL], ['agent=a\nb']);
1953 // the caps ride on the first line only, after its NUL; a shallow line may carry them
1954 advOk([dat(`shallow ${oid(1)}\0report-status\n`), FL], ['report-status']);
1955 advOk([dat(`${oid(1)} refs/heads/a\0report-status\n`), dat(`${oid(2)} refs/heads/b\0extra\n`), FL], ['report-status']);
1956 advOk([dat(`${oid(1)} refs/heads/a\n`), dat(`${oid(2)} refs/heads/b\0extra\n`), FL], []);
1957 advErr([dat('\0report-status\n'), FL], 'receive-pack advertisement: unexpected line ""');
1958 advOk([dat(`${oid(1)} refs/heads/a\0 report-status atomic \n`), FL], ['report-status', 'atomic']);
1959 // object-format=sha256 widens every oid to 64 hex
1960 advOk([dat(`${'ab'.repeat(32)} refs/heads/a\0object-format=sha256\n`), dat(`${'cd'.repeat(32)} refs/heads/b\n`), FL], ['object-format=sha256']);
1961 advErr([dat(`${'ab'.repeat(32)} refs/heads/a\0report-status\n`), FL], `receive-pack advertisement: unexpected line "${'ab'.repeat(30)}"`);
1963 // band 2 is kept as a 4096-character tail, counted per frame
1964 let st = streamInit(true);
1965 assert.equal(st.band2, '');
1966 for (const n of [3000, 2000]) st = unwrap(streamStep(st, { kind: 'data', payload: data(Uint8Array.of(2), 'x'.repeat(n - 1) + 'y') }));
1967 assert.equal(st.band2.length, 4096); assert.ok(st.band2.endsWith('xy')); assert.equal(st.band2Frames, 2);
1969 // through push(): the error a caller sees
1970 const sb = (...frames) => ({ limits: DEFAULT_LIMITS, tracer: tracerStub, parent: null, async *frames() { for (const f of frames) yield f === null ? { kind: 'flush' } : { kind: 'data', payload: f }; } });
1971 const band = (b, ...parts) => data(Uint8Array.of(b), ...parts);
1972 const report = (...lines) => band(1, ...lines.map(l => unwrap(pkt(l))), FLUSH);
1973 const del = { commands: [{ ref: 'refs/heads/a', oldOid: oid(1), newOid: ZERO_OID }], sideband: true };
1974 await assert.rejects(unwrapAsync(push(sb(band(2, 'hook says hi\n'), band(3, ' boom \n'), null), del)), e => e.code === 'ambiguous' && e.message === 'push outcome unknown: remote: boom: band 3' && e.original === 'remote_error' && e.hookOutput === 'hook says hi\n');
1975 // F34 / 2026-11-08: Artifacts under a same-ref race answers band 3 with an empty report. A single-ref update reads
1976 // the tip: not at newOid → cas_conflict (typed), never ambiguous → 502.
1978 const oldOid = oid(1), newOid = oid(3);
1979 const update = { commands: [{ ref: 'refs/heads/a', oldOid, newOid }], sideband: true };
1980 const band3Race = {
1981 limits: DEFAULT_LIMITS, tracer: tracerStub, parent: null,
1982 async *frames(service) {
1983 if (service === 'git-upload-pack') {
1984 yield { kind: 'data', payload: te.encode(`${oldOid} refs/heads/a\n`) };
1985 yield { kind: 'flush' };
1986 return;
1988 yield { kind: 'data', payload: band(3, '') };
1989 yield { kind: 'flush' };
1990 },
1991 };
1992 await assert.rejects(unwrapAsync(push(band3Race, update)), e => e.code === 'cas_conflict' && e.original === 'ambiguous' && e.expected === oldOid && e.actual === oldOid);
1993 // tip already at newOid → the push landed; report was lost
1994 const band3Landed = {
1995 limits: DEFAULT_LIMITS, tracer: tracerStub, parent: null,
1996 async *frames(service) {
1997 if (service === 'git-upload-pack') {
1998 yield { kind: 'data', payload: te.encode(`${newOid} refs/heads/a\n`) };
1999 yield { kind: 'flush' };
2000 return;
2002 yield { kind: 'data', payload: band(3, '') };
2003 yield { kind: 'flush' };
2004 },
2005 };
2006 const landed = await unwrapAsync(push(band3Landed, update));
2007 assert.equal(landed.report.tag, 'statuses');
2008 assert.equal(landed.report.refs[0].ok, true);
2011 await assert.rejects(unwrapAsync(push(sb(report('unpack ok*\n', 'ok refs/heads/a\n'), null), del)), e => e.code === 'ambiguous' && e.message === 'push outcome unknown: no git unpack line first' && e.original === undefined && e.report === null);
2012 await assert.rejects(unwrapAsync(push(sb(report('unpack ok\n', 'ok refs/heads/a\n'), null, null), del)), e => e.code === 'ambiguous' && e.message === 'push outcome unknown: data after the final flush');
2013 await assert.rejects(unwrapAsync(push(sb(report('unpack ok\n', 'ng refs/heads/a hook declined\n'), null), del)), e => e.code === 'push_rejected' && e.message === 'push rejected: hook declined');
2014 // #144: the cap admits what git sends for the commands pushed (unpack + one status each), whatever maxReportLines says;
2015 // a line past that is the cap's
2016 const exact = { ...sb(report('unpack ok\n', 'ok refs/heads/a\n'), null), limits: withLimits({ maxReportLines: 1 }) };
2017 assert.equal((await unwrapAsync(push(exact, del))).report.refs.length, 1);
2018 const many = { ...sb(report('unpack ok\n', 'ok refs/heads/a\n', 'ok refs/heads/b\n'), null), limits: withLimits({ maxReportLines: 1 }) };
2019 await assert.rejects(unwrapAsync(push(many, del)), e => e.code === 'ambiguous' && e.original === 'limit_exceeded' && e.cause.what === 'report lines' && e.cause.got === 3 && e.cause.max === 2);
2020 const over = { ...sb(report('unpack ok\n', 'ok refs/heads/a\n', 'ok refs/heads/b\n', 'ok refs/heads/c\n'), null), limits: withLimits({ maxReportLines: 3 }) };
2021 await assert.rejects(unwrapAsync(push(over, del)), e => e.code === 'ambiguous' && e.cause.what === 'report lines' && e.cause.got === 4 && e.cause.max === 3);
2022 assert.deepEqual([1, 2, 5].map(n => reportLineCap({ maxReportLines: 1, commands: Array(n).fill(del.commands[0]), caps: ['report-status'] })), [2, 3, 6]);
2023 assert.deepEqual([1, 2].map(n => reportLineCap({ maxReportLines: 1, commands: Array(n).fill(del.commands[0]), caps: ['report-status-v2'] })), [6, 11]);
2024 assert.equal(reportLineCap({ maxReportLines: 100, commands: del.commands, caps: ['report-status-v2'] }), 100);
2025 // #144: an HTTP refusal at the status line (no frame read) that the service's advertisement repeats is that error:
2026 // receive-pack is not there for this caller. An advertisement that answers, refuses otherwise, or cannot be asked,
2027 // a refusal after a frame, and every other code stay ambiguous (a middlebox may have forwarded the body).
2028 const refusing = (code, advertised, ...before) => ({ ...sb(), advertise: async () => advertised, async *frames() { for (const f of before) yield { kind: 'data', payload: f }; throw Object.assign(new Error(`HTTP ${code}`), { code }); } });
2029 const adErr = code => ({ ok: false, error: Object.assign(new Error(`ad ${code}`), { code }) });
2030 for (const code of ['auth', 'not_found']) {
2031 await assert.rejects(unwrapAsync(push(refusing(code, adErr(code)), del)), e => e.code === code && e.message === `HTTP ${code}`);
2032 await assert.rejects(unwrapAsync(push(refusing(code, adErr(code), band(2, 'hi\n')), del)), e => e.code === 'ambiguous' && e.original === code);
2033 for (const ad of [{ ok: true, value: new Uint8Array(0) }, adErr(code === 'auth' ? 'not_found' : 'auth'), adErr('network')]) await assert.rejects(unwrapAsync(push(refusing(code, ad), del)), e => e.code === 'ambiguous' && e.original === code);
2034 const noAd = { ...refusing(code, null), advertise: undefined };
2035 await assert.rejects(unwrapAsync(push(noAd, del)), e => e.code === 'ambiguous' && e.original === code);
2037 for (const code of ['upstream_5xx', 'http', 'rate_limited', 'network', 'timeout']) await assert.rejects(unwrapAsync(push(refusing(code, adErr(code)), del)), e => e.code === 'ambiguous' && e.original === code);
2038 assert.deepEqual([refusedBeforeReceivePack({ cause: null, answered: false }), refusedBeforeReceivePack({ cause: { code: 'auth' }, answered: true }), refusedBeforeReceivePack({ cause: { code: 'auth' }, answered: false })], [false, false, true]);
2039 const big = { ...sb(report('unpack ok\n', 'ok refs/heads/a\n'), null), limits: withLimits({ maxPackBytes: 20 }) };
2040 await assert.rejects(unwrapAsync(push(big, del)), e => e.code === 'ambiguous' && e.original === 'limit_exceeded' && e.cause.what === 'report bytes');
2041 const fits = { ...sb(report('unpack ok\n', 'ok refs/heads/a\n'), null), limits: withLimits({ maxPackBytes: 39 }) }; // the report frame's payload is exactly 39 bytes
2042 const ok = await unwrapAsync(push(fits, del));
2043 assert.deepEqual(ok.report, { tag: 'statuses', refs: [{ ref: 'refs/heads/a', ok: true, reason: '' }] }); assert.equal(ok.hookOutput, '');
2044});
2046// A commit-only v2 server over a hand graph ({ name: [parent names] }), answering `want` + `deepen N` + `filter tree:0`
2047// as upload-pack does: the commits within N of the want (the want is depth 1), those at depth N as `shallow` lines; a
2048// want of the empty tree answers it; ls-refs from `t.refs`; receive-pack answers every command `ok` and moves the ref
2049// (the pack is not inspected) — enough for mergeBase and rebaseBranch. Ported from research/18 (7b21380).
2050function graphServer(edges) {
2051 const author = unwrap(ident(unwrap(validateIdent({ name: 'g', email: 'g@x', time: 1700000000 }))));
2052 const tree = hashObject('tree', new Uint8Array(0));
2053 const oids = new Map(), datas = new Map();
2054 const build = name => { if (oids.has(name)) return oids.get(name); const d = unwrap(encodeCommit({ tree, parents: edges[name].map(build), author, committer: author, message: `${name}\n` })); const o = hashObject('commit', d); oids.set(name, o); datas.set(o, d); return o; };
2055 for (const name of Object.keys(edges)) build(name);
2056 const parentsOf = new Map([...oids].map(([name, o]) => [o, edges[name].map(n => oids.get(n))]));
2057 const requests = [];
2058 const line = str => ({ kind: 'data', payload: te.encode(str) });
2059 const sideband = b => ({ kind: 'data', payload: concat([Uint8Array.of(SIDEBAND_PACK), b]) });
2060 const t = { limits: DEFAULT_LIMITS, tracer: new Tracer(), parent: null, objects: new Map(), quirks: {}, requests, refs: new Map(), url: 'stub://graph',
2061 async *frames(service, body) {
2062 // the pkt-lines up to the first flush (a receive-pack body carries the raw pack after it)
2063 const lines = []; for (let p = 0; p + 4 <= body.length;) { const n = parseInt(td.decode(body.subarray(p, p + 4)), 16); if (n === 0) break; if (n > 1) lines.push(td.decode(body.subarray(p + 4, p + n)).replace(/\n$/, '')); p += Math.max(n, 4); }
2064 if (service === 'git-receive-pack') { yield sideband(unwrap(pkt('unpack ok\n'))); for (const l of lines) { const m = /^[0-9a-f]{40} ([0-9a-f]{40}) ([^\0 ]+)/.exec(l); if (m) { t.refs.set(m[2], m[1]); yield sideband(unwrap(pkt(`ok ${m[2]}\n`))); } } yield sideband(FLUSH); yield { kind: 'flush' }; return; } // git's shape: the report's flush inside band 1, then the outer flush (I36)
2065 if (lines[0] === 'command=ls-refs') { const prefixes = lines.filter(l => l.startsWith('ref-prefix ')).map(l => l.slice(11)); for (const [ref, o] of t.refs) if (prefixes.some(p => ref.startsWith(p))) yield line(`${o} ${ref}\n`); yield { kind: 'flush' }; return; }
2066 const want = lines.find(l => l.startsWith('want '))?.slice(5), deepen = +(lines.find(l => l.startsWith('deepen '))?.slice(7) ?? 0);
2067 const filter = lines.find(l => l.startsWith('filter '))?.slice(7) ?? null;
2068 requests.push({ want, deepen, filter });
2069 if (want === tree) { const { pack } = await buildPack([{ type: TYPE.TREE, data: new Uint8Array(0) }]); yield line('packfile\n'); yield sideband(pack); yield { kind: 'flush' }; return; }
2070 const depth = new Map([[want, 1]]); const q = [want];
2071 for (let i = 0; i < q.length; i++) { const c = q[i]; if (depth.get(c) >= deepen) continue; for (const p of parentsOf.get(c) ?? []) if (!depth.has(p)) { depth.set(p, depth.get(c) + 1); q.push(p); } }
2072 const window = [...depth.keys()], shallow = window.filter(c => depth.get(c) === deepen && parentsOf.get(c).length);
2073 const { pack } = await buildPack(window.map(c => ({ type: TYPE.COMMIT, data: datas.get(c) })));
2074 if (shallow.length) { yield line('shallow-info\n'); for (const c of shallow) yield line(`shallow ${c}\n`); yield { kind: 'delim' }; }
2075 yield line('packfile\n');
2076 for (let i = 0; i < pack.length; i += 65000) yield sideband(pack.subarray(i, Math.min(i + 65000, pack.length)));
2077 yield { kind: 'flush' };
2078 } };
2079 return { t, oid: name => oids.get(name) };
2081const chain = (name, n, below) => Object.fromEntries(Array.from({ length: n }, (_, i) => [`${name}${i + 1}`, [i + 1 < n ? `${name}${i + 2}` : below]]));
2083test('a missing ref is not_found with a ref miss, for attributes and for rebase', async () => {
2084 const s = graphServer({ root: [] });
2085 const gone = { kind: 'ref', ref: 'refs/heads/gone', candidates: ['refs/heads/gone'] };
2086 s.t.refs.set('refs/heads/main', s.oid('root'));
2087 s.t.refs.set('refs/heads/feat', s.oid('root'));
2088 assert.match(await unwrapAsync(attributeTree(s.t, 'refs/heads/main', {})), /^[0-9a-f]{40}$/);
2089 const same = await unwrapAsync(mergeBranch('stub://graph', { ref: 'refs/heads/main', from: 'refs/heads/feat', author: { name: 'm', email: 'm@x' }, time: 1700000000 }, { transport: s.t, tracer: s.t.tracer }));
2090 assert.equal(same.outcome, 'up_to_date');
2091 await assert.rejects(unwrapAsync(mergeBranch('stub://graph', { ref: 'refs/heads/main', from: 'refs/heads/gone', author: { name: 'm', email: 'm@x' }, time: 1700000000 }, { transport: s.t, tracer: s.t.tracer })), e => e.code === 'not_found' && e.message === 'ref refs/heads/gone not found' && e.miss?.kind === 'ref' && e.miss.ref === 'refs/heads/gone');
2092 s.t.refs.delete('refs/heads/main');
2093 s.t.refs.delete('refs/heads/feat');
2094 await assert.rejects(unwrapAsync(attributeTree(s.t, 'refs/heads/gone', {})), e => e.code === 'not_found' && e.message === 'attributes ref refs/heads/gone not found' && e.miss.kind === 'ref' && e.miss.ref === gone.ref && e.miss.candidates.length === 1 && e.miss.candidates[0] === gone.ref);
2095 await assert.rejects(unwrapAsync(mergeBranch('stub://graph', { ref: 'refs/heads/gone', from: 'refs/heads/main', author: { name: 'm', email: 'm@x' }, time: 1700000000 }, { transport: s.t, tracer: s.t.tracer })), e => e.code === 'not_found' && e.message === 'ref refs/heads/gone not found' && e.miss?.kind === 'ref' && e.miss.ref === 'refs/heads/gone' && e.miss.candidates[0] === 'refs/heads/gone');
2096 await assert.rejects(unwrapAsync(rebaseBranch('stub://graph', { ref: 'refs/heads/gone', onto: 'refs/heads/main', committer: { name: 'r', email: 'r@x' }, time: 1700000000 }, { transport: s.t, tracer: s.t.tracer })), e => e.code === 'not_found' && e.message === 'ref refs/heads/gone not found' && e.miss.kind === 'ref' && e.miss.ref === 'refs/heads/gone');
2097 s.t.refs.set('refs/heads/feat', s.oid('root'));
2098 await assert.rejects(unwrapAsync(rebaseBranch('stub://graph', { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer: { name: 'r', email: 'r@x' }, time: 1700000000 }, { transport: s.t, tracer: s.t.tracer })), e => e.code === 'not_found' && e.message === 'ref refs/heads/main not found' && e.miss.ref === 'refs/heads/main' && e.miss.candidates[0] === 'refs/heads/main');
2099});
2101test('#41/#42: the merge-base window\'s cut — rebase fetches a base it was only named, and a cut below two candidates deepens', async () => {
2102 // #41: the base exactly at the window's cut (8 a side) is named by both sides' children, never fetched by mergeBase; it
2103 // is fetched once (deepen 1) before the first replay. With the base inside the window (7) it is not fetched again.
2104 for (const n of [8, 7]) {
2105 const s = graphServer({ root: [], ...chain('f', n, 'root'), ...chain('m', n, 'root') });
2106 s.t.refs.set('refs/heads/feat', s.oid('f1')); s.t.refs.set('refs/heads/main', s.oid('m1'));
2107 const res = await unwrapAsync(rebaseBranch('stub://graph', { ref: 'refs/heads/feat', onto: 'refs/heads/main', committer: { name: 'r', email: 'r@x' }, time: 1700000000 }, { transport: s.t, tracer: s.t.tracer }));
2108 assert.deepEqual([res.outcome, res.commits, res.base, res.onto], ['rebase', n, s.oid('root'), s.oid('m1')]);
2109 assert.equal(s.t.refs.get('refs/heads/feat'), res.oid, 'the branch was force-updated to the new tip');
2110 assert.equal(s.t.requests.filter(q => q.want === s.oid('root') && q.deepen === 1).length, n === 8 ? 1 : 0, 'base fetched once at the cut (n=8), not when the window held it (n=7)');
2112 // #42: a,b → [c, z]; c → p1..p6 → x → z. Depth 8 cuts x: {c, z} looks criss-cross; one more window shows z below c.
2113 { const s = graphServer({ a: ['c', 'z'], b: ['c', 'z'], ...chain('p', 6, 'x'), c: ['p1'], x: ['z'], z: [] });
2114 const res = await unwrapAsync(mergeBase(s.t, s.oid('a'), s.oid('b')));
2115 assert.deepEqual([res.base, res.bases, res.depth], [s.oid('c'), [s.oid('c')], 16]); }
2116 // a true criss-cross with nothing cut below it is final at the first window (two bases, not a deeper search)
2117 { const s = graphServer({ a: ['x', 'y'], b: ['y', 'x'], x: ['r'], y: ['r'], r: [] });
2118 const res = await unwrapAsync(mergeBase(s.t, s.oid('a'), s.oid('b')));
2119 assert.deepEqual([res.base, res.bases, res.depth], [null, [s.oid('x'), s.oid('y')].sort(), 8]); }
2120 // ...nor over deep shared history: a cut both bases reach cannot join them (the research rule deepened to the cap here:
2121 // limit_exceeded where git answers two bases)
2122 { const s = graphServer({ a: ['x', 'y'], b: ['y', 'x'], x: ['h1'], y: ['h1'], ...chain('h', 40, 'r'), r: [] });
2123 const res = await unwrapAsync(mergeBase(s.t, s.oid('a'), s.oid('b'), { maxDepth: 16 }));
2124 assert.deepEqual([res.base, res.bases, res.depth], [null, [s.oid('x'), s.oid('y')].sort(), 8]); }
2125 // a single base over a settled cut stays final at the first window
2126 { const s = graphServer({ a: ['c'], b: ['c'], ...chain('p', 9, 'r'), c: ['p1'], r: [] });
2127 const res = await unwrapAsync(mergeBase(s.t, s.oid('a'), s.oid('b')));
2128 assert.deepEqual([res.base, res.depth], [s.oid('c'), 8]); }
2129 const G = o => new Map(Object.entries(o).map(([k, parents]) => [k, { parents }]));
2130 assert.deepEqual(bestBases(G({ a: ['c', 'z'], b: ['c', 'z'], c: ['x'], z: [] }), 'a', 'b'), { best: ['c', 'z'], truncated: true });
2131 assert.deepEqual(bestBases(G({ a: ['c', 'z'], b: ['c', 'z'], c: ['x'], z: ['x'] }), 'a', 'b'), { best: ['c', 'z'], truncated: false }, 'a cut both bases reach');
2132 assert.deepEqual(bestBases(G({ a: ['c', 'z'], b: ['c', 'z'], c: ['x'], z: ['w'] }), 'a', 'b'), { best: ['c', 'z'], truncated: true }, 'each base reaches a cut the other does not');
2133});
2135test('parseOid is 40 lowercase hex; parseCommitId also refuses the zero oid', () => {
2136 const code = n => String.fromCharCode(n);
2137 const place = (i, ch) => `${'b'.repeat(i)}${ch}${'b'.repeat(OID_HEX_CHARS - 1 - i)}`;
2138 const refused = (fn, value) => {
2139 const r = fn(value);
2140 assert.equal(r.ok, false);
2141 if (r.ok) return null;
2142 assert.equal(r.error.code, 'bad_request');
2143 return r.error;
2144 };
2145 const accepted = value => {
2146 const r = parseOid(value);
2147 assert.equal(r.ok, true, typeof value === 'string' ? JSON.stringify(value) : typeof value);
2148 if (!r.ok) return '';
2149 assert.equal(r.value, value);
2150 assert.equal(typeof r.value, 'string');
2151 return r.value;
2152 };
2153 assert.equal(accepted(code(HEX_DIGIT_MIN).repeat(OID_HEX_CHARS)), ZERO_OID);
2154 assert.equal(ZERO_OID, '0'.repeat(OID_HEX_CHARS));
2155 accepted(code(HEX_DIGIT_MAX).repeat(OID_HEX_CHARS));
2156 accepted(code(HEX_LOWER_MIN).repeat(OID_HEX_CHARS));
2157 accepted(code(HEX_LOWER_MAX).repeat(OID_HEX_CHARS));
2158 accepted(place(0, code(HEX_DIGIT_MIN)));
2159 accepted(place(OID_HEX_CHARS - 1, code(HEX_LOWER_MAX)));
2160 for (const ch of [code(HEX_DIGIT_MIN - 1), code(HEX_DIGIT_MAX + 1), code(HEX_LOWER_MIN - 1), code(HEX_LOWER_MAX + 1), 'A', 'F']) {
2161 for (const i of [0, 7, OID_HEX_CHARS - 1]) {
2162 const e = refused(parseOid, place(i, ch));
2163 assert.equal(e.message, 'oid must be lowercase hex');
2164 assert.equal(e.what, 'oid');
2165 assert.equal(e.got, place(i, ch));
2168 for (const value of ['', 'b'.repeat(OID_HEX_CHARS - 1), 'b'.repeat(OID_HEX_CHARS + 1)]) {
2169 const e = refused(parseOid, value);
2170 assert.equal(e.message, `oid must be ${OID_HEX_CHARS} lowercase hex characters`);
2171 assert.equal(e.what, 'oid');
2172 assert.equal(e.got, value);
2174 for (const value of [1, null, undefined, true, {}, [], Object('b'.repeat(OID_HEX_CHARS))]) {
2175 const e = refused(parseOid, value);
2176 assert.equal(e.message, 'oid must be a string');
2177 assert.equal(e.what, 'oid');
2178 assert.equal(e.got, value);
2180 const short = refused(parseCommitId, 'b'.repeat(OID_HEX_CHARS - 1));
2181 assert.equal(short.message, `oid must be ${OID_HEX_CHARS} lowercase hex characters`);
2182 assert.equal(short.what, 'oid');
2183 assert.equal(short.got, 'b'.repeat(OID_HEX_CHARS - 1));
2184 const zero = refused(parseCommitId, ZERO_OID);
2185 assert.equal(zero.message, 'the zero oid is not a commit');
2186 assert.equal(zero.what, 'commit');
2187 assert.equal(zero.got, ZERO_OID);
2188 const leading = `${code(HEX_DIGIT_MIN)}${'c'.repeat(OID_HEX_CHARS - 1)}`;
2189 const kept = parseCommitId(leading);
2190 assert.equal(kept.ok, true);
2191 if (kept.ok) assert.equal(kept.value, leading);
2192});
2194test('recordResponseHeaders: a non-list, more than 8 names, or a name outside the telemetry alphabet is bad_request before any request', () => {
2195 const refused = opts => { try { createTransport('http://127.0.0.1/r.git', opts); return null; } catch (e) { return e; } };
2196 const names = n => Array.from({ length: n }, (_, i) => `h${i}`);
2197 // 0 (omitted and empty), 1, limit-1, limit are accepted; limit+1 and far past are the same refusal.
2198 for (const n of [undefined, 0, 1, 7, 8]) assert.equal(refused({ recordResponseHeaders: n === undefined ? undefined : names(n) }), null, `count ${n}`);
2199 for (const n of [9, 10, 64]) {
2200 const e = refused({ recordResponseHeaders: names(n) });
2201 assert.equal(e.code, 'bad_request', `count ${n}`);
2202 assert.equal(e.what, 'recordResponseHeaders');
2203 assert.equal(e.message, 'recordResponseHeaders: at most 8 names');
2205 const list = refused({ recordResponseHeaders: 'cf-ray' });
2206 assert.equal(list.code, 'bad_request');
2207 assert.equal(list.what, 'recordResponseHeaders');
2208 assert.equal(list.message, 'recordResponseHeaders: expected a list of header names');
2209 for (const name of ['', 'cf ray', 'cf-ray\n', 'cf/ray', 'x-foo!', 'Cf-Ray ']) {
2210 const e = refused({ recordResponseHeaders: [name] });
2211 assert.equal(e && e.code, 'bad_request', JSON.stringify(name));
2212 assert.equal(e && e.message, `recordResponseHeaders: ${JSON.stringify(name)} is not a header name`);
2214 const num = refused({ recordResponseHeaders: [1] });
2215 assert.equal(num.message, 'recordResponseHeaders: not a string is not a header name');
2216 let called = 0;
2217 const bad = refused({ recordResponseHeaders: ['ok name'], fetchImpl: () => { called += 1; return Promise.reject(new Error('sent')); } });
2218 assert.equal(bad.code, 'bad_request');
2219 assert.equal(called, 0);
2220});
2222test('validatePushRef: receive-pack\'s funny refname (the cells test/space/refname.test.js observes from git)', () => {
2223 const ok = (ref, del) => { try { return unwrap(validatePushRef(ref, del)) === ref; } catch (e) { assert.equal(e.code, 'bad_request'); assert.equal(e.what, 'ref'); return false; } };
2224 // [ref, create accepted, delete accepted]: git v2.50.0 receive-pack's answers (the space's cells; a..b and a name outside refs/ by receive-pack.c:1477)
2225 for (const [ref, create, del] of [['refs/a', false, true], ['refs/@', false, false], ['refs/@a', false, true], ['refs/-a', false, true],
2226 ['refs/a/b', true, true], ['refs/@/a', true, true], ['refs/a/@', true, true], ['refs/heads/x', true, true], ['refs/a/b/c', true, true], ['refs/a..b/c', false, false], ['heads/a/b', false, false]]) {
2227 assert.equal(ok(ref, false), create, `${ref} create`);
2228 assert.equal(ok(ref, true), del, `${ref} delete`);
2230});
2232// T3 noticed these and left them: a null throw in verifyRef became a TypeError, redirect cancel() was not awaited,
2233// a 2xx with no body was `network`, and spStep matched the short-read message instead of reason (#76, emitted by #99).
2234test('T3 transport: null read-back is ambiguous, cancel is awaited, a 2xx with no body is protocol, short reads switch on reason', async () => {
2235 const root = { set() {} };
2236 const ref = 'refs/heads/main';
2237 const expected = 'a'.repeat(40);
2238 // verifyRef returns its refusal as a value (the I25 rule: any read-back failure is ambiguous)
2239 const readBack = async (thrown) => errOf(await verifyRef({ get limits() { throw thrown; } }, root, 'git.edit', ref, expected));
2240 const eNull = await readBack(null);
2241 assert.equal(eNull.code, 'ambiguous');
2242 assert.equal(eNull.message, 'push reported ok but the verification read-back failed: null');
2243 assert.equal(eNull.cause, null);
2244 assert.equal(eNull.original, undefined);
2245 const eUndef = await readBack(undefined);
2246 assert.equal(eUndef.code, 'ambiguous');
2247 assert.equal(eUndef.message, 'push reported ok but the verification read-back failed: undefined');
2248 const boom = Object.assign(new Error('boom'), { code: 'network' });
2249 const eBoom = await readBack(boom);
2250 assert.equal(eBoom.code, 'ambiguous');
2251 assert.equal(eBoom.message, 'push reported ok but the verification read-back failed: boom');
2252 assert.equal(eBoom.original, 'network');
2253 assert.equal(eBoom.cause, boom);
2254 const eStr = await readBack('nope');
2255 assert.equal(eStr.code, 'ambiguous');
2256 assert.equal(eStr.message, 'push reported ok but the verification read-back failed: nope');
2257 // an empty message is still the message: `??` keeps it, `||` would stringify the Error ("Error")
2258 const eEmpty = await readBack(Object.assign(new Error(''), { code: 'network' }));
2259 assert.equal(eEmpty.message, 'push reported ok but the verification read-back failed: ');
2260 assert.equal(eEmpty.original, 'network');
2262 const unhandled = [];
2263 const onUnhandled = (err) => { unhandled.push(err); };
2264 process.on('unhandledRejection', onUnhandled);
2265 try {
2266 const held = () => {
2267 let release;
2268 const gate = new Promise(r => { release = r; });
2269 let started;
2270 const startedP = new Promise(r => { started = r; });
2271 const body = { cancel: () => { started(); return gate.then(() => { throw new Error('cancel rejected'); }); } };
2272 return { body, startedP, release: () => release() };
2273 };
2274 const postHold = held();
2275 const http = createTransport('http://127.0.0.1:9/r.git', {
2276 fetchImpl: async () => ({
2277 ok: false, status: 302, type: 'basic',
2278 headers: { get: n => (n === 'location' ? 'http://127.0.0.1:9/moved.git/git-upload-pack' : null) },
2279 body: postHold.body,
2280 }),
2281 tracer: tracerStub,
2282 });
2283 let settled = false;
2284 const pending = (async () => { for await (const frame of http.frames('git-upload-pack', new Uint8Array(0), spanStub)) void frame; })();
2285 pending.then(() => { settled = true; }, () => { settled = true; });
2286 await postHold.startedP;
2287 assert.equal(settled, false, 'a redirect waits until body.cancel() settles');
2288 postHold.release();
2289 const redir = await pending.then(() => null, e => e);
2290 assert.equal(redir.code, 'http');
2291 assert.match(redir.message, /redirected to http:\/\/127\.0\.0\.1:9\/moved\.git\/git-upload-pack/);
2293 const advHold = held();
2294 const declared = createTransport('http://127.0.0.1:9/r.git', {
2295 fetchImpl: async () => ({
2296 ok: true, status: 200,
2297 headers: { get: n => (n === 'content-type' ? 'application/x-git-upload-pack-advertisement' : n === 'content-length' ? '999999999' : null) },
2298 body: advHold.body,
2299 }),
2300 limits: withLimits({ maxPackBytes: 2500 }), tracer: tracerStub,
2301 });
2302 let advSettled = false;
2303 const advPending = unwrapAsync(declared.advertise('git-upload-pack', spanStub));
2304 advPending.then(() => { advSettled = true; }, () => { advSettled = true; });
2305 await advHold.startedP;
2306 assert.equal(advSettled, false, 'an over-cap advertisement waits until body.cancel() settles');
2307 advHold.release();
2308 const adv = await advPending.then(() => null, e => e);
2309 assert.equal(adv.code, 'limit_exceeded');
2310 assert.equal(adv.what, 'advertisement bytes');
2311 await new Promise(r => { setImmediate(r); });
2312 assert.deepEqual(unhandled, []);
2313 } finally {
2314 process.off('unhandledRejection', onUnhandled);
2317 const reply = (status, contentType, body) => createTransport('http://127.0.0.1:9/r.git', {
2318 fetchImpl: async () => ({ ok: status >= 200 && status < 300, status, headers: { get: n => (n === 'content-type' ? contentType : null) }, body }),
2319 tracer: tracerStub,
2320 });
2321 const drain = t => (async () => { for await (const frame of t.frames('git-upload-pack', new Uint8Array(0), spanStub)) void frame; })().then(() => null, e => e);
2322 const post = await drain(reply(200, 'application/x-git-upload-pack-result', null));
2323 assert.equal(post.code, 'protocol');
2324 assert.equal(post.message, 'git-upload-pack: HTTP 200 response has no body');
2325 const postUndef = await drain(reply(200, 'application/x-git-upload-pack-result', undefined));
2326 assert.equal(postUndef.message, 'git-upload-pack: HTTP 200 response has no body');
2327 const wrongType = await drain(reply(200, 'text/html', null));
2328 assert.equal(wrongType.code, 'not_git');
2329 const empty = new ReadableStream({ start(c) { c.close(); } });
2330 let frames = 0;
2331 for await (const _frame of reply(200, 'application/x-git-upload-pack-result', empty).frames('git-upload-pack', new Uint8Array(0), spanStub)) frames++;
2332 assert.equal(frames, 0, 'an empty body is bytes, not a missing body');
2333 const advNull = await reply(200, 'application/x-git-upload-pack-advertisement', null).advertise('git-upload-pack', spanStub).then(r => (r.ok ? null : r.error), e => e);
2334 assert.equal(advNull.code, 'protocol');
2335 assert.equal(advNull.message, 'git-upload-pack advertisement: HTTP 200 response has no body');
2336 const advEmpty = await unwrapAsync(reply(200, 'application/x-git-upload-pack-advertisement', new ReadableStream({ start(c) { c.close(); } })).advertise('git-upload-pack', spanStub));
2337 assert.equal(advEmpty.length, 0);
2339 const shaped = (message, reason, code = 'pack_corrupt') => Object.assign(new Error(message), { code, reason });
2340 assert.equal(isShortRead(shaped('not the short-read text', 'short_read')), true);
2341 assert.equal(isShortRead(shaped('short read: need 1 bytes at 0, have 0')), false);
2342 assert.equal(isShortRead(shaped('short read: need 1 bytes at 0, have 0', 'other')), false);
2343 assert.equal(isShortRead(shaped('short read: need 1', 'short_read', 'protocol')), false);
2344 assert.equal(isShortRead(null), false);
2345 assert.equal(isShortRead(undefined), false);
2346 const continued = encodeObjHeader(TYPE.BLOB, 300);
2347 const waited = spStep(spInit(), concat([Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION, 0, 0, 0, 1), continued.subarray(0, 1)]));
2348 assert.equal(waited.state.phase, 'header');
2349 assert.equal(isShortRead(Object.assign(new Error('short read: need 1 bytes at 12, have 13'), { code: 'pack_corrupt', reason: 'short_read' })), true);
2350});
2352// constantRefname (src/refname.ts) runs once, at module load, on the literal 'refs/heads/main', so its refusal is reached
2353// only when the validator refuses: here a stand-in validateRef does, and loading the module fails with the internal error.
2354test('constantRefname: a literal the validator refuses fails the module load as internal, naming the literal', async () => {
2355 const real = await import('../src/refname.js');
2356 assert.equal(real.DEFAULT_REF, 'refs/heads/main');
2357 vi.resetModules();
2358 vi.doMock('../src/validate.js', async orig => ({ ...(await orig()), validateRef: () => ({ ok: false, error: { code: 'bad_request', message: 'refused' } }) }));
2359 try {
2360 await assert.rejects(import('../src/refname.js'), e => e.code === 'internal' && e.message === 'refs/heads/main is not a refname');
2361 } finally {
2362 vi.doUnmock('../src/validate.js');
2363 vi.resetModules();
2365});
2367test('parseLsRefLine: an empty name before the attributes keeps them (the attributes start one past the cut at 0)', () => {
2368 // `<oid> symref-target:x`: the name is "" (cut 0), and the attributes are everything after that one space
2369 assert.deepEqual(parseLsRefLine(`${oid(1)} symref-target:refs/heads/main peeled:${oid(2)}`), { oid: oid(1), name: '', peeled: oid(2), 'symref-target': 'refs/heads/main' });
2370 assert.deepEqual(parseLsRefLine(`${oid(1)} HEAD symref-target:refs/heads/main`), { oid: oid(1), name: 'HEAD', peeled: null, 'symref-target': 'refs/heads/main' });
2371});
2373test('parseCapabilitiesV2: each refusal by its own check, code and message', () => {
2374 const d = l => ({ kind: 'data', payload: te.encode(l) });
2375 const FL = { kind: 'flush' };
2376 const SVC = d('# service=git-upload-pack\n');
2377 const refused = (frames, code, message, extra = {}) => {
2378 const r = parseCapabilitiesV2(frames);
2379 assert.equal(r.ok, false, message);
2380 assert.deepEqual([r.error.code, r.error.message], [code, message]);
2381 for (const [k, v] of Object.entries(extra)) assert.deepEqual(r.error[k], v, k);
2382 };
2383 // the service header's metadata ends at its own flush: a delim there is refused, not read past
2384 refused([SVC, d('version 2\n'), { kind: 'delim' }, FL], 'protocol', 'advertisement: no flush after the service line');
2385 refused([SVC, d('version 2\n')], 'protocol', 'advertisement: no flush after the service line');
2386 // a header and its flush with nothing after
2387 refused([SVC, FL], 'protocol', 'advertisement: nothing after the service line');
2388 // no first data line at all: the first line is "", and it is not `version 2`
2389 refused([FL], 'unsupported', 'server did not answer protocol v2 (first line "")', { lines: [''] });
2390 refused([], 'unsupported', 'server did not answer protocol v2 (first line "")', { lines: [''] });
2391 refused([SVC, FL, FL], 'unsupported', 'server did not answer protocol v2 (first line "")', { lines: [''] });
2392 // the server's ERR line, with or without the header; a line that only ends in "ERR " is not one
2393 refused([d('ERR access denied\n'), FL], 'remote_error', 'remote: access denied');
2394 refused([SVC, FL, d('ERR repository not found\n'), FL], 'remote_error', 'remote: repository not found');
2395 refused([d('x ERR \n'), FL], 'unsupported', 'server did not answer protocol v2 (first line "x ERR ")', { lines: ['x ERR '] });
2396 // the capabilities end at a flush, and nothing follows it
2397 refused([d('version 2\n'), d('agent=x\n'), { kind: 'delim' }], 'protocol', 'advertisement: expected flush after capabilities, got delim');
2398 refused([d('version 2\n'), d('agent=x\n')], 'protocol', 'advertisement: expected flush after capabilities, got end of stream');
2399 refused([d('version 2\n'), FL, FL], 'protocol', "advertisement: flush after the capabilities' flush");
2400 assert.deepEqual(parseCapabilitiesV2([SVC, FL, d('version 2\n'), d('agent=x\n'), FL]), { ok: true, value: ['agent=x'] });
2401 assert.deepEqual(parseCapabilitiesV2([d('version 2\n'), FL]), { ok: true, value: [] });
2402});
2404// syncRef's spec and options (parseSync), over two fake transports: the source answers ls-refs and a fetch of one root
2405// commit, the destination answers ls-refs (its tip, or none) and receive-pack with `ok`. Every request is logged, so a
2406// refusal is seen to send nothing and a default is seen in what was sent.
2407test('syncRef: each spec and option refusal before anything is sent, the defaults, and dstRef', async () => {
2408 const author = 'Ada <ada@ex> 1700000000 +0000';
2409 const tree = unwrap(encodeTree([]));
2410 const treeId = hashObject('tree', tree);
2411 const commit = unwrap(encodeCommit({ tree: treeId, parents: [], author, message: 'root\n' }));
2412 const tip = hashObject('commit', commit);
2413 const { pack } = await buildPack([{ type: TYPE.COMMIT, data: commit }, { type: TYPE.TREE, data: tree }]);
2414 const dat = s => ({ kind: 'data', payload: typeof s === 'string' ? te.encode(s) : s });
2415 const FL = { kind: 'flush' };
2416 const fake = (name, log, answer) => ({
2417 kind: 'http', url: `http://${name}/r.git`, tracer: new Tracer(), parent: null, limits: DEFAULT_LIMITS, signal: null,
2418 async *frames(service, body) {
2419 const text = td.decode(body);
2420 const what = service === 'git-receive-pack' ? 'receive-pack' : /command=fetch/.test(text) ? 'fetch' : 'ls-refs';
2421 log.push(`${name} ${what}${what === 'ls-refs' ? ` ${(text.match(/ref-prefix (\S+)/) ?? [])[1]}` : ''}`);
2422 yield* answer(what, text);
2423 },
2424 async advertise() { log.push(`${name} advertise`); return { ok: false, error: { code: 'http', message: 'no advertisement' } }; },
2425 });
2426 const pair = ({ dstTip = null } = {}) => {
2427 const log = [];
2428 const src = fake('src', log, function* (what) {
2429 if (what === 'ls-refs') { yield dat(`${tip} refs/heads/main\n`); yield FL; return; }
2430 yield dat('packfile\n'); yield dat(concat([Uint8Array.of(SIDEBAND_PACK), pack])); yield FL;
2431 });
2432 const dst = fake('dst', log, function* (what, text) {
2433 if (what === 'ls-refs') {
2434 const asked = (text.match(/ref-prefix (\S+)/) ?? [])[1];
2435 if (dstTip !== null) yield dat(`${dstTip} ${asked}\n`);
2436 yield FL; return;
2438 const ref = (text.match(/ (refs\/\S+?)\0/) ?? [])[1];
2439 yield dat(concat([Uint8Array.of(SIDEBAND_PACK), unwrap(pkt('unpack ok\n')), unwrap(pkt(`ok ${ref}\n`)), FLUSH]));
2440 yield FL;
2441 });
2442 return { log, opts: { src: { transport: src }, dst: { transport: dst } } };
2443 };
2444 const refused = async (spec, extra, what, message) => {
2445 const p = pair();
2446 const r = await syncRef('http://src/r.git', 'http://dst/r.git', spec, { ...p.opts, ...extra });
2447 assert.equal(r.ok, false, message);
2448 assert.deepEqual([r.error.code, r.error.what, r.error.message], ['bad_request', what, message]);
2449 assert.deepEqual(p.log, [], 'nothing sent');
2450 };
2451 await refused({ ref: 'main' }, {}, 'ref', 'invalid ref: must be a full ref name starting with refs/');
2452 // each side's own check: a bad source ref with a good dstRef, and a one-level name either side (receive-pack's funny
2453 // refname: validatePushRef as an update, not a delete)
2454 await refused({ ref: 'main', dstRef: 'refs/heads/main' }, {}, 'ref', 'invalid ref: must be a full ref name starting with refs/');
2455 await refused({ ref: 'refs/main', dstRef: 'refs/heads/main' }, {}, 'ref', 'invalid ref: receive-pack refuses it (funny refname)');
2456 await refused({ ref: 'refs/heads/main', dstRef: 'refs/main' }, {}, 'ref', 'invalid ref: receive-pack refuses it (funny refname)');
2457 await refused({ ref: 'refs/heads/main', dstRef: 'refs/heads/a..b' }, {}, 'ref', 'invalid ref: fails check-ref-format');
2458 await refused({ ref: 'refs/heads/main', dstRef: 'main' }, {}, 'ref', 'invalid ref: must be a full ref name starting with refs/');
2459 await refused({ ref: 'refs/heads/main', force: 'yes' }, {}, 'force', 'syncRef: force must be a boolean');
2460 await refused({ ref: 'refs/heads/main' }, { auth: { token: 't' } }, 'auth', 'syncRef: auth belongs to one remote: pass opts.src.auth and opts.dst.auth');
2462 // the defaults: dstRef is ref, force false, verify false (one ls-refs per side, no re-read after the push)
2463 const created = pair();
2464 const made = await syncRef('http://src/r.git', 'http://dst/r.git', { ref: 'refs/heads/main' }, created.opts);
2465 assert.equal(made.ok, true, made.ok ? '' : `${made.error.code}: ${made.error.message}`);
2466 assert.deepEqual(made.value, { outcome: 'created', ref: 'refs/heads/main', dstRef: 'refs/heads/main', from: null, to: tip, forced: false, objects: 2, pushBytes: made.value.pushBytes });
2467 assert.deepEqual(created.log.toSorted(), ['dst ls-refs refs/heads/main', 'dst receive-pack', 'src fetch', 'src ls-refs refs/heads/main']);
2468 // an explicit null dstRef and force false are the defaults too; a given dstRef is where it lands
2469 const renamed = pair();
2470 const moved = await syncRef('http://src/r.git', 'http://dst/r.git', { ref: 'refs/heads/main', dstRef: 'refs/heads/mirror', force: false }, renamed.opts);
2471 assert.deepEqual([moved.value.outcome, moved.value.ref, moved.value.dstRef], ['created', 'refs/heads/main', 'refs/heads/mirror']);
2472 assert.ok(renamed.log.includes('dst ls-refs refs/heads/mirror'), renamed.log.join());
2473 // verify: true re-reads the destination after the push (here it still answers no tip, so the check fails)
2474 const verified = pair();
2475 const checked = await syncRef('http://src/r.git', 'http://dst/r.git', { ref: 'refs/heads/main', dstRef: null }, { ...verified.opts, verify: true });
2476 assert.deepEqual([checked.ok, checked.error.code, checked.error.message], [false, 'ambiguous', `push reported ok but refs/heads/main is absent, not ${tip}`]);
2477 assert.equal(verified.log.filter(l => l === 'dst ls-refs refs/heads/main').length, 2);
2478 // force defaults to false: a destination tip the source does not descend from is refused, nothing pushed
2479 const diverged = pair({ dstTip: oid(9) });
2480 const nff = await syncRef('http://src/r.git', 'http://dst/r.git', { ref: 'refs/heads/main' }, diverged.opts);
2481 assert.deepEqual([nff.ok, nff.error.code, nff.error.what], [false, 'not_fast_forward', 'ref']);
2482 assert.equal(diverged.log.includes('dst receive-pack'), false);
2483 const forcedPair = pair({ dstTip: oid(9) });
2484 const forced = await syncRef('http://src/r.git', 'http://dst/r.git', { ref: 'refs/heads/main', force: true }, forcedPair.opts);
2485 assert.deepEqual([forced.value.outcome, forced.value.from, forced.value.forced], ['updated', oid(9), true]);
2486});
Diagnostics6 git calls · 229 ms
CallStatusSizeTime0229 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 KiB75 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=75
git.response.bytes=11990
git.response.cache=EXPIRED
git.response.duration_ms=75
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 · hit13 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=13
git.response.bytes=919
git.response.cache=HIT
git.response.duration_ms=13
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 · 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=5724
git.response.cache=HIT
git.response.duration_ms=3
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
fetch20045 KiB135 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=134
git.response.bytes=46113
git.response.cache=MISS
git.response.duration_ms=135
git.fetch.wants=fff4ca0ebbd221c6b11f823638fd932d9bfe1a0c
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=6
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=46113
git.pkt.payload.max=32769
git.sideband.band1.bytes=46076
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
a5e1a115b9d73cd0
Request
bb7ab1fa…
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