You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/codeql-cli.md
+32-14Lines changed: 32 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,37 @@
1
-
###Getting started with the CodeQL CLI
1
+
## Getting started with the CodeQL CLI
2
2
3
3
When you want to generate a CodeQL database locally and run the pre-compiled queries against it, this is the way to go.
4
4
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!
7
6
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)
11
18
12
19
Check to make sure you can use the CodeQL CLI
13
20
14
21
```
15
22
codeql --version
16
23
```
17
24
25
+
## Using the CodeQL CLI
26
+
18
27
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)
19
28
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.
20
29
21
30
Clone this repository and `cd` into it.
22
31
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`
23
35
24
36
### codeql database create
25
37
@@ -30,7 +42,7 @@ You can rely on the autobuild.sh script as well, or you can supply your own buil
30
42
Please review this [list](https://codeql.github.com/docs/codeql-overview/supported-languages-and-frameworks/) of currently supported languages and frameworks.
31
43
32
44
33
-
```
45
+
```bash
34
46
codeql database create db --language=javascript
35
47
```
36
48
@@ -52,18 +64,22 @@ Now that we have a database to work with, let's run some queries against it! We
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`.
58
68
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
61
71
```
62
72
63
73
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.
64
74
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.
65
75
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
67
83
- 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.
68
84
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.
69
85
- 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:
0 commit comments