Skip to content

Commit 206b582

Browse files
authored
Merge pull request #11304 from grails/issue-11271
issue-11271
2 parents 888f0a1 + a054f49 commit 206b582

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

grails-web-common/src/main/groovy/org/grails/web/json/JSONTokener.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,8 @@ public void skipPast(String to) {
467467
* @return A JSONException object, suitable for throwing
468468
*/
469469
public JSONException syntaxError(String message) {
470-
return new JSONException(message + toString());
470+
471+
return new JSONException(message + toRegexSafeString());
471472
}
472473

473474

@@ -480,4 +481,26 @@ public JSONException syntaxError(String message) {
480481
public String toString() {
481482
return " at character " + this.myIndex + " of " + this.mySource;
482483
}
484+
485+
/**
486+
* Make a regex safe printable string of this JSONTokener.
487+
*
488+
* @return " at character [this.myIndex] of [this.mySource]"
489+
*/
490+
public String toRegexSafeString() {
491+
int endIndex = mySource.length();
492+
boolean appendDots = false;
493+
if (endIndex > 20) {
494+
// only show first 20 characters of source to prevent reDOS attacks, especially in Java 8 regexp engine
495+
// see https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS for more info
496+
endIndex = 19;
497+
appendDots = true;
498+
}
499+
StringBuffer output = new StringBuffer(" at character " + this.myIndex + " of " + this.mySource.substring(0, endIndex));
500+
if (appendDots) {
501+
output.append("...");
502+
}
503+
return Matcher.quoteReplacement(output.toString());
504+
}
505+
483506
}

0 commit comments

Comments
 (0)