Skip to content

Commit 5428358

Browse files
authored
Quote regex meta characters in Rewrite (#1541)
Currently there is a half and half situation where the user can't use regex (fully) because * will be replaced with (.*), yet they also can't just enter any old string, because meta chars like . would need escaping. e.g. currently *.html wouldn't work as intended, and instead *\.html should be used. Work around this by using regexp's QuoteMeta function to sanitise the input before handling it.
1 parent fc4b1c0 commit 5428358

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

middleware/rewrite.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc {
5757

5858
// Initialize
5959
for k, v := range config.Rules {
60-
k = strings.Replace(k, "*", "(.*)", -1)
60+
k = regexp.QuoteMeta(k)
61+
k = strings.Replace(k, `\*`, "(.*)", -1)
6162
k = k + "$"
6263
config.rulesRegex[regexp.MustCompile(k)] = v
6364
}

0 commit comments

Comments
 (0)