From 5b350aaebed97602db7c12e5255f36db374441f7 Mon Sep 17 00:00:00 2001 From: Thomas Durieux <5577568+tdurieux@users.noreply.github.com> Date: Sat, 28 Nov 2026 21:17:27 +0000 Subject: [PATCH] fix: keep auto-update repos readable and refuse LFS pointers from jsDelivr Without owner access, a reviewer visit to an auto-update repository tried a GitHub API refresh and failed with token_expired; it now skips the refresh and serves the stored tree. jsDelivr returns Git LFS pointers, which match their blob SHA, so they are now refused like on the raw path. Co-Authored-By: Claude Opus 5.5 --- src/core/Repository.ts | 4 +++ src/core/source/GitHubStream.ts | 49 +++++++++++++++++++------------- test/public-cdn-fallback.test.js | 18 ++++++++++++ 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/core/Repository.ts b/src/core/Repository.ts index 0b48dec..a5d1f22 100644 --- a/src/core/Repository.ts +++ b/src/core/Repository.ts @@ -39,6 +39,7 @@ import { } from "./source/GitHubRepository"; import { getToken } from "./GitHubUtils"; import config from "../config"; +import { PUBLIC_CDN_TOKEN } from "./github-token-context"; import FileModel from "./model/files/files.model"; import AnonymizedRepositoryModel from "./model/anonymizedRepositories/anonymizedRepositories.model"; import { createLogger, serializeError } from "./logger"; @@ -737,6 +738,9 @@ export default class Repository { // Only GitHubBase can be update for the moment if (this.source instanceof GitHubBase) { const token = await this.getToken(); + // Without owner access the stored tree is still served through the CDN; + // a reviewer visit must not fail on an update that needs the GitHub API. + if (!opt?.force && token === PUBLIC_CDN_TOKEN) return; const ghRepo = await getRepositoryFromGitHub({ accessToken: token, diff --git a/src/core/source/GitHubStream.ts b/src/core/source/GitHubStream.ts index 468e61a..7b95473 100644 --- a/src/core/source/GitHubStream.ts +++ b/src/core/source/GitHubStream.ts @@ -79,6 +79,31 @@ export function githubRawFileUrl( )}/raw/${encodeURIComponent(commit)}/${encodedPath}`; } +const LFS_POINTER = "version https://git-lfs.github.com/spec/"; + +// Yields the content unchanged, but fails with upstream_error when it is a Git LFS pointer. +async function* lfsPointerGuard(input: AsyncIterable) { + let probe = Buffer.alloc(0); + let checked = false; + for await (const chunk of input) { + if (checked) { yield chunk; continue; } + probe = Buffer.concat([probe, Buffer.from(chunk)]); + if (probe.length < 150) continue; + if (probe.toString("utf8").startsWith(LFS_POINTER)) throw new AnonymousError("upstream_error", { httpStatus: 502 }); + checked = true; yield probe; + } + if (!checked && probe.length) { + if (probe.toString("utf8").startsWith(LFS_POINTER)) throw new AnonymousError("upstream_error", { httpStatus: 502 }); + yield probe; + } +} + +function rejectLfsPointer(input: stream.Readable): stream.Readable { + const output = stream.Readable.from(lfsPointerGuard(input), { objectMode: false }); + stopSourceOnDestroy(output, () => input.destroy()); + return output; +} + export function jsDelivrFileUrl(owner: string, repo: string, commit: string, filePath: string): string { const segments = filePath.split("/"); // Dot segments would be normalized away and could address another repository. @@ -206,23 +231,7 @@ export default class GitHubStream extends GitHubBase { active = got.stream(metadata.download_url, { hooks: { beforeRedirect: [options => { assertDownloadUrl(options.url); }] }, }); - let probe = Buffer.alloc(0); - let checked = false; - for await (const chunk of active) { - if (checked) { yield chunk; continue; } - probe = Buffer.concat([probe, Buffer.from(chunk)]); - if (probe.length < 150) continue; - if (probe.toString("utf8").startsWith("version https://git-lfs.github.com/spec/")) { - throw new AnonymousError("upstream_error", { httpStatus: 502 }); - } - checked = true; yield probe; - } - if (!checked && probe.length) { - if (probe.toString("utf8").startsWith("version https://git-lfs.github.com/spec/")) { - throw new AnonymousError("upstream_error", { httpStatus: 502 }); - } - yield probe; - } + yield* lfsPointerGuard(active); } })(), { objectMode: false }); stopSourceOnDestroy(output, () => { cancellation.abort(); active?.destroy(); }); @@ -231,7 +240,9 @@ export default class GitHubStream extends GitHubBase { // jsDelivr mirrors public GitHub repositories, so reading through it spends // no GitHub quota. A commit-pinned URL is immutable, and the shared cache - // still checks the bytes against the Git blob SHA from the stored tree. + // checks the bytes against the Git blob SHA from the stored tree. jsDelivr + // does not resolve Git LFS, and a pointer matches its blob SHA, so pointers + // are refused rather than served or cached as the file. private downloadViaJsDelivr(filePath: string): stream.Readable { const commit = this.data.commit; if (!commit || !/^[a-f0-9]{40}$/.test(commit)) throw ownerGitHubAccessRequired(); @@ -239,7 +250,7 @@ export default class GitHubStream extends GitHubBase { const assertHost = (value: URL) => { if (value.protocol !== "https:" || value.hostname !== "cdn.jsdelivr.net") throw new AnonymousError("upstream_error", { httpStatus: 502 }); }; - return got.stream(url, { hooks: { beforeRedirect: [options => { assertHost(options.url); }] } }); + return rejectLfsPointer(got.stream(url, { hooks: { beforeRedirect: [options => { assertHost(options.url); }] } })); } private deferredRawDownload(token: string, filePath: string): stream.Readable { diff --git a/test/public-cdn-fallback.test.js b/test/public-cdn-fallback.test.js index e988bae..abebfc0 100644 --- a/test/public-cdn-fallback.test.js +++ b/test/public-cdn-fallback.test.js @@ -38,6 +38,24 @@ describe("jsDelivr access without owner credentials", function () { expect(error.message).to.equal("token_expired"); }); + it("refuses Git LFS pointers, which jsDelivr serves instead of the file", async () => { + const pointer = "version https://git-lfs.github.com/spec/v1\noid sha256:" + "b".repeat(64) + "\nsize 123\n"; + got.stream = () => Readable.from([pointer]); + const source = new GitHubStream({ repoId: "r", organization: "owner", repoName: "repo", commit, getToken: () => PUBLIC_CDN_TOKEN }); + const error = await (async () => { for await (const _ of await source.downloadWithFallback(PUBLIC_CDN_TOKEN, "blob-sha", "big.bin")) void _; })().catch(e => e); + expect(error.message).to.equal("upstream_error"); + }); + + it("skips the automatic refresh, which needs the GitHub API", async () => { + const Repository = require("../src/core/Repository").default; + const GitHubBase = require("../src/core/source/GitHubBase").default; + const repo = Object.create(Repository.prototype); + repo._model = { options: { update: true, expirationMode: "never" }, lastView: new Date(0), source: {} }; + Object.defineProperty(repo, "source", { value: Object.create(GitHubBase.prototype) }); + repo.getToken = async () => PUBLIC_CDN_TOKEN; + await repo.refreshIfNeeded(); + }); + it("encodes owner, repository and path segments", () => { expect(jsDelivrFileUrl("o", "r", commit, "x/%2e")).to.equal(`https://cdn.jsdelivr.net/gh/o/r@${commit}/x/%252e`); for (const path of ["../x", "a/./b", "a//b", ""]) expect(() => jsDelivrFileUrl("o", "r", commit, path)).to.throw("invalid_path");