From 542835808e41723e5ecf5864b189a0ad36b8f3f6 Mon Sep 17 00:00:00 2001 From: Leaf Date: Sun, 29 Mar 2020 00:12:39 +0000 Subject: [PATCH] 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. --- middleware/rewrite.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/middleware/rewrite.go b/middleware/rewrite.go index a64e10bb..d1387af0 100644 --- a/middleware/rewrite.go +++ b/middleware/rewrite.go @@ -57,7 +57,8 @@ func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc { // Initialize for k, v := range config.Rules { - k = strings.Replace(k, "*", "(.*)", -1) + k = regexp.QuoteMeta(k) + k = strings.Replace(k, `\*`, "(.*)", -1) k = k + "$" config.rulesRegex[regexp.MustCompile(k)] = v }