Skip to content

Commit 9e6d428

Browse files
authored
Merge pull request #22753 from github/nickrolfe/docs-json-data-extensions
Docs: update "Customizing library models for <lang>" to cover JSON data extensions
2 parents 2a29eaf + a6d96fb commit 9e6d428

9 files changed

Lines changed: 1700 additions & 948 deletions

‎docs/codeql/codeql-language-guides/customizing-library-models-for-actions.rst‎

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,29 @@ Customizing library models for GitHub Actions
77

88
GitHub Actions analysis can be customized by adding library models in data extension files.
99

10-
A data extension for GitHub Actions is a YAML file of the form:
10+
A data extension for GitHub Actions can be written using either JSON or YAML. JSON is the preferred format, for performance reasons, and takes the following form:
11+
12+
.. code-block:: json
13+
14+
{
15+
"extensions": [
16+
{
17+
"addsTo": {
18+
"pack": "codeql/actions-all",
19+
"extensible": "<name of extensible predicate>"
20+
},
21+
"data": [
22+
["<value for row 1, column 1>", "<value for row 1, column 2>", "..."],
23+
["<value for row 2, column 1>", "<value for row 2, column 2>", "..."]
24+
// ...
25+
]
26+
}
27+
]
28+
}
29+
30+
Files in the JSON format must use the ``.json`` file extension. Single-line (``//``) and multi-line (``/* ... */``) comments are supported as a non-standard JSON extension.
31+
32+
A YAML file has the following form:
1133

1234
.. code-block:: yaml
1335
@@ -16,8 +38,8 @@ A data extension for GitHub Actions is a YAML file of the form:
1638
pack: codeql/actions-all
1739
extensible: <name of extensible predicate>
1840
data:
19-
- <tuple1>
20-
- <tuple2>
41+
- ["<value for row 1, column 1>", "<value for row 1, column 2>", "..."]
42+
- ["<value for row 2, column 1>", "<value for row 2, column 2>", "..."]
2143
- ...
2244
2345
The CodeQL library for GitHub Actions exposes the following extensible predicates:
@@ -57,16 +79,23 @@ If there is an Action publisher that you trust, you can include the owner name/o
5779

5880
To allow any Action from the publisher ``octodemo``, such as ``octodemo/3rd-party-action``, follow these steps:
5981

60-
1. Create a data extension file ``/models/trusted-owner.model.yml`` with the following content:
82+
1. Create a data extension file ``/models/trusted-owner.model.json`` with the following content:
6183

62-
.. code-block:: yaml
84+
.. code-block:: json
6385
64-
extensions:
65-
- addsTo:
66-
pack: codeql/actions-all
67-
extensible: trustedActionsOwnerDataModel
68-
data:
69-
- ["octodemo"]
86+
{
87+
"extensions": [
88+
{
89+
"addsTo": {
90+
"pack": "codeql/actions-all",
91+
"extensible": "trustedActionsOwnerDataModel"
92+
},
93+
"data": [
94+
["octodemo"]
95+
]
96+
}
97+
]
98+
}
7099
71100
2. Create a model pack file ``/codeql-pack.yml`` with the following content:
72101

@@ -78,7 +107,7 @@ To allow any Action from the publisher ``octodemo``, such as ``octodemo/3rd-part
78107
extensionTargets:
79108
codeql/actions-all: '*'
80109
dataExtensions:
81-
- models/**/*.yml
110+
- models/**/*.json
82111
83112
3. Ensure that the model pack is included in your CodeQL analysis.
84113

@@ -91,14 +120,21 @@ GitHub's own organizations (``actions``, ``github`` and ``advanced-security``) a
91120

92121
To distrust the first-party ``github`` owner, add a data extension file with the following content:
93122

94-
.. code-block:: yaml
95-
96-
extensions:
97-
- addsTo:
98-
pack: codeql/actions-all
99-
extensible: trustedActionsOwnerDataModel
100-
data:
101-
- ["!github"]
123+
.. code-block:: json
124+
125+
{
126+
"extensions": [
127+
{
128+
"addsTo": {
129+
"pack": "codeql/actions-all",
130+
"extensible": "trustedActionsOwnerDataModel"
131+
},
132+
"data": [
133+
["!github"]
134+
]
135+
}
136+
]
137+
}
102138
103139
With this in place, the query will once again report unpinned tags for Actions published by ``github``.
104140

‎docs/codeql/codeql-language-guides/customizing-library-models-for-cpp.rst‎

Lines changed: 106 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,29 @@ Syntax used to define an element in an extension file
2525
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2626

2727
Each model of an element is defined using a data extension where each tuple constitutes a model.
28-
A data extension file to extend the standard CPP queries included with CodeQL is a YAML file with the form:
28+
A data extension file to extend the standard CPP queries included with CodeQL can be written using either JSON or YAML. JSON is the preferred format, for performance reasons, and takes the following form:
29+
30+
.. code-block:: json
31+
32+
{
33+
"extensions": [
34+
{
35+
"addsTo": {
36+
"pack": "codeql/cpp-all",
37+
"extensible": "<name of extensible predicate>"
38+
},
39+
"data": [
40+
["<value for row 1, column 1>", "<value for row 1, column 2>", "..."],
41+
["<value for row 2, column 1>", "<value for row 2, column 2>", "..."]
42+
// ...
43+
]
44+
}
45+
]
46+
}
47+
48+
Files in the JSON format must use the ``.json`` file extension. Single-line (``//``) and multi-line (``/* ... */``) comments are supported as a non-standard JSON extension.
49+
50+
A YAML file has the following form:
2951

3052
.. code-block:: yaml
3153
@@ -34,11 +56,11 @@ A data extension file to extend the standard CPP queries included with CodeQL is
3456
pack: codeql/cpp-all
3557
extensible: <name of extensible predicate>
3658
data:
37-
- <tuple1>
38-
- <tuple2>
59+
- ["<value for row 1, column 1>", "<value for row 1, column 2>", "..."]
60+
- ["<value for row 2, column 1>", "<value for row 2, column 2>", "..."]
3961
- ...
4062
41-
Each YAML file may contain one or more top-level extensions.
63+
Each data extension file may contain one or more top-level extensions.
4264

4365
- ``addsTo`` defines the CodeQL pack name and extensible predicate that the extension is injected into.
4466
- ``data`` defines one or more rows of tuples that are injected as values into the extensible predicate. The number of columns and their types must match the definition of the extensible predicate.
@@ -79,20 +101,27 @@ This example shows how the CPP query pack models the return value from the ``rea
79101
80102
We need to add a tuple to the ``sourceModel(namespace, type, subtypes, name, signature, ext, output, kind, provenance)`` extensible predicate by updating a data extension file.
81103

82-
.. code-block:: yaml
83-
84-
extensions:
85-
- addsTo:
86-
pack: codeql/cpp-all
87-
extensible: sourceModel
88-
data:
89-
- ["boost::asio", "", False, "read_until", "", "", "Argument[*1]", "remote", "manual"]
104+
.. code-block:: json
105+
106+
{
107+
"extensions": [
108+
{
109+
"addsTo": {
110+
"pack": "codeql/cpp-all",
111+
"extensible": "sourceModel"
112+
},
113+
"data": [
114+
["boost::asio", "", false, "read_until", "", "", "Argument[*1]", "remote", "manual"]
115+
]
116+
}
117+
]
118+
}
90119
91120
The first five values identify the callable (in this case a free function) to be modeled as a source.
92121

93122
- The first value ``"boost::asio"`` is the namespace name.
94123
- The second value ``""`` is the name of the type (class) that contains the method. Because we're modeling a free function, the type is left blank.
95-
- The third value ``False`` is a flag that indicates whether or not the model also applies to all overrides of the method. For a free function, this should be ``False``.
124+
- The third value ``false`` is a flag that indicates whether or not the model also applies to all overrides of the method. For a free function, this should be ``false``.
96125
- The fourth value ``"read_until"`` is the function name.
97126
- The fifth value is the function input type signature, which can be used to narrow down between functions that have the same name. In this case, we want the model to include all functions in ``boost::asio`` called ``read_until``.
98127

@@ -114,20 +143,27 @@ This example shows how the CPP query pack models the second argument of the ``bo
114143
115144
We need to add a tuple to the ``sinkModel(namespace, type, subtypes, name, signature, ext, input, kind, provenance)`` extensible predicate by updating a data extension file.
116145

117-
.. code-block:: yaml
118-
119-
extensions:
120-
- addsTo:
121-
pack: codeql/cpp-all
122-
extensible: sinkModel
123-
data:
124-
- ["boost::asio", "", False, "write", "", "", "Argument[*1]", "remote-sink", "manual"]
146+
.. code-block:: json
147+
148+
{
149+
"extensions": [
150+
{
151+
"addsTo": {
152+
"pack": "codeql/cpp-all",
153+
"extensible": "sinkModel"
154+
},
155+
"data": [
156+
["boost::asio", "", false, "write", "", "", "Argument[*1]", "remote-sink", "manual"]
157+
]
158+
}
159+
]
160+
}
125161
126162
The first five values identify the callable (in this case a free function) to be modeled as a sink.
127163

128164
- The first value ``"boost::asio"`` is the namespace name.
129165
- The second value ``""`` is the name of the type (class) that contains the method. Because we're modeling a free function, the type is left blank.
130-
- The third value ``False`` is a flag that indicates whether or not the model also applies to all overrides of the method. For a free function, this should be ``False``.
166+
- The third value ``false`` is a flag that indicates whether or not the model also applies to all overrides of the method. For a free function, this should be ``false``.
131167
- The fourth value ``"write"`` is the function name.
132168
- The fifth value is the function input type signature, which can be used to narrow down between functions that have the same name. In this case, we want the model to include all functions in ``boost::asio`` called ``write``.
133169

@@ -149,20 +185,27 @@ This example shows how the CPP query pack models flow through a function for a s
149185
150186
We need to add tuples to the ``summaryModel(namespace, type, subtypes, name, signature, ext, input, output, kind, provenance)`` extensible predicate by updating a data extension file:
151187

152-
.. code-block:: yaml
153-
154-
extensions:
155-
- addsTo:
156-
pack: codeql/cpp-all
157-
extensible: summaryModel
158-
data:
159-
- ["boost::asio", "", False, "buffer", "", "", "Argument[*0]", "ReturnValue", "taint", "manual"]
188+
.. code-block:: json
189+
190+
{
191+
"extensions": [
192+
{
193+
"addsTo": {
194+
"pack": "codeql/cpp-all",
195+
"extensible": "summaryModel"
196+
},
197+
"data": [
198+
["boost::asio", "", false, "buffer", "", "", "Argument[*0]", "ReturnValue", "taint", "manual"]
199+
]
200+
}
201+
]
202+
}
160203
161204
The first five values identify the callable (in this case free function) to be modeled as a summary.
162205

163206
- The first value ``"boost::asio"`` is the namespace name.
164207
- The second value ``""`` is the name of the type (class) that contains the method. Because we're modeling a free function, the type is left blank.
165-
- The third value ``False`` is a flag that indicates whether or not the model also applies to all overrides of the method. For a free function, this should be ``False``.
208+
- The third value ``false`` is a flag that indicates whether or not the model also applies to all overrides of the method. For a free function, this should be ``false``.
166209
- The fourth value ``"buffer"`` is the function name.
167210
- The fifth value is the function input type signature, which can be used to narrow down between functions that have the same name. In this case, we want the model to include all functions in ``boost::asio`` called ``buffer``.
168211

@@ -190,20 +233,27 @@ This function escapes special characters in a string for use in an SQL statement
190233
191234
We need to add a tuple to the ``barrierModel(namespace, type, subtypes, name, signature, ext, output, kind, provenance)`` extensible predicate by updating a data extension file.
192235

193-
.. code-block:: yaml
194-
195-
extensions:
196-
- addsTo:
197-
pack: codeql/cpp-all
198-
extensible: barrierModel
199-
data:
200-
- ["", "", False, "mysql_real_escape_string", "", "", "Argument[*1]", "sql-injection", "manual"]
236+
.. code-block:: json
237+
238+
{
239+
"extensions": [
240+
{
241+
"addsTo": {
242+
"pack": "codeql/cpp-all",
243+
"extensible": "barrierModel"
244+
},
245+
"data": [
246+
["", "", false, "mysql_real_escape_string", "", "", "Argument[*1]", "sql-injection", "manual"]
247+
]
248+
}
249+
]
250+
}
201251
202252
The first five values identify the callable (in this case a free function) to be modeled as a barrier.
203253

204254
- The first value ``""`` is the namespace name.
205255
- The second value ``""`` is the name of the type (class) that contains the method. Because we're modeling a free function, the type is left blank.
206-
- The third value ``False`` is a flag that indicates whether or not the model also applies to all overrides of the method. For a free function, this should be ``False``.
256+
- The third value ``false`` is a flag that indicates whether or not the model also applies to all overrides of the method. For a free function, this should be ``false``.
207257
- The fourth value ``"mysql_real_escape_string"`` is the function name.
208258
- The fifth value is the function input type signature, which can be used to narrow down between functions that have the same name.
209259

@@ -229,20 +279,27 @@ Consider a function called ``is_safe`` which returns ``true`` when the data is c
229279
230280
We need to add a tuple to the ``barrierGuardModel(namespace, type, subtypes, name, signature, ext, input, acceptingValue, kind, provenance)`` extensible predicate by updating a data extension file.
231281

232-
.. code-block:: yaml
233-
234-
extensions:
235-
- addsTo:
236-
pack: codeql/cpp-all
237-
extensible: barrierGuardModel
238-
data:
239-
- ["", "", False, "is_safe", "", "", "Argument[*0]", "true", "sql-injection", "manual"]
282+
.. code-block:: json
283+
284+
{
285+
"extensions": [
286+
{
287+
"addsTo": {
288+
"pack": "codeql/cpp-all",
289+
"extensible": "barrierGuardModel"
290+
},
291+
"data": [
292+
["", "", false, "is_safe", "", "", "Argument[*0]", "true", "sql-injection", "manual"]
293+
]
294+
}
295+
]
296+
}
240297
241298
The first five values identify the callable (in this case a free function) to be modeled as a barrier guard.
242299

243300
- The first value ``""`` is the namespace name.
244301
- The second value ``""`` is the name of the type (class) that contains the method. Because we're modeling a free function, the type is left blank.
245-
- The third value ``False`` is a flag that indicates whether or not the model guard also applies to all overrides of the method. For a free function, this should be ``False``.
302+
- The third value ``false`` is a flag that indicates whether or not the model guard also applies to all overrides of the method. For a free function, this should be ``false``.
246303
- The fourth value ``"is_safe"`` is the function name.
247304
- The fifth value is the function input type signature, which can be used to narrow down between functions that have the same name.
248305

0 commit comments

Comments
 (0)