-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: New Query rust/disabled-certificate-check #20829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
f6b7aea
f8ef48b
209f394
c77eef3
bb78fdf
87d66c6
dcae0ef
49063ac
7a62642
0675a29
42aca4a
15fa99a
12cbb64
e43000f
2da0814
8145264
aca7877
89a9c46
785754e
ace7a77
3ad014b
e01c871
8061505
2ce4c47
eb674d0
ff8032a
0ea28b4
993154e
b62968f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| * Provides classes and predicates for reasoning about disabled certificate | ||
| * check vulnerabilities. | ||
| */ | ||
|
|
||
| import rust | ||
| private import codeql.rust.dataflow.DataFlow | ||
| private import codeql.rust.dataflow.FlowSink | ||
| private import codeql.rust.Concepts | ||
|
|
||
| /** | ||
| * Provides default sinks for detecting disabled certificate check | ||
| * vulnerabilities, as well as extension points for adding your own. | ||
| */ | ||
| module DisabledCertificateCheckExtensions { | ||
| /** | ||
| * A data flow sink for disabled certificate check vulnerabilities. | ||
| */ | ||
| abstract class Sink extends QuerySink::Range { | ||
| override string getSinkType() { result = "DisabledCertificateCheck" } | ||
| } | ||
|
|
||
| /** | ||
| * A default sink for disabled certificate check vulnerabilities based on function names. | ||
| */ | ||
| private class DefaultSink extends Sink { | ||
| DefaultSink() { | ||
| exists(CallExprBase fc | | ||
| fc.getStaticTarget().(Function).getName().getText() = | ||
| ["danger_accept_invalid_certs", "danger_accept_invalid_hostnames"] and | ||
| fc.getArg(0) = this.asExpr().getExpr() | ||
|
||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A sink for disabled certificate check vulnerabilities from model data. | ||
| */ | ||
| private class ModelsAsDataSink extends Sink { | ||
| ModelsAsDataSink() { sinkNode(this, "disable-certificate") } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: newQuery | ||
| --- | ||
| * Added a new query `rust/disabled-certificate-check`, to detect disabled TLS certificate checks. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
|
|
||
| <overview> | ||
| <p> | ||
| 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 or any host name, making it susceptible to man-in-the-middle attacks. | ||
geoffw0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
geoffw0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </p> | ||
| </overview> | ||
|
|
||
| <recommendation> | ||
| <p> | ||
| Do not set <code>danger_accept_invalid_certs</code> or <code>danger_accept_invalid_hostnames</code> to <code>true</code>, except in controlled environments such as tests. In production, always ensure certificate and hostname verification are enabled to prevent security risks. | ||
geoffw0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </p> | ||
| </recommendation> | ||
|
|
||
| <example> | ||
| <p> | ||
| The following code snippet shows a function that creates an HTTP client with certificate verification disabled: | ||
| </p> | ||
| <sample src="DisabledCertificateCheckBad.rs"/> | ||
| <p> | ||
| In production code, always configure clients to verify certificates: | ||
| </p> | ||
| <sample src="DisabledCertificateCheckGood.rs"/> | ||
| </example> | ||
| <references> | ||
| <li> | ||
| Rust native-tls crate: <a href="https://docs.rs/native-tls/latest/native_tls/struct.TlsConnectorBuilder.html">TlsConnectorBuilder</a>. | ||
| </li> | ||
| <li> | ||
| Rust reqwest crate: <a href="https://docs.rs/reqwest/latest/reqwest/struct.ClientBuilder.html">ClientBuilder</a>. | ||
| </li> | ||
| <li> | ||
| SSL.com: <a href="https://www.ssl.com/article/browsers-and-certificate-validation/">Browsers and Certificate Validation</a>. | ||
| </li> | ||
| </references> | ||
| </qhelp> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /** | ||
| * @name Disabled TLS certificate check | ||
| * @description If an application disables TLS certificate checking, it may be vulnerable to | ||
geoffw0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * man-in-the-middle attacks. | ||
| * @kind path-problem | ||
| * @problem.severity warning | ||
| * @security-severity 7.5 | ||
| * @precision high | ||
| * @id rust/disabled-certificate-check | ||
| * @tags security | ||
| * external/cwe/cwe-295 | ||
| */ | ||
|
|
||
| import rust | ||
| import codeql.rust.dataflow.DataFlow | ||
| import codeql.rust.security.DisabledCertificateCheckExtensions | ||
|
|
||
| /** | ||
| * A taint configuration for disabled TLS certificate checks. | ||
| */ | ||
| module DisabledCertificateCheckConfig implements DataFlow::ConfigSig { | ||
| import DisabledCertificateCheckExtensions | ||
|
|
||
| predicate isSource(DataFlow::Node node) { | ||
| node.asExpr().getExpr().(BooleanLiteralExpr).getTextValue() = "true" | ||
|
||
| } | ||
|
|
||
| predicate isSink(DataFlow::Node node) { node instanceof Sink } | ||
|
|
||
| predicate observeDiffInformedIncrementalMode() { any() } | ||
| } | ||
|
|
||
| module DisabledCertificateCheckFlow = DataFlow::Global<DisabledCertificateCheckConfig>; | ||
|
|
||
| import DisabledCertificateCheckFlow::PathGraph | ||
|
|
||
| from | ||
| DisabledCertificateCheckFlow::PathNode sourceNode, DisabledCertificateCheckFlow::PathNode sinkNode | ||
| where DisabledCertificateCheckFlow::flowPath(sourceNode, sinkNode) | ||
| select sinkNode.getNode(), sourceNode, sinkNode, | ||
| "Disabling TLS certificate validation can expose the application to man-in-the-middle attacks." | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // BAD: Disabling certificate validation in Rust | ||
|
|
||
| let _client = reqwest::Client::builder() | ||
| .danger_accept_invalid_certs(true) // disables certificate validation | ||
| .build() | ||
| .unwrap(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // GOOD: Certificate validation is enabled (default) | ||
|
|
||
| let _client = reqwest::Client::builder() | ||
| .danger_accept_invalid_certs(false) // certificate validation enabled explicitly | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let _client = native_tls::TlsConnector::builder() // certificate validation enabled by default | ||
| .build() | ||
| .unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this because there are some functions with these names that where not covered by the MaD? Or is it just to avoid enumerating all the variants of these methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could enumerate all the variants of these methods that we find in the MRVA-1000, but:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Also, I see now that you had already explained this in other comments — I missed those when I asked the above 😅