mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-03 01:23:31 +01:00
feat: serve public files through jsDelivr when the owner has no GitHub access
Instead of failing, repositories whose owners have no usable credential read files from jsDelivr at the pinned commit, and archives from codeload. Neither needs a token or spends GitHub API quota, and neither serves private content. The shared cache still verifies every file against its Git blob SHA. Calls that need the GitHub API (tree listing, refresh, metadata) still return token_expired, and a jsDelivr read does not clear the owner's access alert. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
341b0cc973
commit
4e10e6662d
6 files changed
+98
-13
No files matched your search
@@ -182,13 +182,15 @@ describeMongo("credential access (MongoDB)", function () {
|
||||
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" } })),
|
||||
new Repository(await RepoModel.create({ owner: owner._id, repoId: "no-access-repo", source: { type: "GitHubStream", repositoryName: "example/no-access" } })),
|
||||
];
|
||||
for (const resource of resources) {
|
||||
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 () => {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
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("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");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user