main
1// git is the judge of what a zlib member may be: ply's inflate accepts a member if and only if stock zlib, as git links
2// it, accepts it. The expected value of every case is node:zlib's answer on the same bytes and, for the hand-built
3// loose objects, `git cat-file`'s: never ply's.
4//
5// What stock zlib does with the header's window (zlib inflate.c, HEAD state; git calls inflateInit, so wbits is 15):
6// len = BITS(4) + 8; if (len > 15 || len > state->wbits) "invalid window size"; state->dmax = 1U << len;
7// and dmax is read only under `#ifdef INFLATE_STRICT` (inflate.c DIST state, inffast.c), which a stock build does not
8// define. So CINFO > 7 is refused, and a smaller CINFO is not enforced: a distance is refused only when it reaches
9// before the output produced so far (`state->offset > state->whave + out - left`, "invalid distance too far back").
10import { test } from 'vitest';
11import assert from 'node:assert/strict';
12import zlib from 'node:zlib';
13import crypto from 'node:crypto';
14import { spawnSync } from 'node:child_process';
15import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
16import { tmpdir } from 'node:os';
17import { join } from 'node:path';
18import { inflate } from '../src/inflate.js';
20function bitWriter() {
21 const bits = [];
22 return {
23 put(v, n) { for (let i = 0; i < n; i++) bits.push((v >> i) & 1); }, // a field: least significant bit first
24 code(c, n) { for (let i = n - 1; i >= 0; i--) bits.push((c >> i) & 1); }, // a Huffman code: most significant first
25 bytes() { const out = new Uint8Array(Math.ceil(bits.length / 8)); bits.forEach((b, i) => { out[i >> 3] |= b << (i & 7); }); return out; },
26 };
27}
28// RFC 1950 §2.2: CMF = CINFO << 4 | 8 (deflate), FLG the smallest value making CMF * 256 + FLG a multiple of 31
29// (FLEVEL 0, no FDICT). CINFO 0 gives 08 1d.
30const zlibHeader = cinfo => { const cmf = (cinfo << 4) | 8; return Uint8Array.from([cmf, (31 - ((cmf << 8) % 31)) % 31]); };
31const adler32 = bytes => { let a = 1, b = 0; for (const x of bytes) { a = (a + x) % 65521; b = (b + a) % 65521; } const out = Buffer.alloc(4); out.writeUInt32BE(((b << 16) | a) >>> 0); return out; };
32// RFC 1951 §3.2.5 distance codes (5 bits each in a fixed block): [code, base, extra bits]
33const DIST = { 300: [16, 257, 7], 301: [16, 257, 7], 32768: [29, 24577, 13] };
34// A member: `stored` in one non-final stored block, then a final fixed block holding one match of length 3 (symbol 257,
35// 7-bit code 0000001, no extra bits) at `distance` and end-of-block (symbol 256, 0000000). It inflates to `stored`
36// followed by the 3 bytes `distance` back from its end.
37function member(cinfo, stored, distance) {
38 const [code, base, extra] = DIST[distance];
39 const len = Buffer.alloc(4); len.writeUInt16LE(stored.length, 0); len.writeUInt16LE(~stored.length & 0xffff, 2);
40 const w = bitWriter();
41 w.put(1, 1); w.put(1, 2);
42 w.code(1, 7);
43 w.code(code, 5); w.put(distance - base, extra);
44 w.code(0, 7);
45 const at = stored.length - distance;
46 const out = at < 0 ? null : Buffer.concat([stored, stored.subarray(at, at + 3)]);
47 return { out, bytes: Buffer.concat([zlibHeader(cinfo), Buffer.from([0]), len, stored, w.bytes(), adler32(out ?? stored)]) };
48}
49const zlibSays = bytes => { try { return { ok: true, out: zlib.inflateSync(bytes) }; } catch (e) { return { ok: false, why: e.message }; } };
50// A loose object whose file is exactly `bytes`; git reads it with its own zlib. `inflated` is "blob N\0" + content.
51function gitReads(bytes, inflated) {
52 const dir = mkdtempSync(join(tmpdir(), 'ply-loose-'));
53 try {
54 assert.equal(spawnSync('git', ['init', '-q', '--bare', dir]).status, 0);
55 const oid = crypto.createHash('sha1').update(inflated).digest('hex');
56 mkdirSync(join(dir, 'objects', oid.slice(0, 2)), { recursive: true });
57 writeFileSync(join(dir, 'objects', oid.slice(0, 2), oid.slice(2)), bytes);
58 const r = spawnSync('git', ['-C', dir, 'cat-file', 'blob', oid]);
59 return { ok: r.status === 0, out: r.stdout, why: r.stderr.toString().trim() };
60 } finally { rmSync(dir, { recursive: true, force: true }); }
61}
62// "blob 294\0" (9 bytes) + 291 content bytes = 300 stored bytes; the match adds the first 3 ("blo"): 294 content bytes
63const LOOSE = Buffer.concat([Buffer.from('blob 294\0'), Buffer.from(Array.from({ length: 291 }, (_, i) => 32 + (i * 7) % 90))]);
65test('a back-reference past the window CINFO declares is accepted, as zlib and git accept it', () => {
66 const { bytes, out } = member(0, LOOSE, 300);
67 assert.deepEqual([bytes[0], bytes[1]], [0x08, 0x1d]);
68 const z = zlibSays(bytes);
69 assert.equal(z.ok, true, z.why);
70 assert.deepEqual(z.out, out);
71 const g = gitReads(bytes, out);
72 assert.equal(g.ok, true, g.why);
73 assert.deepEqual(g.out, out.subarray(9));
74 const got = inflate(bytes, 0, { expectedSize: out.length });
75 assert.equal(got.ok, true, got.ok ? '' : `${got.error.code}: ${got.error.message}`);
76 assert.deepEqual(Buffer.from(got.value.out), out);
77 assert.equal(got.value.consumed, bytes.length);
78});
80test('every CINFO 0..7 with a distance of 300 and of 32768: ply answers what zlib answers', () => {
81 const far = Buffer.from(Array.from({ length: 32768 }, (_, i) => (i * 31 + (i >> 8)) & 255));
82 for (let cinfo = 0; cinfo <= 7; cinfo++) for (const [stored, distance] of [[LOOSE, 300], [far, 32768]]) {
83 const { bytes, out } = member(cinfo, stored, distance);
84 const z = zlibSays(bytes);
85 assert.equal(z.ok, true, `CINFO ${cinfo} distance ${distance}: zlib ${z.why}`);
86 const got = inflate(bytes, 0);
87 assert.equal(got.ok, true, `CINFO ${cinfo} distance ${distance}: zlib accepts, ply refuses ${got.ok ? '' : got.error.message}`);
88 assert.deepEqual(Buffer.from(got.value.out), z.out);
89 assert.deepEqual(z.out, out);
90 }
91});
93test('what zlib and git refuse stays refused: CINFO 8, and a distance before the first byte of output', () => {
94 // CINFO 8 (88 1c): zlib "invalid window size"
95 const wide = member(8, LOOSE, 300);
96 assert.deepEqual([wide.bytes[0], wide.bytes[1]], [0x88, 0x1c]);
97 assert.deepEqual(zlibSays(wide.bytes), { ok: false, why: 'invalid window size' });
98 assert.equal(gitReads(wide.bytes, wide.out).ok, false);
99 const w = inflate(wide.bytes, 0);
100 assert.equal(w.ok, false);
101 assert.equal(w.error.code, 'pack_corrupt');
102 // distance 301 with 300 bytes of output: zlib "invalid distance too far back"
103 const before = member(7, LOOSE, 301);
104 assert.deepEqual(zlibSays(before.bytes), { ok: false, why: 'invalid distance too far back' });
105 assert.equal(gitReads(before.bytes, LOOSE).ok, false);
106 const b = inflate(before.bytes, 0);
107 assert.equal(b.ok, false);
108 assert.equal(`${b.error.code}: ${b.error.message}`, 'pack_corrupt: inflate: distance too far back');
109});
Diagnostics4 git calls · 393 ms
CallStatusSizeTime0393 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 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 · hit34 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=30 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=30 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 · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=4 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
fetch2002.7 KiB355 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=355 git.response.bytes=2794 git.response.cache=MISS git.response.duration_ms=355 git.fetch.wants=e8bee26208c59c78d7b24f73a67b71a075ec692c 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=2794 git.pkt.payload.max=2773 git.sideband.band1.bytes=2772 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
- a5eb49911e0222ce
- Request
- e2a938c5…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format