Skip to content

Commit 7e75c1d

Browse files
committed
Rust: Add very basic query prototype.
1 parent 513ae2a commit 7e75c1d

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @name 'Secure' attribute is not set to true
3+
* @description Omitting the 'Secure' attribute allows data to be transmitted insecurely
4+
* using HTTP. Always set 'Secure' to 'true' to ensure that HTTPS
5+
* is used at all times.
6+
* @kind problem
7+
* @problem.severity error
8+
* @precision high
9+
* @id rust/insecure-cookie
10+
* @tags security
11+
* external/cwe/cwe-319
12+
* external/cwe/cwe-614
13+
*/
14+
15+
import rust
16+
import codeql.rust.dataflow.DataFlow
17+
import codeql.rust.dataflow.TaintTracking
18+
import InsecureCookieFlow::PathGraph
19+
20+
/**
21+
* A data flow configuration for tracking values representing cookies without the
22+
* 'secure' flag set.
23+
*/
24+
module InsecureCookieConfig implements DataFlow::ConfigSig {
25+
predicate isSource(DataFlow::Node node) {
26+
// creation of a cookie with default settings (insecure)
27+
exists(CallExprBase ce |
28+
ce.getStaticTarget().getCanonicalPath() = "<cookie::Cookie>::build" and
29+
node.asExpr().getExpr() = ce
30+
)
31+
}
32+
33+
predicate isSink(DataFlow::Node node) {
34+
// qualifier of a call to `.build`.
35+
exists(MethodCallExpr ce |
36+
ce.getStaticTarget().getCanonicalPath() = "<cookie::builder::CookieBuilder>::build" and
37+
node.asExpr().getExpr() = ce.getReceiver()
38+
)
39+
}
40+
41+
predicate observeDiffInformedIncrementalMode() { any() }
42+
}
43+
44+
module InsecureCookieFlow = TaintTracking::Global<InsecureCookieConfig>;
45+
46+
from InsecureCookieFlow::PathNode sourceNode, InsecureCookieFlow::PathNode sinkNode
47+
where InsecureCookieFlow::flowPath(sourceNode, sinkNode)
48+
select sinkNode.getNode(), sourceNode, sinkNode, "Cookie attribute 'Secure' is not set to true."
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#select
2+
| main.rs:16:19:16:50 | ...::build(...) | main.rs:16:19:16:50 | ...::build(...) | main.rs:16:19:16:50 | ...::build(...) | Cookie attribute 'Secure' is not set to true. |
3+
edges
4+
nodes
5+
| main.rs:16:19:16:50 | ...::build(...) | semmle.label | ...::build(...) |
6+
subpaths
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: queries/security/CWE-614/InsecureCookie.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql

rust/ql/test/query-tests/security/CWE-614/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn test_cookie(sometimes: bool) {
1313
println!("cookie2 = '{}'", cookie2.to_string());
1414

1515
// secure left as default (which is `None`, equivalent here to `false`)
16-
let cookie3 = Cookie::build(("name", "value")).build(); // $ MISSING: Alert[rust/insecure-cookie]
16+
let cookie3 = Cookie::build(("name", "value")).build(); // $ Alert[rust/insecure-cookie]
1717
println!("cookie3 = '{}'", cookie3.to_string());
1818

1919
// secure setting varies (may be false)

0 commit comments

Comments
 (0)