main
1// Public viewer: URL/SSRF rules, caps, escaping, a real pack fixture, the credential-stripping fetch,
2// a local git http-backend, the SQLite rate-limit object under workerd, and (when reachable) GitHub.
3import { beforeEach, test } from 'node:test';
4import { quirksFor, resetQuirks } from '../src/quirks.ts';
5import assert from 'node:assert/strict';
6import { spawn } from 'node:child_process';
7import { mkdtempSync, readFileSync, copyFileSync, mkdirSync, writeFileSync, rmSync, existsSync } from 'node:fs';
8import { tmpdir } from 'node:os';
9import { join } from 'node:path';
10import { fileURLToPath } from 'node:url';
11import { copyCfBundle } from './cf-bundle.js';
12import { concat } from 'ply';
13import { Sha1 } from 'ply';
14import { TYPE } from 'ply';
15import { encodeObjHeader, spliceDelta, zlibDeflate } from 'ply';
16import { parsePack } from 'ply';
17import { createTransport, forgetQuirks, lsRefs, Tracer } from 'ply';
18import { unwrap, unwrapAsync } from '../src/ply-result.ts';
19import { git, makeBareRepo, startGitServer, tmpRoot, commitFiles } from './server.js';
20import { startProxy } from './proxy.js';
21import { createBudget } from '../src/viewer/budget.ts';
22import { escapeHtml } from '../src/viewer/escape.ts';
23import { credentialFreeHeaders, createViewerFetch, viewerAllowUrl } from '../src/viewer/fetchguard.ts';
24import { handleViewer } from '../src/viewer/handle.ts';
25import { renderError, renderHome, renderLoaded, renderNamespace } from '../src/viewer/html.ts';
26import { nearbyRefs } from '../src/viewer/hint.ts';
27import { KUMO_USED, VIEWER_CSS, cssNodes, selectorClasses } from '../src/viewer/styles.ts';
28import { commitProse } from '../src/viewer/format.ts';
29import { repoIdentity } from '../src/viewer/identity.ts';
30import { PUBLIC_GIT_HOST } from '../src/public.ts';
31import { matchAbbreviatedOid, MIN_ABBREV_HEX } from '../src/abbrev.ts';
32import { describeFailure } from '../src/viewer/failure.ts';
33import { compareTagNames, tagsFromRefs } from '../src/viewer/tags.ts';
34import { plyBuildValue } from '../src/version.ts';
35import { LAYOUT } from '../src/viewer/layout.ts';
36import { none, some } from '../src/option.ts';
38/** Null and a missing value take the fallback. An empty string and 0 stay. */
39function fill<T>(value: unknown, fallback: T): T {
40 return value === null || value === void 0 ? fallback : value as T;
41}
43/** One property. A null receiver and a missing key are holes. A stored null stays null. */
44function slot(receiver: unknown, key: string): unknown {
45 if (receiver === null || receiver === void 0) return void 0;
46 if (typeof receiver !== 'object' && typeof receiver !== 'function') return void 0;
47 return (receiver as Record<string, unknown>)[key];
48}
49import { VIEWER_CAPS, VIEWER_GIT_LIMITS, VIEWER_UA, rateLimitFor } from '../src/viewer/limits.ts';
50import type { Loaded, RateNamespace, TraceModel } from '../src/viewer/model.ts';
51import { renderMarkdown } from '../src/viewer/markdown.ts';
52import { mapPack } from '../src/viewer/packmap.ts';
53import { interpretRemote } from '../src/viewer/paste.ts';
54import { parseViewerRoute, routeOptsFromEnv } from '../src/viewer/route.ts';
55import { VIEWER_CSP } from '../src/viewer/script.ts';
56import { ALLOWED_HOSTS, allowlistFromEnv, assertUpstreamUrl, validatePublicRepoUrl, type AllowedHost, type RepoLocation } from '../src/viewer/url.ts';
59// Every test dials a local server under the github.com origin; the isolate-wide quirks map must not carry over.
60beforeEach(() => resetQuirks());
61const FIXTURE = fileURLToPath(new URL('./fixtures/viewer/small.pack', import.meta.url));
62const FIXTURE_SHA = 'a86b269e52e69445e10d4c1dd7c5a89b323c0988f1d73b1f0b0c7c1d112f846a';
64function treeFixture(over: Partial<{ message: string; host: string; repoPath: string }>): Extract<Loaded, { kind: "tree" }> {
65 const host = fill(over.host, 'github.com')
66 const repoPath = fill(over.repoPath, 'o/r')
67 return {
68 kind: 'tree',
69 header: {
70 host, repoPath, ref: 'refs/heads/main', commit: 'a'.repeat(40), treeHref: `/view/${host}/${repoPath}`,
71 server: { agent: 'git/2', protocol: 2, objectFormat: 'sha1', caps: ['agent'], fetchFeatures: ['filter'], filterAdvertised: true },
72 },
73 path: '',
74 commit: { oid: 'a'.repeat(40), tree: 'b'.repeat(40), parents: [], authorName: 'Ada', authorEmail: 'a@b', authorTime: '2020-01-02T03:04:05Z', authorTz: '+0000', message: fill(over.message, 'subject'), signed: false },
75 log: [], logStopped: false,
76 branches: [{ name: 'refs/heads/main', oid: 'a'.repeat(40), head: true }], branchesTruncated: 0,
77 tags: [{ name: 'refs/tags/v1.0.0', oid: 'd'.repeat(40) }], tagsTruncated: 0,
78 entries: [{ name: 'a.txt', mode: '100644', oid: 'c'.repeat(40), type: 'blob' }], entriesTruncated: 0,
79 readme: null,
80 }
81}
83function loc(raw: string, allowed: readonly AllowedHost[] = ALLOWED_HOSTS): RepoLocation {
84 return validatePublicRepoUrl(raw, { allowedHosts: allowed });
85}
87test('public repo URLs: exact allowlist, case, trailing dot, lookalikes, credentials', () => {
88 const supported = 'Supported hosts: github.com, gitlab.com, codeberg.org, bitbucket.org, git.sr.ht.';
89 assert.equal(loc('https://github.com/octocat/Hello-World.git').repoPath, 'octocat/Hello-World.git');
90 assert.equal(loc('https://GitHub.com/octocat/Hello-World').host, 'github.com');
91 assert.equal(loc('https://GitHub.com/octocat/Hello-World').url, 'https://github.com/octocat/Hello-World');
92 assert.equal(loc('https://gitlab.com/a/b').host, 'gitlab.com');
93 assert.equal(loc('https://codeberg.org/a/b').host, 'codeberg.org');
94 assert.equal(loc('https://bitbucket.org/a/b').host, 'bitbucket.org');
95 assert.equal(loc('https://git.sr.ht/~user/repo').host, 'git.sr.ht');
96 assert.equal(loc('https://git.sr.ht/~user/repo').repoPath, '~user/repo');
97 const bad = [
98 'http://github.com/a/b', 'https://github.com:8443/a/b', 'https://user:pw@github.com/a/b', 'https://github.com/a/b?x=1',
99 'https://github.com/a/b#f', 'https://github.com./octocat/Hello-World', 'https://github.com.evil.com/a/b',
100 'https://evilgithub.com/a/b', 'https://githüb.com/a/b', 'https://xn--githb-nva.com/a/b',
101 'https://127.0.0.1/a/b', 'https://10.0.0.1/a/b', 'https://192.168.1.1/a/b',
102 'https://169.254.169.254/a/b', 'https://0.0.0.0/a/b', 'https://[::1]/a/b', 'https://[::ffff:7f00:1]/a/b',
103 'https://2130706433/a/b', 'https://0x7f000001/a/b', 'https://0177.0.0.1/a/b', 'https://127.1/a/b',
104 'https://localhost/a/b', 'https://foo.localhost/a/b', 'https://git.internal/a/b', 'https://evil.github.com/a/b',
105 'https://notgithub.com/a/b', 'https://127.0.0.1.nip.io/a/b', 'https://acct.artifacts.cloudflare.net/git/repo.git',
106 'https://ply.ivar.workers.dev/a/b', 'https://other.workers.dev/a/b', 'ftp://github.com/a/b', 'not a url', '',
107 ];
108 for (const raw of bad) {
109 assert.throws(() => loc(raw), (err: Partial<{ message: string }>) => {
110 const message = String(fill(slot(err, 'message'), err));
111 return message.includes(supported) && !message.includes('super-secret-token') && !message.includes('pw@');
112 }, raw);
113 }
114 assert.throws(() => loc('https://user:super-secret-token@github.com/octocat/Hello-World'), (err: Partial<{ message: string }>) => {
115 const message = String(fill(slot(err, 'message'), ''));
116 return /credentials/.test(message) && message.includes(supported) && !message.includes('super-secret-token');
117 });
118 const extra = allowlistFromEnv('acct.artifacts.cloudflare.net, *.artifacts.cloudflare.net, https://evil.example, github.com:443, , not a host');
119 assert.equal(extra.some(host => host === 'github.com'), true);
120 assert.equal(extra.some(host => host === 'acct.artifacts.cloudflare.net'), true);
121 assert.equal(extra.some(host => host.includes('*') || host.includes('/') || host.includes(':')), false);
122 assert.equal(allowlistFromEnv(null).length, ALLOWED_HOSTS.length);
123 assert.equal(allowlistFromEnv('').length, ALLOWED_HOSTS.length);
124 assert.equal(allowlistFromEnv('*.artifacts.cloudflare.net').length, ALLOWED_HOSTS.length);
125 const art = loc('https://acct.artifacts.cloudflare.net/git/ns/repo.git', extra);
126 assert.equal(art.host, 'acct.artifacts.cloudflare.net');
127 assert.throws(() => loc('https://other.artifacts.cloudflare.net/a/b.git', extra), (err: Partial<{ message: string }>) => /not an allowlisted host/.test(String(slot(err, 'message'))) && String(slot(err, 'message')).includes('acct.artifacts.cloudflare.net'));
128 assert.throws(() => loc('https://acct.artifacts.cloudflare.net/a/b.git'), /not an allowlisted host/);
129 const repo = loc('https://github.com/octocat/Hello-World');
130 assert.equal(assertUpstreamUrl('https://github.com/octocat/Hello-World.git/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), 'https://github.com/octocat/Hello-World.git/info/refs?service=git-upload-pack');
131 assert.throws(() => assertUpstreamUrl('https://evil.example/octocat/Hello-World/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /not an allowlisted host/);
132 assert.throws(() => assertUpstreamUrl('https://github.com.evil.com/octocat/Hello-World.git/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /not an allowlisted host/);
133 assert.throws(() => assertUpstreamUrl('https://gitlab.com/octocat/Hello-World/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /cross-host/);
134 assert.throws(() => assertUpstreamUrl('http://github.com/octocat/Hello-World/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /upstream URL rejected/);
135 assert.throws(() => assertUpstreamUrl('https://github.com:8443/octocat/Hello-World/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /upstream URL rejected/);
136 assert.throws(() => assertUpstreamUrl('https://github.com./octocat/Hello-World/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /not an allowlisted host/);
137 assert.throws(() => assertUpstreamUrl('https://github.com/other/Hello/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /smart-HTTP/);
138 assert.throws(() => assertUpstreamUrl('https://github.com/octocat/Hello-World/git-upload-pack?x=1', repo.url, repo.allowedHosts), /query/);
139});
141test('paste parsing stays inside the allowlist', () => {
142 assert.equal(interpretRemote('octocat/Hello-World').remote, 'https://github.com/octocat/Hello-World');
143 assert.equal(interpretRemote('github.com/sindresorhus/is').remote, 'https://github.com/sindresorhus/is');
144 assert.equal(interpretRemote('git@github.com:tj/commander.js.git').remote, 'https://github.com/tj/commander.js.git');
145 const web = interpretRemote('https://github.com/octocat/Hello-World/blob/master/README');
146 assert.equal(web.remote, 'https://github.com/octocat/Hello-World');
147 assert.equal(web.ref, 'master');
148 assert.equal(web.path, 'README');
149 const route = parseViewerRoute(new URL('https://ply.example/view?remote=octocat/Hello-World'), routeOptsFromEnv({}));
150 assert.equal(route.kind, 'redirect');
151 if (route.kind === 'redirect') assert.equal(route.location, '/view/github.com/octocat/Hello-World');
152 const tagged = parseViewerRoute(new URL('https://ply.example/view/github.com/octocat/Hello-World?ref=refs/heads/main&at=v1.0.0'), routeOptsFromEnv({}));
153 assert.equal(tagged.kind, 'redirect');
154 if (tagged.kind === 'redirect') assert.equal(tagged.location, '/view/github.com/octocat/Hello-World?ref=v1.0.0');
155 const namespaced = parseViewerRoute(new URL('https://ply.example/r/'), routeOptsFromEnv({}));
156 assert.equal(namespaced.kind, 'namespace');
157 const shortRepo = parseViewerRoute(new URL(`https://ply.example/r/ply?path=README.md`), routeOptsFromEnv({}));
158 assert.equal(shortRepo.kind, 'repo');
159 if (shortRepo.kind === 'repo') assert.equal(shortRepo.loc.repoPath, 'git/public-v0/ply.git');
160 const longPublic = parseViewerRoute(new URL(`https://ply.example/view/${PUBLIC_GIT_HOST}/git/public-v0/ply.git?path=README.md`), routeOptsFromEnv({}));
161 assert.equal(longPublic.kind, 'redirect');
162 if (longPublic.kind === 'redirect') assert.equal(longPublic.location, '/r/ply?path=README.md');
163 assert.throws(() => parseViewerRoute(new URL('https://ply.example/view?remote=evil.example/a/b'), routeOptsFromEnv({})), /Supported hosts/);
164 assert.throws(() => parseViewerRoute(new URL('https://ply.example/view?remote=' + encodeURIComponent('https://user:super-secret-token@github.com/a/b')), routeOptsFromEnv({})), (err: Partial<{ message: string }>) => /credentials/.test(String(slot(err, 'message'))) && !String(slot(err, 'message')).includes('super-secret-token'));
165 const md = renderMarkdown('# Hi\n\n<script>alert(1)</script>\n\n[x](javascript:alert(1))\n\n[ok](https://github.com/octocat/Hello-World)\n');
166 assert.equal(md.includes('<script'), false);
167 assert.match(md, /<script>/);
168 assert.equal(md.includes('javascript:'), true);
169 assert.equal(/href="javascript:/.test(md), false);
170 assert.match(md, /href="https:\/\/github.com\/octocat\/Hello-World"/);
171 assert.equal(VIEWER_CSP.includes('unsafe-inline'), false);
172 assert.match(VIEWER_CSP, /script-src 'none'/);
173 assert.match(VIEWER_CSP, /style-src 'self'/);
174});
176test('abbreviated oids, failure copy, tag order, cache headers, and version text', () => {
177 const oid = '705bd639' + 'a'.repeat(32);
178 const other = '705bd639' + 'b'.repeat(32);
179 const refs = new Map<string, { oid: string } & Partial<{ peeled: string }>>([
180 ['refs/heads/main', { oid }],
181 ['refs/heads/other', { oid: other }],
182 ['refs/tags/v1', { oid: 'c'.repeat(40), peeled: oid }],
183 ]);
184 assert.equal(MIN_ABBREV_HEX, 7);
185 assert.deepEqual(matchAbbreviatedOid('705bd639a', refs), { kind: 'unique', oid });
186 const ambiguous = matchAbbreviatedOid('705bd639', refs);
187 assert.equal(ambiguous.kind, 'ambiguous');
188 assert.match(ambiguous.message, /40-hex/);
189 assert.equal(matchAbbreviatedOid('deadbee', refs).kind, 'none');
190 assert.equal(matchAbbreviatedOid(other.slice(0, 12), refs).kind, 'unique');
191 const missing = describeFailure({ code: 'auth', status: 401, message: 'authentication failed', host: 'github.com', repoPath: 'ivarvong/ply', ref: null, path: '' });
192 assert.equal(missing.status, 404);
193 assert.equal(missing.part, 'repo');
194 assert.equal(missing.summary, 'Repository ivarvong/ply not found, or private');
195 const badRef = describeFailure({ code: 'not_found', status: 404, message: 'ref v99.0.0 not found', host: 'github.com', repoPath: 'ivarvong/ply', ref: 'v99.0.0', path: '' });
196 assert.equal(badRef.part, 'ref');
197 assert.equal(badRef.summary, 'No ref v99.0.0 in ivarvong/ply');
198 const badPath = describeFailure({ code: 'not_found', status: 404, message: 'path component missing not found', host: 'github.com', repoPath: 'o/r', ref: 'main', path: 'src/missing' });
199 assert.equal(badPath.part, 'path');
200 assert.match(badPath.summary, /No path src\/missing in o\/r/);
201 // A /r/<name> the public namespace lacks: sentence case like every other headline.
202 assert.equal(describeFailure({ code: 'not_found', status: 404, message: 'no repository nope in the public namespace', host: 'h.example', repoPath: 'git/public-v0/nope.git', ref: null, path: '' }).summary, 'No repository nope in the public namespace');
203 assert.ok(compareTagNames('refs/tags/v2.0.0', 'refs/tags/v1.9.0') < 0);
204 assert.deepEqual(tagsFromRefs(refs, 10).tags.map(tag => tag.name), ['refs/tags/v1']);
205 assert.match(plyBuildValue('a'.repeat(40), 'b'.repeat(40)), /^ply=[0-9a-f]{40} ply-web=[0-9a-f]{40}$/);
206 assert.match(VIEWER_CSS, new RegExp(`\\.files \\.oid \\{[^}]*color: var\\(--text-color-kumo-default\\)`));
207 assert.match(VIEWER_CSS, new RegExp(`font-size: ${LAYOUT.codeFontPx}px`));
208 assert.match(VIEWER_CSS, new RegExp(`font-size: ${LAYOUT.monoFontPx}px`));
209 assert.match(VIEWER_CSS, /scrollbar-width: thin/);
210 assert.match(VIEWER_CSS, /\.refbox \{ position: static; \} \.ref-pop \{ left: 0; right: 0; width: auto; \}/);
211 const trace: TraceModel = { bars: [], totalMs: 0, cfRay: '', requestId: '' };
212 const page = renderLoaded(treeFixture({ message: 'Subject line\n\nBody that should span the card.\n' }), trace);
213 assert.match(String(page), /refs\/tags\/v1\.0\.0/);
214 assert.match(String(page), /ply=/);
215 const tree = treeFixture({});
216 const file: Loaded = {
217 kind: 'file', header: tree.header, path: 'README.md', branches: tree.branches, branchesTruncated: 0,
218 tags: tree.tags, tagsTruncated: 0, mode: '100644', blob: 'c'.repeat(40), size: 6, note: 'text', text: 'hello\n',
219 };
220 assert.match(String(renderLoaded(file, trace)), /Wrap lines/);
221 const refused = renderError(401, 'auth', 'authentication failed', trace, 'github.com', 'nope/missing', null, 1, '');
222 assert.match(String(refused), /not found, or private/);
223 assert.match(String(refused), /GitHub answers 401/);
224 assert.match(String(refused), /HTTP 404/);
225 assert.match(String(refused), /href="\/view\/github\.com\/nope\/missing"/);
226 assert.equal(String(refused).includes('Check the owner'), false);
227 assert.equal(String(refused).includes('No ref'), false);
228 const refPage = String(renderError(404, 'not_found', 'ref v99.0.0 not found', trace, 'github.com', 'shot/shot', 'v99.0.0', 1, '', {
229 nearby: [{ label: 'main', ref: 'refs/heads/main' }, { label: 'v1.10.0', ref: 'refs/tags/v1.10.0' }],
230 path: null,
231 }));
232 assert.match(refPage, /No ref v99\.0\.0 in shot\/shot/);
233 assert.match(refPage, /<p class="banner[^"]*"><span>That name is not a branch or tag/);
234 assert.match(refPage, /ref picker/);
235 assert.match(refPage, /href="\/view\/github\.com\/shot\/shot\?ref=refs\/heads\/main"/);
236 assert.match(refPage, />v1\.10\.0</);
237 assert.equal(refPage.includes('Check the owner'), false);
238 assert.doesNotMatch(refPage, /HTTP 401|answers 401/); // not a bare '401': request ids and timings on the page can contain it
239 const pathPage = String(renderError(404, 'not_found', 'path component missing not found', trace, 'github.com', 'o/r', 'main', 1, 'src/missing', {
240 nearby: [],
241 path: { missing: 'missing', existingPath: 'src', notDirectory: false },
242 }));
243 assert.match(pathPage, /No path src\/missing in o\/r/);
244 assert.match(pathPage, /No entry named missing/);
245 assert.match(pathPage, /href="\/view\/github\.com\/o\/r\?ref=main&path=src"/);
246 assert.doesNotMatch(pathPage, /HTTP 401|answers 401/); // not a bare '401': request ids and timings on the page can contain it
247 assert.equal(pathPage.includes('Check the owner'), false);
248 // Names from the URL (ref, path) and from the remote (nearby refs, path cue) are text, never markup.
249 const evil = '"><img src=x onerror=alert(1)>'
250 const hostile = [
251 String(renderError(404, 'not_found', `ref ${evil} not found`, trace, 'github.com', 'o/r', evil, 1, '', { nearby: [{ label: evil, ref: `refs/tags/${evil}` }], path: null })),
252 String(renderError(404, 'not_found', `path component ${evil} not found`, trace, 'github.com', 'o/r', 'main', 1, `src/${evil}`, { nearby: [], path: { missing: evil, existingPath: `src/${evil}`, notDirectory: false } })),
253 ]
254 for (const page of hostile) {
255 assert.equal(page.includes('<img'), false, 'a reflected name became markup')
256 assert.match(page, /<img src=x onerror=alert\(1\)>/)
257 }
258 const repos = { names: ['curl', 'deepfile', 'ply', 'ply-web'], unavailable: false };
259 const listedHome = String(renderHome(null, 1, repos));
260 const listedNamespace = String(renderNamespace(repos, 1));
261 const listMarkup = (html: string) => fill(slot(/<ul class="repo-list">[\s\S]*?<\/ul>/.exec(html), '0'), '');
262 assert.equal(listMarkup(listedHome), listMarkup(listedNamespace));
263 assert.match(listMarkup(listedNamespace), /href="\/r\/ply"/);
264 assert.equal(listedNamespace.includes('unavailable'), false);
265 assert.match(String(renderNamespace({ names: [], unavailable: true }, 1)), /unavailable right now/);
266 assert.equal(String(renderNamespace({ names: [], unavailable: false }, 1)).includes('unavailable'), false);
267 assert.deepEqual(nearbyRefs('v99.0.0', [
268 { name: 'refs/heads/main', head: true },
269 { name: 'refs/heads/feature', head: false },
270 ], [
271 { name: 'refs/tags/v1.10.0' },
272 { name: 'refs/tags/v1.2.0' },
273 ]).map(item => item.label), ['v1.10.0', 'v1.2.0', 'main', 'feature']);
274});
276test('ssr markup uses Kumo classes, prose commits, and a strict CSP', () => {
277 const home = renderHome(null, 1_700_000_000_000)
278 assert.match(home, /name="remote"/)
279 assert.match(home, /href="\/view\.[0-9a-f]{12}\.css"/)
280 assert.match(home, /bg-kumo-contrast/)
281 assert.equal(home.includes('<script'), false)
282 assert.equal(home.includes('style='), false)
283 const trace: TraceModel = { bars: [], totalMs: 12, cfRay: 'ray-ssr', requestId: 'req-ssr' }
284 const when = Date.parse('2020-01-02T03:04:05Z')
285 const loaded: Loaded = {
286 kind: 'tree',
287 header: {
288 host: 'github.com', repoPath: 'octocat/Hello-World', ref: 'refs/heads/main', commit: 'a'.repeat(40),
289 treeHref: '/view/github.com/octocat/Hello-World',
290 server: { agent: 'git/2', protocol: 2, objectFormat: 'sha1', caps: ['agent'], fetchFeatures: ['filter'], filterAdvertised: true },
291 },
292 path: '',
293 commit: {
294 oid: 'a'.repeat(40), tree: 'b'.repeat(40), parents: [], authorName: 'Ada', authorEmail: 'a@b',
295 authorTime: '2020-01-02T03:04:05Z', authorTz: '-0700',
296 message: 'Subject line\n\nThis is a wrapped\nparagraph that should flow.\n\nSecond paragraph.',
297 signed: false,
298 },
299 log: [], logStopped: false,
300 branches: [{ name: 'refs/heads/main', oid: 'a'.repeat(40), head: true }], branchesTruncated: 0,
301 tags: [{ name: 'refs/tags/v1.0.0', oid: 'd'.repeat(40) }], tagsTruncated: 0,
302 entries: [], entriesTruncated: 0,
303 readme: null,
304 }
305 const html = renderLoaded(loaded, trace, when + 5 * 86_400_000)
306 // Commit bodies keep their line structure (pre-wrap), paragraph by paragraph.
307 assert.match(html, /<p class="msg-p">This is a wrapped\nparagraph that should flow\.<\/p><p class="msg-p">Second paragraph\.<\/p>/)
308 assert.match(html, /5 days ago/)
309 assert.match(html, /title="02 Jan 2020, 03:04:05 UTC \(-0700\)"/)
310 assert.equal(html.includes('<pre class="msg"'), false)
311 assert.equal(html.includes('style='), false)
312 const error = renderError(400, 'bad_request', 'remote host is not allowed. Supported hosts: github.com, gitlab.com, codeberg.org, bitbucket.org, git.sr.ht.', trace)
313 assert.match(error, /That URL is not supported/)
314 assert.match(error, /Supported hosts: github.com, gitlab.com, codeberg.org, bitbucket.org, git.sr.ht/)
315 assert.equal(error.includes('<script'), false)
316 // The way back up: a missing repo never links to its own (missing) root; a public-namespace name links to /r.
317 const notInNs = String(renderError(404, 'not_found', 'no repository nope in the public namespace', trace, 'h.example', 'git/public-v0/nope.git'))
318 assert.match(notInNs, /<h1 class="display">No repository nope in the public namespace<\/h1>/)
319 assert.match(notInNs, /<a href="\/r">Repository index<\/a>/)
320 assert.equal(notInNs.includes('Repository root'), false)
321 const ghMissing = String(renderError(401, 'auth', 'authentication failed', trace, 'github.com', 'o/missing'))
322 assert.equal(ghMissing.includes('Repository root'), false)
323 assert.match(ghMissing, /<a href="\/view">Back to the viewer<\/a>/)
324 assert.match(String(renderError(404, 'not_found', 'ref nope not found', trace, 'github.com', 'o/r', 'nope')), /<a href="\/view\/github\.com\/o\/r">Repository root<\/a>/)
325 // The footer's build line is two unbreakable tokens (a 375 px footer wraps between them, never inside "ply-web=").
326 assert.match(String(error), /<span class="mono build" title="ply=\S+ ply-web=\S+ [^"]*"><span>ply=[0-9a-z]{1,12}<\/span> <span>ply-web=[0-9a-z]{1,12}<\/span><\/span>/)
327 assert.match(VIEWER_CSS, /\.site-footer \.build > span \{ white-space: nowrap; \}/)
328 const limited = renderError(429, 'rate_limited', 'Too many viewer requests. Retry after 1s.', trace)
329 assert.match(limited, /Too many requests/)
330 assert.match(VIEWER_CSS, /--color-kumo-canvas/)
331 assert.match(VIEWER_CSS, /--text-color-kumo-brand/)
332 assert.match(VIEWER_CSS, /@media \(prefers-color-scheme: dark\)/)
333 assert.match(VIEWER_CSS, /color-scheme: light dark/)
334 assert.match(VIEWER_CSP, /script-src 'none'/)
335 assert.equal(VIEWER_CSP.includes('unsafe-inline'), false)
336 // /view.css carries Kumo's theme and base whole, plus only the utilities kumo-classes.ts can emit.
337 const shipped = readFileSync(fileURLToPath(new URL('../node_modules/@cloudflare/kumo/dist/styles/kumo-standalone.css', import.meta.url)), 'utf8')
338 const layers = cssNodes(shipped)
339 const theme = layers.find(node => node.prelude === '@layer theme')
340 assert.ok(theme != null && theme.body && VIEWER_CSS.includes(theme.body))
341 const shippedUtilities = new Set<string>()
342 const collect = (body: string) => { for (const node of cssNodes(body)) { if (node.body === null) continue; if (node.prelude.startsWith('@')) collect(node.body); else for (const name of selectorClasses(node.prelude)) shippedUtilities.add(name) } }
343 collect(fill(slot(layers.find(node => node.prelude === '@layer utilities'), 'body'), ''))
344 for (const name of KUMO_USED) if (name !== 'kumo-input-placeholder') assert.equal(shippedUtilities.has(name), true, `Kumo has no utility ${name}`)
345 const keptUtilities = new Set<string>()
346 const keptCollect = (body: string) => { for (const node of cssNodes(body)) { if (node.body === null) continue; if (node.prelude.startsWith('@')) keptCollect(node.body); else for (const name of selectorClasses(node.prelude)) keptUtilities.add(name) } }
347 keptCollect(fill(slot(cssNodes(VIEWER_CSS).find(node => node.prelude === '@layer utilities'), 'body'), ''))
348 for (const name of KUMO_USED) if (name !== 'kumo-input-placeholder') assert.equal(keptUtilities.has(name), true, `subset dropped ${name}`)
349 assert.equal(keptUtilities.has('bg-kumo-badge-teal'), false)
350 assert.ok(VIEWER_CSS.length < 80_000, `view.css is ${VIEWER_CSS.length} bytes`)
351})
353test('README markdown: GFM table, task list, fenced code label, headings, relative links', () => {
354 const md = String(renderMarkdown([
355 '# ply', '', 'Intro with `code` and **bold**.', '',
356 '## Wire sequence', '', '| # | Request | Why |', '|---|:---:|--:|', '| 1 | v2 `ls-refs` with `ref-prefix` | head oid |', '| 2 | pipe \\| inside | x |', '',
357 '- [ ] todo', '- [x] done', '', '```js', "const a = '<b>'", '```', '', 'See [docs](docs/a.md#usage) and .',
358 ].join('\n'), { base: '', resolve: (path, kind) => `/view/github.com/o/r?path=${encodeURIComponent(path)}${kind === 'image' ? '&raw=1' : ''}` }))
359 assert.match(md, /<h2 id="ply">ply<\/h2>/)
360 assert.match(md, /<h3 id="wire-sequence">Wire sequence<\/h3>/)
361 assert.equal(md.includes('| # |'), false)
362 assert.match(md, /<div class="md-table"><table><thead><tr><th>#<\/th><th class="al-c">Request<\/th><th class="al-r">Why<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td class="al-c">v2 <code>ls-refs<\/code> with <code>ref-prefix<\/code><\/td><td class="al-r">head oid<\/td><\/tr><tr><td>2<\/td><td class="al-c">pipe \| inside<\/td><td class="al-r">x<\/td><\/tr><\/tbody><\/table><\/div>/)
363 assert.match(md, /<li class="task-item"><span class="task"><\/span>todo<\/li><li class="task-item"><span class="task done">✓<\/span>done<\/li>/)
364 assert.match(md, /<div class="md-code"><div class="md-code-lang">js<\/div><pre><code>const a = '<b>'<\/code><\/pre><\/div>/)
365 assert.match(md, /href="\/view\/github\.com\/o\/r\?path=docs%2Fa\.md#usage"/)
366 assert.match(md, /href="\/view\/github\.com\/o\/r\?path=img%2Fl\.png&raw=1" rel="noopener noreferrer nofollow"><span class="md-img">logo<\/span>/)
367 assert.equal(md.includes('<img'), false)
368})
370test('commit bodies keep their lines; trailers are split out and shown quietly', () => {
371 const prose = commitProse('subject\n\nline one\nline two\n\n- bullet a\n- bullet b\n\nCloses #33\n\nCo-Authored-By: Claude <noreply@anthropic.com>\nClaude-Session: https://claude.ai/code/session_x\n')
372 assert.deepEqual(prose.paragraphs, ['line one\nline two', '- bullet a\n- bullet b'])
373 assert.deepEqual(prose.trailers, [
374 { key: 'Closes', value: '#33' },
375 { key: 'Co-Authored-By', value: 'Claude <noreply@anthropic.com>' },
376 { key: 'Claude-Session', value: 'https://claude.ai/code/session_x' },
377 ])
378 // "Key: value" prose with no recognised trailer stays prose.
379 assert.deepEqual(commitProse('s\n\nBefore: 12 ms\nAfter: 5 ms').trailers, [])
380 const trace: TraceModel = { bars: [], totalMs: 0, cfRay: 'r', requestId: 'q' }
381 const html = String(renderLoaded(treeFixture({ message: 'subject\n\nline one\nline two\n\nCo-Authored-By: Claude <noreply@anthropic.com>\n' }), trace))
382 assert.match(html, /<p class="msg-p">line one\nline two<\/p><dl class="trailers"><div><dt>Co-Authored-By<\/dt><dd><span title="Claude <noreply@anthropic.com>">Claude<\/span><\/dd><\/div><\/dl>/)
383})
385test('header identity: public-v0 leads with namespace / repo; GitHub with owner / repo; hosts live in Diagnostics', () => {
386 const pub = repoIdentity(PUBLIC_GIT_HOST, 'git/public-v0/ply.git')
387 assert.deepEqual({ scope: pub.scope, name: pub.name, hostLabel: pub.hostLabel, scopeHref: pub.scopeHref }, { scope: 'public-v0', name: 'ply', hostLabel: 'Artifacts', scopeHref: '/r' })
388 const gh = repoIdentity('github.com', 'octocat/Hello-World')
389 assert.deepEqual({ scope: gh.scope, name: gh.name, hostLabel: gh.hostLabel }, { scope: 'octocat', name: 'Hello-World', hostLabel: 'GitHub' })
390 const trace: TraceModel = { bars: [], totalMs: 0, cfRay: 'r', requestId: 'q' }
391 const html = String(renderLoaded(treeFixture({ host: PUBLIC_GIT_HOST, repoPath: 'git/public-v0/ply.git' }), trace))
392 assert.match(html, /<title>public-v0\/ply<\/title>/)
393 assert.match(html, /<p class="repo-scope"><span class="repo-host" title="https:\/\/[0-9a-f]{32}\.artifacts\.cloudflare\.net\/git\/public-v0\/ply\.git">Artifacts<\/span><a href="\/r">public-v0<\/a><span class="repo-slash" aria-hidden="true">\/<\/span><\/p><h1 class="repo-name"><a href="\/r\/ply\?ref=refs\/heads\/main">ply<\/a><\/h1>/)
394 // The raw host appears only in hrefs, the title attribute, and the collapsed Diagnostics section.
395 const visible = html.replace(/<section class="diag"[\s\S]*$/, '').replace(/<[^>]+>/g, ' ')
396 assert.equal(visible.includes(PUBLIC_GIT_HOST), false)
397 assert.equal(html.includes('git/public-v0/ply<'), false)
398 const open = String(renderLoaded(treeFixture({}), { ...trace, expanded: true }))
399 assert.match(open, /<details class="diag-box" open="">/)
400 // The waterfall is the demo: open without ?trace=1 too, and summarised under the title by the ply strip.
401 assert.match(html, /<details class="diag-box" open="">/)
402 assert.equal(html.includes('class="ply-strip"'), false, 'no git calls, no strip')
403 const bar = (name: string, startMs: number, durMs: number, bytes: number, cache: string) =>
404 ({ name, startMs, durMs, status: 'ok' as const, httpStatus: 200, bytes, cache, lines: [], packWant: null, packFilter: null })
405 const busy: TraceModel = { ...trace, totalMs: 200, bars: [bar('git.upload-pack discover', 0, 4, 191, 'MISS'), bar('git.upload-pack ls-refs', 4, 150, 23_552, 'MISS'), bar('git.upload-pack fetch', 160, 40, 6_246, 'HIT')] }
406 const strip = String(renderLoaded(treeFixture({}), busy))
407 const inner = String(fill(slot(/<a class="ply-strip" href="#diagnostics"[^>]*>([\s\S]*?)<\/a>/.exec(strip), '1'), ''))
408 assert.ok(inner, 'strip present')
409 const text = inner.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim()
410 assert.match(text, /^ply 3 git calls 200 ms 29 KiB 1 cached protocol v2$/)
411 assert.equal(inner.split('class="wf-bar ').length - 1, 3)
412 assert.ok(strip.indexOf('class="ply-strip"') < strip.indexOf('class="files"'), 'strip sits above the listing')
413 // the waterfall explains itself: a caption, and each call's purpose as its title
414 assert.match(strip, /<p class="wf-caption">Each row is one HTTP round trip ply made/)
415 assert.match(strip, /<span class="wf-name" role="cell" title="Branch and tag tips: names to commit ids \(protocol v2 ls-refs\)">/)
416})
418test('rate and byte caps clamp and fail closed', () => {
419 assert.equal(rateLimitFor('page', none()), 60);
420 assert.equal(rateLimitFor('pack', some('')), 20);
421 assert.equal(rateLimitFor('page', some('99999')), VIEWER_CAPS.rateCeiling);
422 assert.equal(rateLimitFor('page', some('0')), 1);
423 assert.equal(rateLimitFor('page', some('nope')), 60);
424 assert.equal(rateLimitFor('pack', some('3')), 3);
425 const budget = createBudget(100, 2);
426 budget.countSubrequest();
427 budget.countSubrequest();
428 assert.equal(budget.canSpend(1), false);
429 assert.throws(() => budget.countSubrequest(), (err: Partial<{ code: string }>) => err.code === 'limit_exceeded');
430 const bytes = createBudget(4, 16);
431 bytes.addBytes(4);
432 assert.throws(() => bytes.addBytes(1), (err: Partial<{ code: string }>) => err.code === 'limit_exceeded');
433});
435test('escapeHtml and the page renderer keep repository text out of HTML', () => {
436 assert.equal(escapeHtml(`<script>alert(1)</script>"'`), '<script>alert(1)</script>"'');
437 const trace: TraceModel = { bars: [], totalMs: 0, cfRay: 'ray', requestId: 'id' };
438 const loaded: Loaded = {
439 kind: 'tree',
440 header: {
441 host: 'github.com', repoPath: 'o/r', ref: 'refs/heads/main', commit: 'a'.repeat(40),
442 treeHref: '/view/github.com/o/r',
443 server: { agent: 'gitty/1.0', protocol: 2, objectFormat: 'sha1', caps: ['agent'], fetchFeatures: ['filter'], filterAdvertised: true },
444 },
445 path: '',
446 commit: { oid: 'a'.repeat(40), tree: 'b'.repeat(40), parents: [], authorName: '<b>', authorEmail: 'a@b', authorTime: 't', authorTz: '+0000', message: '<script>alert(1)</script>', signed: false },
447 log: [], logStopped: false,
448 branches: [{ name: 'refs/heads/main', oid: 'a'.repeat(40), head: true }], branchesTruncated: 0,
449 tags: [{ name: 'refs/tags/v1.0.0', oid: 'd'.repeat(40) }], tagsTruncated: 0,
450 entries: [{ name: '<script>.txt', mode: '100644', oid: 'c'.repeat(40), type: 'blob' }], entriesTruncated: 0,
451 readme: null,
452 };
453 const html = renderLoaded(loaded, trace);
454 assert.equal(html.includes('<script>alert'), false);
455 assert.equal(html.includes('style='), false);
456 assert.match(html, /<script>alert\(1\)<\/script>/);
457 assert.match(html, /<script>\.txt/);
458 assert.match(html, /gitty\/1\.0/);
459});
461function encodeOfs(distance: number): Uint8Array {
462 const out = [distance & 0x7f];
463 let n = Math.floor(distance / 128);
464 while (n > 0) {
465 n -= 1;
466 out.unshift((n & 0x7f) | 0x80);
467 n = Math.floor(n / 128);
468 }
469 return Uint8Array.from(out);
470}
472async function ofsChain(deltaCount: number): Promise<Uint8Array> {
473 const payload = new TextEncoder().encode('x');
474 const baseZ = await zlibDeflate(payload);
475 const delta = spliceDelta({ baseLen: payload.length, off: 0, oldLen: 0, insert: new Uint8Array() }).delta;
476 const deltaZ = await zlibDeflate(delta);
477 const header = new Uint8Array(12);
478 header.set([0x50, 0x41, 0x43, 0x4b]);
479 new DataView(header.buffer).setUint32(4, 2);
480 new DataView(header.buffer).setUint32(8, deltaCount + 1);
481 const parts: Uint8Array[] = [header];
482 const baseHeader = encodeObjHeader(TYPE.BLOB, payload.length);
483 parts.push(baseHeader, baseZ);
484 let offset = 12;
485 let prev = offset;
486 offset += baseHeader.length + baseZ.length;
487 const dHeader = encodeObjHeader(TYPE.OFS_DELTA, delta.length);
488 for (let i = 0; i < deltaCount; i++) {
489 const ofs = encodeOfs(offset - prev);
490 parts.push(dHeader, ofs, deltaZ);
491 prev = offset;
492 offset += dHeader.length + ofs.length + deltaZ.length;
493 }
494 const body = concat(parts);
495 return concat([body, new Sha1().update(body).digest()]);
496}
498test('pack fixture matches git verify-pack; bombs and long chains fail closed', async () => {
499 const pack = new Uint8Array(readFileSync(FIXTURE));
500 const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', pack));
501 let hex = '';
502 for (const byte of digest) hex += byte.toString(16).padStart(2, '0');
503 assert.equal(hex, FIXTURE_SHA);
504 const map = mapPack(pack);
505 assert.equal(map.count, 9);
506 assert.equal(map.bytes, 930);
507 assert.equal(map.ofsDelta, 0);
508 assert.equal(map.refDelta, 1);
509 assert.equal(map.thin, false);
510 assert.equal(map.maxChain, 1);
511 assert.equal(map.trailer, 'a470ee9652a659aa2aea5ad58df2a80594c8cfff');
512 assert.equal(slot(map.objects[0], 'oid'), '4ce20682b4778f9086b59216533e2ccff8dbb191');
513 assert.equal(slot(map.objects[1], 'wireType'), 'ref_delta');
514 assert.equal(slot(map.objects[1], 'resolvedType'), 'commit');
515 assert.equal(slot(map.objects[1], 'baseOffset'), null);
516 assert.equal(slot(map.objects[1], 'baseOid'), '4ce20682b4778f9086b59216533e2ccff8dbb191');
517 assert.equal(slot(map.objects[1], 'chain'), 1);
518 assert.equal(slot(map.objects[1], 'oid'), 'f2db53ea00f5d51ab58e1392c78bb99760defebd');
519 let cursor = 12;
520 for (const obj of map.objects) {
521 assert.equal(obj.offset, cursor);
522 cursor += obj.length;
523 }
524 assert.equal(cursor + 20, pack.length);
525 const dir = mkdtempSync(join(tmpdir(), 'ply-pack-'));
526 try {
527 copyFileSync(FIXTURE, join(dir, 'small.pack'));
528 git(dir, ['index-pack', 'small.pack']);
529 const verified = git(dir, ['verify-pack', '-v', 'small.pack']);
530 assert.match(verified, /non delta: 8 objects/);
531 assert.match(verified, /chain length = 1: 1 object/);
532 assert.match(verified, /small\.pack: ok/);
533 } finally { rmSync(dir, { recursive: true, force: true }); }
535 const bomb = new Uint8Array(64);
536 bomb.set([0x50, 0x41, 0x43, 0x4b]);
537 new DataView(bomb.buffer).setUint32(4, 2);
538 new DataView(bomb.buffer).setUint32(8, 1);
539 bomb.set(encodeObjHeader(TYPE.BLOB, 50_000_000), 12);
540 assert.throws(() => unwrap(parsePack(bomb, { limits: VIEWER_GIT_LIMITS, maxObjects: VIEWER_CAPS.maxPackObjects, maxInflatedTotal: VIEWER_CAPS.maxInflatedTotal })), (err: Partial<{ code: string; message: string }>) => err.code === 'limit_exceeded' && /pack object size/.test(fill(err.message, '')));
542 const tooMany = new Uint8Array(32);
543 tooMany.set([0x50, 0x41, 0x43, 0x4b]);
544 new DataView(tooMany.buffer).setUint32(4, 2);
545 new DataView(tooMany.buffer).setUint32(8, 401);
546 assert.throws(() => unwrap(parsePack(tooMany, { limits: VIEWER_GIT_LIMITS, maxObjects: VIEWER_CAPS.maxPackObjects, maxInflatedTotal: VIEWER_CAPS.maxInflatedTotal })), (err: Partial<{ code: string }>) => err.code === 'limit_exceeded');
548 const short = mapPack(await ofsChain(1));
549 assert.equal(short.ofsDelta, 1);
550 assert.equal(short.maxChain, 1);
551 assert.equal(slot(short.objects[1], 'resolvedType'), 'blob');
552 await assert.rejects(async () => mapPack(await ofsChain(33)), (err: Partial<{ code: string; message: string }>) => err.code === 'limit_exceeded' && /delta chain depth/.test(fill(err.message, '')));
553});
555test('viewer fetch drops credentials, forces the user agent, hands a same-repo redirect to ply unfollowed, and refuses a cross-host one', async () => {
556 const headers = credentialFreeHeaders({ Authorization: 'Bearer no', Cookie: 'a=b', 'Git-Token': 't', 'Git-Protocol': 'version=2', 'User-Agent': 'python-requests/2' });
557 assert.equal(headers.get('authorization'), null);
558 assert.equal(headers.get('cookie'), null);
559 assert.equal(headers.get('git-token'), null);
560 assert.equal(headers.get('user-agent'), VIEWER_UA);
561 assert.equal(headers.get('git-protocol'), 'version=2');
562 const repo = loc('https://github.com/octocat/Hello-World');
563 const seen: { url: string; authorization: string | null; cookie: string | null; ua: string | null }[] = [];
564 const budget = createBudget();
565 const fetchImpl = createViewerFetch(async (input, init) => {
566 const h = new Headers(slot(init, 'headers') as HeadersInit);
567 seen.push({ url: String(input), authorization: h.get('authorization'), cookie: h.get('cookie'), ua: h.get('user-agent') });
568 const url = String(input);
569 if (url.endsWith('/info/refs?service=git-upload-pack') && !url.includes('.git/')) {
570 return new Response(null, { status: 301, headers: { location: 'https://github.com/octocat/Hello-World.git/info/refs?service=git-upload-pack' } });
571 }
572 if (url.includes('evil.example')) return new Response('no');
573 return new Response('ok', { status: 200 });
574 }, repo.url, repo.allowedHosts, budget, AbortSignal.timeout(1000));
575 // The 301 is ply's to follow (followRedirects 'initial'): the guard returns it and dials nothing more.
576 const moved = await fetchImpl('https://github.com/octocat/Hello-World/info/refs?service=git-upload-pack', { headers: { Authorization: 'Bearer leaked', Cookie: 'session=1' } });
577 assert.equal(moved.status, 301);
578 assert.equal(seen.length, 1);
579 const ok = await fetchImpl('https://github.com/octocat/Hello-World.git/info/refs?service=git-upload-pack', { headers: { Authorization: 'Bearer leaked', Cookie: 'session=1' } });
580 assert.equal(await ok.text(), 'ok');
581 assert.equal(seen.length, 2);
582 assert.equal(slot(seen[1], 'url'), 'https://github.com/octocat/Hello-World.git/info/refs?service=git-upload-pack');
583 for (const hop of seen) {
584 assert.equal(hop.authorization, null);
585 assert.equal(hop.cookie, null);
586 assert.equal(hop.ua, VIEWER_UA);
587 }
588 await assert.rejects(fetchImpl('https://github.com/octocat/Hello-World/info/refs?service=git-upload-pack&x=1'), (err: Partial<{ code: string }>) => err.code === 'bad_request');
589 const cross = createViewerFetch(async (input) => {
590 if (String(input).includes('evil.example')) throw new Error('forwarded cross-host');
591 return new Response(null, { status: 302, headers: { location: 'https://evil.example/octocat/Hello-World/info/refs?service=git-upload-pack' } });
592 }, repo.url, repo.allowedHosts, createBudget(), AbortSignal.timeout(1000));
593 await assert.rejects(cross('https://github.com/octocat/Hello-World/info/refs?service=git-upload-pack'), (err: Partial<{ code: string; message: string }>) => err.code === 'bad_request' && /not an allowlisted host/.test(fill(err.message, '')));
594 const otherForge = createViewerFetch(async (input) => {
595 if (!String(input).includes('github.com/')) throw new Error(`followed redirect to ${String(input)}`);
596 return new Response(null, { status: 302, headers: { location: 'https://gitlab.com/octocat/Hello-World/info/refs?service=git-upload-pack' } });
597 }, repo.url, repo.allowedHosts, createBudget(), AbortSignal.timeout(1000));
598 await assert.rejects(otherForge('https://github.com/octocat/Hello-World/info/refs?service=git-upload-pack'), (err: Partial<{ code: string; message: string }>) => err.code === 'bad_request' && /cross-host/.test(fill(err.message, '')));
599 const lookalike = createViewerFetch(async (input) => {
600 if (String(input).includes('evil.com')) throw new Error('followed lookalike redirect');
601 return new Response(null, { status: 302, headers: { location: 'https://github.com.evil.com/octocat/Hello-World/info/refs?service=git-upload-pack' } });
602 }, repo.url, repo.allowedHosts, createBudget(), AbortSignal.timeout(1000));
603 await assert.rejects(lookalike('https://github.com/octocat/Hello-World/info/refs?service=git-upload-pack'), (err: Partial<{ code: string; message: string }>) => err.code === 'bad_request' && /not an allowlisted host/.test(fill(err.message, '')));
604 const outOfRepo = createViewerFetch(async () => new Response(null, { status: 302, headers: { location: '/octocat/other/info/refs?service=git-upload-pack' } }), repo.url, repo.allowedHosts, createBudget(), AbortSignal.timeout(1000));
605 await assert.rejects(outOfRepo('https://github.com/octocat/Hello-World/info/refs?service=git-upload-pack'), (err: Partial<{ code: string }>) => err.code === 'bad_request');
607 // ply's allowUrl: the repo itself, and a Location that is still this repo's info/refs; nothing else.
608 const allow = viewerAllowUrl(repo.url, repo.allowedHosts);
609 assert.equal(allow(new URL(repo.url)).ok, true);
610 assert.equal(allow(new URL('https://github.com/octocat/Hello-World.git/info/refs?service=git-upload-pack')).ok, true);
611 for (const bad of ['https://gitlab.com/octocat/Hello-World/info/refs?service=git-upload-pack', 'https://github.com/octocat/other/info/refs?service=git-upload-pack', 'https://github.com/octocat/other', 'http://github.com/octocat/Hello-World.git/info/refs?service=git-upload-pack', 'https://github.com/octocat/Hello-World/info/refs?service=git-receive-pack']) {
612 const got = allow(new URL(bad));
613 assert.equal(got.ok, false, bad);
614 if (!got.ok) assert.equal(got.error.code, 'bad_request', bad);
615 }
616});
618function fakeRate(): RateNamespace {
619 const buckets = new Map<string, number[]>();
620 return {
621 idFromName: name => name,
622 get: id => ({
623 admit(bucket, now, lim, windowMs) {
624 const key = `${String(id)}:${bucket}`;
625 const list = fill(buckets.get(key), [] as number[]).filter(ts => ts > now - windowMs);
626 if (list.length >= lim) return { ok: false, remaining: 0, retryAfterMs: 1000 };
627 list.push(now);
628 buckets.set(key, list);
629 return { ok: true, remaining: lim - list.length, retryAfterMs: 0 };
630 },
631 }),
632 };
633}
635function viewerEnv(extra: Record<string, string> = {}) {
636 return { VIEWER_RATE: fakeRate(), PACK_CACHE: 'off', ...extra };
637}
639/** Dial a local git http-backend while the viewer only ever validates an allowlisted https URL. */
640function rewriteAllowlisted(localBase: string, publicHost = 'github.com'): typeof fetch {
641 const local = new URL(localBase);
642 return async (input, init) => {
643 const raw = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
644 const u = new URL(raw);
645 if (u.hostname !== publicHost) throw new Error(`refusing to dial ${u.hostname}`);
646 u.protocol = local.protocol;
647 u.hostname = local.hostname;
648 u.port = local.port;
649 const res = await fetch(u, init);
650 const location = res.headers.get('location');
651 if (!location) return res;
652 const next = new URL(location, u);
653 if (next.hostname === local.hostname && next.port === local.port) {
654 next.protocol = 'https:';
655 next.hostname = publicHost;
656 next.port = '';
657 const headers = new Headers(res.headers);
658 headers.set('location', next.href);
659 return new Response(res.body, { status: res.status, statusText: res.statusText, headers });
660 }
661 return res;
662 };
663}
665test('viewer reads a local public repo, escapes hostile names, and never forwards credentials', async () => {
666 const { root, cleanup } = tmpRoot();
667 const srv = await startGitServer(root);
668 try {
669 const name = 'demo';
670 const bare = makeBareRepo(root, name);
671 const work = join(root, 'work');
672 mkdirSync(work);
673 git(work, ['init', '-q', '-b', 'main']);
674 const head = commitFiles(work, bare, { 'README.md': 'hello viewer\n', 'pic.bin': Uint8Array.from([0, 1, 2, 255]), '<script>.txt': 'nope\n' }, 'seed <script>alert(1)</script>');
675 git(work, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit', '--allow-empty', '-q', '-m', 'second']);
676 git(work, ['push', '-q', bare, 'HEAD:refs/heads/main']);
677 const seen: { authorization: string | null; cookie: string | null; gitToken: string | null; ua: string | null }[] = [];
678 const dial = rewriteAllowlisted(srv.url);
679 let rayN = 0;
680 const fetchImpl: typeof fetch = async (input, init) => {
681 const h = new Headers(slot(init, 'headers') as HeadersInit);
682 seen.push({ authorization: h.get('authorization'), cookie: h.get('cookie'), gitToken: h.get('git-token'), ua: h.get('user-agent') });
683 const res = await dial(input, init);
684 const headers = new Headers(res.headers);
685 headers.set('cf-ray', `${(++rayN).toString(16).padStart(16, '0')}-IAD`);
686 return new Response(res.body, { status: res.status, statusText: res.statusText, headers });
687 };
688 const env = viewerEnv();
689 const repoPath = `/view/github.com/${name}.git`;
690 const page = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}`, {
691 headers: { authorization: 'Bearer viewer-should-ignore', cookie: 'session=secret', 'git-token': 'git-secret', 'cf-connecting-ip': '203.0.113.9', 'cf-ray': 'ray-test' },
692 }), env, {}, { fetchImpl });
693 assert.equal(page.status, 200, await page.clone().text());
694 const html = await page.text();
695 assert.match(html, /README\.md/);
696 assert.match(html, /hello viewer/);
697 assert.equal(html.includes('127.0.0.1'), false);
698 assert.match(html, /refs\/heads\/main/);
699 assert.match(html, /upload-pack/);
700 assert.match(html, /ray-test/);
701 assert.equal(html.includes('<script>alert'), false);
702 assert.match(html, /<script>/);
703 assert.match(fill(page.headers.get('content-security-policy'), ''), /script-src 'none'/);
704 assert.equal((fill(page.headers.get('content-security-policy'), '')).includes('unsafe-inline'), false);
705 assert.match(fill(page.headers.get('content-security-policy'), ''), /style-src 'self'/);
706 assert.equal(page.headers.get('x-content-type-options'), 'nosniff');
707 assert.equal(page.headers.get('cache-control'), 'no-store');
708 const rayHeader = fill(page.headers.get('x-artifacts-ray'), '');
709 assert.match(rayHeader, /ls-refs=[0-9a-f]{16}-IAD;dur=\d+/);
710 assert.match(fill(page.headers.get('server-timing'), ''), /ls-refs;dur=\d+/);
711 assert.match(fill(page.headers.get('server-timing'), ''), /total;dur=\d+/);
712 for (const secret of ['127.0.0.1', 'viewer-should-ignore', 'git-secret', 'github.com', 'session=secret']) assert.equal(rayHeader.includes(secret), false, secret);
713 assert.equal(html.includes('viewer-should-ignore'), false);
714 assert.equal(html.includes('git-secret'), false);
715 assert.ok(seen.length > 0);
716 for (const hop of seen) {
717 assert.equal(hop.authorization, null);
718 assert.equal(hop.cookie, null);
719 assert.equal(hop.gitToken, null);
720 assert.equal(hop.ua, VIEWER_UA);
721 }
722 const alias = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}?at=v99.0.0`), env, {}, { fetchImpl });
723 assert.equal(alias.status, 302);
724 assert.equal(alias.headers.get('location'), `${repoPath}?ref=v99.0.0`);
725 const missingRef = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}?ref=v99.0.0`), env, {}, { fetchImpl });
726 assert.equal(missingRef.status, 404);
727 const missingHtml = await missingRef.text();
728 assert.match(missingHtml, /No ref v99\.0\.0 in demo/);
729 assert.match(missingHtml, /ref picker/);
730 assert.match(missingHtml, /href="\/view\/github\.com\/demo\.git\?ref=refs\/heads\/main"/);
731 assert.equal(missingHtml.includes('Check the owner'), false);
732 assert.doesNotMatch(missingHtml, /HTTP 401|answers 401/); // not a bare '401': request ids and timings on the page can contain it
733 assert.match(missingHtml, /href="\/view\/github\.com\/demo\.git"/);
734 const missingPath = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}?path=missing`), env, {}, { fetchImpl });
735 assert.equal(missingPath.status, 404);
736 const missingPathHtml = await missingPath.text();
737 assert.match(missingPathHtml, /No path missing in demo/);
738 assert.match(missingPathHtml, /No entry named missing/);
739 assert.match(missingPathHtml, /the repository root/);
740 assert.doesNotMatch(missingPathHtml, /HTTP 401|answers 401/); // not a bare '401': request ids and timings on the page can contain it
741 const fileAsDir = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}?path=README.md/extra`), env, {}, { fetchImpl });
742 assert.equal(fileAsDir.status, 404);
743 const fileAsDirHtml = await fileAsDir.text();
744 assert.match(fileAsDirHtml, /README\.md is a file, not a directory/);
745 assert.match(fileAsDirHtml, /the repository root/);
746 const tip = git(bare, ['rev-parse', 'refs/heads/main']).trim();
747 const short = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}?ref=${tip.slice(0, 8)}`), env, {}, { fetchImpl });
748 assert.equal(short.status, 200, await short.clone().text());
749 const file = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}?ref=main&path=README.md`), env, {}, { fetchImpl });
750 assert.equal(file.status, 200);
751 const fileHtml = await file.text();
752 assert.match(fileHtml, /class="ln"/);
753 assert.match(fileHtml, /id="L1"/);
754 assert.match(fileHtml, /hello viewer/);
755 assert.match(fileHtml, /Wrap lines/);
756 assert.match(fileHtml, /class="code"/);
757 const raw = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}?path=README.md&raw=1`), env, {}, { fetchImpl });
758 assert.equal(raw.status, 200);
759 assert.match(fill(raw.headers.get('content-type'), ''), /^text\/plain/);
760 assert.equal(await raw.text(), 'hello viewer\n');
761 const bin = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}?path=pic.bin&raw=1`), env, {}, { fetchImpl });
762 assert.equal(bin.headers.get('content-type'), 'application/octet-stream');
763 assert.deepEqual(new Uint8Array(await bin.arrayBuffer()), new Uint8Array([0, 1, 2, 255]));
764 const pack = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}?want=${head}&filter=tree:0`), env, {}, { fetchImpl });
765 assert.equal(pack.status, 200);
766 const packHtml = await pack.text();
767 assert.match(packHtml, /Pack map/);
768 assert.match(packHtml, /ofs-delta/);
769 assert.equal(packHtml.includes('style='), false);
770 const css = await handleViewer(new Request('https://ply.ivar.workers.dev/view.css'), env, {});
771 assert.equal(css.status, 200);
772 assert.match(fill(css.headers.get('content-type'), ''), /text\/css/);
773 const home = await handleViewer(new Request('https://ply.ivar.workers.dev/view'), env, {});
774 assert.equal(home.headers.get('x-ply-trace'), null);
775 assert.match(fill(home.headers.get('x-ply-build'), ''), /^ply=/);
776 const homeHtml = await home.text();
777 assert.match(homeHtml, /octocat\/Hello-World/);
778 assert.match(homeHtml, /bitbucket\.org/);
779 assert.match(homeHtml, /git\.sr\.ht/);
780 assert.equal(homeHtml.includes('artifacts.cloudflare.net'), false);
781 const redir = await handleViewer(new Request(`https://ply.ivar.workers.dev/view?remote=${encodeURIComponent(`https://github.com/${name}.git`)}`), env, {});
782 assert.equal(redir.status, 302);
783 assert.equal(redir.headers.get('location'), `/view/github.com/${name}.git`);
784 const localRemote = await handleViewer(new Request(`https://ply.ivar.workers.dev/view?remote=${encodeURIComponent(`${srv.url}/${name}.git`)}`), env, {});
785 assert.equal(localRemote.status, 400);
786 const localHtml = await localRemote.text();
787 assert.match(localHtml, /Supported hosts/);
788 assert.equal(localHtml.includes('127.0.0.1'), false);
789 const extra = await handleViewer(new Request(`https://ply.ivar.workers.dev/view?remote=${encodeURIComponent('https://acct.artifacts.cloudflare.net/git/ns/repo.git')}`), viewerEnv({ VIEWER_EXTRA_HOSTS: 'acct.artifacts.cloudflare.net' }), {});
790 assert.equal(extra.status, 302);
791 assert.equal(extra.headers.get('location'), '/view/acct.artifacts.cloudflare.net/git/ns/repo.git');
792 const wildcard = await handleViewer(new Request(`https://ply.ivar.workers.dev/view?remote=${encodeURIComponent('https://acct.artifacts.cloudflare.net/git/ns/repo.git')}`), viewerEnv({ VIEWER_EXTRA_HOSTS: '*.artifacts.cloudflare.net' }), {});
793 assert.equal(wildcard.status, 400);
794 const secret = await handleViewer(new Request('https://ply.ivar.workers.dev/view?remote=https://github.com/a/b&token=super-secret-token'), env, {});
795 assert.equal(secret.status, 400);
796 assert.equal((await secret.text()).includes('super-secret-token'), false);
797 const ssrf = await handleViewer(new Request('https://ply.ivar.workers.dev/view/169.254.169.254/latest'), env, {}, { fetchImpl: async () => { throw new Error('ssrf fetched'); } });
798 assert.equal(ssrf.status, 400);
799 assert.match(await ssrf.text(), /Supported hosts: github.com, gitlab.com, codeberg.org, bitbucket.org, git.sr.ht/);
800 const hostile = await handleViewer(new Request(`https://ply.ivar.workers.dev${repoPath}?path=${encodeURIComponent('<script>alert(1)<script>')}`), env, {}, { fetchImpl });
801 assert.equal(hostile.status, 404);
802 const hostileHtml = await hostile.text();
803 assert.equal(hostileHtml.includes('<script>alert'), false);
804 assert.match(hostileHtml, /<script>/);
805 } finally {
806 await srv.close();
807 cleanup();
808 }
809});
811test('a tree:0 rejection is visible, then the blob:none retry, and a denied rate limit does no fetch', async () => {
812 const { root, cleanup } = tmpRoot();
813 const upstream = await startGitServer(root);
814 const filterOf = (body: Uint8Array): string | null => fill(slot(/filter ([^\n]+)\n/.exec(Buffer.from(body).toString('latin1')), '1'), null);
815 const proxy = await startProxy(upstream.url, (ctx: { stage: string; reqBody: Uint8Array }) => (
816 ctx.stage === 'fetch' && filterOf(ctx.reqBody) && filterOf(ctx.reqBody) !== 'blob:none'
817 ? { type: 'status', status: 400, contentType: 'text/plain', body: 'Unsupported Git fetch filter; only blob:none is supported.' }
818 : { type: 'pass' }
819 ));
820 try {
821 const bare = makeBareRepo(root, 'art');
822 const work = join(root, 'art-work');
823 mkdirSync(work);
824 git(work, ['init', '-q', '-b', 'main']);
825 commitFiles(work, bare, { 'README.md': 'artifacts-shaped\n' }, 'one');
826 // Twelve commits, each touching its own directory, so a blob:none window carries trees as well as commits.
827 const subjects = ['one'];
828 for (let n = 2; n <= 12; n++) { subjects.push(`commit ${n}`); commitFiles(work, bare, { [`d${n}/f.txt`]: `${n}\n` }, `commit ${n}`); }
829 let fetches = 0;
830 const bodies: string[] = [];
831 const dial = rewriteAllowlisted(proxy.url);
832 const fetchImpl: typeof fetch = async (input, init) => {
833 fetches++;
834 const body = slot(init, 'body');
835 if (body) bodies.push(Buffer.from(body as Uint8Array).toString('latin1'));
836 return dial(input, init);
837 };
838 const env = viewerEnv();
839 const res = await handleViewer(new Request('https://ply.ivar.workers.dev/view/github.com/art.git', { headers: { 'cf-connecting-ip': '203.0.113.10' } }), env, {}, { fetchImpl });
840 assert.equal(res.status, 200, await res.clone().text());
841 const html = await res.text();
842 assert.match(html, /filter tree:0 was rejected/);
843 assert.match(html, /<span class="num wf-status" role="cell">400<\/span>/);
844 // the refusal ply handled (blob:none answered it) is drawn as the fallback, not as a failed call
845 assert.equal(html.includes('<details class="wf-row err"'), false, 'a handled tree:0 refusal is not an error row');
846 assert.match(html, /<details class="wf-row" role="row"><summary class="wf-sum"><span class="wf-name"[^>]*><span class="wf-call">fetch<\/span><span class="wf-tag">tree:0[^<]*<\/span><\/span><span class="num wf-status" role="cell">400<\/span>[\s\S]*?wf-bar c-fallback/);
847 assert.match(html, /blob:none/);
848 // The log fix: on a server that refuses tree:0, Recent commits used to stop after one commit ("Log stopped").
849 // One fetch (want HEAD, deepen 10, blob:none after the 400) now carries the whole first-parent window.
850 const shown = [...html.matchAll(/<span class="log-subject">([^<]*)<\/span>/g)].map(m => m[1]);
851 assert.deepEqual(shown, subjects.slice().reverse().slice(0, VIEWER_CAPS.maxLogCommits));
852 assert.equal(html.includes('Log stopped'), false);
853 const fetchBodies = bodies.filter(body => body.includes('command=fetch'));
854 // tree:0 window (400) + blob:none window + README blob. No per-commit fetches.
855 assert.equal(fetchBodies.length, 3, fetchBodies.join('\n---\n'));
856 assert.equal(fetchBodies.filter(body => body.includes(`deepen ${VIEWER_CAPS.maxLogCommits}`)).length, 2);
857 assert.ok(fetches <= 5, `fetches ${fetches}`);
858 const limited = fakeRate();
859 let called = 0;
860 const denyEnv = { VIEWER_RATE: limited, PACK_CACHE: 'off', VIEWER_RATE_PER_MIN: '1' };
861 const first = await handleViewer(new Request('https://ply.ivar.workers.dev/view', { headers: { 'cf-connecting-ip': '203.0.113.11' } }), denyEnv, {}, { fetchImpl: async () => { called++; throw new Error('no'); }, now: 1_000 });
862 assert.equal(first.status, 200);
863 const second = await handleViewer(new Request('https://ply.ivar.workers.dev/view/github.com/art.git', { headers: { 'cf-connecting-ip': '203.0.113.11' } }), denyEnv, {}, { fetchImpl: async () => { called++; throw new Error('fetched while limited'); }, now: 1_500 });
864 assert.equal(second.status, 429);
865 assert.equal(second.headers.get('retry-after'), '1');
866 assert.match(await second.text(), /Too many requests/);
867 assert.equal(called, 0);
868 assert.ok(fetches > 0);
869 const missing = await handleViewer(new Request('https://ply.ivar.workers.dev/view'), {}, {});
870 assert.equal(missing.status, 503);
871 } finally {
872 await proxy.close();
873 await upstream.close();
874 cleanup();
875 }
876});
878const WORKERD = [process.env.WORKERD, fileURLToPath(new URL('../node_modules/.bin/workerd', import.meta.url))].filter((p): p is string => !!p && existsSync(p));
880test('workerd: sqlite rate limit, and /v1 still requires the bearer', { skip: WORKERD.length === 0 && 'workerd not installed' }, async () => {
881 const { root, cleanup } = tmpRoot();
882 const out = join(root, 'wk');
883 mkdirSync(out);
884 copyCfBundle(join(out, 'worker.mjs'));
885 const port = 19100 + (process.pid % 40);
886 const ply = 'test-ply-token-' + 'x'.repeat(20);
887 writeFileSync(join(out, 'config.capnp'), `using Workerd = import "/workerd/workerd.capnp";
888const config :Workerd.Config = (
889 services = [ (name = "main", worker = (
890 modules = [ (name = "worker.mjs", esModule = embed "worker.mjs") ],
891 compatibilityDate = "2026-10-18",
892 durableObjectNamespaces = [ (className = "ViewerRateLimit", uniqueKey = "viewer-rate-test", enableSql = true) ],
893 durableObjectStorage = (inMemory = void),
894 bindings = [
895 (name = "PLY_TOKEN", text = "${ply}"),
896 (name = "WRITES", text = "off"),
897 (name = "PACK_CACHE", text = "off"),
898 (name = "VIEWER_RATE_PER_MIN", text = "2"),
899 (name = "VIEWER_RATE", durableObjectNamespace = "ViewerRateLimit")
900 ]
901 )), (name = "internet", network = (allow = ["private", "public", "local"])) ],
902 sockets = [ (name = "http", address = "127.0.0.1:${port}", http = (), service = "main") ]
903);
904`);
905 let logs = '';
906 const proc = spawn(WORKERD[0] as string, ['serve', join(out, 'config.capnp'), '--verbose'], { detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
907 proc.stdout.on('data', d => { logs += d; });
908 proc.stderr.on('data', d => { logs += d; });
909 try {
910 let up = false;
911 for (let i = 0; i < 80 && !up; i++) {
912 try { up = (await fetch(`http://127.0.0.1:${port}/`)).ok; } catch { await new Promise(r => setTimeout(r, 100)); }
913 }
914 assert.equal(up, true, logs.slice(-1500));
915 const hit = (ip: string) => fetch(`http://127.0.0.1:${port}/view`, { headers: { 'cf-connecting-ip': ip } });
916 assert.equal((await hit('198.51.100.8')).status, 200);
917 assert.equal((await hit('198.51.100.8')).status, 200);
918 const limited = await hit('198.51.100.8');
919 const limitedBody = await limited.text();
920 assert.equal(limited.status, 429, `${limitedBody}\n${logs.slice(-2000)}`);
921 assert.equal((await hit('198.51.100.9')).status, 200);
922 const blocked = await fetch(`http://127.0.0.1:${port}/view/127.0.0.1/latest/meta-data`);
923 assert.equal(blocked.status, 400);
924 const blockedHtml = await blocked.text();
925 assert.match(blockedHtml, /Supported hosts: github.com, gitlab.com, codeberg.org, bitbucket.org, git.sr.ht/);
926 const gated = await fetch(`http://127.0.0.1:${port}/v1/refs?remote=${encodeURIComponent('https://github.com/octocat/Hello-World.git')}`);
927 assert.equal(gated.status, 401);
928 const leaked = await fetch(`http://127.0.0.1:${port}/view/user:super-secret-token@github.com/octocat/Hello-World`);
929 assert.equal(leaked.status, 400);
930 assert.equal((await leaked.text()).includes('super-secret-token'), false);
931 await new Promise(r => setTimeout(r, 200));
932 assert.equal(logs.includes('super-secret-token'), false);
933 } finally {
934 if (proc.pid) { try { process.kill(-proc.pid, 'SIGKILL'); } catch { proc.kill('SIGKILL'); } }
935 else proc.kill('SIGKILL');
936 cleanup();
937 }
938});
940test('a public GitHub repository renders when the network can reach it', { timeout: 40_000 }, async (t) => {
941 const probe = await fetch('https://github.com/octocat/Hello-World.git/info/refs?service=git-upload-pack', {
942 headers: { 'git-protocol': 'version=2', 'user-agent': VIEWER_UA },
943 signal: AbortSignal.timeout(8000),
944 }).catch(() => null);
945 if (probe === null || !probe.ok) {
946 const body = slot(probe, 'body');
947 if (body !== null && body !== void 0) {
948 const cancel = slot(body, 'cancel');
949 if (cancel !== null && cancel !== void 0) await (cancel as (this: unknown) => Promise<unknown>).call(body).catch(() => {});
950 }
951 return t.skip('GitHub is unreachable from this environment');
952 }
953 await probe.arrayBuffer();
954 const env = { VIEWER_RATE: fakeRate(), PACK_CACHE: 'off' };
955 const res = await handleViewer(new Request('https://ply.ivar.workers.dev/view/github.com/octocat/Hello-World', { headers: { 'cf-connecting-ip': '203.0.113.50', 'cf-ray': 'github-ray' } }), env, {});
956 assert.equal(res.status, 200, await res.clone().text());
957 const html = await res.text();
958 assert.match(html, /Hello-World/);
959 assert.match(html, /upload-pack/);
960 assert.match(html, /github-ray/);
961 assert.match(fill(res.headers.get('content-security-policy'), ''), /script-src 'none'/);
962 assert.equal((fill(res.headers.get('content-security-policy'), '')).includes('unsafe-inline'), false);
963 const want = slot(/[0-9a-f]{40}/.exec(html), '0');
964 assert.ok(want);
965 const pack = await handleViewer(new Request(`https://ply.ivar.workers.dev/view/github.com/octocat/Hello-World?want=${want}&filter=blob:none`), env, {});
966 assert.equal(pack.status, 200, await pack.clone().text());
967 const packHtml = await pack.text();
968 assert.match(packHtml, /Pack map/);
969 assert.match(packHtml, /agent /);
970});
972// git's http.followRedirects=initial (ply-web#117): the viewer's transport lets ply follow the first GET info/refs and
973// rebase the repo there (viewerAllowUrl checks it is still this repo). createViewerFetch never follows a 3xx itself. A
974// redirected POST is an error naming the target, and a loop is cut at VIEWER_CAPS.maxRedirects by ply. This pins it
975// against a real git http-backend.
976test('same-repo redirects: ply follows the first info/refs and rebases; a POST redirect is an error; a loop is cut', async () => {
977 const { root, cleanup } = tmpRoot();
978 const srv = await startGitServer(root);
979 try {
980 const name = 'moved';
981 const bare = makeBareRepo(root, name);
982 const work = join(root, 'work');
983 mkdirSync(work);
984 git(work, ['init', '-q', '-b', 'main']);
985 const head = commitFiles(work, bare, { 'README.md': 'redirected\n' }, 'seed');
986 const dial = rewriteAllowlisted(srv.url);
987 // The host answers `/moved/...` (GET advertisement and POST upload-pack alike) with a 301 to `/moved.git/...`.
988 // `postOnly` redirects only the POSTs and serves the GET from `/moved.git/` in place.
989 const hops: { method: string; url: string; status: number }[] = [];
990 const redirector = (postOnly: boolean): typeof fetch => async (input, init) => {
991 const url = new URL(typeof input === 'string' ? input : input instanceof URL ? input.href : input.url);
992 const method = String(fill(slot(init, 'method'), 'GET'));
993 if (url.pathname.startsWith(`/${name}/`) && (method === 'POST' || !postOnly)) {
994 hops.push({ method, url: url.href, status: 301 });
995 return new Response(null, { status: 301, headers: { location: url.href.replace(`/${name}/`, `/${name}.git/`) } });
996 }
997 const res = await dial(postOnly ? url.href.replace(`/${name}/`, `/${name}.git/`) : input, init);
998 hops.push({ method, url: url.href, status: res.status });
999 return res;
1000 };
1001 const redirecting = redirector(false);
1002 const expected = git(bare, ['rev-parse', 'refs/heads/main']).trim();
1003 assert.equal(expected, head);
1004 const repo = loc(`https://github.com/${name}`);
1005 // Each scenario fakes a different host at the same URL: ply's per-remote memo of the settled redirect is cleared
1006 // first, as its docs say for this case.
1007 const viewerTransport = (fetchImpl: typeof fetch) => {
1008 forgetQuirks();
1009 return unwrap(createTransport(`https://github.com/${name}`, { fetchImpl, limits: VIEWER_GIT_LIMITS, tracer: new Tracer(), followRedirects: 'initial', allowUrl: viewerAllowUrl(repo.url, ALLOWED_HOSTS), maxRedirects: VIEWER_CAPS.maxRedirects }));
1010 };
1012 // Through createViewerFetch: the first GET is redirected once, then everything goes to the new base.
1013 hops.length = 0;
1014 const t = viewerTransport(createViewerFetch(redirecting, repo.url, ALLOWED_HOSTS, createBudget(), AbortSignal.timeout(10_000)));
1015 const { refs } = await unwrapAsync(lsRefs(t, ['refs/heads/']));
1016 assert.equal(slot(refs.get('refs/heads/main'), 'oid'), expected);
1017 assert.equal(t.url, `https://github.com/${name}.git`);
1018 assert.deepEqual(hops.map(h => [h.method, h.status, h.url.split('?')[0]]), [
1019 ['GET', 301, `https://github.com/${name}/info/refs`],
1020 ['GET', 200, `https://github.com/${name}.git/info/refs`],
1021 ['POST', 200, `https://github.com/${name}.git/git-upload-pack`],
1022 ]);
1024 // A POST redirect is not followed: ply's http error names the target.
1025 hops.length = 0;
1026 const p = viewerTransport(createViewerFetch(redirector(true), repo.url, ALLOWED_HOSTS, createBudget(), AbortSignal.timeout(10_000)));
1027 await assert.rejects(unwrapAsync(lsRefs(p, ['refs/heads/'])), (err: Partial<{ code: string; status: number; location: string; message: string }>) =>
1028 err.code === 'http' && err.status === 301 && err.location === `https://github.com/${name}.git/git-upload-pack` && /redirected to/.test(fill(err.message, '')));
1029 assert.deepEqual(hops.map(h => [h.method, h.status]), [['GET', 200], ['POST', 301]], 'the redirected POST was not re-sent');
1031 // End to end: /view on the redirecting spelling renders the repository.
1032 forgetQuirks();
1033 const page = await handleViewer(new Request(`https://ply.ivar.workers.dev/view/github.com/${name}?path=README.md`), viewerEnv(), {}, { fetchImpl: redirecting });
1034 assert.equal(page.status, 200, await page.clone().text());
1035 assert.match(await page.text(), /redirected/);
1037 // A redirect loop is cut by ply at the hop cap, with the http error.
1038 let loops = 0;
1039 const loop = createViewerFetch(async () => { loops++; return new Response(null, { status: 302, headers: { location: `https://github.com/${name}/info/refs?service=git-upload-pack` } }); }, repo.url, ALLOWED_HOSTS, createBudget(), AbortSignal.timeout(1000));
1040 await assert.rejects(unwrapAsync(lsRefs(viewerTransport(loop), ['refs/heads/'])), (err: Partial<{ code: string; status: number }>) => err.code === 'http' && err.status === 302);
1041 assert.equal(loops, VIEWER_CAPS.maxRedirects + 1);
1042 } finally {
1043 await srv.close();
1044 cleanup();
1045 }
1046});
1049test('history in one call: on a tree:0 server the page fetches the head and its parents with one deepen fetch', async () => {
1050 const { root, cleanup } = tmpRoot();
1051 const srv = await startGitServer(root);
1052 try {
1053 const bare = makeBareRepo(root, 'hist');
1054 const work = join(root, 'w-hist');
1055 mkdirSync(work);
1056 git(work, ['init', '-q', '-b', 'main']);
1057 commitFiles(work, bare, { 'README.md': 'history\n' }, 'subject-0');
1058 for (let i = 1; i <= 6; i++) {
1059 git(work, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit', '--allow-empty', '-q', '-m', `subject-${i}`]);
1060 }
1061 git(work, ['push', '-q', bare, 'HEAD:refs/heads/main']);
1062 const calls: { method: string; body: string }[] = [];
1063 const dial = rewriteAllowlisted(srv.url);
1064 const fetchImpl: typeof fetch = async (input, init) => {
1065 const body = slot(init, 'body');
1066 calls.push({ method: fill(slot(init, 'method'), 'GET'), body: body instanceof Uint8Array ? Buffer.from(body).toString('latin1') : '' });
1067 return dial(input, init);
1068 };
1069 const page = await handleViewer(new Request('https://ply.ivar.workers.dev/view/github.com/hist.git'), viewerEnv(), {}, { fetchImpl });
1070 assert.equal(page.status, 200);
1071 const html = await page.text();
1072 for (let i = 6; i >= 2; i--) assert.match(html, new RegExp(`subject-${i}\\b`), `subject-${i} in the log`);
1073 const fetches = calls.filter(c => c.body.includes('command=fetch'));
1074 const history = fetches.filter(c => new RegExp(`deepen ${VIEWER_CAPS.maxLogCommits}\n`).test(c.body) && /filter tree:0\n/.test(c.body));
1075 assert.equal(history.length, 1, 'one history fetch');
1076 assert.equal(fetches.filter(c => /deepen 1\n/.test(c.body)).length, 0, 'no per-commit fetches');
1077 // discover + ls-refs + history + root tree + README blob
1078 assert.equal(calls.length, 5, calls.map(c => c.body.slice(4, 30)).join(' | '));
1079 } finally {
1080 await srv.close();
1081 cleanup();
1082 }
1083});
1085test('path walk (ply walkCommit, #222): a server that takes combine:blob:none+tree:K gets the commit and two tree levels beside the window', async () => {
1086 const { root, cleanup } = tmpRoot();
1087 const srv = await startGitServer(root);
1088 try {
1089 const bare = makeBareRepo(root, 'walk');
1090 const work = join(root, 'w-walk');
1091 mkdirSync(work);
1092 git(work, ['init', '-q', '-b', 'main']);
1093 commitFiles(work, bare, { 'README.md': 'walk\n', 'a/b/c/f.txt': 'deep\n', 'a/x.txt': 'x\n' }, 'seed');
1094 const calls: string[] = [];
1095 const dial = rewriteAllowlisted(srv.url);
1096 const fetchImpl: typeof fetch = async (input, init) => {
1097 const body = slot(init, 'body');
1098 const text = body instanceof Uint8Array ? Buffer.from(body).toString('latin1') : '';
1099 const filter = /filter ([^\n]+)\n/.exec(text);
1100 const deepen = /deepen (\d+)\n/.exec(text);
1101 calls.push(text === '' ? 'GET' : text.includes('command=ls-refs') ? 'ls-refs' : `fetch ${filter ? filter[1] : 'none'}${deepen ? ` d${deepen[1]}` : ''}`);
1102 return dial(input, init);
1103 };
1104 const page = async (): Promise<string[]> => {
1105 calls.length = 0;
1106 const res = await handleViewer(new Request('https://ply.ivar.workers.dev/view/github.com/walk.git?path=a/b/c/f.txt'), viewerEnv(), {}, { fetchImpl });
1107 assert.equal(res.status, 200);
1108 assert.match(await res.text(), /deep/);
1109 return calls.slice();
1110 };
1111 // first contact: nothing known about tree:0, so the walk starts from the window's commit, one tree per level
1112 const first = await page();
1113 assert.equal(first.filter(c => c.startsWith('fetch combine')).length, 0, first.join(' | '));
1114 assert.equal(first.filter(c => c === 'fetch tree:0').length, 4, first.join(' | '));
1115 // tree:0 known: walkCommit beside the window races the combined fetch once (and its tree:0 commit read)
1116 const second = await page();
1117 assert.deepEqual(second.filter(c => c.startsWith('fetch combine')), ['fetch combine:blob:none+tree:2 d1'], second.join(' | '));
1118 // learned: the combined fetch alone brings the root and a/; a/b and a/b/c are read per level, then the blob
1119 const third = await page();
1120 const fetches = (cs: string[]) => [...cs.filter(c => c.startsWith('fetch'))].sort();
1121 assert.deepEqual(fetches(third), ['fetch combine:blob:none+tree:2 d1', 'fetch none', 'fetch tree:0', 'fetch tree:0', `fetch tree:0 d${VIEWER_CAPS.maxLogCommits}`].sort(), third.join(' | '));
1122 assert.deepEqual(fetches(first), ['fetch none', 'fetch tree:0', 'fetch tree:0', 'fetch tree:0', 'fetch tree:0', `fetch tree:0 d${VIEWER_CAPS.maxLogCommits}`].sort(), first.join(' | '));
1123 // a missing component keeps the viewer's hint
1124 const missing = await handleViewer(new Request('https://ply.ivar.workers.dev/view/github.com/walk.git?path=a/nope'), viewerEnv(), {}, { fetchImpl });
1125 assert.equal(missing.status, 404);
1126 assert.match(await missing.text(), /No entry named nope/);
1127 const notDir = await handleViewer(new Request('https://ply.ivar.workers.dev/view/github.com/walk.git?path=a/x.txt/y'), viewerEnv(), {}, { fetchImpl });
1128 assert.equal(notDir.status, 404);
1129 } finally {
1130 await srv.close();
1131 cleanup();
1132 }
1133});
1135test('a server that advertises no filter (git.sr.ht, stock git; #121): pages read the window and trees without a filter', async () => {
1136 const { root, cleanup } = tmpRoot();
1137 const srv = await startGitServer(root);
1138 try {
1139 const bare = makeBareRepo(root, 'nofilter');
1140 git(bare, ['config', 'uploadpack.allowFilter', 'false']);
1141 const work = join(root, 'w-nofilter');
1142 mkdirSync(work);
1143 git(work, ['init', '-q', '-b', 'main']);
1144 commitFiles(work, bare, { 'README.md': 'no filter here\n', 'src/a.txt': 'a\n' }, 'first');
1145 commitFiles(work, bare, { 'src/b.txt': 'b\n' }, 'second');
1146 const filters: string[] = [];
1147 const dial = rewriteAllowlisted(srv.url);
1148 const fetchImpl: typeof fetch = async (input, init) => {
1149 const body = slot(init, 'body');
1150 const text = body instanceof Uint8Array ? Buffer.from(body).toString('latin1') : '';
1151 if (text.includes('command=fetch')) filters.push(fill(slot(/filter ([^\n]+)\n/.exec(text), '1'), 'none'));
1152 return dial(input, init);
1153 };
1154 for (const path of ['', 'src', 'src/b.txt']) {
1155 const res = await handleViewer(new Request(`https://ply.ivar.workers.dev/view/github.com/nofilter.git${path ? `?path=${path}` : ''}`), viewerEnv(), {}, { fetchImpl });
1156 const html = await res.text();
1157 assert.equal(res.status, 200, html.slice(0, 400));
1158 if (path === '') { assert.match(html, /no filter here/); assert.match(html, /second/); assert.match(html, /first/); }
1159 }
1160 assert.deepEqual([...new Set(filters)], ['none'], filters.join(' | '));
1161 } finally {
1162 await srv.close();
1163 cleanup();
1164 }
1165});
1167test('scraping contract: li[data-oid] rows equal git ls-tree, and the commit card carries data-commit', async () => {
1168 // External monitors (ply issue #44) count these rows and compare the oids to the forge's tree API.
1169 // A redesign may restyle anything, but must keep: li[data-oid][data-mode][data-type][data-name], ul.files[data-entries],
1170 // and [data-commit] on the latest-commit card. See README "Machine-readable markup".
1171 const { root, cleanup } = tmpRoot();
1172 const srv = await startGitServer(root);
1173 try {
1174 const bare = makeBareRepo(root, 'scrape');
1175 const work = join(root, 'w-scrape');
1176 mkdirSync(work);
1177 git(work, ['init', '-q', '-b', 'main']);
1178 const files = { 'README.md': 'x\n', 'src/a.js': 'a\n', 'src/b.ts': 'b\n', 'src/run.sh': { data: '#!/bin/sh\n', mode: '100755' }, 'src/lib/c.js': 'c\n', 'src/lib/d/e.js': 'e\n' };
1179 const head = commitFiles(work, bare, files, 'seed');
1180 const dial = rewriteAllowlisted(srv.url);
1181 const page = await handleViewer(new Request('https://ply.ivar.workers.dev/view/github.com/scrape.git?path=src'), viewerEnv(), {}, { fetchImpl: dial });
1182 assert.equal(page.status, 200);
1183 const html = await page.text();
1184 const rows = [...html.matchAll(/<li data-oid="([0-9a-f]{40})" data-mode="([0-7]{6})" data-type="(blob|tree|commit)" data-name="([^"]*)">/g)]
1185 .map(m => `${m[2]} ${m[3]} ${m[1]}\t${m[4]}`).sort();
1186 const expected = git(bare, ['ls-tree', `${head}:src`]).trim().split('\n').sort();
1187 assert.deepEqual(rows, expected);
1188 assert.match(html, new RegExp(`<ul class="files"[^>]* data-path="src" data-entries="${expected.length}" data-shown="${expected.length}">`));
1189 assert.match(html, new RegExp(`data-commit="${head}"`));
1190 // The parent-directory row is not an entry.
1191 assert.equal([...html.matchAll(/<li data-oid=/g)].length, expected.length);
1192 } finally {
1193 await srv.close();
1194 cleanup();
1195 }
1196});
1198test('stylesheet URL is content-hashed and immutable; HTML is no-store and links the sheet it was rendered with', async () => {
1199 // 2026-10-31: an iPhone kept the old /view.css (max-age=3600) and rendered the redesigned markup unstyled.
1200 const env = viewerEnv();
1201 const home = await handleViewer(new Request('https://ply.ivar.workers.dev/view'), env, {});
1202 assert.equal(home.headers.get('cache-control'), 'no-store');
1203 const href = /<link rel="stylesheet" href="(\/view\.([0-9a-f]{12})\.css)"\/>/.exec(await home.text());
1204 assert.ok(href !== null && href[1] && href[2], 'hashed stylesheet href');
1205 const sheet = await handleViewer(new Request(`https://ply.ivar.workers.dev${href[1]}`), env, {});
1206 assert.equal(sheet.status, 200);
1207 assert.equal(sheet.headers.get('cache-control'), 'public, max-age=31536000, immutable');
1208 const body = await sheet.text();
1209 // The href names the bytes actually served.
1210 assert.equal(new Sha1().update(new TextEncoder().encode(body)).digest().reduce((s: string, b: number) => s + b.toString(16).padStart(2, '0'), '').slice(0, 12), href[2]);
1211 assert.equal(body, VIEWER_CSS);
1212 // Old HTML (/view.css) and a previous deploy's hash get the current sheet, but must revalidate.
1213 for (const old of ['/view.css', '/view.000000000000.css']) {
1214 const res = await handleViewer(new Request(`https://ply.ivar.workers.dev${old}`), env, {});
1215 assert.equal(res.status, 200, old);
1216 assert.equal(res.headers.get('cache-control'), 'no-cache', old);
1217 assert.equal(await res.text(), VIEWER_CSS, old);
1218 }
1219 const revalidated = await handleViewer(new Request('https://ply.ivar.workers.dev/view.css', { headers: { 'if-none-match': `"${href[2]}"` } }), env, {});
1220 assert.equal(revalidated.status, 304);
1221 // Icons carry intrinsic sizes, so even an unstyled page never draws a giant SVG.
1222 const html = String(renderLoaded(treeFixture({}), { bars: [], totalMs: 0, cfRay: 'r', requestId: 'q' }));
1223 for (const m of html.matchAll(/<svg class="i [^"]*"([^>]*)>/g)) assert.match(fill(m[1], ''), /width="16" height="16"/);
1224});
1226test('quirks: the public namespace (Cloudflare Artifacts, blob:none only) starts on blob:none; any other origin starts on tree:0', () => {
1227 assert.equal(quirksFor(`https://${PUBLIC_GIT_HOST}`).treeFilter, 'blob:none');
1228 assert.equal(quirksFor('https://github.com').treeFilter, 'tree:0');
1229 assert.equal(quirksFor('https://acct.artifacts.cloudflare.net').treeFilter, 'tree:0', 'only the configured public namespace host is seeded');
1230});
Diagnostics4 git calls · 28 ms
CallStatusSizeTime028 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 · hit4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=4 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 · hit3 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=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=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
fetch20021 KiB · hit21 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=21 git.response.bytes=21154 git.response.cache=HIT git.response.duration_ms=21 git.fetch.wants=4ad4a74c857cc1a8c8f179361bb4524b3fe5e21c 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=21154 git.pkt.payload.max=21133 git.sideband.band1.bytes=21132 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
- a5e2e0f07f8a4af4
- Request
- 0f017a32…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format