Skip to content

Commit eccc888

Browse files
Copilotfelickz
andauthored
Clarify tools input resolution order and add URL disambiguation tests
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent 16c4f3e commit eccc888

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

‎src/setup-codeql.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,61 @@ test.serial(
118118
},
119119
);
120120

121+
/**
122+
* A URL should never be misidentified as a bare CLI version number or a version range, even when
123+
* it contains version-like path segments, since neither `semver.valid` nor `semver.validRange`
124+
* can ever match a string containing `://`.
125+
*/
126+
const URL_NOT_MISTAKEN_FOR_VERSION_TEST_CASES = [
127+
{
128+
name: "a bundle URL without a recognizable bundle tag",
129+
toolsInput: "https://example.com/assets/codeql-bundle-linux64.tar.gz",
130+
},
131+
{
132+
name: "a bundle URL containing a bare-version-like path segment",
133+
toolsInput:
134+
"https://example.com/download/2.19.0/codeql-bundle-linux64.tar.gz",
135+
},
136+
{
137+
name: "a bundle URL containing a version-range-like path segment",
138+
toolsInput:
139+
"https://example.com/download/2.24.x/codeql-bundle-linux64.tar.gz",
140+
},
141+
] as const;
142+
143+
for (const { name, toolsInput } of URL_NOT_MISTAKEN_FOR_VERSION_TEST_CASES) {
144+
test.serial(
145+
`getCodeQLSource resolves ${name} as a URL, not a version or range`,
146+
async (t) => {
147+
const features = createFeatures([]);
148+
149+
await withTmpDir(async (tmpDir) => {
150+
setupActionsVars(tmpDir, tmpDir);
151+
const source = await setupCodeql.getCodeQLSource(
152+
toolsInput,
153+
SAMPLE_DEFAULT_CLI_VERSION,
154+
undefined, // rawLanguages
155+
false, // useOverlayAwareDefaultCliVersion
156+
SAMPLE_DOTCOM_API_DETAILS,
157+
GitHubVariant.DOTCOM,
158+
false,
159+
features,
160+
getRunnerLogger(true),
161+
);
162+
163+
t.is(source.sourceType, "download");
164+
if (source.sourceType === "download") {
165+
t.is(source.codeqlURL, toolsInput);
166+
t.is(source.compressionMethod, "gzip");
167+
}
168+
// Neither a bare CLI version number nor a version range was detected: the bundle tag
169+
// could not be determined from the URL, so no CLI version is known.
170+
t.is(source["cliVersion"], undefined);
171+
});
172+
},
173+
);
174+
}
175+
121176
const LINKED_BUNDLE_TEST_CASES = [
122177
{
123178
platform: "linux",

‎src/setup-codeql.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ export function convertToSemVer(version: string, logger: Logger): string {
232232
* name or URL of the bundle asset for their platform: the appropriate bundle for the runner's OS,
233233
* and the best compression method it supports, are selected automatically, in the same way as
234234
* they are for the default and `linked` versions of the tools.
235+
*
236+
* `semver.valid` only succeeds if the *entire* input string is a valid semantic version, and the
237+
* semantic versioning spec never permits `:` or `/` characters in a version string. Since every
238+
* URL contains `://`, a CodeQL Bundle URL can never be misidentified as a bare CLI version number
239+
* by this function.
235240
*/
236241
function tryGetCliVersionFromToolsInput(
237242
toolsInput: string,
@@ -263,6 +268,11 @@ function tryGetLatestOffsetFromToolsInput(
263268
* A range must contain a `.`, so that bare numbers or strings such as `2` or `20200601` are not
264269
* misinterpreted as version ranges and are instead handled as local paths, as they were before
265270
* version ranges were supported.
271+
*
272+
* As with `tryGetCliVersionFromToolsInput`, this delegates to `semver.validRange`, which also
273+
* requires the whole input string to be a syntactically valid range and never permits `:` or `/`
274+
* characters. A CodeQL Bundle URL, which always contains `://`, can therefore never be
275+
* misidentified as a version range here either.
266276
*/
267277
function tryGetCliVersionRangeFromToolsInput(
268278
toolsInput: string,
@@ -507,6 +517,30 @@ async function resolveDefaultCliVersion(
507517
* Determines where the CodeQL CLI we want to use comes from. This can be from a local file,
508518
* the Actions toolcache, or a download.
509519
*
520+
* The `tools` input is classified into exactly one of the following mutually exclusive
521+
* categories, listed here in order of precedence:
522+
*
523+
* 1. Not specified: the default CLI version for this environment.
524+
* 2. A reserved keyword: `nightly`/`nightly-latest`, `linked`/`latest`, or `toolcache` (see
525+
* `CODEQL_NIGHTLY_TOOLS_INPUTS`, `CODEQL_BUNDLE_VERSION_ALIAS`, and `CODEQL_TOOLCACHE_INPUT`
526+
* below).
527+
* 3. A URL, i.e. a string starting with `http`: the CodeQL Bundle downloaded from that URL.
528+
* 4. A bare CLI version number, e.g. `2.19.0` or `v2.19.0`: the CodeQL Bundle release
529+
* containing that CLI version (see `tryGetCliVersionFromToolsInput`).
530+
* 5. `latest-<N>`, e.g. `latest-1` or `LATEST-2`: the stable CLI release `N` positions before
531+
* the most recent one (see `tryGetLatestOffsetFromToolsInput`).
532+
* 6. A semantic version range, e.g. `2.24.x`, `2.x`, `~2.24.0`, or `^2.24.0`: the newest stable
533+
* CLI release satisfying that range (see `tryGetCliVersionRangeFromToolsInput`).
534+
* 7. Anything else: a local path to a CodeQL Bundle tarball.
535+
*
536+
* Categories 4-6 are all detected using the `semver` package, which only matches a bare version
537+
* or a range if the *entire* input string conforms to the semantic versioning spec. That spec
538+
* never permits a `:` or `/` character in a version or a range, whereas a URL (category 3) always
539+
* contains `://`. So even though the checks for categories 4-6 don't explicitly exclude URLs,
540+
* they can never match one: a value such as `http://example.com/codeql-bundle-linux64.tar.gz` is
541+
* always resolved as a URL, never as a bare version like `2.19.0` or a range like `2.24.x`, no
542+
* matter what version-like path segments or filenames it contains.
543+
*
510544
* @param toolsInput The argument provided for the `tools` input, if any.
511545
* @param defaultCliVersion The default CLI version that's linked to the CodeQL Action.
512546
* @param rawLanguages Raw set of languages.
@@ -533,7 +567,8 @@ export async function getCodeQLSource(
533567
// If there is an explicit `tools` input, it's not one of the reserved values, it doesn't appear
534568
// to point to a URL, and it isn't a bare CodeQL CLI version number, a `latest-<N>` version
535569
// offset, or a semantic version range, then we assume it is a local path and use the CLI from
536-
// there.
570+
// there. See the order-of-operations note in this function's doc comment above for the full
571+
// list of categories and why a URL is never confused with a version number or a range.
537572
// TODO: This appears to misclassify filenames that happen to start with `http` as URLs.
538573
if (
539574
toolsInput &&

0 commit comments

Comments
 (0)