Skip to content

Commit bd07350

Browse files
committed
Rust: Add qhelp and examples.
1 parent 94afc82 commit bd07350

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
7+
<p>Failing to set the 'Secure' attribute on a cookie allows it to be transmitted over an unencrypted (HTTP) connection. If an attacker can observe a user's network traffic (for example over an insecure Wi‑Fi network), they can access sensitive information in the cookie and potentially use it to impersonate the user.</p>
8+
9+
</overview>
10+
<recommendation>
11+
12+
<p>Always set the cookie 'Secure' attribute so that the browser only sends the cookie over HTTPS.</p>
13+
14+
</recommendation>
15+
<example>
16+
17+
<p>The following example creates a cookie using the <code>cookie</code> crate without the 'Secure' attribute:</p>
18+
19+
<sample src="InsecureCookieBad.rs" />
20+
21+
<p>In the fixed example, we either call <code>secure(true)</code> on the <code>CookieBuilder</code> or <code>set_secure(true)</code> on the <code>Cookie</code> itself:</p>
22+
23+
<sample src="InsecureCookieGood.rs" />
24+
25+
</example>
26+
<references>
27+
28+
<li>MDN Web Docs: <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies">Using HTTP cookies</a>.</li>
29+
<li>OWASP Cheat Sheet Series: <a href="https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#transport-layer-security">Session Management Cheat Sheet - Transport Layer Security</a>.</li>
30+
<li>MDN Web Docs: <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Set-Cookie#secure">Set-Cookie header - Secure</a>.</li>
31+
32+
</references>
33+
</qhelp>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use cookie::Cookie;
2+
3+
// BAD: creating a cookie without specifying the `secure` attribute
4+
let cookie = Cookie::build("session", "abcd1234").build();
5+
let mut jar = cookie::CookieJar::new();
6+
jar.add(cookie.clone());
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use cookie::Cookie;
2+
3+
// 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();
5+
let mut jar = cookie::CookieJar::new();
6+
jar.add(secure_cookie.clone());
7+
8+
// GOOD: alternatively, set the 'Secure' attribute on an existing `Cookie`
9+
let mut secure_cookie2 = Cookie::new("session", "abcd1234");
10+
secure_cookie2.set_secure(true);
11+
jar.add(secure_cookie2);

0 commit comments

Comments
 (0)