Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8534017

Browse files
authored
Limit GitHub tokens to github.com download URLs (#878)
This makes the Astral mirror slightly less special.
1 parent 7568f55 commit 8534017

3 files changed

Lines changed: 47 additions & 8 deletions

File tree

__tests__/download/download-version.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ describe("download-version", () => {
223223
);
224224
});
225225

226-
it("does not rewrite non-GitHub URLs", async () => {
226+
it("does not send the token to non-GitHub URLs from the default manifest", async () => {
227227
mockGetArtifact.mockResolvedValue({
228228
archiveFormat: "tar.gz",
229229
checksum: "abc123",
@@ -241,8 +241,30 @@ describe("download-version", () => {
241241
expect(mockDownloadTool).toHaveBeenCalledWith(
242242
"https://example.com/uv.tar.gz",
243243
undefined,
244+
undefined,
245+
);
246+
});
247+
248+
it("does not send the token to GitHub lookalike hosts", async () => {
249+
mockGetArtifact.mockResolvedValue({
250+
archiveFormat: "tar.gz",
251+
checksum: "abc123",
252+
downloadUrl: "https://github.com.evil.test/uv.tar.gz",
253+
});
254+
255+
await downloadVersion(
256+
"unknown-linux-gnu",
257+
"x86_64",
258+
"0.9.26",
259+
undefined,
244260
"token",
245261
);
262+
263+
expect(mockDownloadTool).toHaveBeenCalledWith(
264+
"https://github.com.evil.test/uv.tar.gz",
265+
undefined,
266+
undefined,
267+
);
246268
});
247269

248270
it("falls back to GitHub Releases when the mirror fails", async () => {

dist/setup/index.cjs

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/download/download-version.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ export async function downloadVersion(
5454

5555
const mirrorUrl = rewriteToMirror(artifact.downloadUrl);
5656
const downloadUrl = mirrorUrl ?? artifact.downloadUrl;
57-
// Don't send the GitHub token to the Astral mirror.
58-
const downloadToken = mirrorUrl !== undefined ? undefined : githubToken;
5957

6058
try {
6159
return await downloadArtifact(
@@ -65,7 +63,7 @@ export async function downloadVersion(
6563
arch,
6664
version,
6765
resolvedChecksum,
68-
downloadToken,
66+
githubTokenForUrl(downloadUrl, githubToken),
6967
);
7068
} catch (err) {
7169
if (mirrorUrl === undefined) {
@@ -83,7 +81,7 @@ export async function downloadVersion(
8381
arch,
8482
version,
8583
resolvedChecksum,
86-
githubToken,
84+
githubTokenForUrl(artifact.downloadUrl, githubToken),
8785
);
8886
}
8987
}
@@ -100,6 +98,19 @@ export function rewriteToMirror(url: string): string | undefined {
10098
return ASTRAL_MIRROR_PREFIX + url.slice(GITHUB_RELEASES_PREFIX.length);
10199
}
102100

101+
function githubTokenForUrl(
102+
downloadUrl: string,
103+
githubToken: string,
104+
): string | undefined {
105+
try {
106+
return new URL(downloadUrl).origin === "https://github.com"
107+
? githubToken
108+
: undefined;
109+
} catch {
110+
return undefined;
111+
}
112+
}
113+
103114
async function downloadArtifact(
104115
downloadUrl: string,
105116
artifactName: string,

0 commit comments

Comments
 (0)