Merge pull request #978 from tdurieux/fix/remove-shared-token-fallback

fix: stop using the shared GitHub token; serve public files via jsDelivr
This commit is contained in:
Thomas Durieux authored and GitHub committed 2026-11-28 13:17:54 -08:00
commit 28b154b0eb
12 files changed
+179 -38

No files matched your search

+20
View File
@@ -173,6 +173,26 @@ describeMongo("credential access (MongoDB)", function () {
expect((await GistModel.collection.findOne({ _id: gist.model._id })).source.accessToken).to.equal(undefined);
expect((await PullModel.collection.findOne({ _id: pull.model._id })).source.accessToken).to.equal(undefined);
});
it("never borrows a shared operator token when the owner has no GitHub access", async () => {
const Gist = require("../src/core/Gist").default;
const PullRequest = require("../src/core/PullRequest").default;
const Repository = require("../src/core/Repository").default;
process.env.GITHUB_TOKEN = "operator-token";
try {
const resources = [
new Gist(await GistModel.create({ owner: owner._id, gistId: "no-access-gist", source: { gistId: "no-access" } })),
new PullRequest(await PullModel.create({ owner: owner._id, pullRequestId: "no-access-pr", source: { pullRequestId: "2" } })),
new Repository(await RepoModel.create({ owner: owner._id, repoId: "no-access-repo", source: { type: "GitHubStream", repositoryName: "example/no-access" } })),
];
for (const resource of resources.slice(0, 2)) {
const error = await resource.getToken().then(() => null, e => e);
expect(error?.message).to.equal("token_expired");
expect(error.httpStatus).to.equal(401);
}
// Repository files are still read from jsDelivr when public.
expect(await resources[2].getToken()).to.equal("public-cdn");
} finally { delete process.env.GITHUB_TOKEN; }
});
it("refreshes a repository credential without copying it onto the repository", async () => {
const Repository = require("../src/core/Repository").default;
const repo = new Repository(await RepoModel.create({ owner: owner._id, repoId: "test-repo", source: { type: "GitHubStream" } }));
+63
View File
@@ -0,0 +1,63 @@
const { expect } = require("chai");
const { Readable } = require("stream");
const got = require("got");
const { URL } = require("url");
require("ts-node/register/transpile-only");
const { PUBLIC_CDN_TOKEN, githubTokenForStreamer } = require("../src/core/github-token-context");
const { octokit } = require("../src/core/GitHubUtils");
const GitHubStream = require("../src/core/source/GitHubStream").default;
const { jsDelivrFileUrl } = require("../src/core/source/GitHubStream");
describe("jsDelivr access without owner credentials", function () {
const commit = "a".repeat(40);
let original;
beforeEach(() => { original = got.stream; });
afterEach(() => { got.stream = original; });
it("never reaches the GitHub API and reaches the streamer unchanged", async () => {
let error; try { octokit(PUBLIC_CDN_TOKEN); } catch (e) { error = e; }
expect(error.message).to.equal("token_expired"); expect(error.httpStatus).to.equal(401);
expect(await githubTokenForStreamer(PUBLIC_CDN_TOKEN, "owner/repo")).to.equal(PUBLIC_CDN_TOKEN);
});
it("downloads commit-pinned bytes from jsDelivr without any credential", async () => {
const calls = [];
got.stream = (url, options) => { calls.push({ url: String(url), options }); return Readable.from(["public bytes"]); };
const source = new GitHubStream({ repoId: "r", organization: "owner", repoName: "repo", commit, getToken: () => PUBLIC_CDN_TOKEN });
const chunks = []; for await (const chunk of await source.downloadWithFallback(PUBLIC_CDN_TOKEN, "blob-sha", "src/a b#.py")) chunks.push(Buffer.from(chunk));
expect(Buffer.concat(chunks).toString()).to.equal("public bytes");
expect(calls.map(c => c.url)).to.deep.equal([`https://cdn.jsdelivr.net/gh/owner/repo@${commit}/src/a%20b%23.py`]);
expect(calls[0].options.headers).to.equal(undefined);
expect(() => calls[0].options.hooks.beforeRedirect[0]({ url: new URL("https://evil.example/x") })).to.throw("upstream_error");
});
it("requires a pinned commit so a moving branch is never served", async () => {
got.stream = () => { throw new Error("must not download"); };
const source = new GitHubStream({ repoId: "r", organization: "owner", repoName: "repo", commit: "main", getToken: () => PUBLIC_CDN_TOKEN });
const error = await source.downloadWithFallback(PUBLIC_CDN_TOKEN, "blob-sha", "a.py").catch(e => e);
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");
});
});