-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Add cleartext transmission query #19000
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
4 commits
Select commit
Hold shift + click to select a range
4de69c7
Rust: Add cleartext transmission query
paldepind 1ae28c7
Merge branch 'main' into rust-cleartext-transmission
paldepind fb71866
Rust: Generate more sinks and update query description
paldepind 0bf8265
Rust: Apply qhelp suggestions from review
paldepind 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Models for the `url` crate | ||
extensions: | ||
- addsTo: | ||
pack: codeql/rust-all | ||
extensible: summaryModel | ||
data: | ||
- ["repo:https://github.com/servo/rust-url:url", "<crate::Url>::parse", "Argument[0].Reference", "ReturnValue.Field[crate::result::Result::Ok(0)]", "taint", "manual"] |
38 changes: 38 additions & 0 deletions
38
rust/ql/lib/codeql/rust/security/CleartextTransmissionExtensions.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,38 @@ | ||
/** | ||
* Provides classes and predicates for reasoning about cleartext transmission | ||
* vulnerabilities. | ||
*/ | ||
|
||
private import codeql.util.Unit | ||
private import rust | ||
private import codeql.rust.dataflow.DataFlow | ||
private import codeql.rust.dataflow.FlowSink | ||
|
||
/** | ||
* A data flow sink for cleartext transmission vulnerabilities. That is, | ||
* a `DataFlow::Node` of something that is transmitted over a network. | ||
*/ | ||
abstract class CleartextTransmissionSink extends DataFlow::Node { } | ||
|
||
/** | ||
* A barrier for cleartext transmission vulnerabilities. | ||
*/ | ||
abstract class CleartextTransmissionBarrier extends DataFlow::Node { } | ||
|
||
/** | ||
* A unit class for adding additional flow steps. | ||
*/ | ||
class CleartextTransmissionAdditionalFlowStep extends Unit { | ||
/** | ||
* Holds if the step from `node1` to `node2` should be considered a flow | ||
* step for paths related to cleartext transmission vulnerabilities. | ||
*/ | ||
abstract predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo); | ||
} | ||
|
||
/** | ||
* A sink defined through MaD. | ||
*/ | ||
private class MadCleartextTransmissionSink extends CleartextTransmissionSink { | ||
MadCleartextTransmissionSink() { sinkNode(this, "transmission") } | ||
} |
23 changes: 23 additions & 0 deletions
23
.../ql/lib/ext/generated/reqwest/repo-https-g.yxqyang.asia-seanmonstar-reqwest-reqwest.model.yml
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,23 @@ | ||
# THIS FILE IS AN AUTO-GENERATED MODELS AS DATA FILE. DO NOT EDIT. | ||
extensions: | ||
- addsTo: | ||
pack: codeql/rust-all | ||
extensible: sinkModel | ||
data: | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::async_impl::client::Client>::delete", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::async_impl::client::Client>::get", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::async_impl::client::Client>::head", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::async_impl::client::Client>::patch", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::async_impl::client::Client>::post", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::async_impl::client::Client>::put", "Argument[0]", "transmission", "df-generated"] | ||
geoffw0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::blocking::client::Client>::delete", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::blocking::client::Client>::get", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::blocking::client::Client>::head", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::blocking::client::Client>::patch", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::blocking::client::Client>::post", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::blocking::client::Client>::put", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::connect::Connector as crate::Service>::call", "Argument[0]", "log-injection", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "<crate::connect::ConnectorService as crate::Service>::call", "Argument[0]", "log-injection", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "crate::blocking::get", "Argument[0]", "transmission", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "crate::blocking::wait::timeout", "Argument[1]", "log-injection", "df-generated"] | ||
- ["repo:https://github.com/seanmonstar/reqwest:reqwest", "crate::get", "Argument[0]", "transmission", "df-generated"] | ||
geoffw0 marked this conversation as resolved.
Show resolved
Hide resolved
|
51 changes: 51 additions & 0 deletions
51
rust/ql/src/queries/security/CWE-311/CleartextTransmission.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,51 @@ | ||
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd"> | ||
<qhelp> | ||
<overview> | ||
|
||
<p> | ||
Sensitive information that is transmitted without encryption may be accessible | ||
to an attacker. | ||
</p> | ||
|
||
</overview> | ||
<recommendation> | ||
|
||
<p> | ||
Ensure that sensitive information is always encrypted before being transmitted | ||
over the network. In general, decrypt sensitive information only at the point | ||
where it is necessary for it to be used in cleartext. Avoid transmitting | ||
sensitive information when it is not necessary to. | ||
</p> | ||
|
||
</recommendation> | ||
<example> | ||
|
||
<p> | ||
The following example shows three cases of transmitting information. In the | ||
'BAD' case, the transmitted data is sensitive (a credit card number) and is | ||
included as cleartext in the URL. URLs are often logged or otherwise visible in | ||
cleartext, and should not contain sensitive information. | ||
</p> | ||
|
||
<p> | ||
In the 'GOOD' cases, the data is either not sensitive, or is protected with | ||
encryption. When encryption is used, ensure that you select a secure modern | ||
encryption algorithm, and put suitable key management practices into place. | ||
</p> | ||
|
||
<sample src="CleartextTransmission.rs" /> | ||
|
||
</example> | ||
<references> | ||
|
||
<li> | ||
OWASP Top 10:2021: | ||
<a href="https://owasp.org/Top10/A02_2021-Cryptographic_Failures/">A02:2021 - Cryptographic Failures</a>. | ||
</li> | ||
<li> | ||
OWASP: | ||
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Key_Management_Cheat_Sheet.html">Key Management Cheat Sheet</a>. | ||
</li> | ||
|
||
</references> | ||
</qhelp> |
50 changes: 50 additions & 0 deletions
50
rust/ql/src/queries/security/CWE-311/CleartextTransmission.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,50 @@ | ||
/** | ||
* @name Cleartext transmission of sensitive information | ||
* @description Transmitting sensitive information across a network in | ||
* cleartext can expose it to an attacker. | ||
* @kind path-problem | ||
* @problem.severity warning | ||
* @security-severity 7.5 | ||
* @precision high | ||
* @id rust/cleartext-transmission | ||
* @tags security | ||
* external/cwe/cwe-319 | ||
*/ | ||
|
||
import rust | ||
import codeql.rust.dataflow.DataFlow | ||
import codeql.rust.security.SensitiveData | ||
import codeql.rust.dataflow.TaintTracking | ||
import codeql.rust.security.CleartextTransmissionExtensions | ||
|
||
/** | ||
* A taint configuration from sensitive information to expressions that are | ||
* transmitted over a network. | ||
*/ | ||
module CleartextTransmissionConfig implements DataFlow::ConfigSig { | ||
predicate isSource(DataFlow::Node node) { node instanceof SensitiveData } | ||
|
||
predicate isSink(DataFlow::Node node) { node instanceof CleartextTransmissionSink } | ||
|
||
predicate isBarrier(DataFlow::Node barrier) { barrier instanceof CleartextTransmissionBarrier } | ||
|
||
predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { | ||
any(CleartextTransmissionAdditionalFlowStep s).step(nodeFrom, nodeTo) | ||
} | ||
|
||
predicate isBarrierIn(DataFlow::Node node) { | ||
// make sources barriers so that we only report the closest instance | ||
isSource(node) | ||
} | ||
} | ||
|
||
module CleartextTransmissionFlow = TaintTracking::Global<CleartextTransmissionConfig>; | ||
|
||
import CleartextTransmissionFlow::PathGraph | ||
|
||
from CleartextTransmissionFlow::PathNode sourceNode, CleartextTransmissionFlow::PathNode sinkNode | ||
where CleartextTransmissionFlow::flowPath(sourceNode, sinkNode) | ||
select sinkNode.getNode(), sourceNode, sinkNode, | ||
"The operation '" + sinkNode.getNode().toString() + | ||
"', transmits data which may contain unencrypted sensitive data from $@.", sourceNode, | ||
sourceNode.getNode().toString() | ||
geoffw0 marked this conversation as resolved.
Show resolved
Hide resolved
|
15 changes: 15 additions & 0 deletions
15
rust/ql/src/queries/security/CWE-311/CleartextTransmission.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,15 @@ | ||
func getData() { | ||
// ... | ||
|
||
// GOOD: not sensitive information | ||
let body = reqwest::get("https://example.com/song/{faveSong}").await?.text().await?; | ||
|
||
// BAD: sensitive information sent in cleartext in the URL | ||
let body = reqwest::get(format!("https://example.com/card/{creditCardNo}")).await?.text().await?; | ||
|
||
// GOOD: encrypted sensitive information sent in the URL | ||
let encryptedPassword = encrypt(password, encryptionKey); | ||
let body = reqwest::get(format!("https://example.com/card/{creditCardNo}")).await?.text().await?; | ||
|
||
// ... | ||
} |
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.
I think this ought to extend
QuerySink::Range
instead (cc @geoffw0 ).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.
Yes it should, but that class is new. I'll make the change and check all the other merged queries do this now.
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.
Here: #19103