Skip to content

Commit a785607

Browse files
committed
Update the codeql cli docs
1 parent 50a498d commit a785607

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

exercises/codeql-cli.md

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
1-
### Getting started with the CodeQL CLI
1+
## Getting started with the CodeQL CLI
22

33
When you want to generate a CodeQL database locally and run the pre-compiled queries against it, this is the way to go.
44

5-
First let's download the CodeQL bundle! Head over [here](https://github.com/github/codeql-action/releases ) and download the approprate bundle for your operating system.
6-
Once it's downloaded, untar the content to a CodeQL home folder and you can add CodeQL to your path if you'd like
5+
First let's download the CodeQL bundle!
76

8-
```
9-
export PATH="/Documents/codeql-home/codeql:$PATH"
10-
```
7+
### Pre-requisites
8+
9+
You will need to make sure you have the GitHub CLI installed. For more information on how to install the CLI, check out this installation [doc](https://github.com/cli/cli#installation)
10+
11+
### Install the extension
12+
13+
Using the GitHub CLI, we will install the codeql cli,
14+
15+
1. `gh extensions install github/gh-codeql`
16+
1. `sudo gh codeql install-stub` (this allows you to run `codeql` in your terminal without having to invoke `gh codeql`)
17+
1. `codeql set-version latest` (this will auto download the latest version of the cli)
1118

1219
Check to make sure you can use the CodeQL CLI
1320

1421
```
1522
codeql --version
1623
```
1724

25+
## Using the CodeQL CLI
26+
1827
Now we need to use the CodeQL CLI on an actual repository. Let's start here with our [GHAS training material](https://github.com/ghas-bootcamp/ghas-bootcamp)
1928
There's multiple languages being used here, so for the purposes of this tutorial let's try to scan the Javascript portions of the codebase.
2029

2130
Clone this repository and `cd` into it.
2231

32+
### Install the Javascript Bundle
33+
34+
We will need to download the latest javascript queries to scan the code with. In your terminal, run `codeql pack download codeql/javascript-queries`
2335

2436
### codeql database create
2537

@@ -30,7 +42,7 @@ You can rely on the autobuild.sh script as well, or you can supply your own buil
3042
Please review this [list](https://codeql.github.com/docs/codeql-overview/supported-languages-and-frameworks/) of currently supported languages and frameworks.
3143

3244

33-
```
45+
```bash
3446
codeql database create db --language=javascript
3547
```
3648

@@ -52,18 +64,22 @@ Now that we have a database to work with, let's run some queries against it! We
5264
- `$CODEQL_SUPPORT_LANGUAGE-security-extended.qls`
5365
- `$CODEQL_SUPPORT_LANGUAGE-security-and-quality.qls`
5466

55-
If you have the CodeQL bundle on path, you can reference these query suites by their filenames. If you don't, you can use the full path to the query suite.
56-
As mentioned in the beginning, the queries from the CodeQL bundle are pre-compiled.
57-
If you have a custom query suite, you will see that CodeQL will create a compiled query plan.
67+
By default when we scan with `codeql/javascript-queries` it will default to `javascript-code-scanning.qls`.
5868

59-
```
60-
codeql database analyze db javascript-code-scanning.qls --format=sarif-latest --output=codeql-javascript-results.sarif
69+
```bash
70+
codeql database analyze db --format=sarif-latest --output=codeql-javascript-results.sarif codeql/javascript-queries
6171
```
6272

6373
You will see the queries being evaluated. When this process is done, a SARIF should have been created. The SARIF contains results from the analysis.
6474
If the results array is empty, it means no results were found. If you want to view the SARIF, you can use `jq` to parse through it, or you can use a SARIF Viewer, such as this [one](https://marketplace.visualstudio.com/items?itemName=WDGIS.MicrosoftSarifViewer). Also if you have the `vs-codeql-starter` [workspace](https://github.com/github/vscode-codeql-starter), you can run particular queries against an imported CodeQL database and see the analysis in the IDE.
6575

66-
Here are some advanced things to note:
76+
To scan your code using the other query suites, you just need to append that to the original command
77+
78+
```bash
79+
codeql database analyze db --format=sarif-latest --output=codeql-javascript-results.sarif codeql/javascript-queries:codeql-suites/javascript-security-extended.qls
80+
```
81+
82+
#### Things to note
6783
- When dealing with multiple analyses for the same commit (whether you're analysing multiple languages or have parallelized builds for a monorepo), make sure to use the `--sarif-category` flag to categorize the analyses.
6884
Failure to do so, in particular on a pull request, can cause confusion in that Code Scanning may not be able to detect a baseline analysis to compare the PR results.
6985
- Use this [endpoint](https://docs.github.com/en/rest/reference/code-scanning#list-code-scanning-analyses-for-a-repository) to list the CodeQL analyses of a repository, so that you can inspect the category for each analysis.
@@ -84,7 +100,9 @@ The `--ref` and `--commit` flag combinations can be one of the following:
84100
- ` curl -H "Accept: application/vnd.github.v3+json" \\n -H "Authorization: token $GH_TOKEN" \\n https://api.github.com/repos/<org-name>/<repo-name>/pulls/<pull-request-number> | jq '.merge_commit_sha'`
85101
- The merge commit is a commit created to make sure PR checks are ran; this commit doesn't exist in the actual source tree/`git log`.
86102

87-
```
103+
If you are supplying the `--commit` flag, make sure you use the full commit hash and not the shortened one
104+
105+
```bash
88106
codeql github upload-results --repository=$GITHUB_REPOSITORY --ref=$GITHUB_REF --commit=$GITHUB_SHA --sarif=codeql-javascript-results.sarif --github-auth-stdin=<YOUR TOKEN>
89107
```
90108

0 commit comments

Comments
 (0)