Skip to content

Commit 7e7bf6e

Browse files
felickzCopilot
andcommitted
docs: add guide for adding a CodeQL data extension
Add a new "Adding a data extension" section to CONTRIBUTING.md covering how to build a data-extension (MaD) pack: an explicit rule of thumb for choosing between `ext` and `ext-library-sources` (sink/summary models vs. remote-source models, and which languages have each), the MaD row shape, the `manual/` vs `generated/` convention, the `extensionTargets` version-pinning gotcha that causes silent zero-result failures on a mismatched local CLI, and how data extensions get picked up automatically by `codeql test run`. Also tightens the pack-directory bullet in "Shipping a change to a query/library pack" to correctly scope `ext-library-sources` to csharp/java only (previously bundled with `ext`'s wider csharp/go/java/ python list), and links it to the new section. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f02ef23b-e35d-4536-bd58-f2fbc2976ba9
1 parent c4de11a commit 7e7bf6e

1 file changed

Lines changed: 93 additions & 2 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,95 @@ Queries and libraries may not be actively maintained as the supported libraries
6060

6161
After the query is merged, we welcome pull requests to improve it.
6262

63+
## Adding a data extension
64+
65+
Sometimes a false negative or false positive isn't a gap in a query's own logic, but a third-party
66+
library API that the standard `codeql/<language>-all` libraries simply don't know about — a
67+
framework method that's really a SQL/command/path-injection sink, or a getter that hands back
68+
untrusted remote data. For those, add a **data extension** (a "MaD" — Model-as-Data — model)
69+
instead of, or alongside, a query change.
70+
71+
1. **Pick the right pack: `ext` vs `ext-library-sources`**
72+
73+
Every language with data-extension support has a `<language>/ext` pack (`csharp`/`go`/`java`/
74+
`python`). Two of those languages additionally have a second, narrower pack,
75+
`<language>/ext-library-sources` (`csharp`/`java` only). They hold different *kinds* of models,
76+
and picking the right one matters:
77+
78+
- **`<language>/ext`** (published as `githubsecuritylab/codeql-<language>-extensions`) is what
79+
you want for almost every contribution: **sink models** (this library method is a dangerous
80+
operation), **summary models** (taint flows from one argument/return value to another through
81+
a library method), and, where appropriate, hand-curated **source models** for a specific,
82+
well-known framework API (e.g. a web framework's `getParameter()`-style methods). This is
83+
where our own [Spring R2DBC `DatabaseClient` SQL injection sink models][pr-204] live
84+
(`java/ext/manual/org.springframework.r2dbc.model.yml`) — use it as a concrete reference for
85+
both the file layout and the row shape.
86+
- **`<language>/ext-library-sources`** (published as
87+
`githubsecuritylab/codeql-<language>-library-sources`) exists only for `csharp`/`java` and
88+
holds a narrower category: `sourceModel` rows (almost always `kind: "remote"`) that flag a
89+
third-party library API as a place where untrusted/attacker-controlled data enters an
90+
application. Because a `RemoteFlowSource` feeds *every* standard security query, not just one
91+
vulnerability class, these models have a much larger blast radius than a typical sink model,
92+
so they're kept in a separate, more conservatively-reviewed pack. Most of the content here is
93+
bulk-generated from real-world library usage (see
94+
`<language>/src/library_sources/ExternalAPIsUsedWithUntrustedData.ql`) rather than
95+
hand-written — `manual/` in this pack is essentially unused today.
96+
97+
**Rule of thumb: if you're modeling a dangerous sink, or how taint flows through a library
98+
method, use `ext`. Only reach for `ext-library-sources` if you're contributing a genuinely new
99+
remote-data-entry-point (source) model, and only for csharp/java.**
100+
101+
2. **Row shape**
102+
103+
Each data extension is a `.yml` file with an `extensions:` list. Every entry has an
104+
`addsTo.pack` (always `codeql/<language>-all`), an `addsTo.extensible`
105+
(`sinkModel`/`sourceModel`/`summaryModel`/`neutralModel`), and a `data:` list of rows. See the
106+
[CodeQL model pack
107+
documentation](https://docs.github.com/en/code-security/tutorials/customize-code-scanning/create-and-work-with-codeql-packs#creating-a-codeql-model-pack)
108+
for the full column reference (`namespace, type, subtypes, name, signature, ext, input/output,
109+
kind, provenance`). `java/ext/manual/org.springframework.r2dbc.model.yml` shows a real
110+
`sinkModel`/`summaryModel` pair side by side, with comments explaining *why* each row is shaped
111+
the way it is — comment your own rows the same way whenever the mapping from source code to a
112+
MaD row isn't obvious at a glance.
113+
114+
3. **`manual/` vs `generated/`**
115+
116+
Both `ext` and `ext-library-sources` load `manual/*.yml` and `generated/*.yml` (see each pack's
117+
`qlpack.yml`, `dataExtensions:`). By convention, `manual/` is for hand-authored, human-reviewed
118+
models — this is where a new library's sink/summary models should go — while `generated/` is
119+
bulk output from an automated model-generation tool/query and isn't meant to be hand-edited.
120+
121+
4. **The `extensionTargets` version gotcha**
122+
123+
Your pack's `qlpack.yml` pins `extensionTargets: codeql/<language>-all: '<version>'` (see
124+
`<language>/ext/qlpack.yml`). This must be satisfied by the `codeql/<language>-all` version
125+
bundled with whatever CodeQL CLI is running the analysis — **if it isn't, your new models
126+
silently attach zero results, with no error**, instead of failing loudly. This bit us during
127+
development of the Spring R2DBC models above: a local CLI older than this repo's pinned
128+
[`.codeqlversion`][codeqlversion] bundled a `codeql/java-all` below the pinned
129+
`extensionTargets` range, so `codeql test run` produced 0 results locally for a genuinely-working
130+
model — while CI (on the correctly pinned CLI) passed. If a local test run for a new data
131+
extension doesn't find what you expect, check `codeql version` against
132+
[`.codeqlversion`][codeqlversion] before assuming your model is wrong.
133+
134+
Also see the [note below](#ext-packs-no-install) on why these packs are never `codeql pack
135+
install`ed/`upgrade`d.
136+
137+
5. **Testing**
138+
139+
Add a test under `<language>/test/security/<CWE-id>/<your-library>/`: minimal source stubs that
140+
reproduce the vulnerable call shape, a `.ql` query that imports the *real* standard query (e.g.
141+
`SqlInjectionQuery`/`QueryInjectionFlow`, not a reimplementation of it), and a `.expected` file.
142+
`<language>/test/qlpack.yml` already depends on `githubsecuritylab/codeql-<language>-extensions`
143+
(and `-library-sources` where applicable) — that dependency is what makes `codeql test run`'s
144+
normal dependency resolution load your new data extensions automatically, unlike ad-hoc `codeql
145+
database analyze`/`query run`, which need an explicit `--model-packs` flag. Confirm the test
146+
actually exercises your model (not just that it compiles) by checking the CI job log for a
147+
`PASSED` line naming your `.ql` file.
148+
149+
Once merged, publish your change the same way as any other pack — see [Shipping a change to a
150+
query/library pack](#shipping-a-change-to-a-querylibrary-pack) below.
151+
63152
## Supported CodeQL versions
64153

65154
Every query pack in this repository is compiled and tested against a specific, pinned version of the upstream CodeQL standard libraries (e.g. `codeql/java-all`). These queries are **only guaranteed to compile** against those exact library versions (see the latest release for the current versions): newer or older CodeQL CLI/library versions may rename or remove APIs the queries depend on (see [#151](https://github.com/GitHubSecurityLab/CodeQL-Community-Packs/pull/151) for an example, and [#145](https://github.com/GitHubSecurityLab/CodeQL-Community-Packs/issues/145) for the ongoing effort to refresh these pins).
@@ -197,8 +286,9 @@ merge. Bumping your pack's own `version:` in a regular PR just *stages* the chan
197286
To ship a change:
198287

199288
- [ ] Make your change in the pack directory you intend to publish: `<language>/src` (queries),
200-
`<language>/lib` (library), or `<language>/ext`/`<language>/ext-library-sources` (extensions,
201-
`csharp`/`go`/`java`/`python` only).
289+
`<language>/lib` (library), `<language>/ext` (extensions, `csharp`/`go`/`java`/`python` only),
290+
or `<language>/ext-library-sources` (extensions, `csharp`/`java` only). See [Adding a data
291+
extension](#adding-a-data-extension) above if you're not sure which extensions pack to use.
202292
- [ ] Bump `version:` in that pack's `qlpack.yml`, following [semver](https://semver.org/). Only bump
203293
the specific pack(s) you changed; other languages/pack types are unaffected and don't need
204294
touching.
@@ -463,3 +553,4 @@ Please do get in touch (privacy@github.com) if you have any questions about this
463553
[pr-155]: https://github.com/GitHubSecurityLab/CodeQL-Community-Packs/pull/155
464554
[pr-158]: https://github.com/GitHubSecurityLab/CodeQL-Community-Packs/pull/158
465555
[pr-159]: https://github.com/GitHubSecurityLab/CodeQL-Community-Packs/pull/159
556+
[pr-204]: https://github.com/GitHubSecurityLab/CodeQL-Community-Packs/pull/204

0 commit comments

Comments
 (0)