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: content/docs/static-analysis/codeql/10-advanced.md
+66-2Lines changed: 66 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,55 @@ by default.
38
38
If you are using the CodeQL VSCode extension to write and run queries, [it can
39
39
initialize the query pack and create the `qlpack.yml` file automatically](#running-custom-queries-using-the-vscode-extension).
40
40
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
+
41
90
### Adding dependencies
42
91
43
92
To be able to define a custom query we need to import the CodeQL standard
@@ -199,6 +248,17 @@ dependencies:
199
248
of the version you want, you can use `"*"` which always resolves to the latest
200
249
version.)
201
250
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:
@@ -476,8 +536,12 @@ directory should contain the following three files:
476
536
- `MemcpyCall.expected`: A text file containing the expected output from
477
537
running the query against the source file
478
538
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:
481
545
482
546
```sh
483
547
codeql test run -- path/to/test/pack/root/directory
0 commit comments