main
1// followRedirects 'initial' (#113) against git's own client.
2//
3// Product, judged by `git -c http.followRedirects=… ls-remote` with GIT_TRACE_CURL (method, path, host, and whether
4// Authorization was sent) and by the refs ls-remote prints when both succeed:
5//
6// origin same (one listener; Location is a path) | cross (Location is another port)
7// auth the caller's Authorization | none
8// follow initial | off (git `false`, ply `never`)
9// kind keep (301 whose Location ends with /info/refs?service=git-upload-pack)
10// | drop (301 that drops the ?service= suffix)
11// | post (info/refs is 200, the POST is 307)
12// | dotdot (Location is ../new.git/info/refs?service=…, same-origin only: a relative URL has one origin)
13//
14// Two places git and ply are not the same request list, both checked rather than papered over:
15// - a Location that fails the suffix rule: git's curl requests it, then update_url_from_redirect dies; ply reports
16// the http error and does not request it.
17// - after a cross-origin rebase, git reapplies `http.extraHeader` on the POST it builds itself. ply drops the
18// caller's Authorization for every request to the new origin (a redirect must not move a credential). The
19// redirected GET matches git: curl already strips Authorization there.
20//
21// maxRedirects is ply's cap (git follows up to curl's 50). It is swept through 0, 1, the default 3, 4, 15, 16 and the
22// values refused past 16. Status codes sit on the 3xx boundary git actually answers: 299, 300, 301, 398, 399, 400.
23import http from 'node:http';
24import { test, beforeAll as before, afterAll as after } from 'vitest';
25import assert from 'node:assert/strict';
26import { join } from 'node:path';
27import { git, gitAsync, tmpRoot, makeBareRepo, commitFiles, startGitServer } from './server.js';
28import { GitHttp } from '../src/client.js';
29import { lsRefs as lsRefsR } from '../src/lsrefs.js';
30import { createTransport as createTransportR, baseFromRedirect, redirectPolicy as redirectPolicyR, streamRedirect } from '../src/http.js';
31import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
32import { ok } from '../src/result.js';
33import { forgetQuirks } from '../src/transport.js';
34import { lsRefsBody } from '../src/lsrefs.js';
35import { fail } from '../src/errors.js';
37import { unwrap, unwrapAsync } from './unwrap.js';
38// Throwing adapters over the Result API (0.2.0): the assertions below are about the throw.
39const lsRefs = (...a) => unwrapAsync(lsRefsR(...a));
40const createTransport = (...a) => unwrap(createTransportR(...a));
41const redirectPolicy = (...a) => unwrap(redirectPolicyR(...a));
42const TOK = 's3cret-token';
43const BASIC = `Basic ${Buffer.from(`x-access-token:${TOK}`).toString('base64')}`;
44const EVIL = `Basic ${Buffer.from('evil:s3cret').toString('base64')}`;
45const SERVICE = 'git-upload-pack';
46const TAIL = `/info/refs?service=${SERVICE}`;
47const ORIGINS = ['same', 'cross'];
48const AUTHS = [false, true];
49const FOLLOWS = ['initial', 'off'];
50const KINDS = ['keep', 'drop', 'post', 'dotdot'];
51const HOP_CAP = 16;
52const DEFAULT_HOPS = 3;
54let root, cleanup, srv, gate, gateHits, gatePost;
55before(async () => {
56 ({ root, cleanup } = tmpRoot());
57 const bare = makeBareRepo(root, 'new');
58 const work = join(root, 'work');
59 git(root, ['init', '-q', '-b', 'main', work]);
60 commitFiles(work, bare, { 'README.md': 'hello\n' });
61 srv = await startGitServer(root);
62 gateHits = [];
63 gatePost = null;
64 gate = await listen((req, res) => {
65 gateHits.push(hit(req));
66 if (req.method === 'POST' && gatePost !== null) { res.writeHead(gatePost.status, gatePost.headers); res.end(); return; }
67 proxy(req, res, srv.url, req.url);
68 });
69});
70after(async () => { await gate.close(); await srv.close(); cleanup(); });
72const hit = req => ({ method: req.method, path: req.url, host: req.headers.host, auth: req.headers.authorization ?? null });
73function listen(handler) {
74 const s = http.createServer(handler);
75 return new Promise(resolve => s.listen(0, '127.0.0.1', () => resolve({
76 url: `http://127.0.0.1:${s.address().port}`,
77 host: `127.0.0.1:${s.address().port}`,
78 close: () => new Promise(r => { s.closeAllConnections?.(); s.close(r); }),
79 })));
80}
81function proxy(req, res, upstream, path) {
82 const up = new URL(upstream);
83 const p = http.request({ hostname: up.hostname, port: up.port, path, method: req.method, headers: { ...req.headers, host: up.host } }, r => {
84 res.writeHead(r.statusCode, r.headers); r.pipe(res);
85 });
86 req.pipe(p);
87}
88const traceRequests = stderr => {
89 const reqs = [];
90 let cur = null;
91 for (const line of stderr.split('\n')) {
92 const send = /=> Send header: (GET|POST) (\S+) HTTP\/1\.[01]/.exec(line);
93 if (send) {
94 let path = send[2];
95 if (path.startsWith('http://') || path.startsWith('https://')) { const u = new URL(path); path = `${u.pathname}${u.search}`; }
96 cur = { method: send[1], path, host: '', auth: false };
97 reqs.push(cur);
98 continue;
99 }
100 const host = /=> Send header: Host: (\S+)/.exec(line);
101 if (host && cur && cur.host === '') cur.host = host[1];
102 if (/=> Send header: Authorization:/.test(line) && cur) cur.auth = true;
103 }
104 return reqs;
105};
106const clientView = (gitReqs, originHost) => gitReqs.map(r => ({ method: r.method, path: r.path, host: r.host, auth: r.auth && r.host === originHost }));
107const refLines = text => text.trim().split('\n').filter(Boolean).sort();
108const plyLines = refs => [...refs.values()].map(r => `${r.oid}\t${r.name}`).sort();
110async function gitLs(url, follow, auth) {
111 const config = [['protocol.version', '2'], ['http.followRedirects', follow === 'initial' ? 'initial' : 'false']];
112 if (auth) config.push(['http.extraHeader', `Authorization: ${BASIC}`]);
113 const r = await gitAsync(root, ['ls-remote', url], { GIT_TRACE_CURL: '1' }, { config });
114 return { status: r.status, stdout: r.stdout, stderr: r.stderr, reqs: traceRequests(r.stderr) };
115}
116function plyClient(url, { follow, auth, max, allowUrl, fetchImpl, exporter }) {
117 const tracer = new Tracer(exporter ? { exporter } : {});
118 const opts = { tracer, followRedirects: follow === 'off' ? 'never' : follow, allowUrl, fetchImpl };
119 if (max !== undefined) opts.maxRedirects = max;
120 if (auth) opts.auth = { token: TOK };
121 return unwrap(GitHttp.create(url, opts));
122}
123async function plyLs(url, spec) {
124 const c = plyClient(url, spec);
125 try { const r = await unwrapAsync(c.lsRefs()); return { ok: true, refs: r.refs, url: c.url, error: null }; }
126 catch (error) { return { ok: false, refs: null, url: c.url, error }; }
127}
129// What `kind` answers on `front`, proxying a kept repo through to git http-backend. `stolen` records a POST Location
130// that nobody should request. Cross-origin Locations point at `gate` (another port in front of the same backend).
131function frontHandler(kind, status, stolen) {
132 return (req, res) => {
133 const info = req.method === 'GET' && req.url.startsWith('/old.git/info/refs');
134 if (info && kind === 'keep') {
135 const loc = stolen.cross ? `${gate.url}/new.git${TAIL}` : `/new.git${TAIL}`;
136 res.writeHead(status, { location: loc }); res.end(); return;
137 }
138 if (info && kind === 'drop') {
139 const loc = stolen.cross ? `${gate.url}/new.git/info/refs` : '/new.git/info/refs';
140 res.writeHead(status, { location: loc }); res.end(); return;
141 }
142 if (info && kind === 'userinfo') {
143 res.writeHead(status, { location: `http://evil:s3cret@${stolen.frontHost}/new.git${TAIL}` }); res.end(); return;
144 }
145 if (info && kind === 'dotdot') {
146 res.writeHead(status, { location: `../new.git${TAIL}` }); res.end(); return;
147 }
148 if (req.method === 'POST' && kind === 'post' && req.url.startsWith('/old.git/')) {
149 res.writeHead(307, { location: `${stolen.url}/stolen.git/${SERVICE}` }); res.end(); return;
150 }
151 const path = req.url.replace(/^\/old\.git\/new\.git\//, '/new.git/').replace(/^\/renamed\.git\//, '/new.git/');
152 if (path.startsWith('/new.git/') || path.startsWith('/old.git/')) proxy(req, res, srv.url, path.startsWith('/old.git/') ? path.replace(/^\/old\.git\//, '/new.git/') : path);
153 else { res.writeHead(404); res.end('no such repo'); }
154 };
155}
157test('baseFromRedirect is the suffix rule; redirectPolicy refuses anything outside 0..16', () => {
158 const asked = `http://h/old.git${TAIL}`;
159 const keep = (href, want) => assert.equal(baseFromRedirect(asked, new URL(href), SERVICE), want, href);
160 keep(`http://h/new.git${TAIL}`, 'http://h/new.git');
161 keep(`http://user:pass@h/new.git${TAIL}`, 'http://h/new.git');
162 keep(`http://h/new.git/${TAIL}`, 'http://h/new.git');
163 keep(`http://h/old.git/new.git${TAIL}`, 'http://h/old.git/new.git');
164 assert.equal(baseFromRedirect(asked, new URL(`../new.git${TAIL}`, asked), SERVICE), 'http://h/old.git/new.git');
165 for (const href of [
166 `http://h/new.git/info/refs?service=git-receive-pack`,
167 `http://h/new.git/info/refs`,
168 `http://h/x${TAIL}&x=1`,
169 `http://h/new.git${TAIL}#frag`,
170 `http://h/info/refs?service=${SERVICE}/more`,
171 `ftp://h/new.git${TAIL}`,
172 ]) assert.equal(baseFromRedirect(asked, new URL(href), SERVICE), null, href);
173 assert.equal(redirectPolicy(undefined, undefined).follow, 'never');
174 assert.equal(redirectPolicy(undefined, undefined).max, DEFAULT_HOPS);
175 assert.equal(redirectPolicy('initial', 0).max, 0);
176 assert.equal(redirectPolicy('initial', 1).max, 1);
177 assert.equal(redirectPolicy('never', HOP_CAP - 1).max, HOP_CAP - 1);
178 assert.equal(redirectPolicy('initial', HOP_CAP).max, HOP_CAP);
179 assert.throws(() => redirectPolicy('always', 0), e => e.code === 'bad_request' && e.message === `followRedirects must be 'never' or 'initial', got "always"`);
180 assert.throws(() => redirectPolicy('', 0), e => e.message === `followRedirects must be 'never' or 'initial', got ""`);
181 assert.throws(() => redirectPolicy(1, 0), e => e.message === `followRedirects must be 'never' or 'initial', got 1`);
182 assert.throws(() => redirectPolicy(null, 0), e => e.message === `followRedirects must be 'never' or 'initial', got null`);
183 assert.throws(() => redirectPolicy({}, 0), e => e.message === `followRedirects must be 'never' or 'initial', got object`);
184 const capMsg = `maxRedirects must be an integer from 0 to ${HOP_CAP}`;
185 for (const bad of [-1, 1.5, NaN, 2 ** 53, HOP_CAP + 1, 64, null, '3']) {
186 assert.throws(() => createTransport('http://127.0.0.1:9/r.git', { followRedirects: 'never', maxRedirects: bad }), e => e.code === 'bad_request' && e.message === capMsg, `max ${String(bad)}`);
187 }
188 assert.throws(() => createTransport('http://127.0.0.1:9/r.git', { followRedirects: 'always' }), e => e.message === `followRedirects must be 'never' or 'initial', got "always"`);
189});
191test('git oracle: origin × auth × follow × kind', async () => {
192 const stolen = await listen((req, res) => { stolen.hits.push(hit(req)); res.writeHead(200); res.end('stolen'); });
193 stolen.hits = [];
194 try {
195 for (const origin of ORIGINS) for (const auth of AUTHS) for (const follow of FOLLOWS) for (const kind of KINDS) {
196 if (kind === 'dotdot' && origin === 'cross') continue;
197 const label = `${origin}/${auth ? 'auth' : 'anon'}/${follow}/${kind}`;
198 gateHits.length = 0;
199 gatePost = null;
200 const hits = [];
201 const cross = origin === 'cross';
202 const front = await listen((req, res) => { hits.push(hit(req)); frontHandler(kind, 301, { cross, url: stolen.url, frontHost: front.host })(req, res); });
203 try {
204 const git = await gitLs(`${front.url}/old.git`, follow, auth);
205 hits.length = 0; gateHits.length = 0;
206 const ply = follow === 'off' && kind !== 'post'
207 ? await (async () => { const c = plyClient(`${front.url}/old.git`, { follow, auth }); try { await unwrapAsync(c.discover()); return { ok: true, refs: null, url: c.url, error: null }; } catch (error) { return { ok: false, refs: null, url: c.url, error }; } })()
208 : await plyLs(`${front.url}/old.git`, { follow, auth });
209 const seen = hits.concat(kind === 'keep' && cross ? gateHits : []);
210 const plyView = seen.map(h => ({ method: h.method, path: h.path, host: h.host, auth: h.auth !== null }));
211 const gitView = clientView(git.reqs, front.host);
212 if (kind === 'drop' && follow === 'initial') {
213 assert.notEqual(git.status, 0, `${label}: git accepted a Location that drops the suffix`);
214 assert.ok(git.reqs.some(r => r.path === '/new.git/info/refs' || r.path.startsWith('/new.git/info/refs')), `${label}: git did not request the bad Location (${git.reqs.map(r => r.path).join(' ')})`);
215 assert.equal(ply.ok, false, label);
216 assert.equal(ply.error.code, 'http', `${label}: ${ply.error?.code} ${ply.error?.message}`);
217 assert.equal(ply.error.status, 301);
218 assert.ok(!plyView.some(r => r.path.startsWith('/new.git')), `${label}: ply requested the bad Location`);
219 assert.deepEqual(plyView.map(r => r.path), [`/old.git${TAIL}`], label);
220 } else if (kind === 'post') {
221 assert.notEqual(git.status, 0, `${label}: git followed a POST redirect`);
222 assert.equal(ply.ok, false, label);
223 assert.equal(ply.error.code, 'http');
224 assert.equal(ply.error.status, 307);
225 assert.equal(stolen.hits.length, 0, `${label}: the POST Location was requested`);
226 assert.ok(!git.reqs.some(r => r.path.includes('stolen')), label);
227 // ply's readers POST first. 'off' stays that way (no inserted GET); 'initial' does git's GET before the POST.
228 const plyPaths = follow === 'off' ? [`POST /old.git/${SERVICE}`] : [`GET /old.git${TAIL}`, `POST /old.git/${SERVICE}`];
229 assert.deepEqual(plyView.map(r => `${r.method} ${r.path}`), plyPaths, label);
230 if (follow === 'initial') {
231 assert.deepEqual(plyView, gitView, `${label}\ngit ${JSON.stringify(git.reqs)}\nply ${JSON.stringify(plyView)}`);
232 assert.match(ply.error.message, new RegExp(`^HTTP 307 ${SERVICE}: redirected to ${stolen.url}/stolen.git/${SERVICE}; ply does not follow redirects`));
233 }
234 } else {
235 assert.equal(ply.ok, git.status === 0, `${label}: ply ${ply.ok} (${ply.error?.code} ${ply.error?.message}) git ${git.status} ${git.stderr.split('\n').filter(l => /fatal:|error:|warning:/.test(l)).slice(-1)[0] ?? ''}`);
236 assert.deepEqual(plyView, gitView, `${label}\ngit ${JSON.stringify(git.reqs)}\nply ${JSON.stringify(seen)}`);
237 if (git.status === 0) {
238 assert.deepEqual(plyLines(ply.refs), refLines(git.stdout), label);
239 const reb = /redirecting to (\S+)/.exec(git.stderr);
240 assert.ok(reb, `${label}: git printed no redirect`);
241 assert.equal(ply.url, reb[1].replace(/\/$/, ''), label);
242 if (cross && auth) {
243 const post = git.reqs.find(r => r.method === 'POST');
244 assert.equal(post.auth, true, `${label}: git did not reapply Authorization on the cross-origin POST`);
245 assert.notEqual(post.host, front.host);
246 assert.ok(plyView.some(r => r.host !== front.host && r.auth === false), label);
247 }
248 } else {
249 assert.equal(ply.error.code, 'http', label);
250 assert.equal(hits.length, 1, `${label}: a refused redirect still left the front`);
251 assert.equal(gateHits.length, 0, label);
252 }
253 }
254 } finally { await front.close(); }
255 }
256 } finally { await stolen.close(); }
257});
259test('git oracle: the 3xx boundary, and a Location that tries to install userinfo', async () => {
260 const codes = [299, 300, 301, 398, 399, 400];
261 for (const status of codes) {
262 gateHits.length = 0;
263 const hits = [];
264 const front = await listen((req, res) => { hits.push(hit(req)); frontHandler('keep', status, { cross: false, url: '', frontHost: front.host })(req, res); });
265 try {
266 const git = await gitLs(`${front.url}/old.git`, 'initial', true);
267 hits.length = 0;
268 const ply = await plyLs(`${front.url}/old.git`, { follow: 'initial', auth: true });
269 const plyView = hits.map(h => ({ method: h.method, path: h.path, host: h.host, auth: h.auth !== null }));
270 // 299 and 400 are outside the 3xx range. git may fall through to dumb HTTP; ply must not follow them.
271 if (status >= 300 && status < 400) {
272 assert.equal(ply.ok, git.status === 0, `HTTP ${status}: ply ${ply.ok} ${ply.error?.message ?? ''} git ${git.status}`);
273 assert.deepEqual(plyView, clientView(git.reqs, front.host), `HTTP ${status}`);
274 if (git.status === 0) assert.deepEqual(plyLines(ply.refs), refLines(git.stdout), `HTTP ${status}`);
275 } else {
276 assert.equal(ply.ok, false, `HTTP ${status} was followed`);
277 assert.ok(!String(ply.error.message).includes('redirected'), ply.error.message);
278 assert.ok(!plyView.some(r => r.path.startsWith('/new.git')), `HTTP ${status}: ${JSON.stringify(plyView)}`);
279 }
280 } finally { await front.close(); }
281 }
282 const hits = [];
283 const front = await listen((req, res) => { hits.push(hit(req)); frontHandler('userinfo', 301, { cross: false, url: '', frontHost: front.host })(req, res); });
284 try {
285 const git = await gitLs(`${front.url}/old.git`, 'initial', true);
286 hits.length = 0;
287 const ply = await plyLs(`${front.url}/old.git`, { follow: 'initial', auth: true });
288 assert.equal(git.status, 0, git.stderr);
289 assert.equal(ply.ok, true, ply.error?.message);
290 assert.deepEqual(plyLines(ply.refs), refLines(git.stdout));
291 assert.ok(hits.every(h => h.auth === null || h.auth === BASIC), JSON.stringify(hits));
292 assert.ok(!hits.some(h => h.auth === EVIL || h.path.includes('evil') || h.path.includes('s3cret')));
293 assert.equal(ply.url, `${front.url}/new.git`);
294 const reb = /redirecting to (\S+)/.exec(git.stderr);
295 assert.equal(ply.url, reb[1].replace(/\/$/, ''));
296 assert.ok(!reb[1].includes('evil'), reb[1]);
297 } finally { await front.close(); }
298});
300test('maxRedirects: 0, 1, default 3, 4, 15, 16 follow that many and refuse the next', async () => {
301 // `redirects` 3xx responses, the last of them naming the git backend. maxRedirects `max` (undefined → the default).
302 const redirectsOf = async (redirects, max) => {
303 gateHits.length = 0;
304 const hits = [];
305 const front = await listen((req, res) => {
306 hits.push(req.url.split('?')[0]);
307 const m = /^\/r(\d+)\.git\/info\/refs/.exec(req.url);
308 if (!m) { res.writeHead(404); res.end(req.url); return; }
309 const i = Number(m[1]);
310 if (i >= redirects) { proxy(req, res, srv.url, req.url.replace(/^\/r\d+\.git\//, '/new.git/')); return; }
311 const loc = i + 1 === redirects ? `${gate.url}/new.git${TAIL}` : `/r${i + 1}.git${TAIL}`;
312 res.writeHead(301, { location: loc }); res.end();
313 });
314 try {
315 const spec = { follow: 'initial', auth: false };
316 if (max !== undefined) spec.max = max;
317 const ply = await plyLs(`${front.url}/r0.git`, spec);
318 return { ply, hops: hits.length, gate: gateHits.length };
319 } finally { await front.close(); }
320 };
321 const direct = await gitLs(`${gate.url}/new.git`, 'off', false);
322 assert.equal(direct.status, 0, direct.stderr);
323 gateHits.length = 0;
324 const none = await plyLs(`${gate.url}/new.git`, { follow: 'initial', auth: false, max: 0 });
325 assert.equal(none.ok, true, none.error?.message);
326 assert.equal(none.url, `${gate.url}/new.git`);
327 assert.deepEqual(plyLines(none.refs), refLines(direct.stdout));
328 const blocked = await redirectsOf(1, 0);
329 assert.equal(blocked.ply.ok, false, 'maxRedirects 0 followed a redirect');
330 assert.equal(blocked.ply.error.status, 301);
331 assert.equal(blocked.hops, 1);
332 assert.equal(blocked.gate, 0);
333 for (const n of [1, 2, DEFAULT_HOPS, DEFAULT_HOPS + 1, HOP_CAP - 1, HOP_CAP]) {
334 const cap = n === DEFAULT_HOPS ? undefined : n;
335 const followed = await redirectsOf(n, cap);
336 assert.equal(followed.ply.ok, true, `follow ${n}: ${followed.ply.error?.message}`);
337 assert.equal(followed.hops, n, `follow ${n}: front saw ${followed.hops}`);
338 assert.ok(followed.gate >= 1, `follow ${n}: the final host saw nothing`);
339 assert.deepEqual(plyLines(followed.ply.refs), refLines(direct.stdout), `follow ${n}`);
340 assert.equal(followed.ply.url, `${gate.url}/new.git`);
341 const refused = await redirectsOf(n + 1, cap);
342 assert.equal(refused.ply.ok, false, `refuse after ${n}`);
343 assert.equal(refused.ply.error.code, 'http');
344 assert.equal(refused.ply.error.status, 301);
345 assert.equal(refused.hops, n + 1, `refuse after ${n}: requested the refused hop's URL, or skipped one`);
346 assert.equal(refused.gate, 0, `refuse after ${n}: the next Location was requested`);
347 }
348});
350test('a two-hop chain matches git, and the span records the original base once', async () => {
351 gateHits.length = 0;
352 const hits = [];
353 const front = await listen((req, res) => {
354 hits.push(hit(req));
355 if (req.url.startsWith('/old.git/info/refs')) { res.writeHead(301, { location: `/mid.git${TAIL}` }); res.end(); return; }
356 if (req.url.startsWith('/mid.git/info/refs')) { res.writeHead(302, { location: `${gate.url}/new.git${TAIL}` }); res.end(); return; }
357 proxy(req, res, srv.url, req.url);
358 });
359 try {
360 const ex = arrayExporter();
361 const git = await gitLs(`${front.url}/old.git`, 'initial', true);
362 hits.length = 0; gateHits.length = 0;
363 const c = plyClient(`${front.url}/old.git`, { follow: 'initial', auth: true, exporter: ex });
364 const refs = await unwrapAsync(c.lsRefs());
365 assert.equal(git.status, 0, git.stderr);
366 assert.deepEqual(plyLines(refs.refs), refLines(git.stdout));
367 const seen = hits.concat(gateHits).map(h => ({ method: h.method, path: h.path, host: h.host, auth: h.auth !== null }));
368 assert.deepEqual(seen, clientView(git.reqs, front.host));
369 assert.equal(c.url, `${gate.url}/new.git`);
370 const span = ex.lines.flatMap(flattenLine).find(s => s.name === 'git.upload-pack ls-refs');
371 assert.equal(span.attrs['http.redirect.count'], 2);
372 assert.equal(span.attrs['git.remote.rebased_from'], `${front.url}/old.git`);
373 gateHits.length = 0;
374 hits.length = 0;
375 await unwrapAsync(c.lsRefs());
376 assert.deepEqual(hits, [], 'a settled transport advertised at the original host again');
377 assert.deepEqual(gateHits.map(h => `${h.method} ${h.path.split('?')[0]}`), [`POST /new.git/${SERVICE}`]);
378 } finally { await front.close(); }
379});
381test('discover then lsRefs advertises once; the default never follows and never inserts a GET', async () => {
382 const hits = [];
383 const front = await listen((req, res) => {
384 hits.push(`${req.method} ${req.url.split('?')[0]}`);
385 if (req.method === 'GET' && req.url.startsWith('/old.git/')) { res.writeHead(301, { location: `/new.git${TAIL}` }); res.end(); return; }
386 proxy(req, res, srv.url, req.url.replace(/^\/(?:old|new)\.git\//, '/new.git/'));
387 });
388 try {
389 const c = plyClient(`${front.url}/old.git`, { follow: 'initial', auth: false });
390 await unwrapAsync(c.discover());
391 const afterDiscover = hits.slice();
392 await unwrapAsync(c.lsRefs());
393 assert.deepEqual(afterDiscover, ['GET /old.git/info/refs', 'GET /new.git/info/refs']);
394 assert.deepEqual(hits, [...afterDiscover, 'POST /new.git/git-upload-pack']);
395 assert.equal(c.url, `${front.url}/new.git`);
396 const plain = [];
397 const once = await listen((req, res) => { plain.push(`${req.method} ${req.url}`); res.writeHead(301, { location: req.url.replace('/old.git/', '/new.git/') }); res.end(); });
398 try {
399 const err = await plyLs(`${once.url}/old.git`, { follow: 'off', auth: true });
400 assert.equal(err.ok, false);
401 assert.equal(err.error.message, `HTTP 301 ${SERVICE}: redirected to ${once.url}/new.git/${SERVICE}; ply does not follow redirects (update the remote URL)`);
402 assert.equal(err.error.location, `${once.url}/new.git/${SERVICE}`);
403 assert.deepEqual(plain, [`POST /old.git/${SERVICE}`]);
404 const ex = arrayExporter();
405 const d = plyClient(`${once.url}/old.git`, { follow: 'off', auth: false, exporter: ex });
406 await unwrapAsync(d.discover()).catch(() => {});
407 const attrs = ex.lines.flatMap(flattenLine).flatMap(s => Object.keys(s.attrs));
408 assert.ok(!attrs.includes('http.redirect.count') && !attrs.includes('git.remote.rebased_from'));
409 } finally { await once.close(); }
410 } finally { await front.close(); }
411});
413test('discover and lsRefs side by side on a fresh transport: one advertisement GET (chain), the POST waits for it', async () => {
414 const hits = [];
415 const front = await listen((req, res) => {
416 hits.push(`${req.method} ${req.url.split('?')[0]}`);
417 if (req.method === 'GET' && req.url.startsWith('/old.git/')) { res.writeHead(301, { location: `/new.git${TAIL}` }); res.end(); return; }
418 proxy(req, res, srv.url, req.url.replace(/^\/(?:old|new)\.git\//, '/new.git/'));
419 });
420 try {
421 const c = plyClient(`${front.url}/old.git`, { follow: 'initial', auth: false });
422 const [d, r] = await Promise.all([unwrapAsync(c.discover()), unwrapAsync(c.lsRefs())]);
423 assert.ok(d && r.refs.has('refs/heads/main'));
424 // Without the shared GET each caller settled the exchange on its own: 4 GETs, then the POST.
425 assert.deepEqual(hits, ['GET /old.git/info/refs', 'GET /new.git/info/refs', `POST /new.git/${SERVICE}`]);
426 assert.equal(c.url, `${front.url}/new.git`);
427 // A shared GET that fails (network) does not settle the exchange: the waiting POST makes its own GET.
428 hits.length = 0;
429 forgetQuirks(`${front.url}/old.git`);
430 let failed = false;
431 const fetchImpl = async (url, init) => {
432 if (!failed && (init?.method ?? 'GET') === 'GET') { failed = true; throw new TypeError('fetch failed'); }
433 return fetch(url, init);
434 };
435 const e = plyClient(`${front.url}/old.git`, { follow: 'initial', auth: false, fetchImpl });
436 const [bad, good] = await Promise.all([e.discover(), unwrapAsync(e.lsRefs())]);
437 assert.equal(bad.ok, false);
438 assert.equal(bad.error.code, 'network');
439 assert.ok(good.refs.has('refs/heads/main'));
440 assert.deepEqual(hits, ['GET /old.git/info/refs', 'GET /new.git/info/refs', `POST /new.git/${SERVICE}`]);
441 } finally { await front.close(); }
442});
444test('allowUrl sees the base and each Location; its URL is the one requested; a refusal is never fetched', async () => {
445 assert.throws(() => createTransport(`${gate.url}/new.git`, { allowUrl: () => null }), e => e.code === 'bad_request' && e.message === 'allowUrl must return a Result');
446 assert.throws(() => createTransport(`${gate.url}/new.git`, { allowUrl: () => ({ ok: true, value: 'http://h/r.git' }) }), e => e.message === 'allowUrl must return an http(s) URL');
447 const moved = [];
448 const quiet = await listen((req, res) => { moved.push(`fetch ${req.url}`); res.writeHead(500); res.end(); });
449 try {
450 gateHits.length = 0;
451 const t = createTransport(`${quiet.url}/old.git`, {
452 tracer: new Tracer(), auth: { token: TOK }, followRedirects: 'initial',
453 allowUrl(url) { moved.push(`allow ${url.href}`); return ok(new URL(`${gate.url}/new.git`)); },
454 });
455 assert.equal(t.url, `${gate.url}/new.git`);
456 const movedRefs = await lsRefs(t);
457 assert.ok(movedRefs.refs.has('refs/heads/main'));
458 assert.deepEqual(moved, [`allow ${quiet.url}/old.git`]);
459 assert.ok(gateHits.length >= 2 && gateHits.every(h => h.auth === null), JSON.stringify(gateHits));
460 } finally { await quiet.close(); }
461 const calls = [];
462 const hits = [];
463 const front = await listen((req, res) => {
464 hits.push({ path: req.url.split('?')[0], auth: req.headers.authorization ?? null });
465 if (req.method === 'GET' && req.url.startsWith('/old.git/info/refs')) { res.writeHead(301, { location: `/new.git${TAIL}` }); res.end(); return; }
466 proxy(req, res, srv.url, req.url.replace(/^\/renamed\.git\//, '/new.git/'));
467 });
468 try {
469 const ply = await plyLs(`${front.url}/old.git`, {
470 follow: 'initial', auth: true,
471 allowUrl(url) {
472 calls.push(url.href);
473 if (url.pathname === '/new.git/info/refs') return ok(new URL(`${front.url}/renamed.git${TAIL}`));
474 return ok(url);
475 },
476 });
477 assert.equal(ply.ok, true, ply.error?.message);
478 assert.equal(ply.url, `${front.url}/renamed.git`);
479 assert.deepEqual(calls, [`${front.url}/old.git`, `${front.url}/new.git${TAIL}`]);
480 assert.deepEqual(hits.map(h => h.path), ['/old.git/info/refs', '/renamed.git/info/refs', `/renamed.git/${SERVICE}`]);
481 assert.ok(hits.every(h => h.auth === BASIC));
482 calls.length = 0;
483 hits.length = 0;
484 const denied = await plyLs(`${front.url}/old.git`, {
485 follow: 'initial', auth: true,
486 allowUrl(url) { calls.push(url.pathname); return url.pathname.startsWith('/new.git') ? fail('auth', 'host denied') : ok(url); },
487 });
488 assert.equal(denied.ok, false);
489 assert.equal(denied.error.code, 'auth');
490 assert.equal(denied.error.message, 'host denied');
491 assert.deepEqual(hits.map(h => h.path), ['/old.git/info/refs']);
492 hits.length = 0;
493 const ftp = await plyLs(`${front.url}/old.git`, {
494 follow: 'initial', auth: false,
495 allowUrl(url) { return url.pathname.startsWith('/new.git') ? ok(new URL(`ftp://h/new.git${TAIL}`)) : ok(url); },
496 });
497 assert.equal(ftp.error.code, 'bad_request');
498 assert.equal(ftp.error.message, 'allowUrl must return an http(s) URL');
499 assert.deepEqual(hits.map(h => h.path), ['/old.git/info/refs']);
500 } finally { await front.close(); }
501});
503test('a network failure before a response does not settle the initial exchange', async () => {
504 let failed = false;
505 const hits = [];
506 const front = await listen((req, res) => {
507 hits.push(`${req.method} ${req.url.split('?')[0]}`);
508 if (req.method === 'GET' && req.url.startsWith('/old.git/')) { res.writeHead(301, { location: `/new.git${TAIL}` }); res.end(); return; }
509 proxy(req, res, srv.url, req.url.replace(/^\/new\.git\//, '/new.git/'));
510 });
511 try {
512 const fetchImpl = async (url, init) => {
513 if (!failed) { failed = true; throw new TypeError('fetch failed'); }
514 return fetch(url, init);
515 };
516 const c = plyClient(`${front.url}/old.git`, { follow: 'initial', auth: false, fetchImpl });
517 const first = await unwrapAsync(c.lsRefs()).catch(e => e);
518 assert.equal(first.code, 'network');
519 assert.deepEqual(hits, []);
520 const refs = await unwrapAsync(c.lsRefs());
521 assert.ok(refs.refs.has('refs/heads/main'));
522 assert.deepEqual(hits, ['GET /old.git/info/refs', 'GET /new.git/info/refs', `POST /new.git/${SERVICE}`]);
523 assert.equal(c.url, `${front.url}/new.git`);
524 } finally { await front.close(); }
525});
527// The redirect memo (ply-web#117). `moves` maps a repo name to where a GET of its info/refs (and, with `all`, every
528// request) is sent with a 301; anything that reaches a name in `serve` is proxied to new.git. `hits` is every request.
529async function movingHost() {
530 const hits = [];
531 const moves = new Map();
532 const host = await listen((req, res) => {
533 const path = req.url.split('?')[0];
534 hits.push({ line: `${req.method} ${path}`, auth: req.headers.authorization ?? null });
535 const name = /^\/([^/]+)\.git\//.exec(req.url)?.[1];
536 const move = moves.get(name);
537 if (move && (move.all || (req.method === 'GET' && path.endsWith('/info/refs')))) {
538 req.resume();
539 res.writeHead(301, { location: req.url.replace(`/${name}.git/`, `${move.to}/`) }); res.end(); return;
540 }
541 proxy(req, res, srv.url, req.url.replace(/^\/[^/]+\.git\//, '/new.git/'));
542 });
543 return { ...host, hits, moves, lines: () => hits.map(h => h.line) };
544}
545const POST = `POST /new.git/${SERVICE}`;
547test('memo: a second transport for the remote makes no GET, redirected or not; never, maxRedirects and forgetQuirks are respected', async () => {
548 const h = await movingHost();
549 try {
550 h.moves.set('old', { to: '/new.git', all: false });
551 const first = plyClient(`${h.url}/old.git`, { follow: 'initial', auth: true });
552 await unwrapAsync(first.lsRefs());
553 assert.deepEqual(h.lines(), ['GET /old.git/info/refs', 'GET /new.git/info/refs', POST]);
554 h.hits.length = 0;
555 const ex = arrayExporter();
556 const second = plyClient(`${h.url}/old.git`, { follow: 'initial', auth: true, exporter: ex });
557 const refs = await unwrapAsync(second.lsRefs());
558 assert.ok(refs.refs.has('refs/heads/main'));
559 assert.deepEqual(h.lines(), [POST], 'a memo hit asks nothing of the old base and makes no GET');
560 assert.equal(second.url, `${h.url}/new.git`);
561 assert.ok(h.hits.every(x => x.auth === BASIC), 'same origin: the credential is kept');
562 const span = ex.lines.flatMap(flattenLine).find(x => x.attrs['http.redirect.count'] !== undefined);
563 assert.equal(span.attrs['http.redirect.count'], 1);
564 assert.equal(span.attrs['git.remote.rebased_from'], `${h.url}/old.git`);
566 // a remote that does not redirect is remembered too: one GET for the first transport, none after
567 h.hits.length = 0;
568 await unwrapAsync(plyClient(`${h.url}/new.git`, { follow: 'initial', auth: false }).lsRefs());
569 await unwrapAsync(plyClient(`${h.url}/new.git`, { follow: 'initial', auth: false }).lsRefs());
570 assert.deepEqual(h.lines(), ['GET /new.git/info/refs', POST, POST]);
572 // `never` does not use the memo, and a memo longer than maxRedirects is not replayed
573 h.hits.length = 0;
574 const never = await plyLs(`${h.url}/old.git`, { follow: 'off', auth: false });
575 assert.equal(never.ok, true);
576 assert.deepEqual(h.lines(), [`POST /old.git/${SERVICE}`]);
577 h.hits.length = 0;
578 const capped = await plyLs(`${h.url}/old.git`, { follow: 'initial', auth: false, max: 0 });
579 assert.equal(capped.ok, false);
580 assert.equal(capped.error.code, 'http');
581 assert.deepEqual(h.lines(), ['GET /old.git/info/refs']);
583 // forgetQuirks(url) forgets it: the next transport asks again
584 forgetQuirks(`${h.url}/old.git`);
585 h.hits.length = 0;
586 await unwrapAsync(plyClient(`${h.url}/old.git`, { follow: 'initial', auth: false }).lsRefs());
587 assert.deepEqual(h.lines(), ['GET /old.git/info/refs', 'GET /new.git/info/refs', POST]);
588 } finally { await h.close(); }
589});
591test('memo: a cross-origin rebase replayed from the memo still drops the credential', async () => {
592 const h = await movingHost();
593 try {
594 h.moves.set('old', { to: `${gate.url}/new.git`, all: false });
595 await unwrapAsync(plyClient(`${h.url}/old.git`, { follow: 'initial', auth: true }).lsRefs());
596 h.hits.length = 0;
597 gateHits.length = 0;
598 const second = plyClient(`${h.url}/old.git`, { follow: 'initial', auth: true });
599 await unwrapAsync(second.lsRefs());
600 assert.equal(second.url, `${gate.url}/new.git`);
601 assert.deepEqual(h.hits, [], 'nothing asked of the old origin');
602 assert.deepEqual(gateHits.map(x => [x.method, x.path.split('?')[0], x.auth]), [['POST', `/new.git/${SERVICE}`, null]]);
603 } finally { await h.close(); }
604});
606test('memo: a stale memo answered with a 3xx is dropped; bytes are re-sent once after the GET; a stream gets the error', async () => {
607 const h = await movingHost();
608 try {
609 h.moves.set('old', { to: '/a.git', all: false });
610 await unwrapAsync(plyClient(`${h.url}/old.git`, { follow: 'initial', auth: false }).lsRefs());
611 // the repo moves again: a.git now redirects everything to b.git, and old.git's info/refs points at b.git
612 h.moves.set('old', { to: '/b.git', all: false });
613 h.moves.set('a', { to: '/b.git', all: true });
614 h.hits.length = 0;
615 const stale = plyClient(`${h.url}/old.git`, { follow: 'initial', auth: false });
616 const refs = await unwrapAsync(stale.lsRefs());
617 assert.ok(refs.refs.has('refs/heads/main'));
618 assert.deepEqual(h.lines(), [`POST /a.git/${SERVICE}`, 'GET /old.git/info/refs', 'GET /b.git/info/refs', `POST /b.git/${SERVICE}`]);
619 assert.equal(stale.url, `${h.url}/b.git`);
620 h.hits.length = 0;
621 await unwrapAsync(plyClient(`${h.url}/old.git`, { follow: 'initial', auth: false }).lsRefs());
622 assert.deepEqual(h.lines(), [`POST /b.git/${SERVICE}`], 'the memo now says b.git');
624 // a stale memo met on a GET (discover) starts over too
625 h.moves.set('b', { to: '/c.git', all: true });
626 h.moves.set('old', { to: '/c.git', all: false });
627 h.hits.length = 0;
628 const d = plyClient(`${h.url}/old.git`, { follow: 'initial', auth: false });
629 await unwrapAsync(d.discover());
630 assert.deepEqual(h.lines(), ['GET /b.git/info/refs', 'GET /old.git/info/refs', 'GET /c.git/info/refs']);
631 assert.equal(d.url, `${h.url}/c.git`);
633 // a streamed body is sent once: the stale memo's 3xx is the redirect error, and the memo is dropped
634 h.moves.set('c', { to: '/d.git', all: true });
635 h.hits.length = 0;
636 const t = createTransport(`${h.url}/old.git`, { tracer: new Tracer(), followRedirects: 'initial' });
637 const bytes = unwrap(lsRefsBody([]));
638 const stream = new ReadableStream({ start(c) { c.enqueue(bytes); c.close(); } });
639 const span = new Tracer().start('t');
640 const failed = await (async () => { for await (const _ of t.frames(SERVICE, { stream, length: bytes.length }, span)) { /* drain */ } })().catch(e => e);
641 // undici sends a stream with redirect 'error', so there the 3xx is a network failure; workerd's 'manual' gives the 301
642 if (streamRedirect() === 'error') assert.equal(failed.code, 'network');
643 else { assert.equal(failed.code, 'http'); assert.equal(failed.status, 301); assert.equal(failed.location, `${h.url}/d.git/${SERVICE}`); }
644 assert.deepEqual(h.lines(), [`POST /c.git/${SERVICE}`], 'the stream was not re-sent');
645 h.moves.set('old', { to: '/new.git', all: false });
646 h.hits.length = 0;
647 await unwrapAsync(plyClient(`${h.url}/old.git`, { follow: 'initial', auth: false }).lsRefs());
648 assert.deepEqual(h.lines(), ['GET /old.git/info/refs', 'GET /new.git/info/refs', POST], 'the stale memo was dropped');
649 } finally { await h.close(); }
650});
652test('memo: each Location is approved again by the new transport\'s allowUrl; a refusal there asks the network', async () => {
653 const h = await movingHost();
654 try {
655 h.moves.set('old', { to: '/new.git', all: false });
656 await unwrapAsync(plyClient(`${h.url}/old.git`, { follow: 'initial', auth: false }).lsRefs());
657 const calls = [];
658 h.hits.length = 0;
659 const denied = await plyLs(`${h.url}/old.git`, {
660 follow: 'initial', auth: false,
661 allowUrl(url) { calls.push(url.href); return url.pathname.startsWith('/new.git') ? fail('auth', 'host denied') : ok(url); },
662 });
663 assert.equal(denied.ok, false);
664 assert.equal(denied.error.message, 'host denied');
665 assert.deepEqual(calls, [`${h.url}/old.git`, `${h.url}/new.git${TAIL}`, `${h.url}/new.git${TAIL}`], 'the memo replay asked allowUrl once, then the network did');
666 assert.deepEqual(h.lines(), ['GET /old.git/info/refs'], 'nothing past the refusal was fetched');
667 // an allowUrl that rewrites the Location: the replay takes its answer, with no request
668 h.hits.length = 0;
669 const renamed = await plyLs(`${h.url}/old.git`, {
670 follow: 'initial', auth: false,
671 allowUrl(url) { return url.pathname === '/new.git/info/refs' ? ok(new URL(`${h.url}/renamed.git${TAIL}`)) : ok(url); },
672 });
673 assert.equal(renamed.ok, true, renamed.error?.message);
674 assert.equal(renamed.url, `${h.url}/renamed.git`);
675 assert.deepEqual(h.lines(), [`POST /renamed.git/${SERVICE}`]);
676 } finally { await h.close(); }
677});
Diagnostics4 git calls · 24 ms
CallStatusSizeTime024 ms
discover200152 B2 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 · 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=145 git.response.cache=HIT git.response.duration_ms=2 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=19 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
fetch2009.2 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=9408 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=19bf3357c23c402b79244446f8f4186a719b229c 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=9408 git.pkt.payload.max=9387 git.sideband.band1.bytes=9386 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
- a5e2c011ac5589da
- Request
- 4e7b9c89…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format