[type: bug] Fix RewritePlugin placeholder drops first character and StringIndexOutOfBounds (#6883) - #7013
Merged
Conversation
…tringIndexOutOfBounds (apache#6883) Signed-off-by: zhang-arvin <arvin.zhang@htx-inc.com>
Contributor
|
pls add my wechat: aias00 |
Aias00
approved these changes
Sep 1, 2026
Aias00
left a comment
Contributor
There was a problem hiding this comment.
Review: Fix RewritePlugin placeholder drops first character (#6883)
Verdict: APPROVE
Analysis
The rewrite placeholder substitution uses PathMatchUtils.replaceAll, which performs a literal replacement:
public static String replaceAll(final String path, final String regex, final String replacement) {
return path.replaceAll(Pattern.quote(regex), Matcher.quoteReplacement(replacement));
}The first arg is the template (getReplace()), the second is the literal placeholder segment extracted from getRegex() (getRegex().substring(indexOf("{"))), and the third is the actual value extracted from rewriteUri.
Off-by-one fix (correct):
- Old:
rewriteUri.substring(getRegex().indexOf("{") + 1)— dropped the first character of the captured value. E.g. forrewriteUri = /shenyu/123with the{...}placeholder starting at indexi, this produced23instead of123. - New:
rewriteUri.substring(getRegex().indexOf("{"))— keeps the full captured segment. This matches the prior fix in #6951 and is consistent.
New crash guard (defensive, good):
- Old condition only checked
getReplace().contains("{"). IfgetReplace()contained{butgetRegex()did not, thengetRegex().indexOf("{")returned-1, andsubstring(-1)threwStringIndexOutOfBoundsException. - New condition
getReplace().contains("{") && getRegex().contains("{")routes the no-placeholder case to the saferewriteUri.replaceAll(getRegex(), getReplace())branch, eliminating the crash.
Conclusion
Minimal, correct, and aligns with the existing placeholder handling. No behavioral regression for the normal (no-placeholder) path. Approving.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6883.
Bug Description
The RewritePlugin has two bugs in its placeholder branch:
First character of path-variable value is dropped:
rewriteUri.substring(rewriteHandle.getRegex().indexOf("{") + 1)skips the first character of the actual path-variable value because the+1is meant to skip the{character in the regex pattern, but it's applied to the rewriteUri which has the actual value at that position.StringIndexOutOfBoundsException when regex lacks
{: The condition only checksrewriteHandle.getReplace().contains("{")but notrewriteHandle.getRegex().contains("{"). When the replace pattern contains{but the regex doesn't,indexOf("{")returns -1, causingsubstring(-1)to throw.Fix
rewriteHandle.getRegex().contains("{")to the guard condition+1offset fromrewriteUri.substring()to preserve the first character