-
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
Open
paldepind
wants to merge
10
commits into
github:main
Choose a base branch
from
paldepind:rust/xss-query
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.
+5,212
−1
Open
Changes from 6 commits
Commits
Show all changes
10 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 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().(Function).getName().getText().regexpMatch(".*(escape|encode).*") and | ||
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
|
||
| 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 new a query `rust/xss`, to detect XSS 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,46 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
|
|
||
| <overview> | ||
| <p>Directly writing user input (for example, an HTTP request parameter) to a web | ||
| page, without properly sanitizing the input first, allows for a cross-site | ||
| scripting vulnerability.</p> | ||
| </overview> | ||
|
|
||
| <recommendation> | ||
| <p>To guard against cross-site scripting, consider encoding/escaping the unstrusted | ||
| input before including it in the HTML.</p> | ||
| </recommendation> | ||
|
|
||
| <example> | ||
|
|
||
| <p>The following example shows a simple web handler that writes a path of the | ||
| URL 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:</p> | ||
|
|
||
| <sample src="XSSGood.rs" /> | ||
|
|
||
| </example> | ||
|
|
||
| <references> | ||
| <li> | ||
| OWASP: | ||
| <a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html">XSS | ||
| (Cross Site Scripting) Prevention Cheat Sheet</a>. | ||
| </li> | ||
| <li> | ||
| WiMISSING: Alert[rust/xss]kipedia: <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a>. | ||
paldepind marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </li> | ||
| <li> | ||
| OWASP: | ||
| <a href="https://owasp.org/www-community/attacks/xss/">Cross-site Scripting (XSS)</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 Cross-site scripting | ||
| * @description Writing user input directly to a web page | ||
| * 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,23 @@ | ||
| use actix_web::{web, HttpResponse, Result}; | ||
| use askama::Template; | ||
geoffw0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // GOOD: Manual HTML encoding using an `html_escape` function | ||
| async fn safe_handler_with_encoding(path: web::Path<String>) -> impl Responder { | ||
| let user_input = path.into_inner(); | ||
| let escaped_input = html_escape(&user_input); | ||
geoffw0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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 | ||
| } | ||
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.