-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Add example queries #20776
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
geoffw0
wants to merge
6
commits into
github:main
Choose a base branch
from
geoffw0:rustexamples
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.
+120
−0
Open
Rust: Add example queries #20776
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
84b5060
Rust: Add example queries pack.
geoffw0 6ce0a0d
Rust: Add example from the basic-query-for-rust-code.rst.
geoffw0 49aefe2
Rust: Add simple SQL injection example.
geoffw0 7b6e06e
Rust: Add simple constant password example.
geoffw0 7e3ab99
Rust: Add much more detailed code comments, since these are examples.
geoffw0 61481b5
Rust: Change note.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| - `CodeQL queries for Rust <https://github.com/github/codeql/tree/main/rust/ql/src>`__ | ||
| - `Example queries for Rust <https://github.com/github/codeql/tree/main/rust/ql/examples>`__ | ||
| - `CodeQL library reference for Rust <https://codeql.github.com/codeql-standard-libraries/rust/>`__ |
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 @@ | ||
| --- | ||
| dependencies: {} | ||
| compiled: false | ||
| lockVersion: 1.0.0 |
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 @@ | ||
| name: codeql/rust-examples | ||
| groups: | ||
| - rust | ||
| - examples | ||
| dependencies: | ||
| codeql/rust-all: ${workspace} | ||
| warnOnImplicitThis: true |
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,18 @@ | ||
| /** | ||
| * @name Empty 'if' statement | ||
| * @description Finds 'if' statements where the "then" branch is empty and no | ||
| * "else" branch exists. | ||
| * @id rust/examples/empty-if | ||
| * @tags example | ||
| */ | ||
|
|
||
| import rust | ||
|
|
||
| // find 'if' statements... | ||
| from IfExpr ifExpr | ||
| where | ||
| // where the 'then' branch is empty | ||
| ifExpr.getThen().(BlockExpr).getStmtList().getNumberOfStmtOrExpr() = 0 and | ||
| // and no 'else' branch exists | ||
| not exists(ifExpr.getElse()) | ||
| select ifExpr, "This 'if' expression is redundant." | ||
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 Constant password | ||
| * @description Finds places where a string literal is used in a function call | ||
| * argument that looks like a password. | ||
| * @id rust/examples/simple-constant-password | ||
| * @tags example | ||
| */ | ||
|
|
||
| import rust | ||
| import codeql.rust.dataflow.DataFlow | ||
| import codeql.rust.dataflow.TaintTracking | ||
|
|
||
| /** | ||
| * A data flow configuration for tracking flow from a string literal to a function | ||
| * call argument that looks like a password. For example: | ||
| * ``` | ||
| * fn set_password(password: &str) { ... } | ||
| * | ||
| * ... | ||
| * | ||
| * let pwd = "123456"; // source | ||
| * set_password(pwd); // sink (argument 0) | ||
| * ``` | ||
| */ | ||
| module ConstantPasswordConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node node) { | ||
| // `node` is a string literal | ||
| node.asExpr().getExpr() instanceof StringLiteralExpr | ||
| } | ||
|
|
||
| predicate isSink(DataFlow::Node node) { | ||
| // `node` is an argument whose corresponding parameter name matches the pattern "pass%" | ||
| exists(CallExpr call, Function target, int argIndex | | ||
| call.getStaticTarget() = target and | ||
| target.getParam(argIndex).getPat().(IdentPat).getName().getText().matches("pass%") and | ||
| call.getArg(argIndex) = node.asExpr().getExpr() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // instantiate the data flow configuration as a global taint tracking module | ||
| module ConstantPasswordFlow = TaintTracking::Global<ConstantPasswordConfig>; | ||
|
|
||
| // report flows from sources to sinks | ||
| from DataFlow::Node sourceNode, DataFlow::Node sinkNode | ||
| where ConstantPasswordFlow::flow(sourceNode, sinkNode) | ||
| select sinkNode, "The value $@ is used as a constant password.", sourceNode, sourceNode.toString() |
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,39 @@ | ||
| /** | ||
| * @name Database query built from user-controlled sources | ||
| * @description Finds places where a value from a remote or local user input | ||
| * is used as the first argument of a call to `sqlx_core::query::query`. | ||
| * @id rust/examples/simple-sql-injection | ||
| * @tags example | ||
| */ | ||
|
|
||
| import rust | ||
| import codeql.rust.dataflow.DataFlow | ||
| import codeql.rust.dataflow.TaintTracking | ||
| import codeql.rust.Concepts | ||
|
|
||
| /** | ||
| * A data flow configuration for tracking flow from a user input (threat model | ||
| * source) to the first argument of a call to `sqlx_core::query::query`. | ||
| */ | ||
| module SqlInjectionConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node node) { | ||
| // `node` is a user input (threat model source) | ||
| node instanceof ActiveThreatModelSource | ||
| } | ||
|
|
||
| predicate isSink(DataFlow::Node node) { | ||
| // `node` is the first argument of a call to `sqlx_core::query::query` | ||
| exists(CallExpr call | | ||
| call.getStaticTarget().getCanonicalPath() = "sqlx_core::query::query" and | ||
| call.getArg(0) = node.asExpr().getExpr() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // instantiate the data flow configuration as a global taint tracking module | ||
| module SqlInjectionFlow = TaintTracking::Global<SqlInjectionConfig>; | ||
|
|
||
| // report flows from sources to sinks | ||
| from DataFlow::Node sourceNode, DataFlow::Node sinkNode | ||
| where SqlInjectionFlow::flow(sourceNode, sinkNode) | ||
| select sinkNode, "This query depends on a $@.", sourceNode, "user-provided value" |
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 three example queries (`rust/examples/empty-if`, `rust/examples/simple-sql-injection` and `rust/examples/simple-constant-password`) to help developers learn to write CodeQL queries for Rust. |
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.
Check warning
Code scanning / CodeQL
Redundant cast Warning