Limit GitHub tokens to github.com download URLs

This commit is contained in:
Zsolt Dollenstein
2026-05-11 17:52:04 +01:00
parent 88aa608651
commit 2f9f369997
3 changed files with 47 additions and 6 deletions

View File

@@ -223,7 +223,7 @@ describe("download-version", () => {
); );
}); });
it("does not rewrite non-GitHub URLs", async () => { it("does not send the token to non-GitHub URLs from the default manifest", async () => {
mockGetArtifact.mockResolvedValue({ mockGetArtifact.mockResolvedValue({
archiveFormat: "tar.gz", archiveFormat: "tar.gz",
checksum: "abc123", checksum: "abc123",
@@ -241,8 +241,30 @@ describe("download-version", () => {
expect(mockDownloadTool).toHaveBeenCalledWith( expect(mockDownloadTool).toHaveBeenCalledWith(
"https://example.com/uv.tar.gz", "https://example.com/uv.tar.gz",
undefined, undefined,
undefined,
);
});
it("does not send the token to GitHub lookalike hosts", async () => {
mockGetArtifact.mockResolvedValue({
archiveFormat: "tar.gz",
checksum: "abc123",
downloadUrl: "https://github.com.evil.test/uv.tar.gz",
});
await downloadVersion(
"unknown-linux-gnu",
"x86_64",
"0.9.26",
undefined,
"token", "token",
); );
expect(mockDownloadTool).toHaveBeenCalledWith(
"https://github.com.evil.test/uv.tar.gz",
undefined,
undefined,
);
}); });
it("falls back to GitHub Releases when the mirror fails", async () => { it("falls back to GitHub Releases when the mirror fails", async () => {

11
dist/setup/index.cjs generated vendored
View File

@@ -96989,7 +96989,7 @@ async function downloadVersion(platform2, arch3, version3, checksum, githubToken
const resolvedChecksum = manifestUrl === void 0 ? checksum : resolveChecksum(checksum, artifact.checksum); const resolvedChecksum = manifestUrl === void 0 ? checksum : resolveChecksum(checksum, artifact.checksum);
const mirrorUrl = rewriteToMirror(artifact.downloadUrl); const mirrorUrl = rewriteToMirror(artifact.downloadUrl);
const downloadUrl = mirrorUrl ?? artifact.downloadUrl; const downloadUrl = mirrorUrl ?? artifact.downloadUrl;
const downloadToken = mirrorUrl !== void 0 ? void 0 : githubToken; const downloadToken = githubTokenForUrl(downloadUrl, githubToken);
try { try {
return await downloadArtifact( return await downloadArtifact(
downloadUrl, downloadUrl,
@@ -97014,7 +97014,7 @@ async function downloadVersion(platform2, arch3, version3, checksum, githubToken
arch3, arch3,
version3, version3,
resolvedChecksum, resolvedChecksum,
githubToken githubTokenForUrl(artifact.downloadUrl, githubToken)
); );
} }
} }
@@ -97024,6 +97024,13 @@ function rewriteToMirror(url2) {
} }
return ASTRAL_MIRROR_PREFIX + url2.slice(GITHUB_RELEASES_PREFIX.length); return ASTRAL_MIRROR_PREFIX + url2.slice(GITHUB_RELEASES_PREFIX.length);
} }
function githubTokenForUrl(downloadUrl, githubToken) {
try {
return new URL(downloadUrl).origin === "https://github.com" ? githubToken : void 0;
} catch {
return void 0;
}
}
async function downloadArtifact(downloadUrl, artifactName, platform2, arch3, version3, checksum, githubToken) { async function downloadArtifact(downloadUrl, artifactName, platform2, arch3, version3, checksum, githubToken) {
info(`Downloading uv from "${downloadUrl}" ...`); info(`Downloading uv from "${downloadUrl}" ...`);
const downloadPath = await downloadTool( const downloadPath = await downloadTool(

View File

@@ -54,8 +54,7 @@ export async function downloadVersion(
const mirrorUrl = rewriteToMirror(artifact.downloadUrl); const mirrorUrl = rewriteToMirror(artifact.downloadUrl);
const downloadUrl = mirrorUrl ?? artifact.downloadUrl; const downloadUrl = mirrorUrl ?? artifact.downloadUrl;
// Don't send the GitHub token to the Astral mirror. const downloadToken = githubTokenForUrl(downloadUrl, githubToken);
const downloadToken = mirrorUrl !== undefined ? undefined : githubToken;
try { try {
return await downloadArtifact( return await downloadArtifact(
@@ -83,7 +82,7 @@ export async function downloadVersion(
arch, arch,
version, version,
resolvedChecksum, resolvedChecksum,
githubToken, githubTokenForUrl(artifact.downloadUrl, githubToken),
); );
} }
} }
@@ -100,6 +99,19 @@ export function rewriteToMirror(url: string): string | undefined {
return ASTRAL_MIRROR_PREFIX + url.slice(GITHUB_RELEASES_PREFIX.length); return ASTRAL_MIRROR_PREFIX + url.slice(GITHUB_RELEASES_PREFIX.length);
} }
function githubTokenForUrl(
downloadUrl: string,
githubToken: string,
): string | undefined {
try {
return new URL(downloadUrl).origin === "https://github.com"
? githubToken
: undefined;
} catch {
return undefined;
}
}
async function downloadArtifact( async function downloadArtifact(
downloadUrl: string, downloadUrl: string,
artifactName: string, artifactName: string,