1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

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.
This commit is contained in:
Leaf 2020-03-29 00:12:39 +00:00 committed by GitHub
parent fc4b1c0a83
commit 542835808e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -57,7 +57,8 @@ func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc {
// Initialize // Initialize
for k, v := range config.Rules { for k, v := range config.Rules {
k = strings.Replace(k, "*", "(.*)", -1) k = regexp.QuoteMeta(k)
k = strings.Replace(k, `\*`, "(.*)", -1)
k = k + "$" k = k + "$"
config.rulesRegex[regexp.MustCompile(k)] = v config.rulesRegex[regexp.MustCompile(k)] = v
} }