This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git
The following commit(s) were added to refs/heads/master by this push:
new 035d181 feat(SLING-13162): [Code Quality] High: The regex constant
PATH_PARAMETERS_REGEX contains a typo (#80)
035d181 is described below
commit 035d181099461a3917fb5d850d8e6abaae0c5183
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Mon Jun 1 09:25:32 2026 +0200
feat(SLING-13162): [Code Quality] High: The regex constant
PATH_PARAMETERS_REGEX contains a typo (#80)
* feat: address SLING-13162
Co-authored-by: Maia <maia@noreply>
* SLING-13162 widen path parameter key regex and reuse compiled patterns
Co-authored-by: Copilot <[email protected]>
---------
Co-authored-by: Maia <maia@noreply>
Co-authored-by: Copilot <[email protected]>
---
.../org/apache/sling/api/uri/SlingUriBuilder.java | 14 +++++++-------
.../org/apache/sling/api/uri/SlingUriTest.java | 22 ++++++++++++++++++++++
2 files changed, 29 insertions(+), 7 deletions(-)
diff --git a/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
b/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
index ac48dd1..21e8459 100644
--- a/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
+++ b/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
@@ -82,9 +82,12 @@ public class SlingUriBuilder {
static final String CHAR_DOT = ".";
static final String CHAR_SLASH = "/";
static final String SELECTOR_DOT_REGEX = "\\.(?!\\.?/)"; // (?!\\.?/) to
avoid matching ./ and ../
- static final String PATH_PARAMETERS_REGEX =
";([a-zA-z0-9]+)=(?:\\'([^']*)\\'|([^/]+))";
+ static final String PATH_PARAMETERS_REGEX =
";([a-zA-Z0-9._-]+)=(?:\\'([^']*)\\'|([^/]+))";
static final String BEST_EFFORT_INVALID_URI_MATCHER =
"^(?:([^:#@]+):)?(?://(?:([^@#]+)@)?([^/#:]+)(?::([0-9]+))?)?(?:([^?#]+))?(?:\\?([^#]*))?(?:#(.*))?$";
+ private static final Pattern SELECTOR_DOT_PATTERN =
Pattern.compile(SELECTOR_DOT_REGEX);
+ private static final Pattern PATH_PARAMETERS_PATTERN =
Pattern.compile(PATH_PARAMETERS_REGEX);
+ private static final Pattern BEST_EFFORT_INVALID_URI_PATTERN =
Pattern.compile(BEST_EFFORT_INVALID_URI_MATCHER);
/**
* Creates a builder without any URI parameters set.
@@ -264,7 +267,7 @@ public class SlingUriBuilder {
}
private static SlingUriBuilder parseBestEffort(String uriStr,
ResourceResolver resourceResolver) {
- Matcher matcher =
Pattern.compile(BEST_EFFORT_INVALID_URI_MATCHER).matcher(uriStr);
+ Matcher matcher = BEST_EFFORT_INVALID_URI_PATTERN.matcher(uriStr);
matcher.find();
String scheme = matcher.group(1);
@@ -392,8 +395,7 @@ public class SlingUriBuilder {
if (path != null && path.startsWith(SlingUriBuilder.CHAR_SLASH) &&
resourceResolver != null) {
setResourcePath(path);
rebaseResourcePath();
- } else if (path != null
- && (dotMatcher =
Pattern.compile(SELECTOR_DOT_REGEX).matcher(path)).find()) {
+ } else if (path != null && (dotMatcher =
SELECTOR_DOT_PATTERN.matcher(path)).find()) {
int firstDotPosition = dotMatcher.start();
setPathWithDefinedResourcePosition(path, firstDotPosition);
} else {
@@ -1018,10 +1020,8 @@ public class SlingUriBuilder {
// we rebuild the parameters from scratch as given in path (if path is
set to null we also reset)
pathParameters.clear();
if (path != null) {
- Pattern pathParameterRegex =
Pattern.compile(PATH_PARAMETERS_REGEX);
-
StringBuffer resultString = null;
- Matcher regexMatcher = pathParameterRegex.matcher(path);
+ Matcher regexMatcher = PATH_PARAMETERS_PATTERN.matcher(path);
while (regexMatcher.find()) {
if (resultString == null) {
resultString = new StringBuffer();
diff --git a/src/test/java/org/apache/sling/api/uri/SlingUriTest.java
b/src/test/java/org/apache/sling/api/uri/SlingUriTest.java
index 9a60d45..9783561 100644
--- a/src/test/java/org/apache/sling/api/uri/SlingUriTest.java
+++ b/src/test/java/org/apache/sling/api/uri/SlingUriTest.java
@@ -462,6 +462,28 @@ public class SlingUriTest {
asList(resolver, null));
}
+ @Test
+ public void testAbsolutePathWithInvalidPathParameterKeyCharacters() {
+ SlingUri uriWithBracketInKey =
SlingUriBuilder.parse("/test/to/path;foo[='bar'.sel1.html", null)
+ .build();
+ assertTrue(uriWithBracketInKey.getPathParameters().isEmpty());
+
+ SlingUri uriWithBackslashInKey =
SlingUriBuilder.parse("/test/to/path;foo\\='bar'.sel1.html", null)
+ .build();
+ assertTrue(uriWithBackslashInKey.getPathParameters().isEmpty());
+ }
+
+ @Test
+ public void testAbsolutePathWithPathParameterKeySpecialCharacters() {
+ SlingUri uri =
SlingUriBuilder.parse("/test/to/path;foo.bar='bar';foo_bar='baz';foo-bar='qux'.sel1.html",
null)
+ .build();
+
+ assertEquals(3, uri.getPathParameters().size());
+ assertEquals("bar", uri.getPathParameters().get("foo.bar"));
+ assertEquals("baz", uri.getPathParameters().get("foo_bar"));
+ assertEquals("qux", uri.getPathParameters().get("foo-bar"));
+ }
+
@Test
public void testAbsolutePathWithPathParameterAfterExtension() {
String testUriStr =
"/test/to/path.sel1.html;v='1.0'/suffix/path?p1=2&p2=3#frag3939";