main
1// Markdown breakout, the /v1 remote policy, viewer dot-segments, and security headers.
2import { test } from 'node:test';
3import assert from 'node:assert/strict';
4import { parseFragment, type DefaultTreeAdapterMap } from 'parse5';
5import { createGuardedFetch, decideRemote, isAllowedRemote, redirectOptions, remoteAllowUrl, type AllowedRemote } from '../src/remote.ts';
6import { createTransport, forgetQuirks, lsRefs, Tracer } from 'ply';
7import { unwrap } from '../src/ply-result.ts';
8import { classifyHref, renderMarkdown } from '../src/viewer/markdown.ts';
9import { handleViewer } from '../src/viewer/handle.ts';
10import { renderLoaded } from '../src/viewer/html.ts';
11import { parseViewerRequest, routeOptsFromEnv } from '../src/viewer/route.ts';
12import { viewerHref } from '../src/viewer/url.ts';
13import type { Loaded, TraceModel } from '../src/viewer/model.ts';
15/** Null and a missing value take the fallback. An empty string and 0 stay. */
16function fill<T>(value: unknown, fallback: T): T {
17 return value === null || value === void 0 ? fallback : value as T;
18}
20/** One property. A null receiver and a missing key are holes. A stored null stays null. */
21function slot(receiver: unknown, key: string): unknown {
22 if (receiver === null || receiver === void 0) return void 0;
23 if (typeof receiver !== 'object' && typeof receiver !== 'function') return void 0;
24 return (receiver as Record<string, unknown>)[key];
25}
27type Node = DefaultTreeAdapterMap['node'];
28type Element = DefaultTreeAdapterMap['element'];
30const URL_ATTRS = new Set(['href', 'src', 'action', 'formaction', 'poster', 'data', 'xlink:href', 'srcset']);
31const MARKDOWN_TAGS = new Set(['p', 'a', 'code', 'strong', 'em', 'del', 'ul', 'ol', 'li', 'pre', 'blockquote', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'div', 'span', 'br', 'hr', 'h2', 'h3', 'h4', 'h5', 'h6']);
32// Markdown output may carry only these attributes, with these values: the renderer never copies source text into one,
33// except href (allowlisted, checked below), an integer `start`, and a heading id slug.
34const MARKDOWN_CLASSES = new Set(['md-code', 'md-code-lang', 'md-table', 'task', 'task done', 'task-item', 'al-l', 'al-c', 'al-r', 'md-img', 'md-frontmatter']);
35function markdownAttrOk(name: string, value: string): boolean {
36 if (name === 'href') return true;
37 if (name === 'rel') return value === 'noopener noreferrer nofollow';
38 if (name === 'class') return MARKDOWN_CLASSES.has(value);
39 if (name === 'start') return /^\d{1,9}$/.test(value);
40 if (name === 'id') return /^[a-z0-9][a-z0-9-]{0,79}$/.test(value);
41 return false;
42}
44function isElement(node: Node): node is Element {
45 return node.nodeName !== '#text' && node.nodeName !== '#comment' && node.nodeName !== '#document-fragment' && node.nodeName !== '#document' && 'attrs' in node;
46}
48function walk(node: Node, tags: string[], attrs: { name: string; value: string }[]): void {
49 if (isElement(node)) {
50 tags.push(node.nodeName.toLowerCase());
51 for (const attr of node.attrs) attrs.push({ name: attr.name.toLowerCase(), value: attr.value });
52 for (const child of node.childNodes) walk(child, tags, attrs);
53 return;
54 }
55 if ('childNodes' in node) for (const child of node.childNodes) walk(child, tags, attrs);
56}
58function scan(html: string): { tags: string[]; attrs: { name: string; value: string }[] } {
59 const tags: string[] = [];
60 const attrs: { name: string; value: string }[] = [];
61 walk(parseFragment(html), tags, attrs);
62 return { tags, attrs };
63}
65function assertHtmlSafe(html: string, where: string, markdown = false): void {
66 const { tags, attrs } = scan(html);
67 for (const tag of tags) {
68 assert.equal(['script', 'style', 'iframe', 'object', 'embed', 'frame'].includes(tag), false, `${where} tag ${tag}`);
69 if (markdown) assert.equal(MARKDOWN_TAGS.has(tag), true, `${where} unexpected ${tag}`);
70 }
71 for (const attr of attrs) {
72 if (markdown) assert.equal(markdownAttrOk(attr.name, attr.value), true, `${where} markdown attribute ${attr.name}=${attr.value}`);
73 assert.equal(attr.name.startsWith('on'), false, `${where} handler ${attr.name}=${attr.value}`);
74 assert.equal(/^[-a-z0-9:]+$/.test(attr.name), true, `${where} attribute name ${attr.name}`);
75 if (!URL_ATTRS.has(attr.name)) continue;
76 let compact = '';
77 for (const char of attr.value) {
78 const code = fill<number>(char.codePointAt(0), 0);
79 if (code <= 0x1f || code === 0x7f || code === 0x20) continue;
80 compact += char;
81 }
82 compact = compact.toLowerCase();
83 assert.equal(/^(javascript|data|vbscript):/.test(compact), false, `${where} ${attr.name}=${attr.value}`);
84 }
85}
87function mustAllow(raw: string, allow: readonly string[] = []): AllowedRemote {
88 const decision = decideRemote(raw, allow);
89 assert.equal(decision.kind, 'allowed', `${raw} -> ${decision.kind === 'rejected' ? decision.reason : ''}`);
90 if (decision.kind !== 'allowed') throw new Error('unreachable');
91 assert.equal(isAllowedRemote(decision.remote), true);
92 return decision.remote;
93}
95function mustReject(raw: string, reason: string, allow: readonly string[] = []): void {
96 const decision = decideRemote(raw, allow);
97 assert.equal(decision.kind, 'rejected', raw);
98 if (decision.kind === 'rejected') assert.equal(decision.reason, reason, raw);
99}
101test('markdown hrefs are escaped, scheme-allowlisted, and carry rel', () => {
102 const cases = [
103 'See [docs](https://example.com/" onfocus="alert(1)" autofocus="x) for more.',
104 'x [y](https://e.com/"onclick=alert(1) x=") z',
105 'x [y](https://e.com/"/><img/src=x onerror=alert(1)><x=") z',
106 'x [y](https://e.com/\'onclick=alert(1)//) z',
107 'See [x](https://e.com/"onclick=alert(1)) here',
108 '[x](javascript:alert(1))',
109 '[x](JaVaScRiPt:alert(1))',
110 '[x](java\tscript:alert(1))',
111 '[x](java\nscript:alert(1))',
112 '[x](javascript:alert(1))',
113 '[x](javascript:alert(1))',
114 '[x](data:text/html,<script>alert(1)</script>)',
115 '[x](vbscript:msgbox(1))',
116 '[x](//evil.example/a)',
117 '[x](/\\evil.example)',
118 ];
119 for (const src of cases) {
120 const html = String(renderMarkdown(src));
121 assertHtmlSafe(html, src, true);
122 const { attrs } = scan(html);
123 assert.equal(attrs.some(attr => attr.name === 'onfocus' || attr.name === 'onerror' || attr.name === 'onclick'), false, src);
124 assert.equal(/href\s*=\s*["']?\s*javascript:/i.test(html), false, src);
125 }
126 const ok = String(renderMarkdown('[ok](https://github.com/octocat/Hello-World) and [mail](mailto:a@b.example) and [rel](/view)'));
127 assert.match(ok, /href="https:\/\/github.com\/octocat\/Hello-World" rel="noopener noreferrer nofollow"/);
128 assert.match(ok, /href="mailto:a@b\.example" rel="noopener noreferrer nofollow"/);
129 assert.match(ok, /href="\/view" rel="noopener noreferrer nofollow"/);
130 assert.equal(classifyHref('https://example.com/a?b=1&c=2'), 'https://example.com/a?b=1&c=2');
131 const escaped = String(renderMarkdown('[q](https://example.com/a?b=1&c=2)'));
132 assert.match(escaped, /href="https:\/\/example.com\/a\?b=1&c=2"/);
133 assertHtmlSafe(escaped, 'query', true);
134});
136test('hostile markdown, filenames, commits, refs, and authors do not break out of HTML', () => {
137 const payloads = [
138 '<script>alert(1)</script>', '<style>@import "javascript:alert(1)"</style>', '<iframe src="javascript:alert(1)">',
139 '<object data="javascript:alert(1)">', '" onfocus="alert(1)" autofocus="', '\' onclick=alert(1) x=\'',
140 'javascript:alert(1)', 'JaVaScRiPt:alert(1)', 'java\tscript:alert(1)', 'java\nscript:alert(1)', 'java\rscript:alert(1)',
141 'javascript:alert(1)', 'javascript:alert(1)', 'javascript:alert(1)', 'javascript:alert(1)',
142 'data:text/html,<script>alert(1)</script>', 'vbscript:msgbox(1)', 'livescript:alert(1)', 'blob:https://example.com/x',
143 '//evil.example', '/\\evil.example', 'https://example.com/"onclick=alert(1)', 'https://e.com/"/><img src=x onerror=alert(1)>',
144 '"><svg onload=alert(1)>', '`onclick`', '{{constructor.constructor("alert(1)")()}}', '%2e%2e/%2e%2e/secret',
145 '..\\..\\windows', 'mailto:a@b.example', 'https://example.com/a?b=1&c=2', '../README', './README', '/view',
146 'file:///etc/passwd', 'gopher://evil.example', 'dict://evil.example', ' 	javascript:alert(1)',
147 ];
148 const rand = mulberry(0x5ec);
149 for (let n = 0; n < 40; n++) {
150 const href = fill(payloads[Math.floor(rand() * payloads.length)], 'x');
151 const label = fill(payloads[Math.floor(rand() * payloads.length)], 'y');
152 const text = fill(payloads[Math.floor(rand() * payloads.length)], 'z');
153 const shapes = [
154 `# ${text}\n\n[${label}](${href})\n\n- ${text}\n- [${label}](${href})\n\n\`\`\`\n${text}\n\`\`\`\n\n**${text}** \`${label}\``,
155 `paragraph ${text} [${label}](${href}) tail`,
156 `## ${label}\n\n${text}`,
157 // GFM: tables (with alignment), task lists, images, reference and auto links, quotes, fences with a hostile info string.
158 `| ${label} | b |\n|:--|--:|\n| [${label}](${href}) | ${text} |\n| \`${text}\` |  |`,
159 `- [ ] ${text}\n- [x] [${label}](${href})\n 1. nested ${text}\n\n7. ${label}\n8. ${text}`,
160 `[${label}][r] and [r] and <${href}> and ${href}\n\n[r]: ${href}`,
161 `> ${text}\n> [${label}](${href})\n\n\`\`\`${label}\n${text}\n\`\`\``,
162 `${text}\n===\n\n${label}\n---\n\n***\n\n~~${text}~~ _${label}_`,
163 ];
164 for (const src of shapes) {
165 assertHtmlSafe(String(renderMarkdown(src)), `sample ${n}`, true);
166 // A repository-relative resolver must not widen what reaches href either.
167 assertHtmlSafe(String(renderMarkdown(src, { base: 'docs', resolve: (path, kind) => `/view/github.com/o/r?path=${encodeURIComponent(path)}${kind === 'image' ? '&raw=1' : ''}` })), `resolved ${n}`, true);
168 }
169 }
170 const trace: TraceModel = { bars: [], totalMs: 0, cfRay: 'ray', requestId: 'id' };
171 for (let n = 0; n < 20; n++) {
172 const name = fill(payloads[n % payloads.length], 'file');
173 const message = fill(payloads[(n + 3) % payloads.length], 'm');
174 const author = fill(payloads[(n + 7) % payloads.length], 'a');
175 const ref = `refs/heads/${fill(payloads[(n + 11) % payloads.length], 'main')}`.slice(0, 80);
176 const loaded: Loaded = {
177 kind: 'tree',
178 header: {
179 host: 'github.com', repoPath: 'o/r', ref, commit: 'a'.repeat(40),
180 treeHref: '/view/github.com/o/r',
181 server: { agent: author, protocol: 2, objectFormat: 'sha1', caps: ['agent'], fetchFeatures: ['filter'], filterAdvertised: true },
182 },
183 path: '',
184 commit: {
185 oid: 'a'.repeat(40), tree: 'b'.repeat(40), parents: [], authorName: author, authorEmail: 'a@b',
186 authorTime: '2020-01-02T03:04:05Z', authorTz: '+0000', message, signed: false,
187 },
188 log: [], logStopped: false,
189 branches: [{ name: ref, oid: 'a'.repeat(40), head: true }], branchesTruncated: 0,
190 tags: [], tagsTruncated: 0,
191 entries: [{ name, mode: '100644', oid: 'c'.repeat(40), type: 'blob' }], entriesTruncated: 0,
192 readme: { name: 'README.md', text: `# ${name}\n\n[${author}](${message})\n` },
193 };
194 assertHtmlSafe(String(renderLoaded(loaded, trace)), `page ${n}`);
195 }
196});
198test('checkRemote rejects a table of hostile URLs and brands the ones it allows', () => {
199 const rejected: Array<[string, string]> = [];
200 const push = (raw: string, reason: string) => rejected.push([raw, reason]);
201 for (const host of ['127.0.0.1', '10.0.0.1', '172.16.0.1', '192.168.0.1', '169.254.169.254', '0.0.0.0', '8.8.8.8', '255.255.255.255', '127.000.000.001', '0177.0.0.1', '0x7f.0.0.1', '127.1', '127.0.1']) {
202 push(`https://${host}/x.git`, 'ip_literal');
203 }
204 for (const host of ['2130706433', '0x7f000001', '017700000001', '3232235777', '0xA9FEA9FE']) {
205 push(`https://${host}/x.git`, 'ip_literal');
206 }
207 for (const host of ['[::1]', '[::]', '[fe80::1]', '[fd00::1]', '[fc00::1]', '[::ffff:127.0.0.1]', '[::ffff:7f00:1]', '[::ffff:169.254.169.254]', '[0:0:0:0:0:ffff:127.0.0.1]', '[::127.0.0.1]', '[::ffff:7f00:0001]']) {
208 push(`https://${host}/x.git`, 'ip_literal');
209 }
210 for (const host of [
211 '127.0.0.1.nip.io', '169.254.169.254.nip.io', 'app.10.0.0.1.nip.io', 'customer.127.0.0.1.nip.io', 'foo.nip.io', 'nip.io',
212 '127.0.0.1.sslip.io', '127-0-0-1.sslip.io', '7f000001.sslip.io', 'sslip.io', 'foo.sslip.io',
213 '127.0.0.1.xip.io', 'xip.io', 'localtest.me', 'foo.localtest.me', 'customer.localtest.me',
214 'lvh.me', 'foo.lvh.me', 'vcap.me', 'foo.vcap.me', 'lacolhost.com', 'localho.st', 'foo.traefik.me', 'traefik.me',
215 '10.0.0.1.example.com', '169.254.169.254.attacker.example',
216 ]) {
217 const reason = /(^|\.)(\d{1,3}\.){3}\d{1,3}(\.|$)/.test(host) ? 'ip_literal' : 'wildcard_dns';
218 push(`https://${host}/x.git`, reason);
219 }
220 for (const host of ['github.com.', 'example.com.', 'localhost.', '127.0.0.1.', 'GitHub.com.']) push(`https://${host}/x.git`, 'trailing_dot');
221 for (const host of ['localhost', 'foo.localhost', 'git.internal', 'metadata.google.internal', 'foo.local', 'intranet', 'foo.lan', 'bar.home.arpa', 'corp', 'foo.corp', 'private', 'foo.private', 'localdomain', 'foo.localdomain']) {
222 const reason = host.includes('.') ? 'private_name' : 'single_label';
223 push(`https://${host}/x.git`, reason);
224 }
225 push('http://example.com/x.git', 'scheme');
226 push('http://github.com/x.git', 'scheme');
227 push('ftp://github.com/x.git', 'scheme');
228 push('file:///etc/passwd', 'scheme');
229 push('https://github.com:8443/x.git', 'port');
230 push('https://github.com:444/x.git', 'port');
231 push('https://user:pw@github.com/x.git', 'userinfo');
232 push('https://token@github.com/x.git', 'userinfo');
233 push('https://github.com/x.git?a=b', 'query');
234 push('https://github.com/x.git#frag', 'fragment');
235 push('https://github.com/a/../../b.git', 'bad_path');
236 push('https://github.com/a/%2e%2e/b.git', 'bad_path');
237 push('https://github.com/a/%2E%2E/%2e%2e/evil.com/x.git', 'bad_path');
238 push('https://github.com/a/%2f../b.git', 'bad_path');
239 push('https://github.com/a/%5c..%5csecret.git', 'bad_path');
240 push('https://github.com/a/./b.git', 'bad_path');
241 push('https://github.com/a\\b.git', 'control');
242 push('not-a-url', 'not_url');
243 push('', 'empty');
244 push(' ', 'empty');
245 push(`https://example.com/${'a'.repeat(2100)}`, 'too_long');
246 push('https://github.com/a b.git', 'control');
247 push('https://github .com/a.git', 'control');
248 push('https://0/x.git', 'ip_literal');
249 push('https://127.0.0.0/x.git', 'ip_literal');
250 push('https://[ff02::1]/x.git', 'ip_literal');
251 push('https://0177.0.0.01/x.git', 'ip_literal');
252 push('https://0x7f.0.0.01/x.git', 'ip_literal');
253 push('https://foo.xip.io/x.git', 'wildcard_dns');
254 assert.ok(rejected.length >= 100, `expected >= 100 hostile URLs, got ${rejected.length}`);
255 for (const [raw, reason] of rejected) mustReject(raw, reason);
257 const github = mustAllow('https://GitHub.com/Foo/Bar');
258 assert.equal(github.href, 'https://github.com/Foo/Bar');
259 assert.equal(github.hostname, 'github.com');
260 assert.equal(mustAllow('https://example.com/a/b.git').origin, 'https://example.com');
261 const local = mustAllow('http://127.0.0.1:9/r.git', ['http://127.0.0.1:9']);
262 assert.equal(local.origin, 'http://127.0.0.1:9');
263 mustReject('http://127.0.0.1:9/r.git', 'scheme');
264 mustReject('https://user:pw@127.0.0.1:9/r.git', 'userinfo', ['http://127.0.0.1:9']);
265});
267test('guarded fetch never follows a 3xx: a same-origin one goes back to ply as is, any other Location is refused', async () => {
268 const remote = mustAllow('https://github.com/acme/widgets.git');
269 const seen: { url: string; redirect: unknown }[] = [];
270 const good = createGuardedFetch(async (input, init) => {
271 const url = String(input);
272 seen.push({ url, redirect: slot(init, 'redirect') });
273 if (url === 'https://github.com/acme/widgets/info/refs?service=git-upload-pack') {
274 return new Response(null, { status: 302, headers: { location: 'https://github.com/acme/widgets.git/info/refs?service=git-upload-pack' } });
275 }
276 return new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } });
277 }, remote, []);
278 const res = await good('https://github.com/acme/widgets/info/refs?service=git-upload-pack', { method: 'GET' });
279 assert.equal(res.status, 302);
280 assert.equal(res.headers.get('location'), 'https://github.com/acme/widgets.git/info/refs?service=git-upload-pack');
281 assert.deepEqual(seen, [{ url: 'https://github.com/acme/widgets/info/refs?service=git-upload-pack', redirect: 'manual' }], 'one dial, manual');
282 // Every request is checked on its own: another origin is refused before it is dialled.
283 await assert.rejects(() => good('https://gitlab.com/acme/widgets.git/info/refs?service=git-upload-pack'), (err: Partial<{ code: string }>) => err.code === 'bad_request');
284 assert.equal(seen.length, 1);
286 for (const location of [
287 'https://127.0.0.1.nip.io/latest',
288 'https://169.254.169.254/latest',
289 'https://github.com.evil.com/acme/widgets.git/info/refs?service=git-upload-pack',
290 'https://evil.example/acme/widgets.git/info/refs',
291 'https://github.com/acme/../../other/secret.git/info/refs?service=git-upload-pack',
292 'https://github.com./acme/widgets.git/info/refs?service=git-upload-pack',
293 '../secret.git/info/refs?service=git-upload-pack',
294 'https://github.com/acme/%2e%2e/secret.git/info/refs',
295 'http://github.com/acme/widgets.git/info/refs?service=git-upload-pack',
296 ]) {
297 let hops = 0;
298 const fetchImpl = createGuardedFetch(async () => {
299 hops++;
300 return new Response(null, { status: 302, headers: { location } });
301 }, remote, []);
302 await assert.rejects(() => fetchImpl(`${remote.href}/info/refs?service=git-upload-pack`), (err: Partial<{ code: string }>) => err.code === 'bad_request');
303 assert.equal(hops, 1, location);
304 }
306 assert.throws(() => createGuardedFetch(async () => new Response('x'), { href: 'https://evil.example', origin: 'https://evil.example', hostname: 'evil.example', host: 'evil.example' } as AllowedRemote));
307});
309test('remoteAllowUrl (ply\'s allowUrl) applies decideRemote and the remote\'s origin to the base and to each rebase', () => {
310 const remote = mustAllow('https://github.com/acme/widgets');
311 const allow = remoteAllowUrl(remote);
312 assert.equal(allow(new URL(remote.href)).ok, true);
313 assert.equal(allow(new URL('https://github.com/acme/widgets.git/info/refs?service=git-upload-pack')).ok, true);
314 for (const bad of ['https://127.0.0.1.nip.io/acme/widgets.git/info/refs?service=git-upload-pack', 'https://169.254.169.254/latest', 'https://github.com.evil.com/acme/widgets.git/info/refs?service=git-upload-pack', 'https://gitlab.com/acme/widgets.git/info/refs?service=git-upload-pack', 'https://github.com./acme/widgets.git/info/refs?service=git-upload-pack', 'http://github.com/acme/widgets.git/info/refs?service=git-upload-pack']) {
315 const got = allow(new URL(bad));
316 assert.equal(got.ok, false, bad);
317 if (!got.ok) assert.equal(got.error.code, 'bad_request', bad);
318 }
319 const local = mustAllow('http://127.0.0.1:9/r.git', ['http://127.0.0.1:9']);
320 assert.equal(remoteAllowUrl(local, ['http://127.0.0.1:9'])(new URL('http://127.0.0.1:9/s.git/info/refs?service=git-upload-pack')).ok, true);
321 assert.equal(remoteAllowUrl(local, ['http://127.0.0.1:9', 'http://127.0.0.1:10'])(new URL('http://127.0.0.1:10/r.git/info/refs?service=git-upload-pack')).ok, false, 'another allowlisted origin is still another origin');
322 assert.throws(() => remoteAllowUrl({ href: 'https://evil.example', origin: 'https://evil.example', hostname: 'evil.example', host: 'evil.example' } as AllowedRemote));
323});
325const V2_AD = new TextEncoder().encode('000eversion 2\n0013ls-refs=unborn\n0000');
327test('through the guard, ply follows only the first info/refs (git\'s rule): the base is rewritten, the credential stays on the origin, a POST 3xx is an error naming the target', async () => {
328 const remote = mustAllow('https://github.com/acme/widgets');
329 type Call = { method: string; url: string; auth: string | null };
330 const host = (mode: 'moved' | 'post' | 'away') => {
331 // a different fake host at the same URL each time: forget ply's per-remote memo of the settled redirect
332 forgetQuirks();
333 const calls: Call[] = [];
334 const inner: typeof fetch = async (input, init) => {
335 const url = String(input);
336 calls.push({ method: String(fill(slot(init, 'method'), 'GET')), url, auth: new Headers(slot(init, 'headers') as HeadersInit).get('authorization') });
337 if (url === 'https://github.com/acme/widgets/info/refs?service=git-upload-pack') {
338 const location = mode === 'away' ? 'https://gitlab.com/acme/widgets.git/info/refs?service=git-upload-pack' : mode === 'post' ? '' : '/acme/widgets.git/info/refs?service=git-upload-pack';
339 if (location) return new Response(null, { status: 302, headers: { location } });
340 }
341 if (url.endsWith('/info/refs?service=git-upload-pack')) return new Response(V2_AD, { status: 200, headers: { 'content-type': 'application/x-git-upload-pack-advertisement' } });
342 if (mode === 'post') return new Response(null, { status: 307, headers: { location: 'https://github.com/acme/elsewhere.git/git-upload-pack' } });
343 return new Response(new TextEncoder().encode('0000'), { status: 200, headers: { 'content-type': 'application/x-git-upload-pack-result' } });
344 };
345 return { calls, t: unwrap(createTransport(remote.href, { fetchImpl: createGuardedFetch(inner, remote), auth: { bearer: 'sekrit' }, tracer: new Tracer(), ...redirectOptions(remote) })) };
346 };
348 const moved = host('moved');
349 const listed = await lsRefs(moved.t, ['refs/heads/']);
350 assert.equal(listed.ok, true, JSON.stringify(listed));
351 assert.equal(moved.t.url, 'https://github.com/acme/widgets.git');
352 assert.deepEqual(moved.calls.map(c => [c.method, c.url]), [
353 ['GET', 'https://github.com/acme/widgets/info/refs?service=git-upload-pack'],
354 ['GET', 'https://github.com/acme/widgets.git/info/refs?service=git-upload-pack'],
355 ['POST', 'https://github.com/acme/widgets.git/git-upload-pack'],
356 ]);
357 assert.ok(moved.calls.every(c => c.auth === 'Bearer sekrit'), 'same origin: the credential is sent');
359 const post = host('post');
360 const refused = await lsRefs(post.t, ['refs/heads/']);
361 assert.equal(refused.ok, false);
362 if (!refused.ok) {
363 assert.equal(refused.error.code, 'http');
364 assert.match(refused.error.message, /redirected to https:\/\/github\.com\/acme\/elsewhere\.git\/git-upload-pack/);
365 }
366 assert.deepEqual(post.calls.map(c => c.method), ['GET', 'POST'], 'the POST redirect is not followed');
368 const away = host('away');
369 const cross = await lsRefs(away.t, ['refs/heads/']);
370 assert.equal(cross.ok, false);
371 if (!cross.ok) assert.equal(cross.error.code, 'bad_request');
372 assert.equal(away.calls.length, 1, 'another origin is never dialled, so the credential cannot go there');
373});
375test('guarded fetch sends a stream body once, with duplex, and hands a redirect that answers it back to ply unfollowed', async () => {
376 const remote = mustAllow('https://github.com/acme/widgets.git');
377 const url = `${remote.href}/git-receive-pack`;
378 const stream = () => new ReadableStream<Uint8Array>({ start(c) { c.enqueue(new Uint8Array([1, 2, 3])); c.close(); } });
379 const seen: unknown[] = [];
380 const ok = createGuardedFetch(async (_input, init) => {
381 seen.push(slot(init, 'duplex'));
382 return new Response(new Uint8Array(await new Response(slot(init, 'body') as BodyInit).arrayBuffer()), { status: 200 });
383 }, remote, []);
384 const res = await ok(url, { method: 'POST', body: stream() });
385 assert.deepEqual([...new Uint8Array(await res.arrayBuffer())], [1, 2, 3]);
386 assert.deepEqual(seen, ['half']);
388 let hops = 0;
389 const redirected = createGuardedFetch(async () => {
390 hops++;
391 return new Response(null, { status: 307, headers: { location: url } });
392 }, remote, []);
393 const answered = await redirected(url, { method: 'POST', body: stream() });
394 assert.equal(answered.status, 307);
395 assert.equal(hops, 1);
397 await assert.rejects(() => ok(url, { method: 'POST', body: 'text' }), (err: Partial<{ code: string }>) => err.code === 'bad_request');
398});
400test('viewer paths reject dot segments and encoded separators before normalization', () => {
401 const opts = routeOptsFromEnv({});
402 const collapsed = new URL('https://ply.example/view/github.com/a/%2e%2e/%2e%2e/evil.com/x/y');
403 assert.equal(collapsed.pathname, '/view/evil.com/x/y');
404 const hostile = [
405 'https://ply.example/view/github.com/a/../../evil.com/o/r',
406 'https://ply.example/view/github.com/a/%2e%2e/%2e%2e/evil.com/x/y',
407 'https://ply.example/view/github.com/a/%2E%2E/b',
408 'https://ply.example/view/github.com/./octocat/Hello-World',
409 'https://ply.example/view/github.com/%2e/octocat/Hello-World',
410 'https://ply.example/view/github.com/a/%2f../b',
411 'https://ply.example/view/github.com/a%5c..%5c..%5cevil',
412 'https://ply.example/view/github.com/octocat/Hello-World?path=../secret',
413 'https://ply.example/view/github.com/octocat/Hello-World?path=%2e%2e%2fsecret',
414 'https://ply.example/view/github.com/octocat/Hello-World?path=foo%5cbar',
415 'https://github.com/a/../../b',
416 ];
417 for (const raw of hostile) {
418 assert.throws(() => parseViewerRequest(raw, opts), /not allowed|not a viewer/, raw);
419 }
420 for (const raw of ['https://ply.example/view/github.com/o/r?path=a%2Fb%2Fc', 'https://ply.example/view/github.com/o/r?path=a/b/c']) {
421 const nested = parseViewerRequest(raw, opts);
422 assert.equal(nested.kind === 'repo' && nested.path, 'a/b/c', raw);
423 }
424 for (const raw of ['https://ply.example/view/github.com/o/r?path=a%2F..%2Fb', 'https://ply.example/view/github.com/o/r?path=a%2F%2e%2e%2Fb', 'https://ply.example/view/github.com/o/r?path=a%2F%5Cb']) {
425 assert.throws(() => parseViewerRequest(raw, opts), /not allowed|not a valid|path/, raw);
426 }
427 assert.equal(viewerHref('github.com', 'o/r', { ref: 'refs/heads/main', path: 'a/b c/d' }), '/view/github.com/o/r?ref=refs/heads/main&path=a/b+c/d');
428 const ok = parseViewerRequest('https://ply.example/view/github.com/octocat/Hello-World?path=README.md', opts);
429 assert.equal(ok.kind, 'repo');
430 if (ok.kind === 'repo') assert.equal(ok.path, 'README.md');
431 assert.throws(() => parseViewerRequest('https://ply.example/view?remote=' + encodeURIComponent('https://github.com/a/../../b'), opts), /not allowed/);
432});
434test('security headers are present on every viewer route', async () => {
435 const routes = [
436 'https://ply.example/view',
437 'https://ply.example/view/',
438 'https://ply.example/view.css',
439 'https://ply.example/view/evil.example/a/b',
440 'https://ply.example/view/github.com/a/%2f../b',
441 'https://ply.example/view?remote=' + encodeURIComponent('https://evil.example/a/b'),
442 ];
443 for (const url of routes) {
444 for (const method of ['GET', 'HEAD'] as const) {
445 const res = await handleViewer(new Request(url, { method }), {});
446 const csp = fill(res.headers.get('content-security-policy'), '');
447 assert.equal(res.headers.get('x-content-type-options'), 'nosniff', `${method} ${url}`);
448 assert.equal(res.headers.get('referrer-policy'), 'no-referrer', `${method} ${url}`);
449 assert.match(csp, /frame-ancestors 'none'/, `${method} ${url}`);
450 assert.match(csp, /script-src 'none'/, `${method} ${url}`);
451 assert.equal(csp.includes('unsafe-inline'), false, `${method} ${url}`);
452 await res.arrayBuffer();
453 }
454 }
455 const post = await handleViewer(new Request('https://ply.example/view', { method: 'POST' }), {});
456 assert.equal(post.status, 405);
457 assert.match(fill(post.headers.get('content-security-policy'), ''), /frame-ancestors 'none'/);
458 assert.equal(fill(post.headers.get('content-security-policy'), '').includes('unsafe-inline'), false);
459});
461function mulberry(seed: number): () => number {
462 let state = seed >>> 0;
463 return () => {
464 state = (state + 0x6d2b79f5) >>> 0;
465 let t = Math.imul(state ^ (state >>> 15), 1 | state);
466 t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
467 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
468 };
469}
Diagnostics4 git calls · 32 ms
CallStatusSizeTime032 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 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=3 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
fetch2007.8 KiB · hit25 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=7971 git.response.cache=HIT git.response.duration_ms=25 git.fetch.wants=63a72be4a671654343c817718042701448d50ab6 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=7971 git.pkt.payload.max=7950 git.sideband.band1.bytes=7949 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
- a5e2c87729ba4b67
- Request
- cb23ab0e…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format