GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Java: Promote regex injection query from experimental by jcogs33 · Pull Request #11070 · github/codeql

Java: Promote regex injection query from experimental #11070

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.

Sign up for GitHub

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
jcogs33 merged 23 commits into github:main from jcogs33:java-regex-injection
Nov 21, 2022

Uh oh!

There was an error while loading. Please reload this page.

Merged

Java: Promote regex injection query from experimental #11070

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
32b1400
move files out of experimental
Oct 24, 2022
25436fe
update options and qlref files
Oct 24, 2022
833c5ed
move to .qll file and switch to InlineExpectations tests
Oct 24, 2022
6545cff
add Pattern.quote sanitizer
Oct 25, 2022
6ba7449
adjust imports
Oct 27, 2022
037a05c
add classes for Pattern, Matcher, and RegExUtils
Oct 27, 2022
f6f26fe
refactor code; add change note
Oct 31, 2022
50d638d
create RegexInjection.qll file
Oct 31, 2022
91491d9
refactor into more classes; add more test cases; add LITERAL sanitizer
Oct 31, 2022
5b089bb
split sanitizer into three
Nov 1, 2022
81ad10b
update sink names
Nov 1, 2022
eb30e8f
move Pattern.quote and Pattern.LITERAL models to Regex.qll
Nov 1, 2022
32f7348
update help file
Nov 1, 2022
5dcd3b2
clean up files
Nov 1, 2022
be548c1
switch sink to use csv models
Nov 3, 2022
5402001
remove original sanitizer
Nov 3, 2022
695d6f0
move files to regexp directory
Nov 3, 2022
0e93e71
update tests
Nov 3, 2022
e49c521
update change note
Nov 3, 2022
b99a1d2
update sink and tests
Nov 4, 2022
bada986
apply review comments
Nov 8, 2022
13decd3
update sink
Nov 8, 2022
9e2ec9d
apply docs review suggestion
Nov 21, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

21 changes: 21 additions & 0 deletions 21 java/ql/lib/semmle/code/java/frameworks/Regex.qll
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

private import semmle.code.java.dataflow.ExternalFlow

/** The class `java.util.regex.Pattern`. */
class TypeRegexPattern extends Class {
TypeRegexPattern() { this.hasQualifiedName("java.util.regex", "Pattern") }
}

/** The `quote` method of the `java.util.regex.Pattern` class. */
class PatternQuoteMethod extends Method {
PatternQuoteMethod() {
this.getDeclaringType() instanceof TypeRegexPattern and
this.hasName("quote")
}
}

/** The `LITERAL` field of the `java.util.regex.Pattern` class. */
class PatternLiteralField extends Field {
PatternLiteralField() {
this.getDeclaringType() instanceof TypeRegexPattern and
this.hasName("LITERAL")
}
}

private class RegexModel extends SummaryModelCsv {
override predicate row(string s) {
s =
Expand Down
6 changes: 6 additions & 0 deletions 6 java/ql/lib/semmle/code/java/regex/RegexFlowModels.qll
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ private class RegexSinkCsv extends SinkModelCsv {
"com.google.common.base;Splitter;false;split;(CharSequence);;Argument[-1];regex-use[0];manual",
"com.google.common.base;Splitter;false;splitToList;(CharSequence);;Argument[-1];regex-use[0];manual",
"com.google.common.base;Splitter$MapSplitter;false;split;(CharSequence);;Argument[-1];regex-use[0];manual",
"org.apache.commons.lang3;RegExUtils;false;removeAll;(String,String);;Argument[1];regex-use;manual",
"org.apache.commons.lang3;RegExUtils;false;removeFirst;(String,String);;Argument[1];regex-use;manual",
"org.apache.commons.lang3;RegExUtils;false;removePattern;(String,String);;Argument[1];regex-use;manual",
"org.apache.commons.lang3;RegExUtils;false;replaceAll;(String,String,String);;Argument[1];regex-use;manual",
"org.apache.commons.lang3;RegExUtils;false;replaceFirst;(String,String,String);;Argument[1];regex-use;manual",
"org.apache.commons.lang3;RegExUtils;false;replacePattern;(String,String,String);;Argument[1];regex-use;manual",
]
}
}
48 changes: 48 additions & 0 deletions 48 java/ql/lib/semmle/code/java/security/regexp/RegexInjection.qll
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/** Provides classes and predicates related to regex injection in Java. */

import java
private import semmle.code.java.dataflow.DataFlow
private import semmle.code.java.frameworks.Regex
private import semmle.code.java.regex.RegexFlowModels

/** A data flow sink for untrusted user input used to construct regular expressions. */
abstract class RegexInjectionSink extends DataFlow::ExprNode { }

/** A sanitizer for untrusted user input used to construct regular expressions. */
abstract class RegexInjectionSanitizer extends DataFlow::ExprNode { }

/** A method call that takes a regular expression as an argument. */
private class DefaultRegexInjectionSink extends RegexInjectionSink {
DefaultRegexInjectionSink() {
// we only select sinks where there is direct regex creation, not regex uses
sinkNode(this, ["regex-use[]", "regex-use[f1]", "regex-use[f-1]", "regex-use[-1]", "regex-use"])
}
}

/**
* A call to the `Pattern.quote` method, which gives metacharacters or escape sequences
* no special meaning.
*/
private class PatternQuoteCall extends RegexInjectionSanitizer {
PatternQuoteCall() {
exists(MethodAccess ma, Method m | m = ma.getMethod() |
ma.getArgument(0) = this.asExpr() and
m instanceof PatternQuoteMethod
)
}
}

/**
* Use of the `Pattern.LITERAL` flag with `Pattern.compile`, which gives metacharacters
* or escape sequences no special meaning.
*/
private class PatternLiteralFlag extends RegexInjectionSanitizer {
PatternLiteralFlag() {
exists(MethodAccess ma, Method m, PatternLiteralField field | m = ma.getMethod() |
ma.getArgument(0) = this.asExpr() and
m.getDeclaringType() instanceof TypeRegexPattern and
m.hasName("compile") and
ma.getArgument(1) = field.getAnAccess()
)
}
}
17 changes: 17 additions & 0 deletions 17 java/ql/lib/semmle/code/java/security/regexp/RegexInjectionQuery.qll
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** Provides configurations to be used in queries related to regex injection. */

import java
import semmle.code.java.dataflow.FlowSources
import semmle.code.java.dataflow.TaintTracking
import semmle.code.java.security.regexp.RegexInjection

/** A taint-tracking configuration for untrusted user input used to construct regular expressions. */
class RegexInjectionConfiguration extends TaintTracking::Configuration {
RegexInjectionConfiguration() { this = "RegexInjection" }

override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }

override predicate isSink(DataFlow::Node sink) { sink instanceof RegexInjectionSink }

override predicate isSanitizer(DataFlow::Node node) { node instanceof RegexInjectionSanitizer }
}
22 changes: 22 additions & 0 deletions 22 java/ql/src/Security/CWE/CWE-730/RegexInjection.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.regex.Pattern;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

public class RegexInjectionDemo extends HttpServlet {

public boolean badExample(javax.servlet.http.HttpServletRequest request) {
String regex = request.getParameter("regex");
String input = request.getParameter("input");

// BAD: Unsanitized user input is used to construct a regular expression
return input.matches(regex);
}

public boolean goodExample(javax.servlet.http.HttpServletRequest request) {
String regex = request.getParameter("regex");
String input = request.getParameter("input");

// GOOD: User input is sanitized before constructing the regex
return input.matches(Pattern.quote(regex));
}
}
13 changes: 8 additions & 5 deletions 13 ...Security/CWE/CWE-730/RegexInjection.qhelp → ...Security/CWE/CWE-730/RegexInjection.qhelp
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ perform a Denial of Service attack.
<recommendation>
<p>
Before embedding user input into a regular expression, use a sanitization function
to escape meta-characters that have special meaning.
such as <code>Pattern.quote</code> to escape meta-characters that have special meaning.
</p>
</recommendation>

<example>
<p>
The following example shows a HTTP request parameter that is used to construct a regular expression:
The following example shows an HTTP request parameter that is used to construct a regular expression.
</p>
<sample src="RegexInjection.java" />
<p>
In the first case the user-provided regex is not escaped.
If a malicious user provides a regex that has exponential worst case performance,
If a malicious user provides a regex whose worst-case performance is exponential,
then this could lead to a Denial of Service.
</p>
<p>
In the second case, the user input is escaped using <code>escapeSpecialRegexChars</code> before being included
In the second case, the user input is escaped using <code>Pattern.quote</code> before being included
in the regular expression. This ensures that the user cannot insert characters which have a special
meaning in regular expressions.
</p>
<sample src="RegexInjection.java" />
</example>

<references>
Expand All @@ -44,5 +44,8 @@ OWASP:
<li>
Wikipedia: <a href="https://en.wikipedia.org/wiki/ReDoS">ReDoS</a>.
</li>
<li>
Java API Specification: <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html#quote(java.lang.String)">Pattern.quote</a>.
</li>
</references>
</qhelp>
23 changes: 23 additions & 0 deletions 23 java/ql/src/Security/CWE/CWE-730/RegexInjection.ql
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @name Regular expression injection
* @description User input should not be used in regular expressions without first being escaped,
* otherwise a malicious user may be able to provide a regex that could require
* exponential time on certain inputs.
* @kind path-problem
* @problem.severity error
* @security-severity 7.5
* @precision high
* @id java/regex-injection
* @tags security
* external/cwe/cwe-730
* external/cwe/cwe-400
*/

import java
import semmle.code.java.security.regexp.RegexInjectionQuery
import DataFlow::PathGraph

from DataFlow::PathNode source, DataFlow::PathNode sink, RegexInjectionConfiguration c
where c.hasFlowPath(source, sink)
select sink.getNode(), source, sink, "This regular expression is constructed from a $@.",
source.getNode(), "user-provided value"
4 changes: 4 additions & 0 deletions 4 java/ql/src/change-notes/2022-11-03-regex-injection.md
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: newQuery
---
* The query, `java/regex-injection`, has been promoted from experimental to the main query pack. Its results will now appear by default. This query was originally [submitted as an experimental query by @edvraa](https://github.com/github/codeql/pull/5704).
38 changes: 0 additions & 38 deletions 38 java/ql/src/experimental/Security/CWE/CWE-730/RegexInjection.java
Show comments
View file
Open in desktop

This file was deleted.

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

89 changes: 0 additions & 89 deletions 89 java/ql/src/experimental/Security/CWE/CWE-730/RegexInjection.ql
Show comments
View file
Open in desktop

This file was deleted.

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.