This is an automated email from the ASF dual-hosted git repository.
papegaaij pushed a commit to branch wicket-10.x
in repository https://gitbox.apache.org/repos/asf/wicket.git
The following commit(s) were added to refs/heads/wicket-10.x by this push:
new 6b501b6fe4 fix quadratic backtracking in UrlValidator authority pattern
6b501b6fe4 is described below
commit 6b501b6fe4761294af8b61ad298b0e25f7ee4df6
Author: obsidian_nomad <[email protected]>
AuthorDate: Sat Oct 31 00:56:37 2026 +0530
fix quadratic backtracking in UrlValidator authority pattern
The userinfo part of AUTHORITY_PATTERN was (.+(:.*)?@)?. The outer .+ and
the inner (:.*)? are both greedy and overlap, so when the authority contains
no '@' the matcher explores every way of splitting the two quantifiers
before
failing. isValidAuthority receives the authority straight from the submitted
URL, so an input like http:// followed by tens of thousands of colons drives
that into quadratic work: ~32KB of colons takes tens of seconds, and it
scales
O(n^2). A UrlValidator is commonly attached to a public URL field, so a
handful
of such requests exhaust a request thread each.
The inner (:.*)? is redundant: .+ already matches ':' and every other
character, and the group is never read. Rewriting the userinfo as (.+@)?
matches the exact same strings and captures the same host, port and extra
groups, while reducing userinfo matching to a single linear scan. The host,
port and extra group indices move from 3/4/5 to 2/3/4 to match.
Applications relying on the old behaviour see no change: the set of accepted
URLs and the parsed host and port are identical.
(cherry picked from commit 15fcacc9f6c4416c7f6351cbddb2f4fba3a07b48)
---
.../wicket/validation/validator/UrlValidatorTest.java | 17 +++++++++++++++++
.../wicket/validation/validator/UrlValidator.java | 8 ++++----
2 files changed, 21 insertions(+), 4 deletions(-)
diff --git
a/wicket-core-tests/src/test/java/org/apache/wicket/validation/validator/UrlValidatorTest.java
b/wicket-core-tests/src/test/java/org/apache/wicket/validation/validator/UrlValidatorTest.java
index 07e4788bf3..206bc3034d 100644
---
a/wicket-core-tests/src/test/java/org/apache/wicket/validation/validator/UrlValidatorTest.java
+++
b/wicket-core-tests/src/test/java/org/apache/wicket/validation/validator/UrlValidatorTest.java
@@ -17,8 +17,12 @@
package org.apache.wicket.validation.validator;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.time.Duration;
+
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -226,6 +230,19 @@ class UrlValidatorTest
assertTrue(urlVal.isValid("http://user:password@host:80/path"));
}
+ /**
+ * An authority made of nothing but colons used to make the authority
pattern backtrack
+ * quadratically, so a submitted value of a few tens of kilobytes cost
tens of seconds of CPU
+ * in {@link UrlValidator#isValidAuthority(String)}.
+ */
+ @Test
+ void authorityWithManyColonsIsRejectedQuickly()
+ {
+ UrlValidator urlVal = new UrlValidator(null,
UrlValidator.ALLOW_ALL_SCHEMES);
+ String url = "http://" + ":".repeat(50_000);
+ assertTimeoutPreemptively(Duration.ofSeconds(2), () ->
assertFalse(urlVal.isValid(url)));
+ }
+
/**
*
* @param testPartsIndex
diff --git
a/wicket-core/src/main/java/org/apache/wicket/validation/validator/UrlValidator.java
b/wicket-core/src/main/java/org/apache/wicket/validation/validator/UrlValidator.java
index 37b8ebc15c..d1af179300 100644
---
a/wicket-core/src/main/java/org/apache/wicket/validation/validator/UrlValidator.java
+++
b/wicket-core/src/main/java/org/apache/wicket/validation/validator/UrlValidator.java
@@ -109,12 +109,12 @@ public class UrlValidator implements IValidator<String>
*/
private static final String SCHEME_PATTERN = "^[" + SCHEME_CHARS +
"].*$";
- private static final String AUTHORITY_PATTERN = "^(.+(:.*)?@)?([" +
AUTHORITY_CHARS +
+ private static final String AUTHORITY_PATTERN = "^(.+@)?([" +
AUTHORITY_CHARS +
"]*)(:\\d*)?(.*)?";
- private static final int PARSE_AUTHORITY_HOST_IP = 3;
- private static final int PARSE_AUTHORITY_PORT = 4;
- private static final int PARSE_AUTHORITY_EXTRA = 5; // Should always be
empty.
+ private static final int PARSE_AUTHORITY_HOST_IP = 2;
+ private static final int PARSE_AUTHORITY_PORT = 3;
+ private static final int PARSE_AUTHORITY_EXTRA = 4; // Should always be
empty.
private static final String PATH_PATTERN =
"^(/[-\\w:@&?=+,.!/~*'%$_;\\(\\)]*)?$";