main
ply4 git calls298 ms43 KiB2 cachedprotocol v2
130 lines7.0 KiBRaw
1// The only fetch the viewer uses. It cannot carry credentials: forbidden headers are dropped, the
2// User-Agent is forced, redirects are never followed here (ply follows the first GET info/refs, git's rule), and
3// every request and every Location must stay on the same git smart-HTTP endpoint. A Request with Authorization cannot be passed through this type.
4import { plyError } from 'ply';
5import type { PlyError } from '../ply-result.ts';
6import { atIndex, chainGet } from '../absent.ts';
7import { unwrapOr } from '../option.ts';
8import type { Budget } from './budget.ts';
9import { VIEWER_UA } from './limits.ts';
10import { extractRequestPath, rawPathHasUnsafeSegment } from '../pathguard.ts';
11import { assertUpstreamUrl, type AllowedHost, type ValidatedPublicRepoUrl } from './url.ts';
13declare const credentialFree: unique symbol;
14export type CredentialFreeHeaders = Headers & { readonly [credentialFree]: 'CredentialFreeHeaders' };
16const ALLOWED = new Set(['content-type', 'accept', 'git-protocol', 'x-ply-cache']);
18export function credentialFreeHeaders(source: HeadersInit | null): CredentialFreeHeaders {
19 const incoming = source === null ? new Headers() : new Headers(source);
20 const out = new Headers();
21 for (const [key, value] of incoming) {
22 if (ALLOWED.has(key.toLowerCase())) out.set(key, value);
23 }
24 out.set('user-agent', VIEWER_UA);
25 return out as CredentialFreeHeaders;
28export type ViewerUpstreamInit = {
29 readonly credential: 'absent';
30 method: string;
31 headers: CredentialFreeHeaders;
32 body: Uint8Array | null;
33 signal: AbortSignal | null;
34 redirect: 'manual';
35};
37export type FetchLike = typeof globalThis.fetch;
39function limitBody(res: Response, budget: Budget): Response {
40 if (!res.body) return res;
41 const reader = res.body.getReader();
42 const stream = new ReadableStream<Uint8Array>({
43 async pull(controller) {
44 try {
45 const step = await reader.read();
46 if (step.done) { controller.close(); return; }
47 budget.addBytes(step.value.byteLength);
48 controller.enqueue(step.value);
49 } catch (err) {
50 await reader.cancel(err).catch(() => {});
51 controller.error(err);
52 }
53 },
54 cancel(reason) { return reader.cancel(reason); },
55 });
56 return new Response(stream, { status: res.status, statusText: res.statusText, headers: res.headers });
59// A 3xx Location the viewer will let ply see: no controls, no encoded dot or slash, no unsafe segment, and once resolved
60// still this repo's smart-HTTP endpoint (assertUpstreamUrl). Anything else throws the bad_request it always has.
61function vetLocation(location: string, current: string, repo: ValidatedPublicRepoUrl, allowed: readonly AllowedHost[]): void {
62 // oxlint-disable-next-line no-control-regex -- controls in a Location are not a URL
63 if (/[\u0000-\u001f\u007f]/.test(location) || location.includes('\\') || /%(2e|2f|5c)/i.test(location)) throw plyError('bad_request', 'redirect host is not an allowlisted host');
64 const locationPath = unwrapOr(atIndex(location.split(/[?#]/), 0), '');
65 if (rawPathHasUnsafeSegment(locationPath.startsWith('/') || /^[a-z][a-z0-9+.-]*:/i.test(location) ? extractRequestPath(location.startsWith('/') ? `https://reject.invalid${locationPath}` : location) : locationPath)) {
66 throw plyError('bad_request', 'redirect host is not an allowlisted host');
67 }
68 let next: string;
69 try { next = new URL(location, current).href; } catch { throw plyError('bad_request', 'redirect host is not an allowlisted host'); }
70 assertUpstreamUrl(next, repo, allowed);
73type Vetted = { readonly ok: true; readonly value: URL } | { readonly ok: false; readonly error: PlyError };
75/**
76 * ply's `allowUrl` for the viewer's transport (followRedirects 'initial'). ply calls it with the repo URL before any
77 * request, and with the Location of a 3xx on the first GET info/refs before rebasing there: that Location must still be
78 * this repo's upload-pack endpoint on the same host (assertUpstreamUrl), e.g. `name` → `name.git`.
79 */
80export function viewerAllowUrl(repo: ValidatedPublicRepoUrl, allowed: readonly AllowedHost[]): (url: URL) => Vetted {
81 return url => {
82 const isBase = url.search === '' && !url.pathname.endsWith('/info/refs');
83 const probe = isBase ? `${url.href.replace(/\/+$/, '')}/info/refs?service=git-upload-pack` : url.href;
84 try { assertUpstreamUrl(probe, repo, allowed); } catch (e) { return { ok: false, error: e as PlyError }; }
85 return { ok: true, value: url };
86 };
89/**
90 * Every request must be this repo's smart-HTTP endpoint on an allowlisted host, and carries no credential. A 3xx is
91 * never followed here: ply follows the first GET info/refs (git's http.followRedirects=initial, vetted by
92 * viewerAllowUrl) and reports any other. A Location that leaves the repo is refused here, before ply sees it.
93 */
94export function createViewerFetch(inner: FetchLike, repo: ValidatedPublicRepoUrl, allowed: readonly AllowedHost[], budget: Budget, pageSignal: AbortSignal): FetchLike {
95 const guarded: FetchLike = async (input, init) => {
96 const raw = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
97 const current = assertUpstreamUrl(raw, repo, allowed);
98 const methodField = chainGet(init, 'method');
99 const method = methodField.tag === 'none' ? 'GET' : methodField.value as string;
100 const headerField = chainGet(init, 'headers');
101 const headers = credentialFreeHeaders(headerField.tag === 'none' ? null : headerField.value as HeadersInit);
102 const bodyField = chainGet(init, 'body');
103 const initBody = bodyField.tag === 'none' ? null : bodyField.value;
104 // The transport posts a Uint8Array (the viewer never pushes).
105 if (initBody !== null && !(initBody instanceof Uint8Array)) throw plyError('bad_request', 'upstream body is not replayable');
106 const signals: AbortSignal[] = [pageSignal];
107 const signalField = chainGet(init, 'signal');
108 if (signalField.tag === 'some' && signalField.value) signals.push(signalField.value as AbortSignal);
109 budget.countSubrequest();
110 const upstream: ViewerUpstreamInit = { credential: 'absent', method, headers, body: initBody, redirect: 'manual', signal: signals.length ? AbortSignal.any(signals) : null };
111 if (upstream.credential !== 'absent') throw plyError('bad_request', 'credentialed upstream request');
112 const requestInit: RequestInit = { method: upstream.method, headers: upstream.headers, redirect: 'manual' };
113 if (upstream.body !== null) requestInit.body = upstream.body as BodyInit;
114 if (upstream.signal !== null) requestInit.signal = upstream.signal;
115 const res = await inner(current, requestInit);
116 if (res.status >= 300 && res.status < 400) {
117 const location = res.headers.get('location');
118 try {
119 if (!location) throw plyError('http', 'redirect without location', { status: res.status });
120 vetLocation(location, current, repo, allowed);
121 } catch (e) {
122 if (res.body) await res.body.cancel().catch(() => {});
123 throw e;
124 }
125 return res;
126 }
127 return limitBody(res, budget);
128 };
129 return guarded;
Diagnostics4 git calls · 298 ms
CallStatusSizeTime0298 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 · 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 1020040 KiB · hit12 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=12
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=12
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=526b305930b8ef382f2d7a1f9c0238730a585921
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=41349
git.pkt.payload.max=41254
git.sideband.band1.bytes=41253
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=41253
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c
git.pack.full=61
Open pack map
fetch2002.6 KiB284 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=284
git.response.bytes=2669
git.response.cache=MISS
git.response.duration_ms=284
git.fetch.wants=2a31772a3b26a895117fdb912d36cdf9c4c42caf
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=2669
git.pkt.payload.max=2648
git.sideband.band1.bytes=2647
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-web.git
Ref
refs/heads/main
Commit
526b305930b8ef382f2d7a1f9c0238730a585921
cf-ray
a5e2f9e43f6f90c0
Request
f98fd3ed…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format