import type { SourceControlMediaRequest } from "@t3tools/source-control-core/server/driver"; import * as Clock from "effect/Clock"; import * as Effect from "effect/Effect"; import * as Redacted from "effect/Redacted"; import { FetchHttpClient, HttpClient, HttpClientRequest, type HttpClientResponse, } from "effect/http"; import { GITHUB_CREDENTIALED_MEDIA_HOSTS } from "../shared/gitHubMedia.ts"; import * as GitHubCredentials from "./GitHubCredentials.ts"; import { gitHubApiUrls } from "./GitHubRequestPolicy.ts"; import * as GitHubRest from "./GitHubRest.ts"; /** * Everything a redirect leads to — the presigned object stores GitHub hands assets off to — * authorizes with its own signature, and some of them reject a request that also carries a * bearer token. */ const isCredentialedHost = (url: string, hosts: ReadonlySet) => { const target = URL.parse(url); return ( target !== null && target.protocol === "https:" && target.username === "" && target.password === "" && hosts.has(target.host.toLowerCase()) ); }; /** * The account a request authenticates as, and the hosts its token may be sent to. An Enterprise * Server host's token goes only to that host and its `raw.` subdomain, never to github.com. */ const credentialScope = (request: SourceControlMediaRequest) => { const enterpriseHost = request.host?.trim().toLowerCase(); if (enterpriseHost && !GITHUB_CREDENTIALED_MEDIA_HOSTS.has(enterpriseHost)) { const hosts: ReadonlySet = new Set([enterpriseHost, `raw.${enterpriseHost}`]); if (isCredentialedHost(request.url, hosts)) { return { account: enterpriseHost, hosts, enterprise: true }; } } return { account: "github.com", hosts: GITHUB_CREDENTIALED_MEDIA_HOSTS, enterprise: false }; }; const MAX_REDIRECTS = 3; const MANUAL_REDIRECT: RequestInit = { redirect: "manual" }; const MEDIA_CONTENT_TYPE_PATTERN = /^(?:image|video|audio)\/[\w!#$&^.+-]+$/i; const ATTACHMENT_PATH_PATTERN = /^\/(?:user-attachments\/assets\/[\w-]+|[^/]+\/[^/]+\/assets\/\d+\/[\w-]+)$/u; const LEGACY_ATTACHMENT_REPOSITORY_PATTERN = /^\/([^/]+\/[^/]+)\/assets\//u; const REPOSITORY_PATTERN = /^[\w.-]+\/[\w.-]+$/u; // GitHub's signed upload links last about five minutes. const SIGNED_URL_CACHE_TTL_MS = 4 * 60_000; const SIGNED_URL_CACHE_MAX_ENTRIES = 64; const CONTENTS_RAW_ACCEPT = "application/vnd.github.raw"; const MAX_REF_SEGMENTS = 3; const decodeSegments = (segments: ReadonlyArray) => { const joined = segments.join("/"); try { return decodeURIComponent(joined); } catch { return joined; } }; /** `owner/name` of a repository file URL, `https://////`. */ const fileRepository = (url: string) => { const [owner, name] = URL.parse(url)?.pathname.split("/").slice(1) ?? []; const repository = `${owner}/${name}`; return REPOSITORY_PATTERN.test(repository) ? repository : undefined; }; /** * Enterprise Server's web routes ignore a token and its `raw.` subdomain exists only with * subdomain isolation, so a file is read through the Contents API. A branch can contain slashes, * so each split of `/` is a candidate, shortest ref first. */ const enterpriseContentsReads = (url: string, host: string) => { const repository = fileRepository(url); const refAndPath = URL.parse(url)?.pathname.split("/").slice(3) ?? []; if (repository === undefined) return []; const contents = `${gitHubApiUrls(host).rest}/repos/${repository}/contents`; return refAndPath .slice(0, -1) .slice(0, MAX_REF_SEGMENTS) .map((_, index) => { const read = new URL(`${contents}/${refAndPath.slice(index + 1).join("/")}`); read.searchParams.set("ref", decodeSegments(refAndPath.slice(0, index + 1))); return read.toString(); }); }; const contentTypeOf = (response: HttpClientResponse.HttpClientResponse) => response.headers["content-type"]?.split(";", 1)[0]?.trim().toLowerCase() ?? ""; /** The repository an upload has to be rendered in, or `undefined` when it is not an upload. */ const uploadRepository = (request: SourceControlMediaRequest) => { const target = URL.parse(request.url); if ( target === null || target.protocol !== "https:" || target.hostname !== "github.com" || target.port !== "" || target.username !== "" || target.password !== "" || !ATTACHMENT_PATH_PATTERN.test(target.pathname) ) { return undefined; } // A legacy upload names its repository; one the caller supplies cannot override that. const repository = LEGACY_ATTACHMENT_REPOSITORY_PATTERN.exec(target.pathname)?.[1] ?? request.repository; // An unusable explicit context must not fall back to an unrelated checkout. return repository !== undefined && REPOSITORY_PATTERN.test(repository) ? repository : undefined; }; export const make = Effect.gen(function* () { const credentials = yield* GitHubCredentials.GitHubCredentials; const rest = yield* GitHubRest.GitHubRest; const httpClient = HttpClient.withScope(yield* HttpClient.HttpClient); const signedUrls = new Map(); // github.com's media hosts are served by github.com's account, which is the host `gh` stores it // under. Read on every fetch, so media loads as soon as the user signs in. const credential = (account: string) => Effect.suspend(() => credentials.get(account)).pipe(Effect.orElseSucceed(() => null)); const fetchFollowingRedirects = Effect.fnUntraced(function* ( url: string, headers: Readonly>, token: Redacted.Redacted | null, credentialedHosts: ReadonlySet = GITHUB_CREDENTIALED_MEDIA_HOSTS, // An upload redirects to a signed URL. Enterprise Server serves that from its own host and // answers 404 when the token comes along too. tokenOnRedirects = true, ) { let target = url; for (let hop = 0; ; hop += 1) { const authorization = token !== null && (hop === 0 || tokenOnRedirects) && isCredentialedHost(target, credentialedHosts) ? `Bearer ${Redacted.value(token)}` : null; const response: HttpClientResponse.HttpClientResponse = yield* httpClient .execute( HttpClientRequest.get(target).pipe( HttpClientRequest.setHeaders({ ...headers, // The bytes are streamed straight through, so never let an encoding layer in. "accept-encoding": "identity", ...(authorization === null ? {} : { authorization }), }), ), ) .pipe( Effect.provideService(FetchHttpClient.RequestInit, MANUAL_REDIRECT), // A signed URL's query string is a bearer capability; keep it out of tracing spans. Effect.provideService(HttpClient.TracerDisabledWhen, () => true), ); const location = response.headers.location; if (response.status < 300 || response.status >= 400) return response; if (!location || hop >= MAX_REDIRECTS) return null; const next = URL.parse(location, target); if (next === null || next.protocol !== "https:" || next.username || next.password) { return null; } target = next.toString(); } }); // An organization with SAML SSO answers a token-authenticated upload request with its sign-in // page. Rendering the link as markdown in the repository's context hands back a signed URL. const renderSignedUrl = Effect.fnUntraced(function* (url: string, repository: string) { const rendered = yield* rest .rest({ host: "github.com", operation: "renderMarkdown", method: "POST", path: "markdown", accept: "text/html", body: { text: `![](${url})`, mode: "gfm", context: repository }, }) .pipe(Effect.orElseSucceed(() => null)); if (rendered === null || rendered.status !== 200 || rendered.truncated) return null; for (const match of rendered.body.matchAll(/]*\ssrc\s*=\s*(["'])(.*?)\1/giu)) { const signed = URL.parse(match[2]?.replaceAll("&", "&") ?? ""); if ( signed?.protocol === "https:" && signed.hostname === "private-user-images.githubusercontent.com" && signed.port === "" && signed.username === "" && signed.password === "" && signed.searchParams.get("jwt") ) { return signed.toString(); } } return null; }); return Effect.fn("GitHubMediaFetch.fetchMedia")(function* (request: SourceControlMediaRequest) { const scope = credentialScope(request); const signedIn = yield* credential(scope.account); const upload = ATTACHMENT_PATH_PATTERN.test(URL.parse(request.url)?.pathname ?? ""); if (scope.enterprise) { // The client names an Enterprise host, so only one the user has signed in to is fetched. if (signedIn === null) return null; if (upload) { return yield* fetchFollowingRedirects( request.url, request.headers, signedIn.token, scope.hosts, false, ); } let response: HttpClientResponse.HttpClientResponse | null = null; for (const read of enterpriseContentsReads(request.url, scope.account)) { response = yield* fetchFollowingRedirects( read, { ...request.headers, accept: CONTENTS_RAW_ACCEPT }, signedIn.token, scope.hosts, ); if (response?.status !== 404) return response; } return response; } const repository = signedIn === null ? undefined : uploadRepository(request); const cacheKey = signedIn === null || repository === undefined ? null : [signedIn.fingerprint, repository, request.url].map(encodeURIComponent).join("|"); const now = yield* Clock.currentTimeMillis; const cached = cacheKey === null ? undefined : signedUrls.get(cacheKey); let response: HttpClientResponse.HttpClientResponse | null; if (cacheKey !== null && cached !== undefined && now - cached.at < SIGNED_URL_CACHE_TTL_MS) { response = yield* fetchFollowingRedirects(cached.url, request.headers, null); if (response === null || response.status >= 400) signedUrls.delete(cacheKey); // A signed URL can expire before its TTL; only then is a fresh one worth rendering. if (response?.status !== 403) return response; } else { if (cacheKey !== null) signedUrls.delete(cacheKey); response = yield* fetchFollowingRedirects( request.url, request.headers, signedIn?.token ?? null, scope.hosts, !upload, ); if (response === null || contentTypeOf(response) !== "text/html") return response; } if (cacheKey === null || repository === undefined || signedIn === null) return response; const signed = yield* renderSignedUrl(request.url, repository); if (signed === null) return response; const media = yield* fetchFollowingRedirects(signed, request.headers, null); if ( (media?.status === 200 || media?.status === 206) && MEDIA_CONTENT_TYPE_PATTERN.test(contentTypeOf(media)) ) { // The render can run under another account if the user switched mid-request. const current = yield* credential(scope.account); if (current?.fingerprint === signedIn.fingerprint) { if (signedUrls.size >= SIGNED_URL_CACHE_MAX_ENTRIES) { const oldest = signedUrls.keys().next().value; if (oldest !== undefined) signedUrls.delete(oldest); } signedUrls.set(cacheKey, { at: now, url: signed }); } } return media; }); });