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
Add barrier and barrier guard documentation for Rust
Add barrierModel and barrierGuardModel sections to the Rust library
models documentation, following the pattern established in PR #21523
for other languages.
Includes:
- New extensible predicate descriptions in the overview
- Example: barrier for SQL injection using escape_sql
- Example: barrier guard for path injection using is_safe_path
- Reference material for both barrierModel and barrierGuardModel
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy file name to clipboardExpand all lines: docs/codeql/codeql-language-guides/customizing-library-models-for-rust.rst
+114Lines changed: 114 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,6 +54,8 @@ The CodeQL library for Rust analysis exposes the following extensible predicates
54
54
- ``sinkModel(path, input, kind, provenance)``. This is used to model sinks where tainted data may be used in a way that makes the code vulnerable.
55
55
- ``summaryModel(path, input, output, kind, provenance)``. This is used to model flow through elements.
56
56
- ``neutralModel(path, kind, provenance)``. This is similar to a summary model but used to indicate that a callable has no flow for a given category. Manual neutral models (those with a provenance such as ``manual``) can be used to override generated summary, source, or sink models (those with a provenance such as ``df-generated``), so that the generated model will be ignored.
57
+
- ``barrierModel(path, output, kind, provenance)``. This is used to model barriers, which are elements that stop the flow of taint.
58
+
- ``barrierGuardModel(path, input, acceptingValue, kind, provenance)``. This is used to model barrier guards, which are elements that can stop the flow of taint depending on a conditional check.
57
59
58
60
The extensible predicates are populated using the models defined in data extension files.
59
61
@@ -319,6 +321,75 @@ Since we are adding a neutral model, we need to add a tuple to the ``neutralMode
319
321
- The second value ``sink`` is the category of model to suppress. This means that any generated sink model for ``Option::map`` will be ignored. The category can be ``source``, ``sink``, or ``summary``.
320
322
- The third value ``manual`` is the provenance of the neutral model.
321
323
324
+
Example: Add a barrier for SQL injection
325
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
326
+
327
+
This example shows how to model a barrier that stops the flow of taint. A barrier model is used to define that the flow of taint stops at the modeled element for the specified kind of query.
328
+
329
+
Consider a hypothetical function ``my_crate::sanitize::escape_sql`` which escapes a SQL string, making it safe to use in a SQL query.
Since we are adding a barrier, we need to add a tuple to the ``barrierModel`` extensible predicate.
351
+
352
+
- The first value ``my_crate::sanitize::escape_sql`` is the canonical path of the function.
353
+
- The second value ``ReturnValue`` is the access path to the output of the barrier, which means that the return value is considered sanitized.
354
+
- The third value ``sql-injection`` is the kind of the barrier. The barrier kind must match the kind used in the query where the barrier should take effect. In this case, it matches the ``sql-injection`` sink kind used by SQL injection queries.
355
+
- The fourth value ``manual`` is the provenance of the barrier.
356
+
357
+
Example: Add a barrier guard
358
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
359
+
360
+
This example shows how to model a barrier guard that stops the flow of taint when a conditional check is performed on data.
361
+
A barrier guard model is used when a function returns a boolean that indicates whether the data is safe to use.
362
+
363
+
Consider a hypothetical function ``my_crate::validate::is_safe_path`` which returns ``true`` when the given path is safe to use in a file system access.
364
+
365
+
.. code-block:: rust
366
+
367
+
fn read_file(user_path: &str) {
368
+
if my_crate::validate::is_safe_path(user_path) { // The check guards the use, so the input is safe.
369
+
let contents = std::fs::read_to_string(user_path).unwrap();
370
+
// ...
371
+
}
372
+
}
373
+
374
+
We need to add a tuple to the ``barrierGuardModel``\(path, input, acceptingValue, kind, provenance) extensible predicate by updating a data extension file.
Since we are adding a barrier guard, we need to add a tuple to the ``barrierGuardModel`` extensible predicate.
386
+
387
+
- The first value ``my_crate::validate::is_safe_path`` is the canonical path of the function.
388
+
- The second value ``Argument[0]`` is the access path to the input whose flow is blocked. In this case, the first argument to the function (``user_path`` in the example).
389
+
- The third value ``true`` is the accepting value of the barrier guard. This is the value that the conditional check must return for the barrier to apply. In this case, when ``is_safe_path`` returns ``true``, the input is considered safe.
390
+
- The fourth value ``path-injection`` is the kind of the barrier guard. The barrier guard kind must match the kind used in the query where the barrier guard should take effect. In this case, it matches the ``path-injection`` sink kind used by tainted path queries.
391
+
- The fifth value ``manual`` is the provenance of the barrier guard.
0 commit comments