Skip to content

Commit 7e3ab99

Browse files
committed
Rust: Add much more detailed code comments, since these are examples.
1 parent 7b6e06e commit 7e3ab99

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

rust/ql/examples/snippets/empty_if.ql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
import rust
1010

11+
// find 'if' statements...
1112
from IfExpr ifExpr
1213
where
14+
// where the 'then' branch is empty
1315
ifExpr.getThen().(BlockExpr).getStmtList().getNumberOfStmtOrExpr() = 0 and
16+
// and no 'else' branch exists
1417
not exists(ifExpr.getElse())
1518
select ifExpr, "This 'if' expression is redundant."

rust/ql/examples/snippets/simple_constant_password.ql

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @name Constant password
33
* @description Finds places where a string literal is used in a function call
4-
* argument named something like "password".
4+
* argument that looks like a password.
55
* @id rust/examples/simple-constant-password
66
* @tags example
77
*/
@@ -10,8 +10,23 @@ import rust
1010
import codeql.rust.dataflow.DataFlow
1111
import codeql.rust.dataflow.TaintTracking
1212

13+
/**
14+
* A data flow configuration for tracking flow from a string literal to a function
15+
* call argument that looks like a password. For example:
16+
* ```
17+
* fn set_password(password: &str) { ... }
18+
*
19+
* ...
20+
*
21+
* let pwd = "123456"; // source
22+
* set_password(pwd); // sink (argument 0)
23+
* ```
24+
*/
1325
module ConstantPasswordConfig implements DataFlow::ConfigSig {
14-
predicate isSource(DataFlow::Node node) { node.asExpr().getExpr() instanceof StringLiteralExpr }
26+
predicate isSource(DataFlow::Node node) {
27+
// `node` is a string literal
28+
node.asExpr().getExpr() instanceof StringLiteralExpr
29+
}
1530

1631
predicate isSink(DataFlow::Node node) {
1732
// `node` is an argument whose corresponding parameter name matches the pattern "pass%"
@@ -23,8 +38,10 @@ module ConstantPasswordConfig implements DataFlow::ConfigSig {
2338
}
2439
}
2540

41+
// instantiate the data flow configuration as a global taint tracking module
2642
module ConstantPasswordFlow = TaintTracking::Global<ConstantPasswordConfig>;
2743

44+
// report flows from sources to sinks
2845
from DataFlow::Node sourceNode, DataFlow::Node sinkNode
2946
where ConstantPasswordFlow::flow(sourceNode, sinkNode)
3047
select sinkNode, "The value $@ is used as a constant password.", sourceNode, sourceNode.toString()
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/**
22
* @name Database query built from user-controlled sources
33
* @description Finds places where a value from a remote or local user input
4-
* is used as an argument to the `sqlx_core::query::query`
5-
* function.
4+
* is used as the first argument of a call to `sqlx_core::query::query`.
65
* @id rust/examples/simple-sql-injection
76
* @tags example
87
*/
@@ -12,19 +11,29 @@ import codeql.rust.dataflow.DataFlow
1211
import codeql.rust.dataflow.TaintTracking
1312
import codeql.rust.Concepts
1413

14+
/**
15+
* A data flow configuration for tracking flow from a user input (threat model
16+
* source) to the first argument of a call to `sqlx_core::query::query`.
17+
*/
1518
module SqlInjectionConfig implements DataFlow::ConfigSig {
16-
predicate isSource(DataFlow::Node node) { node instanceof ActiveThreatModelSource }
19+
predicate isSource(DataFlow::Node node) {
20+
// `node` is a user input (threat model source)
21+
node instanceof ActiveThreatModelSource
22+
}
1723

1824
predicate isSink(DataFlow::Node node) {
25+
// `node` is the first argument of a call to `sqlx_core::query::query`
1926
exists(CallExpr call |
2027
call.getStaticTarget().getCanonicalPath() = "sqlx_core::query::query" and
2128
call.getArg(0) = node.asExpr().getExpr()
2229
)
2330
}
2431
}
2532

33+
// instantiate the data flow configuration as a global taint tracking module
2634
module SqlInjectionFlow = TaintTracking::Global<SqlInjectionConfig>;
2735

36+
// report flows from sources to sinks
2837
from DataFlow::Node sourceNode, DataFlow::Node sinkNode
2938
where SqlInjectionFlow::flow(sourceNode, sinkNode)
3039
select sinkNode, "This query depends on a $@.", sourceNode, "user-provided value"

0 commit comments

Comments
 (0)