-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Implement new query for non-HTTPS URLs (CWE-319) #20432
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
Open
Copilot
wants to merge
13
commits into
main
Choose a base branch
from
copilot/fix-f50317f8-0a91-4bb4-a01b-353dcf0f6f3f
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
459f00a
Initial plan
Copilot e630bf8
Implement Rust non-HTTPS URL query (CWE-319)
Copilot 7b04cf1
Rust: Fix up the test annotations.
geoffw0 0924dec
Rust: Make the tests of the example code closer to the actual example…
geoffw0 9c7fc58
Rust: Add tests for a few more edge cases.
geoffw0 0f5aa85
Rust: Remove unnecessary import.
geoffw0 80ce55a
Rust: Make the private address spaces URL more accurate.
geoffw0 4b281fd
Rust: Use case insensitive regexps.
geoffw0 0eb602a
Rust: Update a redirected URL.
geoffw0 31bf86f
Rust: Improve the flow around the qhelp example.
geoffw0 7c22fe2
Merge branch 'main' into https
geoffw0 6f1fcbf
Rust: Add IPv6 private address range (and explanatory comments).
geoffw0 c26a07b
Apply suggestions from code review
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,62 @@ | ||
/** | ||
* Provides classes and predicates for reasoning about the use of | ||
* non-HTTPS URLs in Rust code. | ||
*/ | ||
|
||
import rust | ||
private import codeql.rust.dataflow.DataFlow | ||
private import codeql.rust.dataflow.FlowSink | ||
private import codeql.rust.Concepts | ||
|
||
/** | ||
* Provides default sources, sinks and barriers for detecting use of | ||
* non-HTTPS URLs, as well as extension points for adding your own. | ||
*/ | ||
module UseOfHttp { | ||
/** | ||
* A data flow source for use of non-HTTPS URLs. | ||
*/ | ||
abstract class Source extends DataFlow::Node { } | ||
|
||
/** | ||
* A data flow sink for use of non-HTTPS URLs. | ||
*/ | ||
abstract class Sink extends QuerySink::Range { | ||
override string getSinkType() { result = "UseOfHttp" } | ||
} | ||
|
||
/** | ||
* A barrier for use of non-HTTPS URLs. | ||
*/ | ||
abstract class Barrier extends DataFlow::Node { } | ||
|
||
/** | ||
* A string containing an HTTP URL. | ||
*/ | ||
class HttpStringLiteral extends StringLiteralExpr { | ||
HttpStringLiteral() { | ||
exists(string s | this.getTextValue() = s | | ||
// match HTTP URLs | ||
s.regexpMatch("(?i)\"http://.*\"") and | ||
// exclude private/local addresses: | ||
// - IPv4: localhost / 127.0.0.1, 192.168.x.x, 10.x.x.x, 172.16.x.x -> 172.31.x.x | ||
// - IPv6 (address inside []): ::1 (or 0:0:0:0:0:0:0:1), fc00::/7 (i.e. anything beginning `fcxx:` or `fdxx:`) | ||
not s.regexpMatch("(?i)\"http://(localhost|127\\.0\\.0\\.1|192\\.168\\.[0-9]+\\.[0-9]+|10\\.[0-9]+\\.[0-9]+\\.[0-9]+|172\\.(1[6-9]|2[0-9]|3[01])\\.[0-9]+|\\[::1\\]|\\[0:0:0:0:0:0:0:1\\]|\\[f[cd][0-9a-f]{2}:.*\\]).*\"") | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* An HTTP string literal as a source. | ||
*/ | ||
private class HttpStringLiteralAsSource extends Source { | ||
HttpStringLiteralAsSource() { this.asExpr().getExpr() instanceof HttpStringLiteral } | ||
} | ||
|
||
/** | ||
* A sink for use of HTTP URLs from model data. | ||
*/ | ||
private class ModelsAsDataSink extends Sink { | ||
ModelsAsDataSink() { sinkNode(this, "request-url") } | ||
} | ||
} |
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/non-https-url`, for detecting the use of non-HTTPS URLs that can be intercepted by third parties. |
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,49 @@ | ||
<!DOCTYPE qhelp PUBLIC | ||
"-//Semmle//qhelp//EN" | ||
"qhelp.dtd"> | ||
<qhelp> | ||
<overview> | ||
|
||
<p>Constructing URLs with the HTTP protocol can lead to insecure connections.</p> | ||
|
||
<p>Furthermore, constructing URLs with the HTTP protocol can create problems if other parts of the | ||
code expect HTTPS URLs. A typical pattern is to use libraries that expect secure connections, | ||
which may fail or fall back to insecure behavior when provided with HTTP URLs instead of HTTPS URLs.</p> | ||
|
||
</overview> | ||
<recommendation> | ||
|
||
<p>When you construct a URL for network requests, ensure that you use an HTTPS URL rather than an HTTP URL. | ||
Then, any connections that are made using that URL are secure TLS connections.</p> | ||
|
||
</recommendation> | ||
<example> | ||
|
||
<p>The following examples show two ways of making a network request using a URL. When the request is | ||
made using an HTTP URL rather than an HTTPS URL, the connection is unsecured and can be intercepted | ||
by attackers:</p> | ||
|
||
<sample src="UseOfHttpBad.rs" /> | ||
|
||
<p>A better approach is to use HTTPS. When the request is made using an HTTPS URL, the connection | ||
is a secure TLS connection:</p> | ||
|
||
<sample src="UseOfHttpGood.rs" /> | ||
|
||
</example> | ||
<references> | ||
|
||
<li> | ||
OWASP: | ||
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Transport_Layer_Security_Cheat_Sheet.html">Transport Layer Security Cheat Sheet</a>. | ||
</li> | ||
<li> | ||
OWASP Top 10: | ||
<a href="https://owasp.org/Top10/A08_2021-Software_and_Data_Integrity_Failures/">A08:2021 - Software and Data Integrity Failures</a>. | ||
</li> | ||
<li>Rust reqwest documentation: | ||
<a href="https://docs.rs/reqwest/">reqwest crate</a>. | ||
</li> | ||
|
||
</references> | ||
</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 @@ | ||
/** | ||
* @name Failure to use HTTPS URLs | ||
* @description Non-HTTPS connections can be intercepted by third parties. | ||
* @kind path-problem | ||
* @problem.severity warning | ||
* @security-severity 8.1 | ||
* @precision high | ||
* @id rust/non-https-url | ||
* @tags security | ||
* external/cwe/cwe-319 | ||
* external/cwe/cwe-345 | ||
*/ | ||
|
||
import rust | ||
import codeql.rust.dataflow.DataFlow | ||
import codeql.rust.dataflow.TaintTracking | ||
import codeql.rust.security.UseOfHttpExtensions | ||
|
||
/** | ||
* A taint configuration for HTTP URL strings that flow to URL-using sinks. | ||
*/ | ||
module UseOfHttpConfig implements DataFlow::ConfigSig { | ||
import UseOfHttp | ||
|
||
predicate isSource(DataFlow::Node node) { node instanceof Source } | ||
|
||
predicate isSink(DataFlow::Node node) { node instanceof Sink } | ||
|
||
predicate isBarrier(DataFlow::Node barrier) { barrier instanceof Barrier } | ||
|
||
predicate observeDiffInformedIncrementalMode() { any() } | ||
} | ||
|
||
module UseOfHttpFlow = TaintTracking::Global<UseOfHttpConfig>; | ||
|
||
import UseOfHttpFlow::PathGraph | ||
|
||
from UseOfHttpFlow::PathNode sourceNode, UseOfHttpFlow::PathNode sinkNode | ||
where UseOfHttpFlow::flowPath(sourceNode, sinkNode) | ||
select sinkNode.getNode(), sourceNode, sinkNode, | ||
"This URL may be constructed with the HTTP protocol, from $@.", sourceNode.getNode(), | ||
"this HTTP URL" | ||
github-advanced-security[bot] marked this conversation as resolved.
Dismissed
Show dismissed
Hide dismissed
|
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 @@ | ||
// BAD: Using HTTP URL which can be intercepted | ||
use reqwest; | ||
|
||
fn main() { | ||
let url = "http://example.com/sensitive-data"; | ||
|
||
// This makes an insecure HTTP request that can be intercepted | ||
let response = reqwest::blocking::get(url).unwrap(); | ||
println!("Response: {}", response.text().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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// GOOD: Using HTTPS URL which provides encryption | ||
use reqwest; | ||
|
||
fn main() { | ||
let url = "https://example.com/sensitive-data"; | ||
|
||
// This makes a secure HTTPS request that is encrypted | ||
let response = reqwest::blocking::get(url).unwrap(); | ||
println!("Response: {}", response.text().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
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.
Uh oh!
There was an error while loading. Please reload this page.