// Public viewer: URL/SSRF rules, caps, escaping, a real pack fixture, the credential-stripping fetch, // a local git http-backend, the SQLite rate-limit object under workerd, and (when reachable) GitHub. import { beforeEach, test } from 'node:test'; import { quirksFor, resetQuirks } from '../src/quirks.ts'; import assert from 'node:assert/strict'; import { spawn } from 'node:child_process'; import { mkdtempSync, readFileSync, copyFileSync, mkdirSync, writeFileSync, rmSync, existsSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { copyCfBundle } from './cf-bundle.js'; import { concat } from 'ply'; import { Sha1 } from 'ply'; import { TYPE } from 'ply'; import { encodeObjHeader, spliceDelta, zlibDeflate } from 'ply'; import { parsePack } from 'ply'; import { createTransport, forgetQuirks, lsRefs, Tracer } from 'ply'; import { unwrap, unwrapAsync } from '../src/ply-result.ts'; import { git, makeBareRepo, startGitServer, tmpRoot, commitFiles } from './server.js'; import { startProxy } from './proxy.js'; import { createBudget } from '../src/viewer/budget.ts'; import { escapeHtml } from '../src/viewer/escape.ts'; import { credentialFreeHeaders, createViewerFetch, viewerAllowUrl } from '../src/viewer/fetchguard.ts'; import { handleViewer } from '../src/viewer/handle.ts'; import { renderError, renderHome, renderLoaded, renderNamespace } from '../src/viewer/html.ts'; import { nearbyRefs } from '../src/viewer/hint.ts'; import { KUMO_USED, VIEWER_CSS, cssNodes, selectorClasses } from '../src/viewer/styles.ts'; import { commitProse } from '../src/viewer/format.ts'; import { repoIdentity } from '../src/viewer/identity.ts'; import { PUBLIC_GIT_HOST } from '../src/public.ts'; import { matchAbbreviatedOid, MIN_ABBREV_HEX } from '../src/abbrev.ts'; import { describeFailure } from '../src/viewer/failure.ts'; import { compareTagNames, tagsFromRefs } from '../src/viewer/tags.ts'; import { plyBuildValue } from '../src/version.ts'; import { LAYOUT } from '../src/viewer/layout.ts'; import { none, some } from '../src/option.ts'; /** Null and a missing value take the fallback. An empty string and 0 stay. */ function fill(value: unknown, fallback: T): T { return value === null || value === void 0 ? fallback : value as T; } /** One property. A null receiver and a missing key are holes. A stored null stays null. */ function slot(receiver: unknown, key: string): unknown { if (receiver === null || receiver === void 0) return void 0; if (typeof receiver !== 'object' && typeof receiver !== 'function') return void 0; return (receiver as Record)[key]; } import { VIEWER_CAPS, VIEWER_GIT_LIMITS, VIEWER_UA, rateLimitFor } from '../src/viewer/limits.ts'; import type { Loaded, RateNamespace, TraceModel } from '../src/viewer/model.ts'; import { renderMarkdown } from '../src/viewer/markdown.ts'; import { mapPack } from '../src/viewer/packmap.ts'; import { interpretRemote } from '../src/viewer/paste.ts'; import { parseViewerRoute, routeOptsFromEnv } from '../src/viewer/route.ts'; import { VIEWER_CSP } from '../src/viewer/script.ts'; import { ALLOWED_HOSTS, allowlistFromEnv, assertUpstreamUrl, validatePublicRepoUrl, type AllowedHost, type RepoLocation } from '../src/viewer/url.ts'; // Every test dials a local server under the github.com origin; the isolate-wide quirks map must not carry over. beforeEach(() => resetQuirks()); const FIXTURE = fileURLToPath(new URL('./fixtures/viewer/small.pack', import.meta.url)); const FIXTURE_SHA = 'a86b269e52e69445e10d4c1dd7c5a89b323c0988f1d73b1f0b0c7c1d112f846a'; function treeFixture(over: Partial<{ message: string; host: string; repoPath: string }>): Extract { const host = fill(over.host, 'github.com') const repoPath = fill(over.repoPath, 'o/r') return { kind: 'tree', header: { host, repoPath, ref: 'refs/heads/main', commit: 'a'.repeat(40), treeHref: `/view/${host}/${repoPath}`, server: { agent: 'git/2', protocol: 2, objectFormat: 'sha1', caps: ['agent'], fetchFeatures: ['filter'], filterAdvertised: true }, }, path: '', 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 }, log: [], logStopped: false, branches: [{ name: 'refs/heads/main', oid: 'a'.repeat(40), head: true }], branchesTruncated: 0, tags: [{ name: 'refs/tags/v1.0.0', oid: 'd'.repeat(40) }], tagsTruncated: 0, entries: [{ name: 'a.txt', mode: '100644', oid: 'c'.repeat(40), type: 'blob' }], entriesTruncated: 0, readme: null, } } function loc(raw: string, allowed: readonly AllowedHost[] = ALLOWED_HOSTS): RepoLocation { return validatePublicRepoUrl(raw, { allowedHosts: allowed }); } test('public repo URLs: exact allowlist, case, trailing dot, lookalikes, credentials', () => { const supported = 'Supported hosts: github.com, gitlab.com, codeberg.org, bitbucket.org, git.sr.ht.'; assert.equal(loc('https://github.com/octocat/Hello-World.git').repoPath, 'octocat/Hello-World.git'); assert.equal(loc('https://GitHub.com/octocat/Hello-World').host, 'github.com'); assert.equal(loc('https://GitHub.com/octocat/Hello-World').url, 'https://github.com/octocat/Hello-World'); assert.equal(loc('https://gitlab.com/a/b').host, 'gitlab.com'); assert.equal(loc('https://codeberg.org/a/b').host, 'codeberg.org'); assert.equal(loc('https://bitbucket.org/a/b').host, 'bitbucket.org'); assert.equal(loc('https://git.sr.ht/~user/repo').host, 'git.sr.ht'); assert.equal(loc('https://git.sr.ht/~user/repo').repoPath, '~user/repo'); const bad = [ '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', 'https://github.com/a/b#f', 'https://github.com./octocat/Hello-World', 'https://github.com.evil.com/a/b', 'https://evilgithub.com/a/b', 'https://githüb.com/a/b', 'https://xn--githb-nva.com/a/b', 'https://127.0.0.1/a/b', 'https://10.0.0.1/a/b', 'https://192.168.1.1/a/b', 'https://169.254.169.254/a/b', 'https://0.0.0.0/a/b', 'https://[::1]/a/b', 'https://[::ffff:7f00:1]/a/b', 'https://2130706433/a/b', 'https://0x7f000001/a/b', 'https://0177.0.0.1/a/b', 'https://127.1/a/b', 'https://localhost/a/b', 'https://foo.localhost/a/b', 'https://git.internal/a/b', 'https://evil.github.com/a/b', 'https://notgithub.com/a/b', 'https://127.0.0.1.nip.io/a/b', 'https://acct.artifacts.cloudflare.net/git/repo.git', 'https://ply.ivar.workers.dev/a/b', 'https://other.workers.dev/a/b', 'ftp://github.com/a/b', 'not a url', '', ]; for (const raw of bad) { assert.throws(() => loc(raw), (err: Partial<{ message: string }>) => { const message = String(fill(slot(err, 'message'), err)); return message.includes(supported) && !message.includes('super-secret-token') && !message.includes('pw@'); }, raw); } assert.throws(() => loc('https://user:super-secret-token@github.com/octocat/Hello-World'), (err: Partial<{ message: string }>) => { const message = String(fill(slot(err, 'message'), '')); return /credentials/.test(message) && message.includes(supported) && !message.includes('super-secret-token'); }); const extra = allowlistFromEnv('acct.artifacts.cloudflare.net, *.artifacts.cloudflare.net, https://evil.example, github.com:443, , not a host'); assert.equal(extra.some(host => host === 'github.com'), true); assert.equal(extra.some(host => host === 'acct.artifacts.cloudflare.net'), true); assert.equal(extra.some(host => host.includes('*') || host.includes('/') || host.includes(':')), false); assert.equal(allowlistFromEnv(null).length, ALLOWED_HOSTS.length); assert.equal(allowlistFromEnv('').length, ALLOWED_HOSTS.length); assert.equal(allowlistFromEnv('*.artifacts.cloudflare.net').length, ALLOWED_HOSTS.length); const art = loc('https://acct.artifacts.cloudflare.net/git/ns/repo.git', extra); assert.equal(art.host, 'acct.artifacts.cloudflare.net'); 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')); assert.throws(() => loc('https://acct.artifacts.cloudflare.net/a/b.git'), /not an allowlisted host/); const repo = loc('https://github.com/octocat/Hello-World'); 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'); assert.throws(() => assertUpstreamUrl('https://evil.example/octocat/Hello-World/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /not an allowlisted host/); 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/); assert.throws(() => assertUpstreamUrl('https://gitlab.com/octocat/Hello-World/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /cross-host/); assert.throws(() => assertUpstreamUrl('http://github.com/octocat/Hello-World/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /upstream URL rejected/); assert.throws(() => assertUpstreamUrl('https://github.com:8443/octocat/Hello-World/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /upstream URL rejected/); assert.throws(() => assertUpstreamUrl('https://github.com./octocat/Hello-World/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /not an allowlisted host/); assert.throws(() => assertUpstreamUrl('https://github.com/other/Hello/info/refs?service=git-upload-pack', repo.url, repo.allowedHosts), /smart-HTTP/); assert.throws(() => assertUpstreamUrl('https://github.com/octocat/Hello-World/git-upload-pack?x=1', repo.url, repo.allowedHosts), /query/); }); test('paste parsing stays inside the allowlist', () => { assert.equal(interpretRemote('octocat/Hello-World').remote, 'https://github.com/octocat/Hello-World'); assert.equal(interpretRemote('github.com/sindresorhus/is').remote, 'https://github.com/sindresorhus/is'); assert.equal(interpretRemote('git@github.com:tj/commander.js.git').remote, 'https://github.com/tj/commander.js.git'); const web = interpretRemote('https://github.com/octocat/Hello-World/blob/master/README'); assert.equal(web.remote, 'https://github.com/octocat/Hello-World'); assert.equal(web.ref, 'master'); assert.equal(web.path, 'README'); const route = parseViewerRoute(new URL('https://ply.example/view?remote=octocat/Hello-World'), routeOptsFromEnv({})); assert.equal(route.kind, 'redirect'); if (route.kind === 'redirect') assert.equal(route.location, '/view/github.com/octocat/Hello-World'); const tagged = parseViewerRoute(new URL('https://ply.example/view/github.com/octocat/Hello-World?ref=refs/heads/main&at=v1.0.0'), routeOptsFromEnv({})); assert.equal(tagged.kind, 'redirect'); if (tagged.kind === 'redirect') assert.equal(tagged.location, '/view/github.com/octocat/Hello-World?ref=v1.0.0'); const namespaced = parseViewerRoute(new URL('https://ply.example/r/'), routeOptsFromEnv({})); assert.equal(namespaced.kind, 'namespace'); const shortRepo = parseViewerRoute(new URL(`https://ply.example/r/ply?path=README.md`), routeOptsFromEnv({})); assert.equal(shortRepo.kind, 'repo'); if (shortRepo.kind === 'repo') assert.equal(shortRepo.loc.repoPath, 'git/public-v0/ply.git'); const longPublic = parseViewerRoute(new URL(`https://ply.example/view/${PUBLIC_GIT_HOST}/git/public-v0/ply.git?path=README.md`), routeOptsFromEnv({})); assert.equal(longPublic.kind, 'redirect'); if (longPublic.kind === 'redirect') assert.equal(longPublic.location, '/r/ply?path=README.md'); assert.throws(() => parseViewerRoute(new URL('https://ply.example/view?remote=evil.example/a/b'), routeOptsFromEnv({})), /Supported hosts/); 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')); const md = renderMarkdown('# Hi\n\n\n\n[x](javascript:alert(1))\n\n[ok](https://github.com/octocat/Hello-World)\n'); assert.equal(md.includes(' { const oid = '705bd639' + 'a'.repeat(32); const other = '705bd639' + 'b'.repeat(32); const refs = new Map>([ ['refs/heads/main', { oid }], ['refs/heads/other', { oid: other }], ['refs/tags/v1', { oid: 'c'.repeat(40), peeled: oid }], ]); assert.equal(MIN_ABBREV_HEX, 7); assert.deepEqual(matchAbbreviatedOid('705bd639a', refs), { kind: 'unique', oid }); const ambiguous = matchAbbreviatedOid('705bd639', refs); assert.equal(ambiguous.kind, 'ambiguous'); assert.match(ambiguous.message, /40-hex/); assert.equal(matchAbbreviatedOid('deadbee', refs).kind, 'none'); assert.equal(matchAbbreviatedOid(other.slice(0, 12), refs).kind, 'unique'); const missing = describeFailure({ code: 'auth', status: 401, message: 'authentication failed', host: 'github.com', repoPath: 'ivarvong/ply', ref: null, path: '' }); assert.equal(missing.status, 404); assert.equal(missing.part, 'repo'); assert.equal(missing.summary, 'Repository ivarvong/ply not found, or private'); 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: '' }); assert.equal(badRef.part, 'ref'); assert.equal(badRef.summary, 'No ref v99.0.0 in ivarvong/ply'); 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' }); assert.equal(badPath.part, 'path'); assert.match(badPath.summary, /No path src\/missing in o\/r/); // A /r/ the public namespace lacks: sentence case like every other headline. 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'); assert.ok(compareTagNames('refs/tags/v2.0.0', 'refs/tags/v1.9.0') < 0); assert.deepEqual(tagsFromRefs(refs, 10).tags.map(tag => tag.name), ['refs/tags/v1']); assert.match(plyBuildValue('a'.repeat(40), 'b'.repeat(40)), /^ply=[0-9a-f]{40} ply-web=[0-9a-f]{40}$/); assert.match(VIEWER_CSS, new RegExp(`\\.files \\.oid \\{[^}]*color: var\\(--text-color-kumo-default\\)`)); assert.match(VIEWER_CSS, new RegExp(`font-size: ${LAYOUT.codeFontPx}px`)); assert.match(VIEWER_CSS, new RegExp(`font-size: ${LAYOUT.monoFontPx}px`)); assert.match(VIEWER_CSS, /scrollbar-width: thin/); assert.match(VIEWER_CSS, /\.refbox \{ position: static; \} \.ref-pop \{ left: 0; right: 0; width: auto; \}/); const trace: TraceModel = { bars: [], totalMs: 0, cfRay: '', requestId: '' }; const page = renderLoaded(treeFixture({ message: 'Subject line\n\nBody that should span the card.\n' }), trace); assert.match(String(page), /refs\/tags\/v1\.0\.0/); assert.match(String(page), /ply=/); const tree = treeFixture({}); const file: Loaded = { kind: 'file', header: tree.header, path: 'README.md', branches: tree.branches, branchesTruncated: 0, tags: tree.tags, tagsTruncated: 0, mode: '100644', blob: 'c'.repeat(40), size: 6, note: 'text', text: 'hello\n', }; assert.match(String(renderLoaded(file, trace)), /Wrap lines/); const refused = renderError(401, 'auth', 'authentication failed', trace, 'github.com', 'nope/missing', null, 1, ''); assert.match(String(refused), /not found, or private/); assert.match(String(refused), /GitHub answers 401/); assert.match(String(refused), /HTTP 404/); assert.match(String(refused), /href="\/view\/github\.com\/nope\/missing"/); assert.equal(String(refused).includes('Check the owner'), false); assert.equal(String(refused).includes('No ref'), false); const refPage = String(renderError(404, 'not_found', 'ref v99.0.0 not found', trace, 'github.com', 'shot/shot', 'v99.0.0', 1, '', { nearby: [{ label: 'main', ref: 'refs/heads/main' }, { label: 'v1.10.0', ref: 'refs/tags/v1.10.0' }], path: null, })); assert.match(refPage, /No ref v99\.0\.0 in shot\/shot/); assert.match(refPage, /

That name is not a branch or tag/); assert.match(refPage, /ref picker/); assert.match(refPage, /href="\/view\/github\.com\/shot\/shot\?ref=refs\/heads\/main"/); assert.match(refPage, />v1\.10\.0' const hostile = [ 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 })), 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 } })), ] for (const page of hostile) { assert.equal(page.includes(' fill(slot(/