Skip to content

Commit bb78fdf

Browse files
committed
Rust: Add qhelp and examples (translated from Go, by Copilot).
1 parent c77eef3 commit bb78fdf

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>
8+
In Rust, the <code>danger_accept_invalid_certs</code> and <code>danger_accept_invalid_hostnames</code> options on TLS connectors and HTTP clients control whether certificate and hostname verification are performed. If set to <code>true</code>, the client will accept any certificate and any host name, making it susceptible to man-in-the-middle attacks.
9+
</p>
10+
</overview>
11+
12+
<recommendation>
13+
<p>
14+
Do not set <code>danger_accept_invalid_certs</code> or <code>danger_accept_invalid_hostnames</code> to <code>true</code> except in tests or controlled environments. In production, always ensure certificate and hostname verification are enabled to prevent security risks.
15+
</p>
16+
</recommendation>
17+
18+
<example>
19+
<p>
20+
The following code snippet shows a function that creates a TLS or HTTP client with certificate verification disabled:
21+
</p>
22+
<sample src="DisabledCertificateCheckBad.rs"/>
23+
<p>
24+
While this may be acceptable in a test, it should not be used in production code. Instead, always configure clients to verify certificates and hostnames:
25+
</p>
26+
<sample src="DisabledCertificateCheckGood.rs"/>
27+
</example>
28+
<references>
29+
<li>
30+
Rust native-tls crate: <a href="https://docs.rs/native-tls/latest/native_tls/struct.TlsConnectorBuilder.html">TlsConnectorBuilder</a>.
31+
</li>
32+
<li>
33+
Rust reqwest crate: <a href="https://docs.rs/reqwest/latest/reqwest/struct.ClientBuilder.html">ClientBuilder</a>.
34+
</li>
35+
<li>
36+
Mozilla: <a href="https://infosec.mozilla.org/guidelines/web_security#https">Web Security Guidelines: HTTPS</a>.
37+
</li>
38+
</references>
39+
</qhelp>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// BAD: Disabling certificate validation in Rust
2+
3+
// Using native_tls
4+
let _client = native_tls::TlsConnector::builder()
5+
.danger_accept_invalid_certs(true) // disables certificate validation
6+
.build()
7+
.unwrap();
8+
9+
// Using reqwest
10+
let _client = reqwest::Client::builder()
11+
.danger_accept_invalid_certs(true) // disables certificate validation
12+
.build()
13+
.unwrap();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// GOOD: Certificate validation is enabled (default)
2+
3+
// Using native_tls
4+
let _client = native_tls::TlsConnector::builder()
5+
.danger_accept_invalid_certs(false) // certificate validation enabled
6+
.build()
7+
.unwrap();
8+
9+
// Using reqwest
10+
let _client = reqwest::Client::builder()
11+
.danger_accept_invalid_certs(false) // certificate validation enabled
12+
.build()
13+
.unwrap();
14+
15+
// Or simply use the default builder (safe)
16+
let _client = native_tls::TlsConnector::builder()
17+
.build()
18+
.unwrap();

0 commit comments

Comments
 (0)