Skip to content

Commit 8f22549

Browse files
Copilotfelickz
andauthored
Fix GHES multi-host resolution bug in getSortedCliVersions
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent 06da642 commit 8f22549

7 files changed

Lines changed: 228 additions & 20 deletions

File tree

‎lib/entry-points.js‎

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

‎src/api-client.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ test.serial("getApiClient", async (t) => {
4646
);
4747
});
4848

49+
test.serial("getUnauthenticatedApiClientForDotcom", async (t) => {
50+
const pluginStub: sinon.SinonStub = sinon.stub(githubUtils.GitHub, "plugin");
51+
const githubStub: sinon.SinonStub = sinon.stub();
52+
pluginStub.returns(githubStub);
53+
54+
const apiClient = api.getUnauthenticatedApiClientForDotcom();
55+
t.truthy(apiClient);
56+
57+
t.true(githubStub.calledOnce);
58+
// No `auth` should be set: this client must be unauthenticated, and it must always target
59+
// GitHub.com's API, regardless of which GitHub instance the Action itself is running on.
60+
t.assert(
61+
githubStub.calledOnceWithExactly({
62+
baseUrl: "https://api.github.com",
63+
log: sinon.match.any,
64+
userAgent: `CodeQL-Action/${actionsUtil.getActionVersion()}`,
65+
request: sinon.match.any,
66+
retry: {
67+
doNotRetry: DO_NOT_RETRY_STATUSES,
68+
},
69+
}),
70+
);
71+
});
72+
4973
function mockGetMetaVersionHeader(
5074
versionHeader: string | undefined,
5175
): sinon.SinonStub<any, any> {

‎src/api-client.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
asHTTPError,
2828
ConfigurationError,
2929
getRequiredEnvParam,
30+
GITHUB_DOTCOM_API_URL,
3031
GITHUB_DOTCOM_URL,
3132
GitHubVariant,
3233
GitHubVersion,
@@ -181,6 +182,38 @@ export function getApiClientWithExternalAuth(
181182
return createApiClientWithDetails(apiDetails, { allowExternal: true, proxy });
182183
}
183184

185+
/**
186+
* Gets an unauthenticated API client scoped to GitHub.com, for use when working with resources
187+
* that only ever exist on GitHub.com -- such as the canonical CodeQL Action repository's release
188+
* history -- regardless of which GitHub instance this Action itself is running on.
189+
*
190+
* This must be used instead of `getApiClient`/`getApiClientWithExternalAuth` whenever the current
191+
* GitHub instance is not GitHub.com (for example GitHub Enterprise Server or GHEC with data
192+
* residency), since in that case the current instance's authentication token cannot authenticate
193+
* to GitHub.com. Sending it there anyway would both fail and needlessly expose that token to a
194+
* different host. Reading public data, such as the release history of a public repository, does
195+
* not require authentication.
196+
*/
197+
export function getUnauthenticatedApiClientForDotcom(
198+
proxy?: ProxyAgent,
199+
): ApiClient {
200+
const retryingOctokit = githubUtils.GitHub.plugin(retry.retry);
201+
return new retryingOctokit({
202+
baseUrl: GITHUB_DOTCOM_API_URL,
203+
userAgent: `CodeQL-Action/${getActionVersion()}`,
204+
log: {
205+
debug: core.debug,
206+
info: core.info,
207+
warn: core.warning,
208+
error: core.error,
209+
},
210+
request: makeProxyRequestOptions(proxy),
211+
retry: {
212+
doNotRetry: DO_NOT_RETRY_STATUSES,
213+
},
214+
});
215+
}
216+
184217
/**
185218
* Gets a value for the `Authorization` header for a request to `url`; or `undefined` if the
186219
* `Authorization` header should not be set for `url`.

‎src/setup-codeql.test.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
LoggedMessage,
1919
SAMPLE_DEFAULT_CLI_VERSION,
2020
SAMPLE_DOTCOM_API_DETAILS,
21+
SAMPLE_GHES_API_DETAILS,
2122
checkExpectedLogMessages,
2223
createFeatures,
2324
createTestConfig,
@@ -435,6 +436,47 @@ function mockListCodeQLBundleReleasesWithNightlyFallback(
435436
} as any);
436437
}
437438

439+
/**
440+
* As `mockListStableCodeQLBundleReleases`, but for use when `getCodeQLSource` is called with a
441+
* non-dotcom `variant` (for example `GitHubVariant.GHES` or `GitHubVariant.GHEC_DR`). The
442+
* canonical CodeQL Action repository's release history only ever exists on GitHub.com, so in that
443+
* case we must mock the unauthenticated GitHub.com client (`getUnauthenticatedApiClientForDotcom`)
444+
* rather than the current instance's client (`getApiClient`).
445+
*
446+
* The current instance's client is also mocked here, but to return a different set of bogus
447+
* releases and to fail to find the bundle by tag, so that tests using this helper fail if
448+
* `getSortedCliVersions` regresses to (incorrectly) querying the current GitHub instance, or if
449+
* `getCodeQLBundleDownloadURL`'s unrelated, pre-existing attempt to look up the bundle on the
450+
* current instance is not handled gracefully.
451+
*/
452+
function mockListStableCodeQLBundleReleasesForNonDotcomVariant(
453+
releases: unknown[] = STABLE_BUNDLE_RELEASES_TEST_SET,
454+
) {
455+
const dotcomClient = github.getOctokit("123");
456+
const listReleases = sinon.stub(dotcomClient.rest.repos, "listReleases");
457+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
458+
listReleases.resolves({
459+
data: releases,
460+
} as any);
461+
sinon
462+
.stub(api, "getUnauthenticatedApiClientForDotcom")
463+
.value(() => dotcomClient);
464+
465+
const currentInstanceClient = github.getOctokit("456");
466+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
467+
sinon.stub(currentInstanceClient.rest.repos, "listReleases").resolves({
468+
data: [
469+
{ tag_name: "codeql-bundle-v9.9.9", prerelease: false, draft: false },
470+
],
471+
} as any);
472+
sinon
473+
.stub(currentInstanceClient.rest.repos, "getReleaseByTag")
474+
.rejects(new Error("Not Found"));
475+
sinon.stub(api, "getApiClient").value(() => currentInstanceClient);
476+
477+
return listReleases;
478+
}
479+
438480
const LATEST_OFFSET_TOOLS_INPUT_TEST_CASES = [
439481
{ toolsInput: "latest-0", expectedCliVersion: "2.25.3" },
440482
{ toolsInput: "latest-1", expectedCliVersion: "2.25.1" },
@@ -924,6 +966,62 @@ for (const {
924966
);
925967
}
926968

969+
/**
970+
* `latest-<N>`, SemVer ranges, `latest-prerelease`, and `nightly-until-<version>`/
971+
* `nightly-until-stable-<version>` all need to look up the canonical CodeQL Action repository's
972+
* release history. That repository only ever exists on GitHub.com, so on a non-dotcom `variant`
973+
* (GHES or GHEC with data residency) this lookup must be made directly, and unauthenticated,
974+
* against GitHub.com, rather than against the current GitHub instance as for all other API
975+
* requests. Otherwise, the request would either fail outright (since the canonical repository
976+
* typically does not exist on the current instance), or -- if a same-named repository happens to
977+
* exist there -- could silently resolve to the wrong CodeQL CLI version.
978+
*/
979+
const NON_DOTCOM_VARIANT_TOOLS_INPUT_TEST_CASES = [
980+
{ toolsInput: "latest-1", expectedCliVersion: "2.25.1" },
981+
{ toolsInput: "^2.24.0", expectedCliVersion: "2.25.3" },
982+
{ toolsInput: "latest-prerelease", expectedCliVersion: "2.26.0" },
983+
{ toolsInput: "nightly-until-2.20.0", expectedCliVersion: "2.26.0" },
984+
] as const;
985+
986+
for (const variant of [GitHubVariant.GHES, GitHubVariant.GHEC_DR]) {
987+
for (const {
988+
toolsInput,
989+
expectedCliVersion,
990+
} of NON_DOTCOM_VARIANT_TOOLS_INPUT_TEST_CASES) {
991+
test.serial(
992+
`getCodeQLSource resolves 'tools: ${toolsInput}' to CodeQL version ${expectedCliVersion} ` +
993+
`on ${variant} using GitHub.com, rather than the current instance`,
994+
async (t) => {
995+
const features = createFeatures([]);
996+
sinon.stub(process, "platform").value("linux");
997+
mockListStableCodeQLBundleReleasesForNonDotcomVariant();
998+
999+
await withTmpDir(async (tmpDir) => {
1000+
setupActionsVars(tmpDir, tmpDir);
1001+
const source = await setupCodeql.getCodeQLSource(
1002+
toolsInput,
1003+
SAMPLE_DEFAULT_CLI_VERSION,
1004+
undefined, // rawLanguages
1005+
false, // useOverlayAwareDefaultCliVersion
1006+
SAMPLE_GHES_API_DETAILS,
1007+
variant,
1008+
false,
1009+
features,
1010+
getRunnerLogger(true),
1011+
);
1012+
1013+
t.is(source.sourceType, "download");
1014+
// If resolution incorrectly used the current (non-dotcom) instance's API instead of
1015+
// GitHub.com, this would instead resolve to the bogus "9.9.9" release configured by
1016+
// `mockListStableCodeQLBundleReleasesForNonDotcomVariant`.
1017+
t.is(source.toolsVersion, expectedCliVersion);
1018+
t.is(source["cliVersion"], expectedCliVersion);
1019+
});
1020+
},
1021+
);
1022+
}
1023+
}
1024+
9271025
test.serial(
9281026
"getCodeQLSource correctly returns bundled CLI version when tools == latest",
9291027
async (t) => {

0 commit comments

Comments
 (0)