-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Add new query for XSS vulnerabilities #20902
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
+5,212
−1
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0f4561e
Rust: Add XSS examples
paldepind ae9c753
Rust: Add XSS query
paldepind 9e2bf76
Rust: Add XSS sinks for Actix and Warp
paldepind 9c2858d
Rust: Add qhelp for XSS query
paldepind 597c81d
Rust: Add change note for XSS query
paldepind ce25def
Rust: Update integration test expected files
paldepind 411d1fa
Rust: Fix grammar and typos
paldepind 9ae4c14
Rust: Address PR feedback
paldepind 7c76636
Rust: Fix typo in change note for XSS query
paldepind 7278bc7
Rust: Remove unused function in XSS tests
paldepind 97dad2d
Rust: Apply suggestions from docs 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
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 cross-site scripting (XSS) | ||
| * vulnerabilities. | ||
| */ | ||
|
|
||
| import rust | ||
| private import codeql.rust.dataflow.DataFlow | ||
| private import codeql.rust.dataflow.FlowSink | ||
| private import codeql.rust.Concepts | ||
| private import codeql.util.Unit | ||
| private import codeql.rust.security.Barriers as Barriers | ||
|
|
||
| /** | ||
| * Provides default sources, sinks and barriers for detecting XSS | ||
| * vulnerabilities, as well as extension points for adding your own. | ||
| */ | ||
| module Xss { | ||
| /** | ||
| * A data flow source for XSS vulnerabilities. | ||
| */ | ||
| abstract class Source extends DataFlow::Node { } | ||
|
|
||
| /** | ||
| * A data flow sink for XSS vulnerabilities. | ||
| */ | ||
| abstract class Sink extends QuerySink::Range { | ||
| override string getSinkType() { result = "Xss" } | ||
| } | ||
|
|
||
| /** | ||
| * A barrier for XSS vulnerabilities. | ||
| */ | ||
| abstract class Barrier extends DataFlow::Node { } | ||
|
|
||
| /** | ||
| * An active threat-model source, considered as a flow source. | ||
| */ | ||
| private class ActiveThreatModelSourceAsSource extends Source, ActiveThreatModelSource { } | ||
|
|
||
| /** | ||
| * A sink for XSS from model data. | ||
| */ | ||
| private class ModelsAsDataSink extends Sink { | ||
| ModelsAsDataSink() { sinkNode(this, "html-injection") } | ||
| } | ||
|
|
||
| /** | ||
| * A barrier for XSS vulnerabilities for nodes whose type is a | ||
| * numeric or boolean type, which is unlikely to expose any vulnerability. | ||
| */ | ||
| private class NumericTypeBarrier extends Barrier instanceof Barriers::NumericTypeBarrier { } | ||
|
|
||
| /** A call to a function with "escape" or "encode" in its name. */ | ||
| private class HeuristicHtmlEncodingBarrier extends Barrier { | ||
| HeuristicHtmlEncodingBarrier() { | ||
| exists(Call fc | | ||
| fc.getStaticTarget().getName().getText().regexpMatch(".*(escape|encode).*") and | ||
| fc.getArgument(_) = this.asExpr() | ||
| ) | ||
| } | ||
| } | ||
| } | ||
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 a query `rust/xss`, to detect cross-site scripting security vulnerabilities. | ||
paldepind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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,48 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
|
|
||
| <overview> | ||
| <p>Directly writing user input (for example, an HTTP request parameter) to a web | ||
paldepind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| page, without properly sanitizing the input first, allows for a cross-site | ||
paldepind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| scripting vulnerability.</p> | ||
| </overview> | ||
|
|
||
| <recommendation> | ||
| <p>To guard against cross-site scripting, consider encoding/escaping the untrusted | ||
| input before including it in the HTML.</p> | ||
| </recommendation> | ||
|
|
||
| <example> | ||
|
|
||
| <p>The following example shows a simple web handler that writes a URL path parameter | ||
| directly to an HTML response, leaving the website vulnerable to cross-site | ||
| scripting:</p> | ||
|
|
||
| <sample src="XSSBad.rs" /> | ||
|
|
||
| <p>To fix this vulnerability, the user input should be HTML-encoded before being | ||
| included in the response. In the following example <code>encode_text</code> from | ||
paldepind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| the <a href="https://docs.rs/html-escape/latest/html_escape/index.html">html_escape</a> | ||
| crate is used:</p> | ||
paldepind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| <sample src="XSSGood.rs" /> | ||
|
|
||
| </example> | ||
|
|
||
| <references> | ||
| <li> | ||
| OWASP: | ||
| <a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html">XSS | ||
paldepind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| (Cross Site Scripting) Prevention Cheat Sheet</a>. | ||
paldepind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </li> | ||
| <li> | ||
| Wikipedia: <a href="https://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a>. | ||
| </li> | ||
| <li> | ||
| OWASP: | ||
| <a href="https://owasp.org/www-community/attacks/xss/">Cross-site Scripting (XSS)</a>. | ||
paldepind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </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 Cross-site scripting | ||
| * @description Writing user input directly to a web page | ||
paldepind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * allows for a cross-site scripting vulnerability. | ||
| * @kind path-problem | ||
| * @problem.severity error | ||
| * @security-severity 6.1 | ||
| * @precision high | ||
| * @id rust/xss | ||
| * @tags security | ||
| * external/cwe/cwe-079 | ||
| * external/cwe/cwe-116 | ||
| */ | ||
|
|
||
| import rust | ||
| import codeql.rust.dataflow.DataFlow | ||
| import codeql.rust.dataflow.TaintTracking | ||
| import codeql.rust.security.XssExtensions | ||
|
|
||
| /** | ||
| * A taint configuration for tainted data that reaches an XSS sink. | ||
| */ | ||
| module XssConfig implements DataFlow::ConfigSig { | ||
| import Xss | ||
|
|
||
| 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 XssFlow = TaintTracking::Global<XssConfig>; | ||
|
|
||
| import XssFlow::PathGraph | ||
|
|
||
| from XssFlow::PathNode sourceNode, XssFlow::PathNode sinkNode | ||
| where XssFlow::flowPath(sourceNode, sinkNode) | ||
| select sinkNode.getNode(), sourceNode, sinkNode, "Cross-site scripting vulnerability due to a $@.", | ||
| sourceNode.getNode(), "user-provided value" | ||
geoffw0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,21 @@ | ||
| use actix_web::{web, HttpResponse, Result}; | ||
|
|
||
| // BAD: User input is directly included in HTML response without sanitization | ||
| async fn vulnerable_handler(path: web::Path<String>) -> impl Responder { | ||
| let user_input = path.into_inner(); | ||
|
|
||
| let html = format!( | ||
| r#" | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head><title>Welcome</title></head> | ||
| <body> | ||
| <h1>Hello, {}!</h1> | ||
| </body> | ||
| </html> | ||
| "#, | ||
| user_input | ||
| ); | ||
|
|
||
| Html::new(html) // Unsafe: User input included directly in the response | ||
| } |
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,21 @@ | ||
| use actix_web::{web, HttpResponse, Result}; | ||
|
|
||
| // GOOD: Manual HTML encoding using an `html_escape::encode_text` function | ||
| async fn safe_handler_with_encoding(path: web::Path<String>) -> impl Responder { | ||
| let user_input = path.into_inner(); | ||
| let escaped_input = html_escape::encode_text(&user_input); | ||
| let html = format!( | ||
| r#" | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head><title>Welcome</title></head> | ||
| <body> | ||
| <h1>Hello, {}!</h1> | ||
| </body> | ||
| </html> | ||
| "#, | ||
| escaped_input | ||
| ); | ||
|
|
||
| Html::new(html) // Safe: user input is HTML-encoded | ||
| } |
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.