Skip to content

Commit 4ac103a

Browse files
Treat prefix literally in UrlTemplate.match(url, prefix) (#17713)
* Treat prefix literally in UrlTemplate.match(url, prefix) match(url, prefix) strips the prefix with matchAgainst.replaceFirst(prefix, ""), which interprets the prefix as a regular expression. A prefix that contains regex metacharacters (for example an unmatched ( ) throws PatternSyntaxException, or silently strips the wrong text. Quote the prefix with Pattern.quote so it is matched literally. * Complete Javadoc for UrlTemplate.match(matchAgainst, prefix) --------- Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
1 parent 78bbfff commit 4ac103a

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

java/src/org/openqa/selenium/remote/http/UrlTemplate.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,21 @@ public UrlTemplate(String template) {
141141
}
142142

143143
/**
144-
* @return A {@link Match} with all parameters filled if successful, null otherwise. Remove
145-
* subPath from matchAgainst before matching.
144+
* Matches a URL against this template after stripping a leading prefix.
145+
*
146+
* <p>The {@code prefix} is treated literally (not as a regex) and stripped from the start of
147+
* {@code matchAgainst} before matching, unless it is empty or {@code "/"}.
148+
*
149+
* @param matchAgainst the URL (or path) to match against this template
150+
* @param prefix a literal prefix to strip from {@code matchAgainst} before matching
151+
* @return a {@link Match} with all parameters filled if successful, {@code null} otherwise
146152
*/
147153
public UrlTemplate.@Nullable Match match(@Nullable String matchAgainst, @Nullable String prefix) {
148154
if (matchAgainst == null || prefix == null) {
149155
return null;
150156
}
151157
if (!prefix.isEmpty() && !prefix.equals("/")) {
152-
matchAgainst = matchAgainst.replaceFirst(prefix, "");
158+
matchAgainst = matchAgainst.replaceFirst(Pattern.quote(prefix), "");
153159
}
154160
return match(matchAgainst);
155161
}

java/test/org/openqa/selenium/remote/http/UrlTemplateTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,15 @@ void noPartialMatches() {
9696
assertThat(new UrlTemplate("/session").match("/session-no")).isNull();
9797
assertThat(new UrlTemplate("/session").match("/no-session-no")).isNull();
9898
}
99+
100+
@Test
101+
void shouldNotThrowWhenPrefixContainsRegexMetacharacters() {
102+
// The prefix contains '(', a regex metacharacter; stripping it must treat it literally
103+
// rather than as a pattern (which would throw PatternSyntaxException).
104+
UrlTemplate.Match match =
105+
new UrlTemplate("/session/{id}").match("/wd(hub/session/1234", "/wd(hub");
106+
107+
assertThat(match).isNotNull();
108+
assertThat(match.getParameters()).containsExactlyInAnyOrderEntriesOf(Map.of("id", "1234"));
109+
}
99110
}

0 commit comments

Comments
 (0)