main
1// Public-repo URL validation. A ValidatedPublicRepoUrl is the only string the viewer will fetch,
2// and it is produced only by validatePublicRepoUrl. Credentialed URLs are rejected, not stripped
3// into an Authorization header (createTransport would otherwise promote userinfo into auth).
4//
5// Host policy is an exact allowlist. There is no suffix match, no wildcard, and no IP-literal
6// folding: a hostname is accepted only when it is one of the allowed hosts, character for character.
7import { plyError } from 'ply';
8import { chainGet } from '../absent.ts';
9import { extractRequestPath, rawPathHasUnsafeSegment } from '../pathguard.ts';
10import { publicRepoShortName } from '../public.ts';
11import { authorityHasTrailingDot } from '../remote.ts';
13declare const validatedPublicRepoUrl: unique symbol;
14export type ValidatedPublicRepoUrl = string & { readonly [validatedPublicRepoUrl]: 'ValidatedPublicRepoUrl' };
16declare const allowedHost: unique symbol;
18/** Built-in public forges. Exact hostnames, lowercase, no leading wildcard and no trailing dot. */
19export const ALLOWED_HOSTS = Object.freeze([
20 'github.com',
21 'gitlab.com',
22 'codeberg.org',
23 'bitbucket.org',
24 'git.sr.ht',
25] as const);
27export type BuiltinAllowedHost = (typeof ALLOWED_HOSTS)[number];
29/**
30 * A hostname the viewer may fetch. The built-in forges are the union above. An operator extra
31 * (VIEWER_EXTRA_HOSTS) is branded only after it passes the exact-hostname grammar.
32 */
33export type AllowedHost = BuiltinAllowedHost | (string & { readonly [allowedHost]: 'AllowedHost' });
35export type ValidateOpts = {
36 allowedHosts: readonly AllowedHost[] | null;
37 maxLength: number | null;
38};
40export type RepoLocation = {
41 url: ValidatedPublicRepoUrl;
42 origin: string;
43 host: AllowedHost;
44 hostname: AllowedHost;
45 repoPath: string;
46 allowedHosts: readonly AllowedHost[];
47};
49const EXTRA_HOST = /^(?=.{1,253}$)[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)+$/;
51function brand(url: string): ValidatedPublicRepoUrl {
52 return url as ValidatedPublicRepoUrl;
53}
55function brandExtra(host: string): AllowedHost {
56 return host as string & { readonly [allowedHost]: 'AllowedHost' };
57}
59/**
60 * Defaults, plus exact hostnames from VIEWER_EXTRA_HOSTS. Unset or blank keeps the defaults.
61 * Tokens with a scheme, port, userinfo, wildcard, or trailing dot are skipped — they are not
62 * turned into suffix rules. Artifacts stays off until the operator names that exact host.
63 */
64export function allowlistFromEnv(raw: string | null): readonly AllowedHost[] {
65 if (raw === null || raw.trim() === '') return ALLOWED_HOSTS;
66 const extras: AllowedHost[] = [];
67 const seen = new Set<string>(ALLOWED_HOSTS);
68 for (const part of raw.split(',')) {
69 const item = part.trim().toLowerCase();
70 if (!item || seen.has(item)) continue;
71 if (/[*@:/\\%\s]/.test(item) || item.startsWith('.') || item.endsWith('.')) continue;
72 if (!EXTRA_HOST.test(item)) continue;
73 seen.add(item);
74 extras.push(brandExtra(item));
75 }
76 if (extras.length === 0) return ALLOWED_HOSTS;
77 return Object.freeze([...ALLOWED_HOSTS, ...extras]);
78}
80/** `allowed` plus one exact hostname (the public namespace's Artifacts host), branded only if it passes the grammar. */
81export function withHost(allowed: readonly AllowedHost[], host: string): readonly AllowedHost[] {
82 const item = host.toLowerCase();
83 if (!EXTRA_HOST.test(item) || allowed.some(h => h === item)) return allowed;
84 return Object.freeze([...allowed, brandExtra(item)]);
85}
87/** Case-insensitive exact match. A trailing dot is not stripped, and a suffix never matches. */
88export function hostAllowed(hostname: string, allowed: readonly AllowedHost[]): boolean {
89 const host = hostname.toLowerCase();
90 for (const item of allowed) if (item === host) return true;
91 return false;
92}
94function supported(allowed: readonly AllowedHost[]): string {
95 return `Supported hosts: ${allowed.join(', ')}.`;
96}
98function reject(allowed: readonly AllowedHost[], message: string): never {
99 throw plyError('bad_request', `${message} ${supported(allowed)}`);
100}
102function authorityEndsWithDot(raw: string): boolean {
103 return authorityHasTrailingDot(raw);
104}
106function checkPath(pathname: string, allowed: readonly AllowedHost[]): string {
107 const trimmed = pathname.replace(/\/+$/, '');
108 if (!trimmed.startsWith('/')) reject(allowed, 'remote path is not allowed.');
109 const parts = trimmed.slice(1).split('/');
110 if (parts.length < 1 || parts.length > 16) reject(allowed, 'remote path is not allowed.');
111 if (trimmed.length > 512) reject(allowed, 'remote path is too long.');
112 for (const part of parts) {
113 if (!part || part === '.' || part === '..') reject(allowed, 'remote path is not allowed.');
114 // oxlint-disable-next-line no-control-regex -- path segments cannot contain controls
115 if (part.includes('@') || part.includes('\\') || part.includes(' ') || /[\u0000-\u001f\u007f]/.test(part)) reject(allowed, 'remote path is not allowed.');
116 if (part.includes('%')) reject(allowed, 'remote path is not allowed.');
117 }
118 return parts.join('/');
119}
121/**
122 * Validate a user-supplied git remote. https, no userinfo, default port, and a hostname that
123 * exactly matches the allowlist. Anything else is rejected before a fetch, with the supported hosts.
124 */
125export function validatePublicRepoUrl(raw: string, opts: unknown = null): RepoLocation {
126 const allowedField = chainGet(opts, 'allowedHosts');
127 const maxField = chainGet(opts, 'maxLength');
128 const allowed = allowedField.tag === 'some' ? allowedField.value as readonly AllowedHost[] : ALLOWED_HOSTS;
129 const maxLength = maxField.tag === 'some' ? maxField.value as number : 2048;
130 if (typeof raw !== 'string' || !raw.trim()) reject(allowed, 'remote is required.');
131 if (raw.length > maxLength) reject(allowed, 'remote is too long.');
132 // oxlint-disable-next-line no-control-regex -- a remote with controls is not a URL
133 if (/[\u0000-\u001f\u007f\s]/.test(raw)) reject(allowed, 'remote is not a URL.');
134 if (raw.includes('\\') || rawPathHasUnsafeSegment(extractRequestPath(raw))) reject(allowed, 'remote path is not allowed.');
135 let u: URL;
136 try { u = new URL(raw); }
137 catch { reject(allowed, 'remote is not a URL.'); }
138 if (u.username || u.password) reject(allowed, 'remote must not carry credentials.');
139 if (u.search || u.hash) reject(allowed, 'remote must not have a query or fragment.');
140 const repoPath = checkPath(u.pathname, allowed);
141 if (u.protocol !== 'https:') reject(allowed, 'remote must be https.');
142 if (u.port !== '') reject(allowed, 'remote must use port 443.');
143 const hostname = u.hostname.toLowerCase();
144 const matched = allowed.find(item => item === hostname);
145 if (authorityEndsWithDot(raw) || hostname.endsWith('.') || !matched) reject(allowed, 'remote host is not an allowlisted host.');
146 const origin = `https://${matched}`;
147 const canonical = `${origin}/${repoPath}`;
148 return { url: brand(canonical), origin, host: matched, hostname: matched, repoPath, allowedHosts: allowed };
149}
151function repoVariants(repoPath: string): string[] {
152 const bare = repoPath.replace(/\.git$/, '');
153 return [...new Set([repoPath, bare, `${bare}.git`])];
154}
156/** A fetch the transport is about to make must be this repo's smart-HTTP endpoint on an allowlisted host. */
157export function assertUpstreamUrl(raw: string, repo: ValidatedPublicRepoUrl, allowed: readonly AllowedHost[]): ValidatedPublicRepoUrl {
158 if (raw.includes('\\') || rawPathHasUnsafeSegment(extractRequestPath(raw))) reject(allowed, 'upstream path is not a git smart-HTTP endpoint.');
159 let u: URL;
160 try { u = new URL(raw); }
161 catch { reject(allowed, 'upstream URL rejected.'); }
162 if (u.username || u.password || u.hash) reject(allowed, 'upstream URL rejected.');
163 if (u.protocol !== 'https:' || u.port !== '') reject(allowed, 'upstream URL rejected.');
164 if (authorityEndsWithDot(raw) || !hostAllowed(u.hostname, allowed)) reject(allowed, 'redirect host is not an allowlisted host.');
165 const base = new URL(repo);
166 if (u.host !== base.host) reject(allowed, 'cross-host upstream rejected.');
167 const path = u.pathname.replace(/\/+$/, '');
168 const variants = repoVariants(new URL(repo).pathname.replace(/^\//, '').replace(/\/+$/, ''));
169 const matched = variants.find(v => path === `/${v}/info/refs` || path === `/${v}/git-upload-pack`);
170 if (!matched) reject(allowed, 'upstream path is not a git smart-HTTP endpoint.');
171 if (path.endsWith('/info/refs')) {
172 const keys = [...u.searchParams.keys()];
173 if (keys.length !== 1 || keys[0] !== 'service' || u.searchParams.get('service') !== 'git-upload-pack') reject(allowed, 'unexpected info/refs query.');
174 } else if (u.search) reject(allowed, 'unexpected upload-pack query.');
175 return brand(u.toString());
176}
178export function viewerHref(host: string, repoPath: string, query: Record<string, string | null> = {}): string {
179 const short = publicRepoShortName(host, repoPath);
180 const path = short
181 ? `/r/${encodeURIComponent(short)}`
182 : `/view/${encodeURIComponent(host)}/${repoPath.split('/').map(part => encodeURIComponent(part)).join('/')}`;
183 const qs = new URLSearchParams();
184 for (const [key, value] of Object.entries(query)) if (value) qs.set(key, value);
185 // `/` is legal in a query value (RFC 3986 §3.4); keep paths and refs readable.
186 const tail = qs.toString().replace(/%2F/gi, '/');
187 return tail ? `${path}?${tail}` : path;
188}
190export function viewerPathFor(loc: RepoLocation, query: Record<string, string | null> = {}): string {
191 return viewerHref(loc.host, loc.repoPath, query);
192}
Diagnostics4 git calls · 29 ms
CallStatusSizeTime029 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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 · 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 1020040 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=41349 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=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=61Open pack map
fetch2003.2 KiB · hit24 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=24 git.response.bytes=3231 git.response.cache=HIT git.response.duration_ms=24 git.fetch.wants=f634b503b0f2534bfe5929060748f857e1b104df 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=3231 git.pkt.payload.max=3210 git.sideband.band1.bytes=3209 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
- a5e3195b5abd3e6e
- Request
- b3cc7f98…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format