Skip to content

Commit 726a341

Browse files
committed
Restrict when tools: toolcache can be used
1 parent 1cc5eb6 commit 726a341

File tree

7 files changed

+129
-53
lines changed

7 files changed

+129
-53
lines changed

lib/analyze-action.js

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

lib/init-action-post.js

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

lib/init-action.js

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

lib/upload-lib.js

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

lib/upload-sarif-action.js

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

src/setup-codeql.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ test("getCodeQLSource correctly returns latest version from toolcache when tools
259259
const loggedMessages: LoggedMessage[] = [];
260260
const logger = getRecordingLogger(loggedMessages);
261261

262+
process.env["GITHUB_EVENT_NAME"] = "dynamic";
263+
262264
const latestToolcacheVersion = "3.2.1";
263265
const latestVersionPath = "/path/to/latest";
264266
const testVersions = ["2.3.1", latestToolcacheVersion, "1.2.3"];
@@ -318,6 +320,8 @@ test("getCodeQLSource falls back to downloading the CLI if the toolcache doesn't
318320
const loggedMessages: LoggedMessage[] = [];
319321
const logger = getRecordingLogger(loggedMessages);
320322

323+
process.env["GITHUB_EVENT_NAME"] = "dynamic";
324+
321325
const testVersions = [];
322326
const findAllVersionsStub = sinon
323327
.stub(toolcache, "findAllVersions")

src/setup-codeql.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { default as deepEqual } from "fast-deep-equal";
77
import * as semver from "semver";
88
import { v4 as uuidV4 } from "uuid";
99

10-
import { isRunningLocalAction } from "./actions-util";
10+
import { isDynamicWorkflow, isRunningLocalAction } from "./actions-util";
1111
import * as api from "./api-client";
1212
import * as defaults from "./defaults.json";
1313
import {
@@ -351,20 +351,37 @@ export async function getCodeQLSource(
351351
toolsInput !== undefined &&
352352
toolsInput === CODEQL_TOOLCACHE_INPUT
353353
) {
354-
// If `toolsInput === "toolcache"`, try to find the latest version of the CLI that's available in the toolcache
355-
// and use that. We perform this check here since we can set `cliVersion` directly and don't want to default to
356-
// the linked version.
357-
logger.info(
358-
`Attempting to use the latest CodeQL CLI version in the toolcache, as requested by 'tools: ${toolsInput}'.`,
359-
);
360-
361-
const latestToolcacheVersion = getLatestToolcacheVersion(logger);
362-
if (latestToolcacheVersion) {
363-
cliVersion = latestToolcacheVersion;
364-
} else {
354+
let latestToolcacheVersion: string | undefined;
355+
356+
// We only allow `toolsInput === "toolcache"` for `dynamic` events. In general, using `toolsInput === "toolcache"`
357+
// can lead to alert wobble and so it shouldn't be used for an analysis where results are intended to be uploaded.
358+
// We also allow this in test mode.
359+
const allowToolcacheValue = isDynamicWorkflow() || util.isInTestMode();
360+
if (allowToolcacheValue) {
361+
// If `toolsInput === "toolcache"`, try to find the latest version of the CLI that's available in the toolcache
362+
// and use that. We perform this check here since we can set `cliVersion` directly and don't want to default to
363+
// the linked version.
365364
logger.info(
366-
`Found no CodeQL CLI in the toolcache, ignoring 'tools: ${toolsInput}'...`,
365+
`Attempting to use the latest CodeQL CLI version in the toolcache, as requested by 'tools: ${toolsInput}'.`,
367366
);
367+
368+
latestToolcacheVersion = getLatestToolcacheVersion(logger);
369+
if (latestToolcacheVersion) {
370+
cliVersion = latestToolcacheVersion;
371+
}
372+
}
373+
374+
if (latestToolcacheVersion === undefined) {
375+
if (allowToolcacheValue) {
376+
logger.info(
377+
`Found no CodeQL CLI in the toolcache, ignoring 'tools: ${toolsInput}'...`,
378+
);
379+
} else {
380+
logger.warning(
381+
`Ignoring 'tools: ${toolsInput}' because the workflow was not triggered dynamically.`,
382+
);
383+
}
384+
368385
cliVersion = defaultCliVersion.cliVersion;
369386
tagName = defaultCliVersion.tagName;
370387
}

0 commit comments

Comments
 (0)