Treat prefix literally in UrlTemplate.match(url, prefix) - #17713
Conversation
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.
PR Summary by QodoFix UrlTemplate.match prefix stripping to treat prefix literally Description
Diagram
High-Level Assessment
Files changed (2)
|
Code Review by Qodo
Context used✅ Compliance rules (platform):
18 rules 1.
|
|
Code review by qodo was updated up to the latest commit db683a8 |
|
Code review by qodo was updated up to the latest commit 0f0b54e |
diemol
left a comment
There was a problem hiding this comment.
Thank you, @vasiliy-mikhailov!
Problem
UrlTemplate.match(url, prefix)strips the prefix with:String.replaceFirsttreats its first argument as a regular expression, so a prefix containing regex metacharacters is mis-handled. For example a prefix with an unmatched(throwsPatternSyntaxException; other metacharacters can silently strip the wrong text.Fix
Quote the prefix with
Pattern.quote(prefix)so it is matched literally.Test
Adds
shouldNotThrowWhenPrefixContainsRegexMetacharacterstoUrlTemplateTest: matching with a prefix containing(throwsPatternSyntaxExceptionontrunkand succeeds with this change.