This repository contains Jenkins-specific CodeQL queries.
You can use the Jenkins CodeQL queries as part of the regular CodeQL code scanning workflow. This is the more flexible approach in terms of your ability to configure the build, and additionally only requires one workflow to be set up to use the generic code scanning rules provided by GitHub in addition to the Jenkins-specific rules. Please note the findings will be reported part of the "CodeQL" code scanning tool on the GitHub UI.
Additionally, code-level suppressions documented as part of finding descriptions do not work by default.
See advanced-security/dismiss-alerts for a GitHub-provided way to support code-level suppression.
The instructions below do not add suppression support, see advanced-security/dismiss-alerts for the necessary configuration changes.
These instructions assume use of the standard CodeQL workflow template as of 42326d0
Update your use of github/codeql-action/init@v3 to specify a with.config (related GitHub documentation).
with:
config: |
packs:
- jenkins-infra/jenkins-codeql-
Install the CodeQL CLI.
-
Run
codeql pack install test/to install the dependencies. -
Run the desired
codeqlcommands.
Generate or download a CodeQL database for the code base you want to run the queries against.
Then, run:
codeql database codeql database analyze --format=sarifv2.1.0 --output=result.sarif <path to database> src/
This will generate the result.sarif file containing the query results.
The following shell script creates a database by running the specified build command, analyzes the database with the specified queries only, and then uploads the results, excluding any suppressed findings, to GitHub.
#!/usr/bin/env bash
set -e errexit
set -e nounset
set -e pipefail
[[ -f pom.xml ]] || { echo "This script must be run from a Maven project directory" ; exit 1 ; }
[[ -v "GITHUB_TOKEN" ]] || { echo "GITHUB_TOKEN is undefined. " ; exit 1 ; } # (1)
[[ -v "GH_REPO" ]] || { echo "GH_REPO is undefined" ; exit 1 ; } # (2)
[[ -v "GH_REF" ]] || { echo "GH_REF is undefined." ; exit 1 ; } # (3)
[[ -v "GH_SHA" ]] || { echo "GH_SHA is undefined." ; exit 1 ; } # (4)
for TOOL in codeql jq mvn ; do
which "$TOOL" >/dev/null || { echo "$TOOL not found on PATH" ; exit 1 ; }
done
TEMPDIR="$( mktemp -d -t jenkins-codeql.XXXX )"
codeql database create "$TEMPDIR"/codeql-java-database \
--language=java \
--command='mvn clean verify -Pquick-build' # (5)
codeql database analyze "$TEMPDIR"/codeql-java-database \
--sarif-add-query-help \
--format=sarif-latest \
--output="$TEMPDIR"/result.sarif \
--download \
jenkins-infra/jenkins-codeql \ (6)
codeql/java-queries:AlertSuppression.ql \
codeql/java-queries:AlertSuppressionAnnotations.ql \
|| { echo "Failed to analyze database" ; exit 1 ; }
jq 'del(.runs[].results[] | select( .suppressions | length != 0 ))' \
"$TEMPDIR"/result.sarif > "$TEMPDIR"/result-filtered.sarif # (7)
echo codeql github upload-results \
--repository="$GH_REPO" \
--ref="$GH_REF" --commit="$GH_SHA" \
--sarif="$TEMPDIR"/result-filtered.sarif
# Optionally:
# rm -rf "$TEMPDIR"-
GITHUB_TOKENis used bycodeql github upload-results. Alternatively, a token can be passed into standard input with--github-auth-stdinargument. -
GH_REPOmust be in the formatowner/repo(e.g.,jenkinsci/matrix-auth-plugin). -
GH_REFmust be in the formatrefs/heads/branchname(e.g.,refs/heads/develop) when analyzing a branch, orrefs/pull/1234/head(when analyzing a pull request’s HEAD commit). -
GH_SHAis the SHA-1 of the analyzed commit. -
Optionally,
--commandspecifies how the Jenkins component is built. This is useful if a custom build command should be used. -
Replace
jenkins-infra/jenkins-codeqlwith/path/to/clone-of-this-repo/src/to use locally modified sources. -
Optionally, this invocation of
jqremoves all results from the report that have suppressions applied (through comments or annotations).
codeql pack install test/ codeql test run test/
The file run-tests.sh in this repository is a self-contained script that installs CodeQL, pack dependencies, and then runs the tests.
Since it downloads and extracts CodeQL CLI binaries, its use is not recommended for local development.
To update to a newer CodeQL release:
-
Determine which release to update to. See the list of CodeQL releases and the corresponding releases of
java-all. -
Edit all
qlpack.ymlfiles in this repository and increase the version ofcodeql/java-allto the corresponding version ingithub/codeql(java/ql/src/qlpack.ymlat the tagged top-level version in tags). -
Run
codeql pack upgrade <dir>on each of the directories containing aqlpack.ymlfile. -
Edit
run-tests.shto download the correct CodeQL release and run it to confirm everything works as expected.
|
Note
|
https://github.com/jenkins-infra/jenkins-security-scan needs a corresponding change. |
To release this as QL packs here:
-
Update the versions from
x.y.z-devtox.y.zinqlpack.ymlfiles andgit committhis (example). -
Define the environment variable
GITHUB_TOKENor prepare to pass the argument--github-auth-stdinto the next command. Either way, you need a token withwrite:packagespermission. -
Run
codeql pack publish --groups=-testto upload everything but the tests as packs. -
Update the versions from
x.y.ztox.y.(z+1)-devinqlpack.ymlfiles andgit committhis (example).