Skip to content

Commit 4662e42

Browse files
committed
Rust: Add examples as tests (and fix them).
1 parent bd07350 commit 4662e42

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cookie::Cookie;
22

33
// BAD: creating a cookie without specifying the `secure` attribute
4-
let cookie = Cookie::build("session", "abcd1234").build();
4+
let cookie = Cookie::build(("session", "abcd1234")).build();
55
let mut jar = cookie::CookieJar::new();
66
jar.add(cookie.clone());

rust/ql/src/queries/security/CWE-614/InsecureCookieGood.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use cookie::Cookie;
22

33
// GOOD: set the `CookieBuilder` 'Secure' attribute so that the cookie is only sent over HTTPS
4-
let secure_cookie = Cookie::build("session", "abcd1234").secure(true).build();
4+
let secure_cookie = Cookie::build(("session", "abcd1234")).secure(true).build();
55
let mut jar = cookie::CookieJar::new();
66
jar.add(secure_cookie.clone());
77

rust/ql/test/query-tests/security/CWE-614/CookieSet.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@
4949
| main.rs:138:13:138:13 | d | secure | true |
5050
| main.rs:142:13:142:13 | e | partitioned | false |
5151
| main.rs:146:13:146:13 | f | secure | false |
52+
| main.rs:180:29:180:66 | ...::build(...) | secure | true |
53+
| main.rs:186:9:186:22 | [SSA] secure_cookie2 | secure | true |
54+
| main.rs:186:9:186:22 | secure_cookie2 | secure | true |

rust/ql/test/query-tests/security/CWE-614/InsecureCookie.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
| main.rs:165:13:165:18 | insert | main.rs:155:13:155:41 | ...::new | main.rs:165:13:165:18 | insert | Cookie attribute 'Secure' is not set to true. |
7878
| main.rs:166:13:166:18 | insert | main.rs:155:13:155:41 | ...::new | main.rs:166:13:166:18 | insert | Cookie attribute 'Secure' is not set to true. |
7979
| main.rs:167:13:167:18 | insert | main.rs:155:13:155:41 | ...::new | main.rs:167:13:167:18 | insert | Cookie attribute 'Secure' is not set to true. |
80+
| main.rs:173:61:173:65 | build | main.rs:173:22:173:34 | ...::build | main.rs:173:61:173:65 | build | Cookie attribute 'Secure' is not set to true. |
8081
edges
8182
| main.rs:8:19:8:31 | ...::build | main.rs:8:19:8:50 | ...::build(...) | provenance | Src:MaD:13 MaD:13 |
8283
| main.rs:8:19:8:50 | ...::build(...) | main.rs:8:19:8:64 | ... .secure(...) | provenance | MaD:41 |
@@ -311,6 +312,8 @@ edges
311312
| main.rs:167:20:167:20 | i | main.rs:167:20:167:28 | i.clone() | provenance | MaD:17 |
312313
| main.rs:167:20:167:28 | i.clone() | main.rs:167:20:167:45 | ... .make_permanent() | provenance | MaD:18 |
313314
| main.rs:167:20:167:45 | ... .make_permanent() | main.rs:167:13:167:18 | insert | provenance | MaD:1 Sink:MaD:1 |
315+
| main.rs:173:22:173:34 | ...::build | main.rs:173:22:173:59 | ...::build(...) | provenance | Src:MaD:13 MaD:13 |
316+
| main.rs:173:22:173:59 | ...::build(...) | main.rs:173:61:173:65 | build | provenance | MaD:2 Sink:MaD:2 |
314317
models
315318
| 1 | Sink: <biscotti::response_cookies::ResponseCookies>::insert; Argument[0]; cookie-use |
316319
| 2 | Sink: <cookie::builder::CookieBuilder>::build; Argument[self]; cookie-use |
@@ -588,4 +591,7 @@ nodes
588591
| main.rs:167:20:167:20 | i | semmle.label | i |
589592
| main.rs:167:20:167:28 | i.clone() | semmle.label | i.clone() |
590593
| main.rs:167:20:167:45 | ... .make_permanent() | semmle.label | ... .make_permanent() |
594+
| main.rs:173:22:173:34 | ...::build | semmle.label | ...::build |
595+
| main.rs:173:22:173:59 | ...::build(...) | semmle.label | ...::build(...) |
596+
| main.rs:173:61:173:65 | build | semmle.label | build |
591597
subpaths

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,30 @@ fn test_biscotti() {
167167
cookies.insert(i.clone().make_permanent()); // $ Alert[rust/insecure-cookie]
168168
}
169169

170+
fn test_qhelp_examples() {use cookie::Cookie;
171+
{
172+
// BAD: creating a cookie without specifying the `secure` attribute
173+
let cookie = Cookie::build(("session", "abcd1234")).build(); // $ Alert[rust/insecure-cookie]
174+
let mut jar = cookie::CookieJar::new();
175+
jar.add(cookie.clone());
176+
}
177+
178+
{
179+
// GOOD: set the `CookieBuilder` 'Secure' attribute so that the cookie is only sent over HTTPS
180+
let secure_cookie = Cookie::build(("session", "abcd1234")).secure(true).build();
181+
let mut jar = cookie::CookieJar::new();
182+
jar.add(secure_cookie.clone());
183+
184+
// GOOD: alternatively, set the 'Secure' attribute on an existing `Cookie`
185+
let mut secure_cookie2 = Cookie::new("session", "abcd1234");
186+
secure_cookie2.set_secure(true);
187+
jar.add(secure_cookie2);
188+
}
189+
}
190+
170191
fn main() {
171192
test_cookie(true);
172193
test_cookie(false);
173194
test_biscotti();
195+
test_qhelp_examples();
174196
}

0 commit comments

Comments
 (0)