Skip to content

Commit 1fae5bf

Browse files
authored
Merge pull request #1051 from github/henrymercer/run-atm-on-windows
Run ML-powered queries on Windows with CodeQL CLI 2.9.0+
2 parents ace076b + 533ce91 commit 1fae5bf

11 files changed

+108
-40
lines changed

.github/workflows/__ml-powered-queries.yml

+15-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.js

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.js

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.test.js

+15-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pr-checks/checks/ml-powered-queries.yml

+14-5
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@ steps:
3232
retention-days: 7
3333

3434
- name: Check results
35+
# Running ML-powered queries on Windows requires CodeQL CLI 2.9.0+. We don't run these checks
36+
# against Windows and `cached` while CodeQL CLI 2.9.0 makes its way into `cached` to avoid the
37+
# test starting to fail when the cached CodeQL Bundle gets updated. Once the CodeQL Bundle
38+
# containing CodeQL CLI 2.9.0 has been fully released, we can drop this line and start running
39+
# these checks on Windows and `cached`.
40+
if: matrix.os != 'windows-latest' || matrix.version != 'cached'
3541
env:
36-
IS_WINDOWS: ${{ matrix.os == 'windows-latest' }}
42+
# Running on Windows requires CodeQL CLI 2.9.0+, which has so far only made it to 'latest'.
43+
SHOULD_RUN_ML_POWERED_QUERIES: ${{ matrix.os != 'windows-latest' || matrix.version == 'latest' || matrix.version == 'nightly-latest' }}
3744
shell: bash
3845
run: |
46+
echo "Expecting ML-powered queries to be run: ${SHOULD_RUN_ML_POWERED_QUERIES}"
47+
3948
cd "$RUNNER_TEMP/results"
4049
# We should run at least the ML-powered queries in `expected_rules`.
4150
expected_rules="js/ml-powered/nosql-injection js/ml-powered/path-injection js/ml-powered/sql-injection js/ml-powered/xss"
@@ -44,10 +53,10 @@ steps:
4453
found_rule=$(jq --arg rule "${rule}" '[.runs[0].tool.extensions[].rules | select(. != null) |
4554
flatten | .[].id] | any(. == $rule)' javascript.sarif)
4655
echo "Did find rule '${rule}': ${found_rule}"
47-
if [[ "${found_rule}" != "true" && "${IS_WINDOWS}" != "true" ]]; then
56+
if [[ "${found_rule}" != "true" && "${SHOULD_RUN_ML_POWERED_QUERIES}" == "true" ]]; then
4857
echo "Expected SARIF output to contain rule '${rule}', but found no such rule."
4958
exit 1
50-
elif [[ "${found_rule}" == "true" && "${IS_WINDOWS}" == "true" ]]; then
59+
elif [[ "${found_rule}" == "true" && "${SHOULD_RUN_ML_POWERED_QUERIES}" != "true" ]]; then
5160
echo "Found rule '${rule}' in the SARIF output which shouldn't have been part of the analysis."
5261
exit 1
5362
fi
@@ -58,10 +67,10 @@ steps:
5867
select(.properties.score != null and (.rule.id | startswith("js/ml-powered/")))] | length' \
5968
javascript.sarif)
6069
echo "Found ${num_alerts} alerts from ML-powered queries.";
61-
if [[ "${num_alerts}" -eq 0 && "${IS_WINDOWS}" != "true" ]]; then
70+
if [[ "${num_alerts}" -eq 0 && "${SHOULD_RUN_ML_POWERED_QUERIES}" == "true" ]]; then
6271
echo "Expected to find at least one alert from an ML-powered query but found ${num_alerts}."
6372
exit 1
64-
elif [[ "${num_alerts}" -ne 0 && "${IS_WINDOWS}" == "true" ]]; then
73+
elif [[ "${num_alerts}" -ne 0 && "${SHOULD_RUN_ML_POWERED_QUERIES}" != "true" ]]; then
6574
echo "Expected not to find any alerts from an ML-powered query but found ${num_alerts}."
6675
exit 1
6776
fi

src/codeql.ts

+7
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ export const CODEQL_VERSION_ML_POWERED_QUERIES = "2.7.5";
232232
*/
233233
export const CODEQL_VERSION_NEW_TRACING = "2.7.0";
234234

235+
/**
236+
* Versions 2.9.0+ of the CodeQL CLI run machine learning models from a temporary directory, which
237+
* resolves an issue on Windows where TensorFlow models are not correctly loaded due to the path of
238+
* some of their files being greater than MAX_PATH (260 characters).
239+
*/
240+
export const CODEQL_VERSION_ML_POWERED_QUERIES_WINDOWS = "2.9.0";
241+
235242
function getCodeQLBundleName(): string {
236243
let platform: string;
237244
if (process.platform === "win32") {

src/config-utils.test.ts

+37-15
Original file line numberDiff line numberDiff line change
@@ -1804,42 +1804,64 @@ test(
18041804
"security-extended",
18051805
undefined
18061806
);
1807+
// Test that the ~0.1.0 version of ML-powered queries is run on v2.8.3 of the CLI.
1808+
test(
1809+
mlPoweredQueriesMacro,
1810+
"2.8.3",
1811+
true,
1812+
undefined,
1813+
"security-extended",
1814+
process.platform === "win32" ? undefined : "~0.1.0"
1815+
);
18071816
// Test that ML-powered queries aren't run when the user hasn't specified that we should run the
18081817
// `security-extended` or `security-and-quality` query suite.
18091818
test(mlPoweredQueriesMacro, "2.7.5", true, undefined, undefined, undefined);
1810-
// Test that ML-powered queries are run on non-Windows platforms running `security-extended`.
1819+
// Test that ML-powered queries are run on non-Windows platforms running `security-extended` on
1820+
// versions of the CodeQL CLI prior to 2.9.0.
18111821
test(
18121822
mlPoweredQueriesMacro,
1813-
"2.7.5",
1823+
"2.8.5",
18141824
true,
18151825
undefined,
18161826
"security-extended",
1817-
process.platform === "win32" ? undefined : "~0.1.0"
1827+
process.platform === "win32" ? undefined : "~0.2.0"
18181828
);
1819-
// Test that ML-powered queries are run on non-Windows platforms running `security-and-quality`.
1829+
// Test that ML-powered queries are run on non-Windows platforms running `security-and-quality` on
1830+
// versions of the CodeQL CLI prior to 2.9.0.
18201831
test(
18211832
mlPoweredQueriesMacro,
1822-
"2.7.5",
1833+
"2.8.5",
18231834
true,
18241835
undefined,
18251836
"security-and-quality",
1826-
process.platform === "win32" ? undefined : "~0.1.0"
1837+
process.platform === "win32" ? undefined : "~0.2.0"
18271838
);
1828-
// Test that we don't inject an ML-powered query pack if the user has already specified one.
1839+
// Test that ML-powered queries are run on all platforms running `security-extended` on CodeQL CLI
1840+
// 2.9.0+.
18291841
test(
18301842
mlPoweredQueriesMacro,
1831-
"2.7.5",
1843+
"2.9.0",
18321844
true,
1833-
1834-
"security-and-quality",
1835-
process.platform === "win32" ? undefined : "0.0.1"
1845+
undefined,
1846+
"security-extended",
1847+
"~0.2.0"
18361848
);
1837-
// Test that the ~0.2.0 version of ML-powered queries is run on v2.8.4 of the CLI.
1849+
// Test that ML-powered queries are run on all platforms running `security-and-quality` on CodeQL
1850+
// CLI 2.9.0+.
18381851
test(
18391852
mlPoweredQueriesMacro,
1840-
"2.8.4",
1853+
"2.9.0",
18411854
true,
18421855
undefined,
1843-
"security-extended",
1844-
process.platform === "win32" ? undefined : "~0.2.0"
1856+
"security-and-quality",
1857+
"~0.2.0"
1858+
);
1859+
// Test that we don't inject an ML-powered query pack if the user has already specified one.
1860+
test(
1861+
mlPoweredQueriesMacro,
1862+
"2.9.0",
1863+
true,
1864+
1865+
"security-and-quality",
1866+
"0.0.1"
18451867
);

src/config-utils.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as api from "./api-client";
88
import {
99
CodeQL,
1010
CODEQL_VERSION_ML_POWERED_QUERIES,
11+
CODEQL_VERSION_ML_POWERED_QUERIES_WINDOWS,
1112
ResolveQueriesOutput,
1213
} from "./codeql";
1314
import * as externalQueries from "./external-queries";
@@ -293,8 +294,12 @@ async function addBuiltinSuiteQueries(
293294
// opted into the ML-powered queries beta, and a user hasn't already added the ML-powered query
294295
// pack, then add the ML-powered query pack so that we run ML-powered queries.
295296
if (
296-
// Disable ML-powered queries on Windows
297-
process.platform !== "win32" &&
297+
// Only run ML-powered queries on Windows if we have a CLI that supports it.
298+
(process.platform !== "win32" ||
299+
(await codeQlVersionAbove(
300+
codeQL,
301+
CODEQL_VERSION_ML_POWERED_QUERIES_WINDOWS
302+
))) &&
298303
languages.includes("javascript") &&
299304
(found === "security-extended" || found === "security-and-quality") &&
300305
!packs.javascript?.some(isMlPoweredJsQueriesPack) &&

0 commit comments

Comments
 (0)