Skip to content

Commit d300fad

Browse files
author
drighetto
committed
Implement ReDOS related methods #1
1 parent 87159bd commit d300fad

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/main/java/eu/righettod/SecurityUtils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,28 +1292,30 @@ public static boolean applyJWTExtraValidation(DecodedJWT token, TokenType expect
12921292
/**
12931293
* Apply a validations on a regular expression to ensure that is not prone to the ReDOS attack.
12941294
* <br>If your technology is supported by <a href="https://github.com/doyensec/regexploit">regexploit</a> then <b>use it instead of this method!</b>
1295-
* <br>Indeed, the <a href="https://www.doyensec.com/">doyensec</a> team has made an intensive and amazing work on this topic.
1295+
* <br>Indeed, the <a href="https://www.doyensec.com/">Doyensec</a> team has made an intensive and amazing work on this topic and created this effective tool.
12961296
*
1297-
* @param regex String expected to be a valid regular expression.
1297+
* @param regex String expected to be a valid regular expression (regex).
12981298
* @param data Test data on which the regular expression is executed for the test.
12991299
* @param maximumRunningTimeInSeconds Optional parameter to specify a number of seconds above which a regex execution time is considered as not safe (default to 4 seconds when not specified).
13001300
* @return True only if the string pass all validations.
13011301
* @see "https://github.blog/security/how-to-fix-a-redos/"
1302-
* @see "https://learn.snyk.io/lesson/redos/?ecosystem=javascript"
1302+
* @see "https://learn.snyk.io/lesson/redos"
13031303
* @see "https://rules.sonarsource.com/java/RSPEC-2631/"
13041304
* @see "https://github.com/doyensec/regexploit"
13051305
* @see "https://wiki.owasp.org/images/2/23/OWASP_IL_2009_ReDoS.pdf"
13061306
* @see "https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS"
13071307
*/
13081308
public static boolean isRegexSafe(String regex, String data, Optional<Integer> maximumRunningTimeInSeconds) {
1309+
Objects.requireNonNull(maximumRunningTimeInSeconds, "Use 'Optional.empty()' to leverage the default value.");
1310+
Objects.requireNonNull(data, "A sample data is needed to perform the test.");
1311+
Objects.requireNonNull(regex, "A regular expression is needed to perform the test.");
13091312
boolean isSafe = false;
1310-
final String testData = (data != null) ? data : "";
13111313
int executionTimeout = maximumRunningTimeInSeconds.orElse(4);
13121314
ExecutorService executor = Executors.newSingleThreadExecutor();
13131315
try {
13141316
Callable<Boolean> task = () -> {
13151317
Pattern pattern = Pattern.compile(regex);
1316-
return pattern.matcher(testData).matches();
1318+
return pattern.matcher(data).matches();
13171319
};
13181320
List<Future<Boolean>> tasks = executor.invokeAll(List.of(task), executionTimeout, TimeUnit.SECONDS);
13191321
if (!tasks.getFirst().isCancelled()) {

0 commit comments

Comments
 (0)