mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
4e10e6662d
commit
5b350aaebe
3 files changed
+52
-19
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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<Buffer | string>) {
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user