GitHubivarvong

ply

main
ply6 git calls74 ms25 KiB4 cachedprotocol v2
89 lines6.5 KiBRaw
1// An error never grows with a caller's text: the tree encoder quotes at most 200 UTF-16 units of a name (or a mode)
2// and says the true length. The bound below is derived by hand from the message's shape, not read from the code:
3// `tree: entry "` + 200 units, each at most 6 characters once JSON-escaped (\uXXXX) + `"… (N UTF-16 units)` + `: ` +
4// a fixed reason (the longest is under 100 characters) + a second quoted value of the same shape for a mode.
5// 2 × (200 × 6 + 40) + 200 = 2680 characters, whatever the caller sends.
6import { test } from 'vitest';
7import assert from 'node:assert/strict';
8import { encodeTree, encodeEntries, treeEntryOf } from '../src/objects.js';
10const BLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391';
11const BOUND = 2680;
12const QUOTE = 200;
13// every string the error holds: the message and each field
14const held = error => Math.max(error.message.length, ...Object.values(error).filter(v => typeof v === 'string').map(v => v.length));
16test('a tree refusal is the same size for a 5 000 and a 1 000 000 character name: the name is cut to 200 units and its length said', () => {
17 const cells = [
18 ['large pathname', n => ({ mode: '100644', name: 'x'.repeat(n), oid: BLOB })],
19 ['NUL in name', n => ({ mode: '100644', name: `a\0${'B'.repeat(n)}`, oid: BLOB })],
20 ['full pathname', n => ({ mode: '100644', name: `a/${'b'.repeat(n)}`, oid: BLOB })],
21 ['bad name', n => ({ mode: '100644', name: `.git${' '.repeat(n)}`, oid: BLOB })],
22 ['invalid mode, long name', n => ({ mode: '644', name: '\u0001'.repeat(n), oid: BLOB })],
23 ['invalid mode, long mode', n => ({ mode: '7'.repeat(n), name: 'a', oid: BLOB })],
24 ['oid, long name and oid', n => ({ mode: '100644', name: 'é'.repeat(n), oid: 'f'.repeat(n) })],
25 ];
26 const wrong = [];
27 for (const [reason, make] of cells) {
28 for (const [fn, encode] of [['encodeTree', e => encodeTree([e])], ['encodeEntries', e => encodeEntries([e])], ['treeEntryOf', treeEntryOf]]) {
29 const sizes = [];
30 for (const n of [5000, 1000000]) {
31 const entry = make(n);
32 const got = encode(entry);
33 // treeEntryOf checks the mode and the oid only: the name's own refusals are the encoder's
34 if (got.ok) { if (fn !== 'treeEntryOf') wrong.push(`${fn} ${reason} n=${n}: encoded`); continue; }
35 assert.equal(got.error.code, 'bad_request');
36 if (held(got.error) > BOUND) wrong.push(`${fn} ${reason} n=${n}: the error holds a ${held(got.error)}-character string`);
37 if (typeof got.error.entry === 'string' && got.error.entry.length > QUOTE) wrong.push(`${fn} ${reason} n=${n}: entry field is ${got.error.entry.length} units`);
38 // the true length of whatever was cut is in the message
39 for (const text of [entry.name, entry.mode]) {
40 if (text.length > QUOTE && !got.error.message.includes(`(${text.length} UTF-16 units)`)) wrong.push(`${fn} ${reason} n=${n}: the message does not say the length ${text.length}: ${got.error.message.slice(0, 100)}`);
41 }
42 sizes.push(got.error.message.length);
43 }
44 // 5 000 and 1 000 000 differ by three digits in each reported length and by nothing else
45 if (sizes.length === 2 && sizes[1] - sizes[0] > 2 * 3) wrong.push(`${fn} ${reason}: the message grew ${sizes[0]} -> ${sizes[1]}`);
46 }
47 }
48 assert.deepEqual(wrong, []);
49});
51test('a short name is quoted whole, exactly as before; 200 units is not cut and 201 is; a duplicate is cut the same way', () => {
52 const short = encodeTree([{ mode: '100644', name: 'a/b', oid: BLOB }]);
53 assert.deepEqual([short.error.message, short.error.entry], ['tree: entry "a/b": full pathname', 'a/b']);
54 const at = 'n'.repeat(200);
55 const edge = encodeTree([{ mode: '644', name: at, oid: BLOB }]);
56 assert.deepEqual([edge.error.message, edge.error.entry], [`tree: entry "${at}": invalid mode "644"`, at]);
57 const over = encodeTree([{ mode: '644', name: `${at}Z`, oid: BLOB }]);
58 assert.deepEqual([over.error.message, over.error.entry], [`tree: entry "${at}"… (201 UTF-16 units): invalid mode "644"`, at]);
59 const name = 'd'.repeat(3000);
60 const dup = encodeTree([{ mode: '100644', name, oid: BLOB }, { mode: '40000', name, oid: BLOB }]);
61 assert.deepEqual([dup.error.message, dup.error.entry], [`tree: duplicate entry "${'d'.repeat(200)}"… (3000 UTF-16 units)`, 'd'.repeat(200)]);
62});
64test('the cut never ends on the first half of a surrogate pair: a well-formed name gives a well-formed message and `entry`', () => {
65 // By hand: 199 `a`, then U+1F600 (the pair d83d de00, units 199 and 200), then ten `b`: 211 units. A cut at 200 units
66 // ends on d83d alone: a string with no byte form, in an error about a name that has one. The cut stops before the
67 // pair, at 199 units, and the length said is still the name's own.
68 const head = 'a'.repeat(199);
69 const name = `${head}\u{1F600}${'b'.repeat(10)}`;
70 for (const [fn, encode] of [['encodeTree', e => encodeTree([e])], ['encodeEntries', e => encodeEntries([e])], ['treeEntryOf', treeEntryOf]]) {
71 const got = encode({ mode: '644', name, oid: BLOB });
72 assert.deepEqual([got.error.message, got.error.entry], [`tree: entry "${head}"… (211 UTF-16 units): invalid mode "644"`, head], fn);
73 }
74 // a pair that ends exactly at unit 200 is kept whole: 198 `a` and the pair are the 200 units quoted
75 const fits = `${'a'.repeat(198)}\u{1F600}`;
76 const kept = encodeTree([{ mode: '644', name: `${fits}bbb`, oid: BLOB }]);
77 assert.deepEqual([kept.error.message, kept.error.entry], [`tree: entry "${fits}"… (203 UTF-16 units): invalid mode "644"`, fits]);
78 // a mode is cut by the same rule
79 const mode = encodeTree([{ mode: `${'7'.repeat(199)}\u{1F600}7`, name: 'a', oid: BLOB }]);
80 assert.equal(mode.error.message, `tree: entry "a": invalid mode "${'7'.repeat(199)}"… (202 UTF-16 units)`);
81 // The edges of the high-surrogate range, d800 to dbff, at unit 199: U+10000 is d800 dc00 and U+10FFFF is dbff dfff
82 // (cut before them: 199 units). U+D7FF and U+E000 are ordinary characters, and a low surrogate with no high one
83 // before it is the caller's own ill-formed name: none is the first half of a pair, so 200 units are kept.
84 for (const [at, units] of [['\u{10000}', 199], ['\u{10FFFF}', 199], ['퟿b', 200], ['\udc00b', 200], ['b', 200]]) {
85 const got = encodeTree([{ mode: '644', name: `${head}${at}bbbb`, oid: BLOB }]);
86 assert.equal(got.error.entry, `${head}${at}`.slice(0, units), `U+${at.codePointAt(0).toString(16)}`);
87 assert.equal(got.error.entry.isWellFormed(), at.charCodeAt(0) !== 0xdc00, `U+${at.codePointAt(0).toString(16)}`);
88 }
89});
Diagnostics6 git calls · 74 ms
CallStatusSizeTime074 ms
discover200191 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
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 KiB · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=11990
git.response.cache=HIT
git.response.duration_ms=2
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 · 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=4347
git.response.cache=HIT
git.response.duration_ms=2
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 · 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=919
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=919
git.pkt.payload.max=892
git.sideband.band1.bytes=892
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=892
git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02005.6 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=66
git.response.bytes=5724
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=5724
git.pkt.payload.max=5697
git.sideband.band1.bytes=5697
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=5697
git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetch2002.3 KiB66 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2386
git.response.cache=MISS
git.response.duration_ms=66
git.fetch.wants=e8d93d0f97ced7bb5ef7ba079756d4749a5fe3e8
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=2386
git.pkt.payload.max=2359
git.sideband.band1.bytes=2359
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
a5e0f8303f03332c
Request
a666e65e…
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