Skip to content

Commit 4a4922e

Browse files
henrymercerCopilot
andcommitted
Parse GitHub release URLs
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 78a5dc2 commit 4a4922e

2 files changed

Lines changed: 152 additions & 1 deletion

File tree

‎src/codeql-release.test.ts‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
BundleSelectionOptions,
66
getPublicRelease,
77
getRelease,
8+
parseCodeQLReleaseUrl,
89
selectBundle,
910
} from "./codeql-release";
1011
import { ActionsEnvVars } from "./environment";
@@ -17,6 +18,7 @@ import {
1718
getTestEnv,
1819
initAllState,
1920
LoggedMessage,
21+
SAMPLE_DOTCOM_API_DETAILS,
2022
} from "./testing-utils";
2123
import { ConfigurationError, GitHubVariant } from "./util";
2224

@@ -198,3 +200,93 @@ test("getPublicRelease constructs download URLs without looking up the release",
198200
);
199201
t.deepEqual(fixture.requests, []);
200202
});
203+
204+
test("parseCodeQLReleaseUrl accepts web and legacy links and decodes tags once", (t) => {
205+
for (const [suffix, tagName] of [
206+
[`tag/${TAG}`, TAG],
207+
[TAG, TAG],
208+
["codeql-bundle-20230120", "codeql-bundle-20230120"],
209+
["codeql-bundle-v2.27.1-rc.1", "codeql-bundle-v2.27.1-rc.1"],
210+
["tag/run-123", "run-123"],
211+
["tag/build/123", "build/123"],
212+
["tag/build%2F123%2Brc%231", "build/123+rc#1"],
213+
["tag/build%252F123", "build%2F123"],
214+
[`tag/${TAG}/?expanded=true#assets`, TAG],
215+
]) {
216+
t.deepEqual(
217+
parseCodeQLReleaseUrl(
218+
`https://github.com/octo/tools/releases/${suffix}`,
219+
SAMPLE_DOTCOM_API_DETAILS,
220+
),
221+
{ ...REFERENCE, isCurrentInstance: true, tagName },
222+
);
223+
}
224+
t.throws(
225+
() =>
226+
parseCodeQLReleaseUrl(
227+
"https://github.com/octo/tools/releases/tag/%zz",
228+
SAMPLE_DOTCOM_API_DETAILS,
229+
),
230+
{ instanceOf: ConfigurationError, message: /Invalid URL encoding/ },
231+
);
232+
});
233+
234+
test("parseCodeQLReleaseUrl matches the current instance and GitHub.com by origin", (t) => {
235+
for (const [url, origin, serverURL] of [
236+
["https://github.example.test", "https://github.com", "https://github.com"],
237+
[
238+
"https://github.example.test/",
239+
"https://github.example.test",
240+
"https://github.example.test",
241+
],
242+
[
243+
"https://GitHub.Example.test",
244+
"https://github.example.test",
245+
"https://github.example.test",
246+
],
247+
[
248+
"https://github.example.test:443",
249+
"https://GITHUB.example.test:443",
250+
"https://github.example.test",
251+
],
252+
]) {
253+
t.deepEqual(
254+
parseCodeQLReleaseUrl(`${origin}/octo/tools/releases/tag/${TAG}`, {
255+
auth: "token",
256+
url,
257+
apiURL: undefined,
258+
}),
259+
{
260+
...REFERENCE,
261+
serverURL,
262+
isCurrentInstance: serverURL !== "https://github.com",
263+
},
264+
`${url} ${origin}`,
265+
);
266+
}
267+
});
268+
269+
test("parseCodeQLReleaseUrl excludes archives, REST references and untrusted URLs", (t) => {
270+
for (const input of [
271+
"/tmp/codeql-bundle.tar.zst",
272+
"nightly",
273+
`https://github.com/octo/tools/releases/download/${TAG}/${JAVA}`,
274+
"https://api.github.com/repos/octo/tools/releases/assets/123",
275+
"https://api.github.com/repos/octo/tools/releases/123",
276+
`https://api.github.com/repos/octo/tools/releases/tags/${TAG}`,
277+
"https://github.com/octo/tools/releases/latest",
278+
"https://github.com/octo/tools/releases/tag/",
279+
"https://github.com/octo/tools/releases/run-123",
280+
`https://github.com.example.test/octo/tools/releases/tag/${TAG}`,
281+
`https://github.com:8443/octo/tools/releases/tag/${TAG}`,
282+
`https://github.com@example.test/octo/tools/releases/tag/${TAG}`,
283+
`https://user@github.com/octo/tools/releases/tag/${TAG}`,
284+
`http://github.com/octo/tools/releases/tag/${TAG}`,
285+
]) {
286+
t.is(
287+
parseCodeQLReleaseUrl(input, SAMPLE_DOTCOM_API_DETAILS),
288+
undefined,
289+
input,
290+
);
291+
}
292+
});

‎src/codeql-release.ts‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as semver from "semver";
22

33
import { ActionState } from "./action-common";
4+
import type { GitHubApiDetails } from "./api-client";
45
import { CodeQLBundle, getCodeQLBundleName } from "./codeql-bundle";
56
import { CODEQL_VERSION_ZSTD_BUNDLE } from "./feature-flags";
67
import {
@@ -9,7 +10,7 @@ import {
910
} from "./per-language-bundles";
1011
import { BundlePlatform } from "./platform";
1112
import type { CompressionMethod } from "./tar";
12-
import { ConfigurationError, GitHubVariant } from "./util";
13+
import { ConfigurationError, GITHUB_DOTCOM_URL, GitHubVariant } from "./util";
1314

1415
/** Identifies a release on a GitHub instance. */
1516
export interface CodeQLReleaseReference {
@@ -27,6 +28,64 @@ export interface CodeQLRelease {
2728
getAssetURL(name: string): string | undefined;
2829
}
2930

31+
/** A release requested by URL, on this GitHub instance or on GitHub.com. */
32+
export interface RequestedRelease extends CodeQLReleaseReference {
33+
isCurrentInstance: boolean;
34+
}
35+
36+
/**
37+
* Recognizes release pages, including legacy bundle links, but never asset URLs.
38+
*
39+
* We only accept https URLs without credentials, on this GitHub instance, whose API we can use, or
40+
* on GitHub.com, whose public releases we can download without credentials. The tag is
41+
* percent-decoded once, and may contain `/`.
42+
*/
43+
export function parseCodeQLReleaseUrl(
44+
input: string,
45+
apiDetails: GitHubApiDetails,
46+
): RequestedRelease | undefined {
47+
let url: URL;
48+
try {
49+
url = new URL(input);
50+
} catch {
51+
return undefined;
52+
}
53+
if (url.protocol !== "https:" || url.username || url.password) {
54+
return undefined;
55+
}
56+
57+
// GitHub instances are served from the root of their origin.
58+
const isCurrentInstance = url.origin === new URL(apiDetails.url).origin;
59+
if (!isCurrentInstance && url.origin !== new URL(GITHUB_DOTCOM_URL).origin) {
60+
return undefined;
61+
}
62+
// Older links to bundle releases omit "tag/", which GitHub still supports. We only accept this
63+
// form for bundle tags, since other names can clash with routes like `releases/latest`.
64+
const match = url.pathname
65+
.replace(/\/$/, "")
66+
.match(
67+
/^\/([\w.-]+)\/([\w.-]+)\/releases\/(?:tag\/(.+)|(codeql-bundle-[^/]+))$/,
68+
);
69+
if (match === null) {
70+
return undefined;
71+
}
72+
let tagName: string;
73+
try {
74+
tagName = decodeURIComponent(match[3] ?? match[4]);
75+
} catch {
76+
throw new ConfigurationError(
77+
"Invalid URL encoding in the CodeQL release tag.",
78+
);
79+
}
80+
return {
81+
serverURL: url.origin,
82+
isCurrentInstance,
83+
owner: match[1],
84+
repo: match[2],
85+
tagName,
86+
};
87+
}
88+
3089
/**
3190
* Encodes a tag for use in a URL path. Slashes stay as path separators, as in GitHub's release URLs
3291
* for tags like `build/123`.

0 commit comments

Comments
 (0)