-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Java: Add and use RegexExecution concept #21310
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
owen-mc
wants to merge
6
commits into
github:main
Choose a base branch
from
owen-mc:java/regex-execution
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.
+178
−100
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e6dbd52
Add `RegexExecution` in `Concepts.qll`
owen-mc 44eeee5
Add and improve classes for regex-related methods
owen-mc fa3fba4
Use new regex-related classes (no functional change)
owen-mc a22fd39
Use RegexExecution in sanitizer definitions (expands scope)
owen-mc 1ee5728
Add missing QLDoc
owen-mc 6a8204d
"dataflow" -> "data flow" in QLDoc
owen-mc 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,88 @@ | ||
| /** | ||
| * Provides abstract classes representing generic concepts such as file system | ||
| * access or system command execution, for which individual framework libraries | ||
| * provide concrete subclasses. | ||
| */ | ||
| overlay[local?] | ||
| module; | ||
|
|
||
| import java | ||
| private import semmle.code.java.dataflow.DataFlow | ||
|
|
||
| /** | ||
| * A data-flow node that executes a regular expression. | ||
| * | ||
| * Extend this class to refine existing API models. If you want to model new APIs, | ||
| * extend `RegexExecution::Range` instead. | ||
| */ | ||
| class RegexExecution extends DataFlow::Node instanceof RegexExecution::Range { | ||
| /** Gets the data flow node for the regex being executed by this node. */ | ||
| DataFlow::Node getRegex() { result = super.getRegex() } | ||
|
|
||
| /** Gets a data flow node for the string to be searched or matched against. */ | ||
| DataFlow::Node getString() { result = super.getString() } | ||
|
|
||
| /** | ||
| * Gets the name of this regex execution, typically the name of an executing method. | ||
| * This is used for nice alert messages and should include the module if possible. | ||
| */ | ||
| string getName() { result = super.getName() } | ||
| } | ||
|
|
||
| /** Provides classes for modeling new regular-expression execution APIs. */ | ||
| module RegexExecution { | ||
| /** | ||
| * A data flow node that executes a regular expression. | ||
| * | ||
| * Extend this class to model new APIs. If you want to refine existing API models, | ||
| * extend `RegexExecution` instead. | ||
| */ | ||
| abstract class Range extends DataFlow::Node { | ||
| /** Gets the data flow node for the regex being executed by this node. */ | ||
| abstract DataFlow::Node getRegex(); | ||
|
|
||
| /** Gets a data flow node for the string to be searched or matched against. */ | ||
| abstract DataFlow::Node getString(); | ||
|
|
||
| /** | ||
| * Gets the name of this regex execution, typically the name of an executing method. | ||
| * This is used for nice alert messages and should include the module if possible. | ||
| */ | ||
| abstract string getName(); | ||
| } | ||
|
|
||
| private class RangeFromExpr extends Range { | ||
| private RegexExecutionExpr::Range ree; | ||
|
|
||
| RangeFromExpr() { this.asExpr() = ree } | ||
|
|
||
| override DataFlow::Node getRegex() { result.asExpr() = ree.getRegex() } | ||
|
|
||
| override DataFlow::Node getString() { result.asExpr() = ree.getString() } | ||
|
|
||
| override string getName() { result = ree.getName() } | ||
| } | ||
| } | ||
|
|
||
| /** Provides classes for modeling new regular-expression execution APIs. */ | ||
| module RegexExecutionExpr { | ||
| /** | ||
| * An expression that executes a regular expression. | ||
| * | ||
| * Extend this class to model new APIs. If you want to refine existing API models, | ||
| * extend `RegexExecution` instead. | ||
| */ | ||
| abstract class Range extends Expr { | ||
| /** Gets the expression for the regex being executed by this node. */ | ||
| abstract Expr getRegex(); | ||
|
|
||
| /** Gets a expression for the string to be searched or matched against. */ | ||
| abstract Expr getString(); | ||
|
|
||
| /** | ||
| * Gets the name of this regex execution, typically the name of an executing method. | ||
| * This is used for nice alert messages and should include the module if possible. | ||
| */ | ||
| abstract string getName(); | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ overlay[local?] | |
| module; | ||
|
|
||
| import java | ||
| private import semmle.code.java.dataflow.DataFlow | ||
|
|
||
| /** The class `java.util.regex.Matcher`. */ | ||
| class TypeRegexMatcher extends Class { | ||
|
|
@@ -24,6 +25,16 @@ class TypeRegexPattern extends Class { | |
| TypeRegexPattern() { this.hasQualifiedName("java.util.regex", "Pattern") } | ||
| } | ||
|
|
||
| /** | ||
| * The `compile` method of `java.util.regex.Pattern`. | ||
| */ | ||
| class PatternCompileMethod extends Method { | ||
| PatternCompileMethod() { | ||
| this.getDeclaringType() instanceof TypeRegexPattern and | ||
| this.hasName("compile") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The `matches` method of `java.util.regex.Pattern`. | ||
| */ | ||
|
|
@@ -59,3 +70,53 @@ class PatternLiteralField extends Field { | |
| this.hasName("LITERAL") | ||
| } | ||
| } | ||
|
|
||
| /** A call to the `compile` method of `java.util.regex.Pattern` */ | ||
| class PatternCompileCall extends MethodCall { | ||
| PatternCompileCall() { this.getMethod() instanceof PatternCompileMethod } | ||
| } | ||
|
|
||
| /** A call to the `matcher` method of `java.util.regex.Pattern` */ | ||
| class PatternMatcherCall extends MethodCall { | ||
| PatternMatcherCall() { this.getMethod() instanceof PatternMatcherMethod } | ||
| } | ||
|
|
||
| /** A call to the `matches` method of `java.util.regex.Pattern` */ | ||
| class PatternMatchesCall extends MethodCall, RegexExecutionExpr::Range { | ||
|
Comment on lines
+74
to
+85
|
||
| PatternMatchesCall() { this.getMethod() instanceof PatternMatchesMethod } | ||
|
|
||
| override Expr getRegex() { result = this.getArgument(0) } | ||
|
|
||
| override Expr getString() { result = this.getArgument(1) } | ||
|
|
||
| override string getName() { result = "Pattern.matches" } | ||
| } | ||
|
|
||
| /** A call to the `matches` method of `java.util.regex.Matcher` */ | ||
| class MatcherMatchesCall extends MethodCall, RegexExecutionExpr::Range { | ||
| MatcherMatchesCall() { this.getMethod() instanceof MatcherMatchesMethod } | ||
|
|
||
| /** | ||
| * Get the call to `java.util.regex.Pattern.matcher` which returned the | ||
| * qualifier of this call. This is needed to determine the string being | ||
| * matched. | ||
| */ | ||
| PatternMatcherCall getPatternMatcherCall() { | ||
| DataFlow::localExprFlow(result, this.getQualifier()) | ||
| } | ||
|
|
||
| /** | ||
| * Get the call to `java.util.regex.Pattern.compile` which returned the | ||
| * `Pattern` used by this matcher. This is needed to determine the regular | ||
| * expression being used. | ||
| */ | ||
| PatternCompileCall getPatternCompileCall() { | ||
| DataFlow::localExprFlow(result, this.getPatternMatcherCall()) | ||
| } | ||
|
|
||
| override Expr getRegex() { result = this.getPatternCompileCall().getArgument(0) } | ||
|
|
||
| override Expr getString() { result = this.getPatternMatcherCall().getArgument(0) } | ||
|
|
||
| override string getName() { result = "Matcher.matches" } | ||
| } | ||
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 was deleted.
Oops, something went wrong.
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.
Grammar: "Gets a expression" should be "Gets an expression".