Files
codeql-action/lib/codeql.js
T

731 lines
33 KiB
JavaScript
Raw Normal View History

2020-06-26 17:22:19 +01:00
"use strict";
2021-07-27 17:59:59 +01:00
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
2020-06-26 17:22:19 +01:00
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
2021-07-27 17:59:59 +01:00
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
2020-06-26 17:22:19 +01:00
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
2020-06-26 17:22:19 +01:00
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExtraOptions = exports.getCodeQLForTesting = exports.getCachedCodeQL = exports.setCodeQL = exports.getCodeQL = exports.convertToSemVer = exports.getCodeQLURLVersion = exports.setupCodeQL = exports.getCodeQLActionRepository = exports.CODEQL_VERSION_NEW_TRACING = exports.CODEQL_VERSION_COUNTS_LINES = exports.CommandInvocationError = void 0;
2020-06-26 17:22:19 +01:00
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
2020-11-20 11:35:59 +01:00
const toolrunner = __importStar(require("@actions/exec/lib/toolrunner"));
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
const query_string_1 = __importDefault(require("query-string"));
const semver = __importStar(require("semver"));
2020-09-29 14:43:37 +01:00
const actions_util_1 = require("./actions-util");
const api = __importStar(require("./api-client"));
const defaults = __importStar(require("./defaults.json")); // Referenced from codeql-action-sync-tool!
const error_matcher_1 = require("./error-matcher");
const languages_1 = require("./languages");
const toolcache = __importStar(require("./toolcache"));
const toolrunner_error_catcher_1 = require("./toolrunner-error-catcher");
const util = __importStar(require("./util"));
2021-06-24 17:30:42 +01:00
class CommandInvocationError extends Error {
constructor(cmd, args, exitCode, error) {
super(`Failure invoking ${cmd} with arguments ${args}.\n
Exit code ${exitCode} and error was:\n
${error}`);
}
}
exports.CommandInvocationError = CommandInvocationError;
/**
2020-07-15 17:36:49 +01:00
* Stores the CodeQL object, and is populated by `setupCodeQL` or `getCodeQL`.
* Can be overridden in tests using `setCodeQL`.
*/
let cachedCodeQL = undefined;
const CODEQL_BUNDLE_VERSION = defaults.bundleVersion;
const CODEQL_DEFAULT_ACTION_REPOSITORY = "github/codeql-action";
2021-08-18 10:15:48 +01:00
/**
* The oldest version of CodeQL that the Action will run with. This should be
* at least three minor versions behind the current version. The version flags
* below can be used to conditionally enable certain features on versions newer
* than this. Please record the reason we cannot support an older version.
*
* Reason: Changes to how the tracing environment is set up.
*/
const CODEQL_MINIMUM_VERSION = "2.3.1";
2021-08-18 09:44:49 +01:00
/**
* Versions of CodeQL that version-flag certain functionality in the Action.
2021-08-18 10:15:48 +01:00
* For convenience, please keep these in descending order. Once a version
* flag is older than the oldest supported version above, it may be removed.
2021-08-18 09:44:49 +01:00
*/
2021-08-12 16:02:19 +01:00
const CODEQL_VERSION_RAM_FINALIZE = "2.5.8";
2021-08-18 09:44:49 +01:00
const CODEQL_VERSION_DIAGNOSTICS = "2.5.6";
const CODEQL_VERSION_METRICS = "2.5.5";
2021-08-18 16:39:00 +01:00
const CODEQL_VERSION_GROUP_RULES = "2.5.5";
const CODEQL_VERSION_SARIF_GROUP = "2.5.3";
2021-09-28 09:25:35 +01:00
exports.CODEQL_VERSION_COUNTS_LINES = "2.6.2";
const CODEQL_VERSION_CUSTOM_QUERY_HELP = "2.7.1";
2021-10-08 12:40:00 +01:00
/**
2021-10-12 13:41:36 +01:00
* This variable controls using the new style of tracing from the CodeQL
* CLI. In particular, with versions above this we will use both indirect
* tracing, and multi-language tracing together with database clusters.
2021-10-08 12:40:00 +01:00
*
2021-10-12 13:41:36 +01:00
* Note that there were bugs in both of these features that were fixed in
* release 2.7.0 of the CodeQL CLI, therefore this flag is only enabled for
* versions above that.
2021-10-08 12:40:00 +01:00
*/
2021-10-12 13:41:36 +01:00
exports.CODEQL_VERSION_NEW_TRACING = "2.7.0";
2020-09-30 16:17:07 +02:00
function getCodeQLBundleName() {
let platform;
if (process.platform === "win32") {
platform = "win64";
}
else if (process.platform === "linux") {
platform = "linux64";
}
else if (process.platform === "darwin") {
platform = "osx64";
}
else {
return "codeql-bundle.tar.gz";
}
return `codeql-bundle-${platform}.tar.gz`;
}
function getCodeQLActionRepository(logger) {
if (!util.isActions()) {
2020-08-27 16:34:09 +01:00
return CODEQL_DEFAULT_ACTION_REPOSITORY;
2020-08-25 16:19:15 +01:00
}
2021-03-16 11:59:10 +00:00
else {
return getActionsCodeQLActionRepository(logger);
}
}
exports.getCodeQLActionRepository = getCodeQLActionRepository;
2021-03-16 11:59:10 +00:00
function getActionsCodeQLActionRepository(logger) {
if (process.env["GITHUB_ACTION_REPOSITORY"] !== undefined) {
return process.env["GITHUB_ACTION_REPOSITORY"];
}
// The Actions Runner used with GitHub Enterprise Server 2.22 did not set the GITHUB_ACTION_REPOSITORY variable.
// This fallback logic can be removed after the end-of-support for 2.22 on 2021-09-23.
if ((0, actions_util_1.isRunningLocalAction)()) {
2020-11-12 14:18:58 +00:00
// This handles the case where the Action does not come from an Action repository,
// e.g. our integration tests which use the Action code from the current checkout.
logger.info("The CodeQL Action is checked out locally. Using the default CodeQL Action repository.");
return CODEQL_DEFAULT_ACTION_REPOSITORY;
}
logger.info("GITHUB_ACTION_REPOSITORY environment variable was not set. Falling back to legacy method of finding the GitHub Action.");
const relativeScriptPathParts = (0, actions_util_1.getRelativeScriptPath)().split(path.sep);
2020-09-14 10:44:43 +01:00
return `${relativeScriptPathParts[0]}/${relativeScriptPathParts[1]}`;
}
async function getCodeQLBundleDownloadURL(apiDetails, variant, logger) {
const codeQLActionRepository = getCodeQLActionRepository(logger);
const potentialDownloadSources = [
// This GitHub instance, and this Action.
[apiDetails.url, codeQLActionRepository],
// This GitHub instance, and the canonical Action.
[apiDetails.url, CODEQL_DEFAULT_ACTION_REPOSITORY],
// GitHub.com, and the canonical Action.
2020-08-25 16:19:15 +01:00
[util.GITHUB_DOTCOM_URL, CODEQL_DEFAULT_ACTION_REPOSITORY],
];
// We now filter out any duplicates.
// Duplicates will happen either because the GitHub instance is GitHub.com, or because the Action is not a fork.
const uniqueDownloadSources = potentialDownloadSources.filter((source, index, self) => {
return !self.slice(0, index).some((other) => (0, fast_deep_equal_1.default)(source, other));
});
2020-09-30 16:17:07 +02:00
const codeQLBundleName = getCodeQLBundleName();
if (variant === util.GitHubVariant.GHAE) {
try {
const release = await api
.getApiClient(apiDetails)
.request("GET /enterprise/code-scanning/codeql-bundle/find/{tag}", {
tag: CODEQL_BUNDLE_VERSION,
});
const assetID = release.data.assets[codeQLBundleName];
if (assetID !== undefined) {
const download = await api
.getApiClient(apiDetails)
.request("GET /enterprise/code-scanning/codeql-bundle/download/{asset_id}", { asset_id: assetID });
const downloadURL = download.data.url;
logger.info(`Found CodeQL bundle at GitHub AE endpoint with URL ${downloadURL}.`);
return downloadURL;
}
else {
logger.info(`Attempted to fetch bundle from GitHub AE endpoint but the bundle ${codeQLBundleName} was not found in the assets ${JSON.stringify(release.data.assets)}.`);
}
}
catch (e) {
logger.info(`Attempted to fetch bundle from GitHub AE endpoint but got error ${e}.`);
}
}
2020-09-14 10:44:43 +01:00
for (const downloadSource of uniqueDownloadSources) {
const [apiURL, repository] = downloadSource;
// If we've reached the final case, short-circuit the API check since we know the bundle exists and is public.
2020-09-14 10:44:43 +01:00
if (apiURL === util.GITHUB_DOTCOM_URL &&
repository === CODEQL_DEFAULT_ACTION_REPOSITORY) {
break;
}
2020-09-14 10:44:43 +01:00
const [repositoryOwner, repositoryName] = repository.split("/");
try {
const release = await api.getApiClient(apiDetails).repos.getReleaseByTag({
owner: repositoryOwner,
repo: repositoryName,
2020-09-14 10:44:43 +01:00
tag: CODEQL_BUNDLE_VERSION,
});
2020-09-14 10:44:43 +01:00
for (const asset of release.data.assets) {
2020-09-30 16:17:07 +02:00
if (asset.name === codeQLBundleName) {
2020-08-25 16:19:15 +01:00
logger.info(`Found CodeQL bundle in ${downloadSource[1]} on ${downloadSource[0]} with URL ${asset.url}.`);
return asset.url;
}
}
}
catch (e) {
2020-08-25 16:19:15 +01:00
logger.info(`Looked for CodeQL bundle in ${downloadSource[1]} on ${downloadSource[0]} but got error ${e}.`);
}
}
2020-09-30 16:17:07 +02:00
return `https://github.com/${CODEQL_DEFAULT_ACTION_REPOSITORY}/releases/download/${CODEQL_BUNDLE_VERSION}/${codeQLBundleName}`;
}
2021-08-18 10:15:48 +01:00
async function setupCodeQL(codeqlURL, apiDetails, tempDir, toolCacheDir, variant, logger, checkVersion) {
2020-06-26 17:22:19 +01:00
try {
2021-06-28 23:33:27 +01:00
// We use the special value of 'latest' to prioritize the version in the
// defaults over any pinned cached version.
const forceLatest = codeqlURL === "latest";
if (forceLatest) {
codeqlURL = undefined;
}
2021-06-28 15:44:13 +01:00
let codeqlFolder;
let codeqlURLVersion;
2021-06-28 23:33:27 +01:00
if (codeqlURL && !codeqlURL.startsWith("http")) {
codeqlFolder = await toolcache.extractTar(codeqlURL, tempDir, logger);
2021-06-28 15:44:13 +01:00
codeqlURLVersion = "local";
2020-06-26 17:22:19 +01:00
}
else {
2021-06-28 15:44:13 +01:00
codeqlURLVersion = getCodeQLURLVersion(codeqlURL || `/${CODEQL_BUNDLE_VERSION}/`);
const codeqlURLSemVer = convertToSemVer(codeqlURLVersion, logger);
// If we find the specified version, we always use that.
codeqlFolder = toolcache.find("CodeQL", codeqlURLSemVer, toolCacheDir, logger);
// If we don't find the requested version, in some cases we may allow a
// different version to save download time if the version hasn't been
// specified explicitly (in which case we always honor it).
if (!codeqlFolder && !codeqlURL && !forceLatest) {
const codeqlVersions = toolcache.findAllVersions("CodeQL", toolCacheDir, logger);
if (codeqlVersions.length === 1) {
const tmpCodeqlFolder = toolcache.find("CodeQL", codeqlVersions[0], toolCacheDir, logger);
if (fs.existsSync(path.join(tmpCodeqlFolder, "pinned-version"))) {
logger.debug(`CodeQL in cache overriding the default ${CODEQL_BUNDLE_VERSION}`);
codeqlFolder = tmpCodeqlFolder;
}
}
}
if (codeqlFolder) {
logger.debug(`CodeQL found in cache ${codeqlFolder}`);
}
else {
2021-06-28 15:44:13 +01:00
if (!codeqlURL) {
codeqlURL = await getCodeQLBundleDownloadURL(apiDetails, variant, logger);
}
const parsedCodeQLURL = new URL(codeqlURL);
const parsedQueryString = query_string_1.default.parse(parsedCodeQLURL.search);
const headers = { accept: "application/octet-stream" };
// We only want to provide an authorization header if we are downloading
// from the same GitHub instance the Action is running on.
// This avoids leaking Enterprise tokens to dotcom.
// We also don't want to send an authorization header if there's already a token provided in the URL.
if (codeqlURL.startsWith(`${apiDetails.url}/`) &&
parsedQueryString["token"] === undefined) {
logger.debug("Downloading CodeQL bundle with token.");
headers.authorization = `token ${apiDetails.auth}`;
}
else {
logger.debug("Downloading CodeQL bundle without token.");
}
logger.info(`Downloading CodeQL tools from ${codeqlURL}. This may take a while.`);
const codeqlPath = await toolcache.downloadTool(codeqlURL, tempDir, headers);
logger.debug(`CodeQL bundle download to ${codeqlPath} complete.`);
const codeqlExtracted = await toolcache.extractTar(codeqlPath, tempDir, logger);
codeqlFolder = await toolcache.cacheDir(codeqlExtracted, "CodeQL", codeqlURLSemVer, toolCacheDir, logger);
}
2020-06-26 17:22:19 +01:00
}
2020-09-14 10:44:43 +01:00
let codeqlCmd = path.join(codeqlFolder, "codeql", "codeql");
if (process.platform === "win32") {
2020-06-26 17:22:19 +01:00
codeqlCmd += ".exe";
}
2020-09-14 10:44:43 +01:00
else if (process.platform !== "linux" && process.platform !== "darwin") {
throw new Error(`Unsupported platform: ${process.platform}`);
2020-06-26 17:22:19 +01:00
}
2021-08-18 10:15:48 +01:00
cachedCodeQL = await getCodeQLForCmd(codeqlCmd, checkVersion);
return { codeql: cachedCodeQL, toolsVersion: codeqlURLVersion };
2020-06-26 17:22:19 +01:00
}
catch (e) {
logger.error(e instanceof Error ? e : new Error(String(e)));
2020-06-26 17:22:19 +01:00
throw new Error("Unable to download and extract CodeQL CLI");
}
}
exports.setupCodeQL = setupCodeQL;
function getCodeQLURLVersion(url) {
2020-06-26 17:22:19 +01:00
const match = url.match(/\/codeql-bundle-(.*)\//);
if (match === null || match.length < 2) {
throw new Error(`Malformed tools url: ${url}. Version could not be inferred`);
}
return match[1];
}
exports.getCodeQLURLVersion = getCodeQLURLVersion;
function convertToSemVer(version, logger) {
2020-06-26 17:22:19 +01:00
if (!semver.valid(version)) {
2020-08-25 16:19:15 +01:00
logger.debug(`Bundle version ${version} is not in SemVer format. Will treat it as pre-release 0.0.0-${version}.`);
2020-09-14 10:44:43 +01:00
version = `0.0.0-${version}`;
2020-06-26 17:22:19 +01:00
}
const s = semver.clean(version);
if (!s) {
throw new Error(`Bundle version ${version} is not in SemVer format.`);
2020-06-26 17:22:19 +01:00
}
return s;
}
exports.convertToSemVer = convertToSemVer;
2020-08-19 15:54:23 +01:00
/**
* Use the CodeQL executable located at the given path.
*/
2021-08-18 10:15:48 +01:00
async function getCodeQL(cmd) {
if (cachedCodeQL === undefined) {
2021-08-18 10:15:48 +01:00
cachedCodeQL = await getCodeQLForCmd(cmd, true);
}
return cachedCodeQL;
2020-06-26 17:22:19 +01:00
}
exports.getCodeQL = getCodeQL;
2020-08-19 15:54:23 +01:00
function resolveFunction(partialCodeql, methodName, defaultImplementation) {
2020-09-14 10:44:43 +01:00
if (typeof partialCodeql[methodName] !== "function") {
2020-08-19 15:54:23 +01:00
if (defaultImplementation !== undefined) {
return defaultImplementation;
}
const dummyMethod = () => {
2020-09-14 10:44:43 +01:00
throw new Error(`CodeQL ${methodName} method not correctly defined`);
};
return dummyMethod;
}
return partialCodeql[methodName];
}
2020-07-15 17:36:49 +01:00
/**
* Set the functionality for CodeQL methods. Only for use in tests.
*
* Accepts a partial object and any undefined methods will be implemented
* to immediately throw an exception indicating which method is missing.
*/
function setCodeQL(partialCodeql) {
cachedCodeQL = {
2020-09-14 10:44:43 +01:00
getPath: resolveFunction(partialCodeql, "getPath", () => "/tmp/dummy-path"),
2021-08-12 16:02:19 +01:00
getVersion: resolveFunction(partialCodeql, "getVersion", () => new Promise((resolve) => resolve("1.0.0"))),
2020-09-14 10:44:43 +01:00
printVersion: resolveFunction(partialCodeql, "printVersion"),
getTracerEnv: resolveFunction(partialCodeql, "getTracerEnv"),
databaseInit: resolveFunction(partialCodeql, "databaseInit"),
databaseInitCluster: resolveFunction(partialCodeql, "databaseInitCluster"),
2020-09-14 10:44:43 +01:00
runAutobuild: resolveFunction(partialCodeql, "runAutobuild"),
extractScannedLanguage: resolveFunction(partialCodeql, "extractScannedLanguage"),
finalizeDatabase: resolveFunction(partialCodeql, "finalizeDatabase"),
2021-05-23 16:27:46 +02:00
resolveLanguages: resolveFunction(partialCodeql, "resolveLanguages"),
2020-09-14 10:44:43 +01:00
resolveQueries: resolveFunction(partialCodeql, "resolveQueries"),
2021-06-03 09:32:44 -07:00
packDownload: resolveFunction(partialCodeql, "packDownload"),
databaseCleanup: resolveFunction(partialCodeql, "databaseCleanup"),
2021-06-22 13:05:12 +01:00
databaseBundle: resolveFunction(partialCodeql, "databaseBundle"),
databaseRunQueries: resolveFunction(partialCodeql, "databaseRunQueries"),
databaseInterpretResults: resolveFunction(partialCodeql, "databaseInterpretResults"),
2021-09-28 09:25:35 +01:00
databasePrintBaseline: resolveFunction(partialCodeql, "databasePrintBaseline"),
};
2020-08-19 15:54:23 +01:00
return cachedCodeQL;
}
exports.setCodeQL = setCodeQL;
2020-08-19 15:54:23 +01:00
/**
* Get the cached CodeQL object. Should only be used from tests.
*
* TODO: Work out a good way for tests to get this from the test context
* instead of having to have this method.
*/
function getCachedCodeQL() {
if (cachedCodeQL === undefined) {
// Should never happen as setCodeQL is called by testing-utils.setupTests
2020-09-14 10:44:43 +01:00
throw new Error("cachedCodeQL undefined");
2020-08-19 15:54:23 +01:00
}
return cachedCodeQL;
}
exports.getCachedCodeQL = getCachedCodeQL;
/**
* Get a real, newly created CodeQL instance for testing. The instance refers to
* a non-existent placeholder codeql command, so tests that use this function
* should also stub the toolrunner.ToolRunner constructor.
*/
async function getCodeQLForTesting() {
return getCodeQLForCmd("codeql-for-testing", false);
}
exports.getCodeQLForTesting = getCodeQLForTesting;
2021-08-18 10:15:48 +01:00
async function getCodeQLForCmd(cmd, checkVersion) {
2021-08-18 09:25:16 +01:00
let cachedVersion = undefined;
2021-08-18 10:15:48 +01:00
const codeql = {
2020-09-14 10:44:43 +01:00
getPath() {
2020-08-19 15:54:23 +01:00
return cmd;
2020-06-26 17:22:19 +01:00
},
2021-08-12 16:02:19 +01:00
async getVersion() {
2021-08-18 09:25:16 +01:00
if (cachedVersion === undefined)
cachedVersion = runTool(cmd, ["version", "--format=terse"]);
return await cachedVersion;
2021-08-12 16:02:19 +01:00
},
2020-09-14 10:44:43 +01:00
async printVersion() {
2021-06-09 13:17:25 -07:00
await runTool(cmd, ["version", "--format=json"]);
2020-06-26 17:22:19 +01:00
},
2020-09-14 10:44:43 +01:00
async getTracerEnv(databasePath) {
2020-08-27 16:34:09 +01:00
// Write tracer-env.js to a temp location.
2021-05-13 22:01:26 +02:00
// BEWARE: The name and location of this file is recognized by `codeql database
// trace-command` in order to enable special support for concatenable tracer
// configurations. Consequently the name must not be changed.
// (This warning can be removed once a different way to recognize the
// action/runner has been implemented in `codeql database trace-command`
// _and_ is present in the latest supported CLI release.)
2020-09-14 10:44:43 +01:00
const tracerEnvJs = path.resolve(databasePath, "working", "tracer-env.js");
2020-08-26 16:18:53 +01:00
fs.mkdirSync(path.dirname(tracerEnvJs), { recursive: true });
fs.writeFileSync(tracerEnvJs, `
const fs = require('fs');
const env = {};
for (let entry of Object.entries(process.env)) {
const key = entry[0];
const value = entry[1];
if (typeof value !== 'undefined' && key !== '_' && !key.startsWith('JAVA_MAIN_CLASS_')) {
env[key] = value;
}
}
process.stdout.write(process.argv[2]);
fs.writeFileSync(process.argv[2], JSON.stringify(env), 'utf-8');`);
2021-05-13 22:01:26 +02:00
// BEWARE: The name and location of this file is recognized by `codeql database
// trace-command` in order to enable special support for concatenable tracer
// configurations. Consequently the name must not be changed.
// (This warning can be removed once a different way to recognize the
// action/runner has been implemented in `codeql database trace-command`
// _and_ is present in the latest supported CLI release.)
2020-09-14 10:44:43 +01:00
const envFile = path.resolve(databasePath, "working", "env.tmp");
2021-06-09 13:17:25 -07:00
await runTool(cmd, [
2020-09-14 10:44:43 +01:00
"database",
"trace-command",
2020-07-02 14:45:14 +01:00
databasePath,
2020-09-14 10:44:43 +01:00
...getExtraOptionsFromEnv(["database", "trace-command"]),
2020-06-26 17:22:19 +01:00
process.execPath,
2020-08-26 16:18:53 +01:00
tracerEnvJs,
2020-09-14 10:44:43 +01:00
envFile,
2021-06-09 13:17:25 -07:00
]);
2020-09-14 10:44:43 +01:00
return JSON.parse(fs.readFileSync(envFile, "utf-8"));
2020-06-26 17:22:19 +01:00
},
2020-09-14 10:44:43 +01:00
async databaseInit(databasePath, language, sourceRoot) {
2021-06-09 13:17:25 -07:00
await runTool(cmd, [
2020-09-14 10:44:43 +01:00
"database",
"init",
2020-07-02 14:45:14 +01:00
databasePath,
2020-09-14 10:44:43 +01:00
`--language=${language}`,
`--source-root=${sourceRoot}`,
...getExtraOptionsFromEnv(["database", "init"]),
2021-06-09 13:17:25 -07:00
]);
2020-06-26 17:22:19 +01:00
},
async databaseInitCluster(databasePath, languages, sourceRoot, processName, processLevel) {
const extraArgs = languages.map((language) => `--language=${language}`);
if (languages.filter(languages_1.isTracedLanguage).length > 0) {
extraArgs.push("--begin-tracing");
if (processName !== undefined) {
extraArgs.push(`--trace-process-name=${processName}`);
}
else {
extraArgs.push(`--trace-process-level=${processLevel || 3}`);
}
}
await runTool(cmd, [
"database",
"init",
"--db-cluster",
databasePath,
`--source-root=${sourceRoot}`,
...extraArgs,
...getExtraOptionsFromEnv(["database", "init"]),
]);
},
2020-09-14 10:44:43 +01:00
async runAutobuild(language) {
const cmdName = process.platform === "win32" ? "autobuild.cmd" : "autobuild.sh";
const autobuildCmd = path.join(path.dirname(cmd), language, "tools", cmdName);
2020-07-02 14:45:14 +01:00
// Update JAVA_TOOL_OPTIONS to contain '-Dhttp.keepAlive=false'
// This is because of an issue with Azure pipelines timing out connections after 4 minutes
// and Maven not properly handling closed connections
// Otherwise long build processes will timeout when pulling down Java packages
// https://developercommunity.visualstudio.com/content/problem/292284/maven-hosted-agent-connection-timeout.html
2020-09-14 10:44:43 +01:00
const javaToolOptions = process.env["JAVA_TOOL_OPTIONS"] || "";
process.env["JAVA_TOOL_OPTIONS"] = [
...javaToolOptions.split(/\s+/),
"-Dhttp.keepAlive=false",
"-Dmaven.wagon.http.pool=false",
].join(" ");
await runTool(autobuildCmd);
2020-07-02 14:45:14 +01:00
},
2020-09-14 10:44:43 +01:00
async extractScannedLanguage(databasePath, language) {
2020-06-26 17:22:19 +01:00
// Get extractor location
2020-09-14 10:44:43 +01:00
let extractorPath = "";
2020-11-20 11:35:59 +01:00
await new toolrunner.ToolRunner(cmd, [
2020-09-14 10:44:43 +01:00
"resolve",
"extractor",
"--format=json",
`--language=${language}`,
...getExtraOptionsFromEnv(["resolve", "extractor"]),
2020-09-03 17:01:51 +01:00
], {
2020-06-26 17:22:19 +01:00
silent: true,
listeners: {
2020-09-14 10:44:43 +01:00
stdout: (data) => {
extractorPath += data.toString();
},
stderr: (data) => {
process.stderr.write(data);
},
},
}).exec();
2020-06-26 17:22:19 +01:00
// Set trace command
2020-09-14 10:44:43 +01:00
const ext = process.platform === "win32" ? ".cmd" : ".sh";
const traceCommand = path.resolve(JSON.parse(extractorPath), "tools", `autobuild${ext}`);
2020-06-26 17:22:19 +01:00
// Run trace command
await (0, toolrunner_error_catcher_1.toolrunnerErrorCatcher)(cmd, [
2020-09-14 10:44:43 +01:00
"database",
"trace-command",
...getExtraOptionsFromEnv(["database", "trace-command"]),
2020-07-02 14:45:14 +01:00
databasePath,
2020-09-14 10:44:43 +01:00
"--",
traceCommand,
2020-09-07 22:50:37 +01:00
], error_matcher_1.errorMatchers);
2020-06-26 17:22:19 +01:00
},
2021-08-12 16:02:19 +01:00
async finalizeDatabase(databasePath, threadsFlag, memoryFlag) {
const args = [
2020-09-14 10:44:43 +01:00
"database",
"finalize",
"--finalize-dataset",
threadsFlag,
2020-09-14 10:44:43 +01:00
...getExtraOptionsFromEnv(["database", "finalize"]),
databasePath,
2021-08-12 16:02:19 +01:00
];
if (await util.codeQlVersionAbove(this, CODEQL_VERSION_RAM_FINALIZE))
args.push(memoryFlag);
await (0, toolrunner_error_catcher_1.toolrunnerErrorCatcher)(cmd, args, error_matcher_1.errorMatchers);
2020-06-26 17:22:19 +01:00
},
2021-05-23 16:27:46 +02:00
async resolveLanguages() {
const codeqlArgs = ["resolve", "languages", "--format=json"];
2021-06-09 13:17:25 -07:00
const output = await runTool(cmd, codeqlArgs);
2021-05-24 11:00:02 +02:00
try {
return JSON.parse(output);
}
catch (e) {
throw new Error(`Unexpected output from codeql resolve languages: ${e}`);
}
2021-05-23 16:27:46 +02:00
},
2020-09-14 10:44:43 +01:00
async resolveQueries(queries, extraSearchPath) {
const codeqlArgs = [
2020-09-14 10:44:43 +01:00
"resolve",
"queries",
2020-06-26 17:22:19 +01:00
...queries,
2020-09-14 10:44:43 +01:00
"--format=bylanguage",
...getExtraOptionsFromEnv(["resolve", "queries"]),
];
if (extraSearchPath !== undefined) {
codeqlArgs.push("--additional-packs", extraSearchPath);
}
2021-06-09 13:17:25 -07:00
const output = await runTool(cmd, codeqlArgs);
2021-05-24 11:00:02 +02:00
try {
return JSON.parse(output);
}
catch (e) {
throw new Error(`Unexpected output from codeql resolve queries: ${e}`);
}
2020-06-26 17:22:19 +01:00
},
async databaseRunQueries(databasePath, extraSearchPath, querySuitePath, memoryFlag, threadsFlag) {
2021-06-09 13:17:25 -07:00
const codeqlArgs = [
2020-09-14 10:44:43 +01:00
"database",
"run-queries",
2020-09-01 14:13:10 +01:00
memoryFlag,
threadsFlag,
2020-07-02 14:45:14 +01:00
databasePath,
2020-09-24 11:42:21 +02:00
"--min-disk-free=1024",
"-v",
...getExtraOptionsFromEnv(["database", "run-queries"]),
];
if (extraSearchPath !== undefined) {
2021-06-09 13:17:25 -07:00
codeqlArgs.push("--additional-packs", extraSearchPath);
}
2021-06-09 13:17:25 -07:00
codeqlArgs.push(querySuitePath);
await runTool(cmd, codeqlArgs);
},
async databaseInterpretResults(databasePath, querySuitePaths, sarifFile, addSnippetsFlag, threadsFlag, automationDetailsId) {
2021-06-09 13:17:25 -07:00
const codeqlArgs = [
"database",
"interpret-results",
threadsFlag,
"--format=sarif-latest",
"-v",
`--output=${sarifFile}`,
addSnippetsFlag,
...getExtraOptionsFromEnv(["database", "interpret-results"]),
];
2021-08-18 09:44:49 +01:00
if (await util.codeQlVersionAbove(this, CODEQL_VERSION_DIAGNOSTICS))
codeqlArgs.push("--print-diagnostics-summary");
if (await util.codeQlVersionAbove(this, CODEQL_VERSION_METRICS))
codeqlArgs.push("--print-metrics-summary");
if (await util.codeQlVersionAbove(this, CODEQL_VERSION_GROUP_RULES))
codeqlArgs.push("--sarif-group-rules-by-pack");
if (await util.codeQlVersionAbove(this, CODEQL_VERSION_CUSTOM_QUERY_HELP))
codeqlArgs.push("--sarif-add-query-help");
2021-08-18 16:39:00 +01:00
if (automationDetailsId !== undefined &&
(await util.codeQlVersionAbove(this, CODEQL_VERSION_SARIF_GROUP))) {
2021-06-09 13:17:25 -07:00
codeqlArgs.push("--sarif-category", automationDetailsId);
2021-05-03 19:41:53 +02:00
}
2021-06-09 13:17:25 -07:00
codeqlArgs.push(databasePath, ...querySuitePaths);
// capture stdout, which contains analysis summaries
2021-06-09 13:17:25 -07:00
return await runTool(cmd, codeqlArgs);
2020-09-14 10:44:43 +01:00
},
2021-09-28 09:25:35 +01:00
async databasePrintBaseline(databasePath) {
const codeqlArgs = [
"database",
"print-baseline",
...getExtraOptionsFromEnv(["database", "print-baseline"]),
databasePath,
];
return await runTool(cmd, codeqlArgs);
},
/**
* Download specified packs into the package cache. If the specified
* package and version already exists (e.g., from a previous analysis run),
* then it is not downloaded again (unless the extra option `--force` is
* specified).
*
* If no version is specified, then the latest version is
* downloaded. The check to determine what the latest version is is done
* each time this package is requested.
*/
2021-06-03 09:32:44 -07:00
async packDownload(packs) {
2021-06-09 13:17:25 -07:00
const codeqlArgs = [
2021-06-03 09:32:44 -07:00
"pack",
"download",
2021-06-04 13:44:24 -07:00
"--format=json",
2021-06-03 09:32:44 -07:00
...getExtraOptionsFromEnv(["pack", "download"]),
...packs.map(packWithVersionToString),
];
2021-06-09 13:17:25 -07:00
const output = await runTool(cmd, codeqlArgs);
2021-06-03 09:32:44 -07:00
try {
2021-06-07 16:05:32 -07:00
const parsedOutput = JSON.parse(output);
if (Array.isArray(parsedOutput.packs) &&
2021-06-08 10:24:08 -07:00
// TODO PackDownloadOutput will not include the version if it is not specified
// in the input. The version is always the latest version available.
// It should be added to the output, but this requires a CLI change
parsedOutput.packs.every((p) => p.name /* && p.version */)) {
2021-06-07 16:05:32 -07:00
return parsedOutput;
}
else {
throw new Error("Unexpected output from pack download");
}
2021-06-03 09:32:44 -07:00
}
catch (e) {
2021-06-07 16:05:32 -07:00
throw new Error(`Attempted to download specified packs but got an error:\n${output}\n${e}`);
2021-06-03 09:32:44 -07:00
}
2020-09-14 10:44:43 +01:00
},
async databaseCleanup(databasePath, cleanupLevel) {
2021-06-09 13:17:25 -07:00
const codeqlArgs = [
"database",
"cleanup",
databasePath,
`--mode=${cleanupLevel}`,
];
2021-06-09 13:17:25 -07:00
await runTool(cmd, codeqlArgs);
},
2021-06-22 13:05:12 +01:00
async databaseBundle(databasePath, outputFilePath) {
const args = [
"database",
"bundle",
databasePath,
`--output=${outputFilePath}`,
];
await new toolrunner.ToolRunner(cmd, args).exec();
},
2020-06-26 17:22:19 +01:00
};
2021-08-18 10:15:48 +01:00
if (checkVersion &&
!(await util.codeQlVersionAbove(codeql, CODEQL_MINIMUM_VERSION))) {
throw new Error(`Expected a CodeQL CLI with version at least ${CODEQL_MINIMUM_VERSION} but got version ${await codeql.getVersion()}`);
}
return codeql;
2020-06-26 17:22:19 +01:00
}
2021-06-03 09:32:44 -07:00
function packWithVersionToString(pack) {
return pack.version ? `${pack.packName}@${pack.version}` : pack.packName;
}
2020-08-10 09:25:14 +02:00
/**
* Gets the options for `path` of `options` as an array of extra option strings.
*/
2020-11-19 23:03:45 +01:00
function getExtraOptionsFromEnv(paths) {
2020-09-14 10:44:43 +01:00
const options = util.getExtraOptionsEnvParam();
2020-11-19 23:03:45 +01:00
return getExtraOptions(options, paths, []);
}
/**
* Gets `options` as an array of extra option strings.
*
* - throws an exception mentioning `pathInfo` if this conversion is impossible.
*/
function asExtraOptions(options, pathInfo) {
if (options === undefined) {
return [];
}
if (!Array.isArray(options)) {
const msg = `The extra options for '${pathInfo.join(".")}' ('${JSON.stringify(options)}') are not in an array.`;
throw new Error(msg);
}
return options.map((o) => {
const t = typeof o;
if (t !== "string" && t !== "number" && t !== "boolean") {
const msg = `The extra option for '${pathInfo.join(".")}' ('${JSON.stringify(o)}') is not a primitive value.`;
throw new Error(msg);
}
return `${o}`;
});
2020-08-10 09:25:14 +02:00
}
/**
* Gets the options for `path` of `options` as an array of extra option strings.
*
* - the special terminal step name '*' in `options` matches all path steps
* - throws an exception if this conversion is impossible.
*
* Exported for testing.
2020-08-10 09:25:14 +02:00
*/
2020-11-19 23:03:45 +01:00
function getExtraOptions(options, paths, pathInfo) {
2021-07-27 17:59:59 +01:00
const all = asExtraOptions(options === null || options === void 0 ? void 0 : options["*"], pathInfo.concat("*"));
2020-11-19 23:03:45 +01:00
const specific = paths.length === 0
2020-09-14 10:44:43 +01:00
? asExtraOptions(options, pathInfo)
2021-07-27 17:59:59 +01:00
: getExtraOptions(options === null || options === void 0 ? void 0 : options[paths[0]], paths === null || paths === void 0 ? void 0 : paths.slice(1), pathInfo.concat(paths[0]));
2020-08-10 09:25:14 +02:00
return all.concat(specific);
}
exports.getExtraOptions = getExtraOptions;
2021-06-24 17:30:42 +01:00
/*
* A constant defining the maximum number of characters we will keep from
* the programs stderr for logging. This serves two purposes:
* (1) It avoids an OOM if a program fails in a way that results it
* printing many log lines.
* (2) It avoids us hitting the limit of how much data we can send in our
* status reports on GitHub.com.
*/
const maxErrorSize = 20000;
2021-06-09 13:17:25 -07:00
async function runTool(cmd, args = []) {
let output = "";
2021-06-24 17:30:42 +01:00
let error = "";
const exitCode = await new toolrunner.ToolRunner(cmd, args, {
2021-06-09 13:17:25 -07:00
listeners: {
stdout: (data) => {
output += data.toString();
},
2021-06-24 17:30:42 +01:00
stderr: (data) => {
const toRead = Math.min(maxErrorSize - error.length, data.length);
error += data.toString("utf8", 0, toRead);
},
2021-06-09 13:17:25 -07:00
},
2021-06-24 17:30:42 +01:00
ignoreReturnCode: true,
2021-06-09 13:17:25 -07:00
}).exec();
2021-06-24 17:30:42 +01:00
if (exitCode !== 0)
throw new CommandInvocationError(cmd, args, exitCode, error);
2021-06-09 13:17:25 -07:00
return output;
}
2020-06-26 17:22:19 +01:00
//# sourceMappingURL=codeql.js.map