Skip to content

Commit 3e636e7

Browse files
authored
Merge pull request #75 from trailofbits/codeql-install-updates
Codeql install updates
2 parents adf67fd + 4a00784 commit 3e636e7

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

content/docs/static-analysis/codeql/10-advanced.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,55 @@ by default.
3838
If you are using the CodeQL VSCode extension to write and run queries, [it can
3939
initialize the query pack and create the `qlpack.yml` file automatically](#running-custom-queries-using-the-vscode-extension).
4040

41+
Finally, you have to create a [workspace file](https://docs.github.com/en/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/about-codeql-workspaces) for the CodeQL CLI to work correctly.
42+
43+
If you know you will write more than one query pack, we recommend creating the following directory structure to make development easier:
44+
45+
```plain
46+
.
47+
├── CODEOWNERS
48+
├── README.md
49+
├── codeql-workspace.yml
50+
├── cpp
51+
│ ├── lib
52+
│ │ ├── qlpack.yml
53+
│ │ └── scope
54+
│ │ └── security
55+
│ │ └── someLibrary.qll
56+
│ ├── src
57+
│ │ ├── qlpack.yml
58+
│ │ ├── suites
59+
│ │ │ ├── scope-cpp-code-scanning.qls
60+
│ │ │ └── scope-cpp-security.qls
61+
│ │ ├── security
62+
│ │ │ └── AppSecAnalysis
63+
│ │ │ ├── AppSecAnalysis.c
64+
│ │ │ ├── AppSecAnalysis.qhelp
65+
│ │ │ └── AppSecAnalysis.ql
66+
│ └── test
67+
│ ├── qlpack.yml
68+
│ ├── include
69+
│ │ ├── libc
70+
│ │ │ ├── stubs.h
71+
│ ├── library-tests
72+
│ │ └── security
73+
│ │ ├── someLibrary
74+
│ │ │ ├── someLibrary.c
75+
│ │ │ ├── someLibrary.expected
76+
│ │ │ └── someLibrary.ql
77+
│ └── query-tests
78+
│ └── security
79+
│ └── AppSecAnalysis
80+
│ ├── AppSecAnalysis.c
81+
│ ├── AppSecAnalysis.expected
82+
│ └── AppSecAnalysis.qlref
83+
...
84+
```
85+
86+
We divide query packs per-language, but also per-type (security, cryptographic, etc.). This also follows the convention used by the GitHub query suites.
87+
88+
For recommendations on how to set up query unit tests, see the [Unit testing custom queries](#unit-testing-custom-queries) section.
89+
4190
### Adding dependencies
4291

4392
To be able to define a custom query we need to import the CodeQL standard
@@ -199,6 +248,17 @@ dependencies:
199248
of the version you want, you can use `"*"` which always resolves to the latest
200249
version.)
201250

251+
### Installing the new packs
252+
253+
Once you have initialized the new query pack, added dependencies and some sample queries, you need to run
254+
`codeql pack install` in every directory that has a qlpack.yml file (including folders with test).
255+
256+
Then, inform the codeql CLI about your new queries by creating `~/.config/codeql/config` file with the following content:
257+
258+
```plain
259+
--search-path /full/path/to/your/codeql/root/directory
260+
```
261+
202262
## Writing custom queries
203263

204264
{{< hint info >}}
@@ -476,8 +536,12 @@ directory should contain the following three files:
476536
- `MemcpyCall.expected`: A text file containing the expected output from
477537
running the query against the source file
478538

479-
The source file must build cleanly without any external dependencies. To test
480-
the query, run the following command:
539+
The source file must build cleanly without any external dependencies.
540+
This requirement is problematic mostly for C/C++ queries: you need to create
541+
stub files with `extern` declarations for libraries you want to `#include`.
542+
Check out [our tests](https://github.com/trailofbits/codeql-queries/blob/d994c7ca05dab30fe195555ef6943f9d51ec38df/cpp/test/query-tests/security/CStrnFinder/test.c#L1) for examples.
543+
544+
To test the query, run the following command:
481545

482546
```sh
483547
codeql test run -- path/to/test/pack/root/directory

0 commit comments

Comments
 (0)