main
1// A delta that asks for a huge result must die before the output buffer exists, and the largest
2// expansion the caps allow is the one SAFETY.md I4 records. A chain is at most maxDeltaChain deltas in any pack order.
3import { test } from 'vitest';
4import assert from 'node:assert/strict';
5import { applyDelta, parsePack, resolvePack, makeDelta } from '../src/packresolve.js';
6import { encodeVarint, encodeObjHeader, zlibDeflate } from '../src/pack.js';
7import { DEFAULT_LIMITS, withLimits } from '../src/limits.js';
8import {
9 DELTA_COPY, DELTA_COPY_MAX, PACK_SIGNATURE, PACK_VERSION, PACK_HEADER_BYTES, UINT32_BYTES,
10 OBJ_CONTINUE, OBJ_SIZE_NEXT_MASK, OBJ_SIZE_NEXT_BASE,
11} from '../src/format.js';
12import { hashObject, TYPE } from '../src/objects.js';
13import { Sha1, fromHex } from '../src/sha1.js';
14import { concat, te } from '../src/bytes.js';
15import { unwrap } from './unwrap.js';
17const MAX = DEFAULT_LIMITS.maxDeltaResult;
19test('the densest in-limit delta expands a 64 KiB base to maxDeltaResult and no further', () => {
20 assert.equal(MAX % DELTA_COPY_MAX, 0);
21 const copies = MAX / DELTA_COPY_MAX;
22 const base = new Uint8Array(DELTA_COPY_MAX);
23 base[0] = 7;
24 const header = concat([encodeVarint(base.length), encodeVarint(MAX)]);
25 const ops = new Uint8Array(copies);
26 ops.fill(DELTA_COPY);
27 const delta = concat([header, ops]);
28 assert.equal(header.length, 7);
29 assert.equal(delta.length, 1031);
30 const { out } = unwrap(applyDelta(base, delta));
31 assert.equal(out.length, MAX);
32 assert.equal(out[0], 7);
33 assert.equal(out[DELTA_COPY_MAX], 7);
34 assert.equal(Math.floor(MAX / delta.length), 65091);
35 assert.throws(
36 () => unwrap(applyDelta(base, concat([encodeVarint(base.length), encodeVarint(MAX + 1), Uint8Array.of(DELTA_COPY)]))),
37 e => e.code === 'limit_exceeded' && e.got === MAX + 1 && e.max === MAX,
38 );
39});
41// ---- chain length (#14) ----
42// A chain of `n` deltas over 'v0' → 'v1' → … → 'vn'. Every object is distinct, so every oid is.
43const CHAIN = DEFAULT_LIMITS.maxDeltaChain;
44const versions = n => Array.from({ length: n + 1 }, (_, i) => te.encode(`v${i} `.repeat(4)));
46// gitformat-pack's OFS_DELTA offset: big-endian 7-bit groups, each continued group biased by one (readOfsDelta's inverse).
47function encodeOfs(off) {
48 const out = [off & OBJ_SIZE_NEXT_MASK];
49 while ((off = Math.floor(off / OBJ_SIZE_NEXT_BASE)) > 0) out.unshift(OBJ_CONTINUE | (--off & OBJ_SIZE_NEXT_MASK));
50 return Uint8Array.of(...out);
51}
53// entries: { type, data } full, { data, baseOid } REF_DELTA, { data, baseIndex } OFS_DELTA to an earlier entry.
54async function packOf(entries) {
55 const parts = [Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION), new Uint8Array(UINT32_BYTES)];
56 new DataView(parts[1].buffer).setUint32(0, entries.length);
57 const offsets = [];
58 let at = PACK_HEADER_BYTES;
59 for (const e of entries) {
60 offsets.push(at);
61 const type = e.baseOid ? TYPE.REF_DELTA : e.baseIndex !== undefined ? TYPE.OFS_DELTA : e.type;
62 const entry = concat([
63 encodeObjHeader(type, e.data.length),
64 ...(e.baseOid ? [fromHex(e.baseOid)] : e.baseIndex !== undefined ? [encodeOfs(at - offsets[e.baseIndex])] : []),
65 await zlibDeflate(e.data),
66 ]);
67 parts.push(entry); at += entry.length;
68 }
69 const body = concat(parts);
70 return unwrap(parsePack(concat([body, new Sha1().update(body).digest()])));
71}
73// n REF_DELTAs, each naming the previous version. childFirst puts the full object last and the tip first.
74function refChain(n, childFirst) {
75 const v = versions(n);
76 const entries = [{ type: TYPE.BLOB, data: v[0] }];
77 for (let i = 1; i <= n; i++) entries.push({ data: makeDelta(v[i - 1], v[i]).delta, baseOid: hashObject('blob', v[i - 1]) });
78 return { v, entries: childFirst ? entries.reverse() : entries };
79}
81// n OFS_DELTAs, each against the entry before it (an OFS base is always earlier, so only base-first exists).
82function ofsChain(n) {
83 const v = versions(n);
84 const entries = [{ type: TYPE.BLOB, data: v[0] }];
85 for (let i = 1; i <= n; i++) entries.push({ data: makeDelta(v[i - 1], v[i]).delta, baseIndex: i - 1 });
86 return { v, entries };
87}
89const tooLong = (n, max = CHAIN) => e => {
90 assert.equal(e.code, 'limit_exceeded');
91 assert.equal(e.what, 'delta chain depth');
92 assert.equal(e.got, max + 1, `${n}-delta chain stops at the first link past the cap`);
93 assert.equal(e.max, max);
94 return true;
95};
97function resolvesWhole(parsed, v, n, external) {
98 const r = unwrap(resolvePack(parsed, external));
99 assert.equal(r.objects.size, external ? n : n + 1);
100 assert.equal(r.stats.maxChain, n);
101 const tip = r.objects.get(hashObject('blob', v[n]));
102 assert.equal(tip.chain, n);
103 assert.deepEqual(tip.data, v[n]);
104}
106test('a delta chain of exactly maxDeltaChain resolves in every order', async () => {
107 assert.equal(CHAIN, 64);
108 for (const childFirst of [false, true]) {
109 const { v, entries } = refChain(CHAIN, childFirst);
110 resolvesWhole(await packOf(entries), v, CHAIN);
111 }
112 const { v, entries } = ofsChain(CHAIN);
113 resolvesWhole(await packOf(entries), v, CHAIN);
114});
116test('maxDeltaChain + 1 deltas are limit_exceeded in every order', async () => {
117 for (const childFirst of [false, true]) {
118 const { entries } = refChain(CHAIN + 1, childFirst);
119 const parsed = await packOf(entries);
120 assert.throws(() => unwrap(resolvePack(parsed)), tooLong(CHAIN + 1));
121 }
122 const parsed = await packOf(ofsChain(CHAIN + 1).entries);
123 assert.throws(() => unwrap(resolvePack(parsed)), tooLong(CHAIN + 1));
124});
126// Issue #14's probes. Base-first, a memoised base came back at depth 0, so 500 links resolved (stats.maxChain = 500).
127// Child-first, the fixpoint stopped after maxDeltaChain passes and returned 65 of 101 objects with no error.
128test('#14 a 500-delta chain emitted base-first is limit_exceeded (REF and OFS)', async () => {
129 const ref = await packOf(refChain(500, false).entries);
130 assert.throws(() => unwrap(resolvePack(ref)), tooLong(500));
131 const ofs = await packOf(ofsChain(500).entries);
132 assert.throws(() => unwrap(resolvePack(ofs)), tooLong(500));
133});
135test('#14 a 101-object chain emitted child-first is limit_exceeded, not 65 objects', async () => {
136 const parsed = await packOf(refChain(100, true).entries);
137 assert.equal(parsed.objects.length, 101);
138 assert.throws(() => unwrap(resolvePack(parsed)), tooLong(100));
139});
141// An OFS chain standing on a thin REF_DELTA: until external bases are allowed, every OFS link recurses through all the
142// unresolved links below it, so recursion depth (not a memoised chain) is what stops a long one, before the stack does.
143test('an OFS chain over a thin REF root is bounded by recursion depth', async () => {
144 const ofsOverThin = async n => {
145 const v = versions(n);
146 const entries = [{ data: makeDelta(v[0], v[1]).delta, baseOid: hashObject('blob', v[0]) }];
147 for (let i = 2; i <= n; i++) entries.push({ data: makeDelta(v[i - 1], v[i]).delta, baseIndex: i - 2 });
148 const base = { type: TYPE.BLOB, data: v[0] };
149 return { v, parsed: await packOf(entries), external: oid => (oid === hashObject('blob', v[0]) ? base : undefined) };
150 };
151 const ok = await ofsOverThin(CHAIN);
152 resolvesWhole(ok.parsed, ok.v, CHAIN, ok.external);
153 const over = await ofsOverThin(CHAIN + 1);
154 assert.throws(() => unwrap(resolvePack(over.parsed, over.external)), tooLong(CHAIN + 1));
155 const deep = await ofsOverThin(20_000); // without the depth check: RangeError (call stack), outside the taxonomy
156 assert.throws(() => unwrap(resolvePack(deep.parsed, deep.external)), tooLong(20_000));
157});
159// Thin packs: a missing base is pack_corrupt (the pack is incomplete), a long chain is limit_exceeded (the pack is too
160// deep). An external base is chain 0, and switching to external bases costs one pass, which used to come out of the
161// pass ceiling: a child-first thin chain of maxDeltaChain lost its last link silently.
162test('thin chains: external base is link 0, a missing base is pack_corrupt, too long is limit_exceeded', async () => {
163 const thin = async (n, childFirst) => {
164 const { v, entries } = refChain(n, childFirst);
165 const base = { type: TYPE.BLOB, data: v[0] };
166 return { v, parsed: await packOf(entries.filter(e => e.type === undefined)), external: oid => (oid === hashObject('blob', v[0]) ? base : undefined) };
167 };
168 for (const childFirst of [false, true]) {
169 const ok = await thin(CHAIN, childFirst);
170 resolvesWhole(ok.parsed, ok.v, CHAIN, ok.external);
171 assert.equal(unwrap(resolvePack(ok.parsed, ok.external)).stats.externalBases, 1);
172 assert.throws(() => unwrap(resolvePack(ok.parsed)), e => e.code === 'pack_corrupt' && /ref delta base [0-9a-f]{40} not found/.test(e.message));
173 const long = await thin(CHAIN + 1, childFirst);
174 assert.throws(() => unwrap(resolvePack(long.parsed, long.external)), tooLong(CHAIN + 1));
175 }
176});
178test('maxDeltaChain is the bound as configured, not a constant', async () => {
179 for (const childFirst of [false, true]) {
180 const { v, entries } = refChain(3, childFirst);
181 const parsed = await packOf(entries);
182 assert.equal(unwrap(resolvePack(parsed, undefined, withLimits({ maxDeltaChain: 3 }))).stats.maxChain, 3);
183 assert.throws(() => unwrap(resolvePack(parsed, undefined, withLimits({ maxDeltaChain: 2 }))), tooLong(3, 2));
184 assert.equal(v.length, 4);
185 }
186});
Diagnostics4 git calls · 32 ms
CallStatusSizeTime032 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=26 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2003.0 KiB · hit26 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3120 git.response.cache=HIT git.response.duration_ms=26 git.fetch.wants=9d7a50a2de39c8446b1a469d5a49c7c5253a528d git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3120 git.pkt.payload.max=3099 git.sideband.band1.bytes=3098 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e1a5da5c715e44
- Request
- 648171db…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format