[BUG] Rewrite plugin can throw when replace uses placeholders but regex has no placeholder
- Dominant language
- Java
- Stars
- 8.8k
- Forks
- 3.1k
- Avg merge
- 7d 1h
- Merged PRs (30d)
- 85
Description
### Search before asking
- [x] I had searched in the [issues](https://github.com/apache/shenyu/issues) and found no similar issues.
### Apache ShenYu Component
shenyu-plugin
### What happened
`RewritePlugin` switches to placeholder replacement whenever `replace` contains `{`:
```java
rewriteUri = rewriteHandle.getReplace().contains("{")
? PathMatchUtils.replaceAll(rewriteHandle.getReplace(), rewriteHandle.getRegex().substring(rewriteHandle.getRegex().indexOf("{")),
rewriteUri.substring(rewriteHandle.getRegex().indexOf("{") + 1))
: rewriteUri.replaceAll(rewriteHandle.getRegex(), rewriteHandle.getReplace());
```
This assumes `regex` also contains `{`. If the rule's `replace` contains a placeholder but `regex` does not, `rewriteHandle.getRegex().indexOf("{")` returns `-1`, and `regex.substring(-1)` throws `StringIndexOutOfBoundsException`.
A misconfigured rewrite rule should not crash request processing with an unchecked substring error.
### Expected behavior
The rewrite plugin should validate placeholder mode consistently. If `replace` contains placeholders, `regex` should also be required to contain the matching placeholder pattern before substring operations are performed. Invalid rewrite config should be rejected when the rule is saved/synced or skipped with a clear error instead of failing the request.
### How to reproduce
1. Enable the rewrite plugin.
2. Create a rewrite rule with a `replace` value containing `{...}` but a `regex` value without `{`, for example:
```json
{
"regex": "/api/(.*)",
"replace": "/v1/{path}"
}
```
3. Send a request matching the rule.
4. `RewritePlugin` enters placeholder mode because `replace` contains `{`, then calls `regex.substring(regex.indexOf("{"))` with `-1`, causing `StringIndexOutOfBoundsException`.
### Debug logs
_No response_
### Environment
Current `master` branch.
### Are you willing to submit a PR?
- [ ] Yes I am willing to submit a PR!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at RewritePlugin and trace the placeholder branch that calls PathMatchUtils.replaceAll; inspect how regex and replace values are validated or processed. Reproduce the example rule with a placeholder only in replace, then verify that invalid configuration no longer causes an unchecked substring error and instead follows the project's chosen clear-error or skip behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 62/100