main
1// What ply reads of a receive-pack answer is bounded by what an honest report can be. The counter here is the test's
2// own: bytes handed out by the response body the test built. It is not a number ply reports.
3import { test } from 'vitest';
4import assert from 'node:assert/strict';
5import { createTransport } from '../src/http.js';
6import { push, ZERO_OID } from '../src/push.js';
7import { WORKER_LIMITS } from '../src/limits.js';
8import { unwrap } from './unwrap.js';
10const te = new TextEncoder();
11// pkt-line and side-band-64k framing written from the protocol (gitprotocol-common: four hex digits of length that
12// count themselves; gitprotocol-pack: the first payload byte is the band), not with ply's encoders.
13const pkt = bytes => { const out = new Uint8Array(bytes.length + 4); out.set(te.encode((bytes.length + 4).toString(16).padStart(4, '0'))); out.set(bytes, 4); return out; };
14const FLUSH = te.encode('0000');
15const cat = (...parts) => { const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0)); let at = 0; for (const p of parts) { out.set(p, at); at += p.length; } return out; };
16const band = (n, body) => pkt(cat(Uint8Array.of(n), body));
17const line = text => pkt(te.encode(text));
18const REF = 'refs/heads/main';
19const KIB = 1024;
20const MIB = KIB * KIB;
21const FLOOD = 120 * MIB;
23// A receive-pack that takes the whole request, then answers with the chunks of `answer()`. The body has no queue
24// (highWaterMark 0), so `pulled` is exactly the bytes the client asked for.
25const host = (answer, limits = {}) => {
26 const seen = { pulled: 0, cancelled: false, posts: 0 };
27 const fetchImpl = async (_url, init) => {
28 if (init.method !== 'POST') return new Response('', { status: 404 });
29 seen.posts++;
30 if (init.body instanceof ReadableStream) for await (const _ of init.body) { /* the request body, taken whole */ }
31 const chunks = answer();
32 const body = new ReadableStream({
33 pull(c) { const r = chunks.next(); if (r.done) { c.close(); return; } seen.pulled += r.value.length; c.enqueue(r.value); },
34 cancel() { seen.cancelled = true; },
35 }, { highWaterMark: 0 });
36 return new Response(body, { status: 200, headers: { 'content-type': 'application/x-git-receive-pack-result' } });
37 };
38 return { seen, t: unwrap(createTransport('https://host.example/repo.git', { fetchImpl, limits })) };
39};
40const pushOne = t => push(t, { ref: REF, oldOid: ZERO_OID, newOid: 'a'.repeat(40), objects: [{ type: 3, data: te.encode('hi\n') }] });
41const repeat = function* (chunk, total, ...tail) { for (let sent = 0; sent < total; sent += chunk.length) yield chunk; yield* tail; };
43// An honest report for this one command is "unpack ok\n" and "ok refs/heads/main\n": two lines, 29 bytes. Each bound
44// below is a round figure far above that and far below the flood.
45test('a band-1 report of 65,000-byte lines that would run to 120 MiB is abandoned at once: ambiguous, the body cancelled', async () => {
46 const big = band(1, line(`${'x'.repeat(65_000)}\n`));
47 const { seen, t } = host(() => repeat(big, FLOOD, cat(band(1, FLUSH), FLUSH)));
48 const r = await pushOne(t);
49 assert.equal(r.ok, false);
50 assert.equal(r.error.code, 'ambiguous', 'the request body was sent');
51 assert.equal(seen.posts, 1);
52 assert.ok(seen.pulled <= 256 * KIB, `ply pulled ${seen.pulled} bytes of a report that cannot be valid from its first line`);
53 assert.equal(seen.cancelled, true, 'the response body is cancelled, not left open');
54});
56test('a report with more ref lines than commands is abandoned at the first one: ambiguous, the body cancelled', async () => {
57 const extra = band(1, line('ok refs/heads/never-pushed\n'));
58 const { seen, t } = host(() => repeat(extra, FLOOD, cat(band(1, FLUSH), FLUSH), band(1, line('unpack ok\n'))));
59 // the unpack line first, so every line is well formed: only the count and the ref names are wrong
60 const { seen: seen2, t: t2 } = host(function* () { yield band(1, line('unpack ok\n')); yield band(1, line(`ok ${REF}\n`)); yield* repeat(extra, FLOOD, cat(band(1, FLUSH), FLUSH)); });
61 for (const [s, tr] of [[seen, t], [seen2, t2]]) {
62 const r = await pushOne(tr);
63 assert.equal(r.ok, false);
64 assert.equal(r.error.code, 'ambiguous');
65 assert.ok(s.pulled <= 64 * KIB, `ply pulled ${s.pulled} bytes`);
66 assert.equal(s.cancelled, true);
67 }
68});
70// Owner decision (5): a cap bounds what ply keeps, never the outcome class. Every answer below is shaped as receive-pack
71// writes it (an unpack line, one status for the one command, the two flushes), so the report grammar fixes its class:
72// "ok <ref>" landed, "ng <ref> <reason>" refused. origin/main reads each the same way (review3 report-diff.mjs).
73const answerOf = (...lines) => cat(band(1, cat(...lines.map(line), FLUSH)), FLUSH);
74const REPORT_OK = answerOf('unpack ok\n', `ok ${REF}\n`);
75const LANDED = { tag: 'statuses', refs: [{ ref: REF, ok: true, reason: '' }] };
77test('band 2 of any size under maxPackBytes is read through to the report: 1 MiB and 120 MiB of hook output both land', async () => {
78 const progress = band(2, te.encode(`${'remote: Resolving deltas: 100% (1234/1234), done.'.padEnd(998, '.')}\n`));
79 for (const total of [MIB, FLOOD]) {
80 const { seen, t } = host(() => repeat(progress, total, REPORT_OK));
81 const landed = await pushOne(t);
82 assert.equal(landed.ok, true, landed.ok ? '' : landed.error.message);
83 assert.deepEqual(landed.value.report, LANDED);
84 assert.ok(seen.pulled > total, 'all of it was read');
85 // what is kept of band 2 is a tail of 4,096 characters, whatever was read
86 assert.ok(landed.value.hookOutput.length <= 4096, `kept ${landed.value.hookOutput.length} characters of band 2`);
87 }
88});
90test('band 2 past maxReportProgressBytes is dropped, and the report is still read', async () => {
91 // three 1,000-byte band-2 frames (the band byte and 999 letters) under a limit of 2,000: a and b fit, c is past it
92 const frame = letter => band(2, te.encode(letter.repeat(999)));
93 const { t } = host(function* () { yield frame('a'); yield frame('b'); yield frame('c'); yield REPORT_OK; }, { maxReportProgressBytes: 2000 });
94 const landed = await pushOne(t);
95 assert.equal(landed.ok, true, landed.ok ? '' : landed.error.message);
96 assert.deepEqual(landed.value.report, LANDED);
97 assert.equal(landed.value.hookOutput, 'a'.repeat(999) + 'b'.repeat(999));
98 // a limit that bounds nothing is refused where the record enters, before any request
99 for (const key of ['maxReportProgressBytes', 'maxReportReasonBytes']) {
100 const made = createTransport('https://host.example/repo.git', { limits: { [key]: -1 } });
101 assert.deepEqual([made.ok, made.ok ? '' : made.error.code, made.ok ? '' : made.error.what], [false, 'bad_request', key]);
102 }
103});
105test('an over-long reason is cut at maxReportReasonBytes and the ref keeps its status', async () => {
106 // [reason bytes sent, limits, reason bytes kept]: the default is 4,096
107 for (const [sent, limits, kept] of [[4096, {}, 4096], [4097, {}, 4096], [20_000, {}, 4096], [300, { maxReportReasonBytes: 100 }, 100]]) {
108 const { t } = host(function* () { yield answerOf('unpack ok\n', `ng ${REF} ${'r'.repeat(sent)}\n`); }, limits);
109 const r = await pushOne(t);
110 assert.equal(r.ok, false);
111 assert.equal(r.error.code, 'push_rejected', `${sent} bytes: ${r.error.message.slice(0, 120)}`);
112 assert.deepEqual(r.error.report, { tag: 'statuses', refs: [{ ref: REF, ok: false, reason: 'r'.repeat(kept) }] });
113 }
114 // a host's unpack failure, its 5,000-byte status echoed on the ref: both are cut at the same byte, so it is still the echo
115 const why = 'e'.repeat(5000);
116 const echoed = host(function* () { yield answerOf(`unpack ${why}\n`, `ng ${REF} ${why}\n`); });
117 const refused = await pushOne(echoed.t);
118 assert.equal(refused.ok, false);
119 assert.equal(refused.error.code, 'push_rejected', refused.error.message.slice(0, 120));
120 assert.deepEqual(refused.error.report, { tag: 'unpackFailed', status: 'e'.repeat(4096), refs: [{ ref: REF, ok: false, reason: 'e'.repeat(4096) }] });
121});
123test('a cut never reads a report that is malformed whole: the bytes dropped are held to the same grammar', async () => {
124 // past the cut: a control byte in the reason, and a line with no LF. Both are malformed whole, so both stay ambiguous.
125 const bad = `${'r'.repeat(4500)}\u0001${'r'.repeat(499)}`;
126 for (const ng of [`ng ${REF} ${bad}\n`, `ng ${REF} ${'r'.repeat(5000)}`]) {
127 const { t } = host(function* () { yield answerOf('unpack ok\n', ng); });
128 const r = await pushOne(t);
129 assert.equal(r.ok, false);
130 assert.equal(r.error.code, 'ambiguous', r.error.message.slice(0, 120));
131 }
132});
134test('an over-long unpack status and ng reason that are not an exact echo whole stay ambiguous: the echo is read before the cut', async () => {
135 // SAFETY.md I36: a host's unpack failure is definite only when every ref's reason is the unpack status, byte for byte.
136 // The expected value is the grammar's, by hand: in each pair below the status and the reason are different strings,
137 // so whole they are no echo and the outcome is unknown, wherever the limit cuts them (4,096 bytes by default, 1,024
138 // under WORKER_LIMITS). `shared` is exactly the bytes a cut keeps, so each pair is equal once cut.
139 for (const [limits, cap] of [[{}, 4096], [WORKER_LIMITS, 1024]]) {
140 const shared = 'e'.repeat(cap);
141 for (const [status, reason] of [
142 [`${shared}${'A'.repeat(900)}`, `${shared}${'B'.repeat(900)}`], // both over the limit, different past it
143 [`${shared}${'e'.repeat(904)}`, shared], // the reason is the first bytes of the status, and under the limit
144 [shared, `${shared}B`], // the status is the first bytes of the reason, and under the limit
145 ]) {
146 const { t } = host(function* () { yield answerOf(`unpack ${status}\n`, `ng ${REF} ${reason}\n`); }, limits);
147 const r = await pushOne(t);
148 assert.equal(r.ok, false);
149 assert.equal(r.error.code, 'ambiguous', `cap ${cap}, status ${status.length} bytes, reason ${reason.length} bytes: ${r.error.message.slice(0, 80)}`);
150 }
151 // the control: the same over-long status echoed exactly is the host's unpack failure, cut where the limit says
152 const why = `${shared}${'A'.repeat(900)}`;
153 const { t } = host(function* () { yield answerOf(`unpack ${why}\n`, `ng ${REF} ${why}\n`); }, limits);
154 const r = await pushOne(t);
155 assert.equal(r.ok, false);
156 assert.equal(r.error.code, 'push_rejected', r.error.message.slice(0, 80));
157 assert.deepEqual(r.error.report, { tag: 'unpackFailed', status: shared, refs: [{ ref: REF, ok: false, reason: shared }] });
158 }
159});
161test('a reason limit below the reasons receive-pack itself writes does not cut them: their classes are read whole', async () => {
162 // receive-pack's own reasons decide the outcome class: non-fast-forward is not_fast_forward, a proc-receive hook that
163 // did not answer is ambiguous, an unpack failure is push_rejected, and "unpack ok" with "ok <ref>" landed.
164 for (const [lines, code] of [
165 [['unpack ok\n', `ok ${REF}\n`], 'ok'], [['unpack ok\n', `ng ${REF} non-fast-forward\n`], 'not_fast_forward'],
166 [['unpack ok\n', `ng ${REF} proc-receive failed to report status\n`], 'ambiguous'],
167 [['unpack index-pack abnormal exit\n', `ng ${REF} unpacker error\n`], 'push_rejected'],
168 ]) {
169 const { t } = host(function* () { yield answerOf(...lines); }, { maxReportReasonBytes: 0 });
170 const r = await pushOne(t);
171 assert.equal(r.ok ? 'ok' : r.error.code, code, lines[1]);
172 }
173});
175test('13,000 band-1 keepalives while a hook runs, then the report: landed', async () => {
176 // copy_to_sideband's keepalive is an empty band-1 packet ("0005" and the band byte), one every 5 s while a hook runs
177 const keepalives = cat(...Array.from({ length: 13_000 }, () => band(1, new Uint8Array(0))));
178 const { t } = host(function* () { yield keepalives; yield REPORT_OK; });
179 const landed = await pushOne(t);
180 assert.equal(landed.ok, true, landed.ok ? '' : landed.error.message);
181 assert.deepEqual(landed.value.report, LANDED);
182});
Diagnostics4 git calls · 747 ms
CallStatusSizeTime0747 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=2 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
fetch2004.0 KiB742 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=742 git.response.bytes=4138 git.response.cache=MISS git.response.duration_ms=742 git.fetch.wants=b5d4c7ae311ff75c7a962ef9992ee5c818ce8380 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=4138 git.pkt.payload.max=4117 git.sideband.band1.bytes=4116 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
- a5e2ce502e0defad
- Request
- ac8e09ee…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format