Fix regexp handling in redirect middleware

This commit is contained in:
Tom Moulard 2022-04-15 17:24:08 +02:00 committed by GitHub
parent ad3625bef3
commit 232e2c1e7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 16 deletions

View file

@ -15,6 +15,8 @@ const (
schemeHTTPS = "https" schemeHTTPS = "https"
) )
var uriRegexp = regexp.MustCompile(`^(https?):\/\/(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$`)
type redirect struct { type redirect struct {
next http.Handler next http.Handler
regex *regexp.Regexp regex *regexp.Regexp

View file

@ -3,7 +3,6 @@ package redirect
import ( import (
"context" "context"
"net/http" "net/http"
"regexp"
"strings" "strings"
"github.com/traefik/traefik/v2/pkg/config/dynamic" "github.com/traefik/traefik/v2/pkg/config/dynamic"
@ -11,9 +10,7 @@ import (
"github.com/traefik/traefik/v2/pkg/middlewares" "github.com/traefik/traefik/v2/pkg/middlewares"
) )
const ( const typeRegexName = "RedirectRegex"
typeRegexName = "RedirectRegex"
)
// NewRedirectRegex creates a redirect middleware. // NewRedirectRegex creates a redirect middleware.
func NewRedirectRegex(ctx context.Context, next http.Handler, conf dynamic.RedirectRegex, name string) (http.Handler, error) { func NewRedirectRegex(ctx context.Context, next http.Handler, conf dynamic.RedirectRegex, name string) (http.Handler, error) {
@ -30,10 +27,7 @@ func rawURL(req *http.Request) string {
port := "" port := ""
uri := req.RequestURI uri := req.RequestURI
schemeRegex := `^(https?):\/\/(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$` if match := uriRegexp.FindStringSubmatch(req.RequestURI); len(match) > 0 {
re, _ := regexp.Compile(schemeRegex)
if re.Match([]byte(req.RequestURI)) {
match := re.FindStringSubmatch(req.RequestURI)
scheme = match[1] scheme = match[1]
if len(match[2]) > 0 { if len(match[2]) > 0 {

View file

@ -5,7 +5,6 @@ import (
"errors" "errors"
"net" "net"
"net/http" "net/http"
"regexp"
"strings" "strings"
"github.com/traefik/traefik/v2/pkg/config/dynamic" "github.com/traefik/traefik/v2/pkg/config/dynamic"
@ -15,7 +14,7 @@ import (
const ( const (
typeSchemeName = "RedirectScheme" typeSchemeName = "RedirectScheme"
schemeRedirectRegex = `^(https?:\/\/)?(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$` uriPattern = `^(https?:\/\/)?(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$`
) )
// NewRedirectScheme creates a new RedirectScheme middleware. // NewRedirectScheme creates a new RedirectScheme middleware.
@ -33,7 +32,7 @@ func NewRedirectScheme(ctx context.Context, next http.Handler, conf dynamic.Redi
port = ":" + conf.Port port = ":" + conf.Port
} }
return newRedirect(next, schemeRedirectRegex, conf.Scheme+"://${2}"+port+"${4}", conf.Permanent, rawURLScheme, name) return newRedirect(next, uriPattern, conf.Scheme+"://${2}"+port+"${4}", conf.Permanent, rawURLScheme, name)
} }
func rawURLScheme(req *http.Request) string { func rawURLScheme(req *http.Request) string {
@ -46,10 +45,7 @@ func rawURLScheme(req *http.Request) string {
} }
uri := req.RequestURI uri := req.RequestURI
schemeRegex := `^(https?):\/\/(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$` if match := uriRegexp.FindStringSubmatch(req.RequestURI); len(match) > 0 {
re, _ := regexp.Compile(schemeRegex)
if re.Match([]byte(req.RequestURI)) {
match := re.FindStringSubmatch(req.RequestURI)
scheme = match[1] scheme = match[1]
if len(match[2]) > 0 { if len(match[2]) > 0 {