-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
f6b7aea
Rust: Add prototype query.
geoffw0 f8ef48b
Rust: Add query test.
geoffw0 209f394
Rust: Fix the alert message.
geoffw0 c77eef3
Rust: Convert the query to a path-problem with global data flow.
geoffw0 bb78fdf
Rust: Add qhelp and examples (translated from Go, by Copilot).
geoffw0 87d66c6
Rust: Clean up the .qhelp a little.
geoffw0 dcae0ef
Rust: I prefer the original certificates reference from the Go .qhelp.
geoffw0 49063ac
Rust: Cut down the example for readability.
geoffw0 7a62642
Rust: Change note.
geoffw0 0675a29
Rust: Minor corrections.
geoffw0 42aca4a
Apply suggestions from code review
geoffw0 15fa99a
Rust: Clarify some confusing text in the .qhelp.
geoffw0 12cbb64
Rust: Add query to suite .expected lists.
geoffw0 e43000f
Rust: Correct ordering in query suite .expected lists.
geoffw0 2da0814
Rust: Add test case involving taint.
geoffw0 8145264
Rust: Add threat model sources as additional sources for the query.
geoffw0 aca7877
Rust: Add some missing path / file metadata models.
geoffw0 89a9c46
Rust: Second change note.
geoffw0 785754e
Rust: Switch the query to taint flow, since some taint summaries are …
geoffw0 ace7a77
Rust: Switch to MaD models.
geoffw0 3ad014b
Rust: Additional sinks found in MRVA-1000.
geoffw0 e01c871
Rust: Accept changes to the dataflow/sources/file test.
geoffw0 8061505
Merge remote-tracking branch 'upstream/main' into cert-checks
geoffw0 2ce4c47
Rust: More sinks from the MRVA-1000.
geoffw0 eb674d0
Rust: Reinstate the original function names model but call it a heuri…
geoffw0 ff8032a
Rust: Fix after merge.
geoffw0 0ea28b4
Rust: Test .expected changes.
geoffw0 993154e
Rust: Avoid duplicating sinks.
geoffw0 b62968f
Rust: Spelling.
geoffw0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Added more detailed models for `std::fs` and `std::path`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| extensions: | ||
| - addsTo: | ||
| pack: codeql/rust-all | ||
| extensible: sinkModel | ||
| data: | ||
| - ["<native_tls::TlsConnectorBuilder>::danger_accept_invalid_certs", "Argument[0]", "disable-certificate", "manual"] | ||
| - ["<native_tls::TlsConnectorBuilder>::danger_accept_invalid_hostnames", "Argument[0]", "disable-certificate", "manual"] | ||
| - ["<async_native_tls::connect::TlsConnector>::danger_accept_invalid_certs", "Argument[0]", "disable-certificate", "manual"] | ||
| - ["<async_native_tls::connect::TlsConnector>::danger_accept_invalid_hostnames", "Argument[0]", "disable-certificate", "manual"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
rust/ql/lib/codeql/rust/security/DisabledCertificateCheckExtensions.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /** | ||
| * 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 | ||
| private import codeql.rust.dataflow.internal.Node as Node | ||
|
|
||
| /** | ||
| * 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 sink for disabled certificate check vulnerabilities from model data. | ||
| */ | ||
| private class ModelsAsDataSink extends Sink { | ||
| ModelsAsDataSink() { sinkNode(this, "disable-certificate") } | ||
| } | ||
|
|
||
| /** | ||
| * A heuristic sink for disabled certificate check vulnerabilities based on function names. | ||
| */ | ||
| private class HeuristicSink extends Sink { | ||
| HeuristicSink() { | ||
| exists(CallExprBase fc | | ||
| fc.getStaticTarget().(Function).getName().getText() = | ||
| ["danger_accept_invalid_certs", "danger_accept_invalid_hostnames"] and | ||
| fc.getArg(0) = this.asExpr() and | ||
| // don't duplicate modeled sinks | ||
| not exists(ModelsAsDataSink s | s.(Node::FlowSummaryNode).getSinkElement().getCall() = fc) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
rust/ql/src/change-notes/2025-11-12-disabled-certificate-check.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
42 changes: 42 additions & 0 deletions
42
rust/ql/src/queries/security/CWE-295/DisabledCertificateCheck.qhelp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
|
|
||
| <overview> | ||
| <p> | ||
| The <code>danger_accept_invalid_certs</code> option on TLS connectors and HTTP clients controls whether certificate verification is performed. If this option is set to <code>true</code>, the client will accept any certificate, making it susceptible to man-in-the-middle attacks. | ||
| </p> | ||
| <p> | ||
| Similarly, the <code>danger_accept_invalid_hostnames</code> option controls whether hostname verification is performed. If this option is set to <code>true</code>, the client will accept any valid certificate regardless of the site that certificate is for, again making it susceptible to man-in-the-middle attacks. | ||
| </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 is enabled to prevent security risks. | ||
| </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> |
47 changes: 47 additions & 0 deletions
47
rust/ql/src/queries/security/CWE-295/DisabledCertificateCheck.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /** | ||
| * @name Disabled TLS certificate check | ||
| * @description An application that disables TLS certificate checking is more vulnerable to | ||
| * 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.dataflow.TaintTracking | ||
| import codeql.rust.security.DisabledCertificateCheckExtensions | ||
| import codeql.rust.Concepts | ||
|
|
||
| /** | ||
| * A taint configuration for disabled TLS certificate checks. | ||
| */ | ||
| module DisabledCertificateCheckConfig implements DataFlow::ConfigSig { | ||
| import DisabledCertificateCheckExtensions | ||
|
|
||
| predicate isSource(DataFlow::Node node) { | ||
| // the constant `true` | ||
| node.asExpr().(BooleanLiteralExpr).getTextValue() = "true" | ||
| or | ||
| // a value controlled by a potential attacker | ||
| node instanceof ActiveThreatModelSource | ||
| } | ||
|
|
||
| predicate isSink(DataFlow::Node node) { node instanceof Sink } | ||
|
|
||
| predicate observeDiffInformedIncrementalMode() { any() } | ||
| } | ||
|
|
||
| module DisabledCertificateCheckFlow = TaintTracking::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." |
6 changes: 6 additions & 0 deletions
6
rust/ql/src/queries/security/CWE-295/DisabledCertificateCheckBad.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
10 changes: 10 additions & 0 deletions
10
rust/ql/src/queries/security/CWE-295/DisabledCertificateCheckGood.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 😅