Extend https redirection tests, and fix incorrect behavior

This commit is contained in:
Daniel Tomcej 2018-08-14 10:38:04 -06:00 committed by Traefiker Bot
parent bd3c8c3cde
commit 870755e90d
7 changed files with 110 additions and 111 deletions

View file

@ -22,15 +22,49 @@ defaultEntryPoints = ["http", "https"]
weight = 1 weight = 1
[frontends] [frontends]
[frontends.frontend1] [frontends.frontend1]
backend = "backend1" backend = "backend1"
[frontends.frontend1.routes.test_1] [frontends.frontend1.routes.test_1]
rule = "Host: example.com; PathPrefixStrip: /api" rule = "Host: example.com; PathPrefixStrip: /api"
[frontends.frontend2] [frontends.frontend2]
backend = "backend1" backend = "backend1"
[frontends.frontend2.routes.test_1] [frontends.frontend2.routes.test_1]
rule = "Host: test.com; AddPrefix: /foo" rule = "Host: example2.com; PathPrefixStrip: /api/"
[frontends.frontend3] [frontends.frontend3]
backend = "backend1" backend = "backend1"
[frontends.frontend3.routes.test_1] [frontends.frontend3.routes.test_1]
rule = "Host: test.com; AddPrefix: /foo"
[frontends.frontend4]
backend = "backend1"
[frontends.frontend4.routes.test_1]
rule = "Host: test2.com; AddPrefix: /foo/"
[frontends.frontend5]
backend = "backend1"
[frontends.frontend5.routes.test_1]
rule = "Host: foo.com; PathPrefixStripRegex: /{id:[a-z]+}" rule = "Host: foo.com; PathPrefixStripRegex: /{id:[a-z]+}"
[frontends.frontend6]
backend = "backend1"
[frontends.frontend6.routes.test_1]
rule = "Host: foo2.com; PathPrefixStripRegex: /{id:[a-z]+}/"
[frontends.frontend7]
backend = "backend1"
[frontends.frontend7.routes.test_1]
rule = "Host: bar.com; ReplacePathRegex: /api /"
[frontends.frontend8]
backend = "backend1"
[frontends.frontend8.routes.test_1]
rule = "Host: bar2.com; ReplacePathRegex: /api/ /"
[frontends.frontend9]
backend = "backend1"
[frontends.frontend9.routes.test_1]
rule = "Host: pow.com; ReplacePath: /api"
[frontends.frontend10]
backend = "backend1"
[frontends.frontend10.routes.test_1]
rule = "Host: pow2.com; ReplacePath: /api/"

View file

@ -3,6 +3,7 @@ package integration
import ( import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"fmt"
"net" "net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -740,7 +741,7 @@ func (s *HTTPSSuite) TestEntrypointHttpsRedirectAndPathModification(c *check.C)
defer cmd.Process.Kill() defer cmd.Process.Kill()
// wait for Traefik // wait for Traefik
err = try.GetRequest("http://127.0.0.1:8080/api/providers", 500*time.Millisecond, try.BodyContains("Host: example.com")) err = try.GetRequest("http://127.0.0.1:8080/api/providers", 1000*time.Millisecond, try.BodyContains("Host: example.com"))
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
client := &http.Client{ client := &http.Client{
@ -750,115 +751,82 @@ func (s *HTTPSSuite) TestEntrypointHttpsRedirectAndPathModification(c *check.C)
} }
testCases := []struct { testCases := []struct {
desc string desc string
host string hosts []string
sourceURL string path string
expectedURL string
}{ }{
{ {
desc: "Stripped URL redirect", desc: "Stripped URL redirect",
host: "example.com", hosts: []string{"example.com", "foo.com", "bar.com"},
sourceURL: "http://127.0.0.1:8888/api", path: "/api",
expectedURL: "https://example.com:8443/api",
}, },
{ {
desc: "Stripped URL with trailing slash redirect", desc: "Stripped URL with trailing slash redirect",
host: "example.com", hosts: []string{"example.com", "example2.com", "foo.com", "foo2.com", "bar.com", "bar2.com"},
sourceURL: "http://127.0.0.1:8888/api/", path: "/api/",
expectedURL: "https://example.com:8443/api/",
}, },
{ {
desc: "Stripped URL with double trailing slash redirect", desc: "Stripped URL with double trailing slash redirect",
host: "example.com", hosts: []string{"example.com", "example2.com", "foo.com", "foo2.com", "bar.com", "bar2.com"},
sourceURL: "http://127.0.0.1:8888/api//", path: "/api//",
expectedURL: "https://example.com:8443/api//",
}, },
{ {
desc: "Stripped URL with path redirect", desc: "Stripped URL with path redirect",
host: "example.com", hosts: []string{"example.com", "example2.com", "foo.com", "foo2.com", "bar.com", "bar2.com"},
sourceURL: "http://127.0.0.1:8888/api/bacon", path: "/api/bacon",
expectedURL: "https://example.com:8443/api/bacon",
}, },
{ {
desc: "Stripped URL with path and trailing slash redirect", desc: "Stripped URL with path and trailing slash redirect",
host: "example.com", hosts: []string{"example.com", "example2.com", "foo.com", "foo2.com", "bar.com", "bar2.com"},
sourceURL: "http://127.0.0.1:8888/api/bacon/", path: "/api/bacon/",
expectedURL: "https://example.com:8443/api/bacon/",
}, },
{ {
desc: "Stripped URL with path and double trailing slash redirect", desc: "Stripped URL with path and double trailing slash redirect",
host: "example.com", hosts: []string{"example.com", "example2.com", "foo.com", "foo2.com", "bar.com", "bar2.com"},
sourceURL: "http://127.0.0.1:8888/api/bacon//", path: "/api/bacon//",
expectedURL: "https://example.com:8443/api/bacon//",
}, },
{ {
desc: "Root Path with redirect", desc: "Root Path with redirect",
host: "test.com", hosts: []string{"test.com", "test2.com", "pow.com", "pow2.com"},
sourceURL: "http://127.0.0.1:8888/", path: "/",
expectedURL: "https://test.com:8443/",
}, },
{ {
desc: "Root Path with double trailing slash redirect", desc: "Root Path with double trailing slash redirect",
host: "test.com", hosts: []string{"test.com", "test2.com", "pow.com", "pow2.com"},
sourceURL: "http://127.0.0.1:8888//", path: "//",
expectedURL: "https://test.com:8443//",
}, },
{ {
desc: "AddPrefix with redirect", desc: "Path modify with redirect",
host: "test.com", hosts: []string{"test.com", "test2.com", "pow.com", "pow2.com"},
sourceURL: "http://127.0.0.1:8888/wtf", path: "/wtf",
expectedURL: "https://test.com:8443/wtf",
}, },
{ {
desc: "AddPrefix with trailing slash redirect", desc: "Path modify with trailing slash redirect",
host: "test.com", hosts: []string{"test.com", "test2.com", "pow.com", "pow2.com"},
sourceURL: "http://127.0.0.1:8888/wtf/", path: "/wtf/",
expectedURL: "https://test.com:8443/wtf/",
}, },
{ {
desc: "AddPrefix with matching path segment redirect", desc: "Path modify with matching path segment redirect",
host: "test.com", hosts: []string{"test.com", "test2.com", "pow.com", "pow2.com"},
sourceURL: "http://127.0.0.1:8888/wtf/foo", path: "/wtf/foo",
expectedURL: "https://test.com:8443/wtf/foo",
},
{
desc: "Stripped URL Regex redirect",
host: "foo.com",
sourceURL: "http://127.0.0.1:8888/api",
expectedURL: "https://foo.com:8443/api",
},
{
desc: "Stripped URL Regex with trailing slash redirect",
host: "foo.com",
sourceURL: "http://127.0.0.1:8888/api/",
expectedURL: "https://foo.com:8443/api/",
},
{
desc: "Stripped URL Regex with path redirect",
host: "foo.com",
sourceURL: "http://127.0.0.1:8888/api/bacon",
expectedURL: "https://foo.com:8443/api/bacon",
},
{
desc: "Stripped URL Regex with path and trailing slash redirect",
host: "foo.com",
sourceURL: "http://127.0.0.1:8888/api/bacon/",
expectedURL: "https://foo.com:8443/api/bacon/",
}, },
} }
for _, test := range testCases { for _, test := range testCases {
test := test sourceURL := fmt.Sprintf("http://127.0.0.1:8888%s", test.path)
for _, host := range test.hosts {
req, err := http.NewRequest("GET", sourceURL, nil)
c.Assert(err, checker.IsNil)
req.Host = host
req, err := http.NewRequest("GET", test.sourceURL, nil) resp, err := client.Do(req)
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
req.Host = test.host defer resp.Body.Close()
resp, err := client.Do(req) location := resp.Header.Get("Location")
c.Assert(err, checker.IsNil) expected := fmt.Sprintf("https://%s:8443%s", host, test.path)
defer resp.Body.Close()
location := resp.Header.Get("Location") c.Assert(location, checker.Equals, expected)
c.Assert(location, checker.Equals, test.expectedURL) }
} }
} }

View file

@ -88,19 +88,7 @@ func (h *handler) ServeHTTP(rw http.ResponseWriter, req *http.Request, next http
if stripPrefix, stripPrefixOk := req.Context().Value(middlewares.StripPrefixKey).(string); stripPrefixOk { if stripPrefix, stripPrefixOk := req.Context().Value(middlewares.StripPrefixKey).(string); stripPrefixOk {
if len(stripPrefix) > 0 { if len(stripPrefix) > 0 {
tempPath := parsedURL.Path
parsedURL.Path = stripPrefix parsedURL.Path = stripPrefix
if len(tempPath) > 0 && tempPath != "/" {
parsedURL.Path = stripPrefix + tempPath
}
if trailingSlash, trailingSlashOk := req.Context().Value(middlewares.StripPrefixSlashKey).(bool); trailingSlashOk {
if trailingSlash {
if !strings.HasSuffix(parsedURL.Path, "/") {
parsedURL.Path = fmt.Sprintf("%s/", parsedURL.Path)
}
}
}
} }
} }
@ -110,6 +98,12 @@ func (h *handler) ServeHTTP(rw http.ResponseWriter, req *http.Request, next http
} }
} }
if replacePath, replacePathOk := req.Context().Value(middlewares.ReplacePathKey).(string); replacePathOk {
if len(replacePath) > 0 {
parsedURL.Path = replacePath
}
}
if newURL != oldURL { if newURL != oldURL {
handler := &moveHandler{location: parsedURL, permanent: h.permanent} handler := &moveHandler{location: parsedURL, permanent: h.permanent}
handler.ServeHTTP(rw, req) handler.ServeHTTP(rw, req)

View file

@ -1,11 +1,17 @@
package middlewares package middlewares
import ( import (
"context"
"net/http" "net/http"
) )
// ReplacedPathHeader is the default header to set the old path to const (
const ReplacedPathHeader = "X-Replaced-Path" // ReplacePathKey is the key within the request context used to
// store the replaced path
ReplacePathKey key = "ReplacePath"
// ReplacedPathHeader is the default header to set the old path to
ReplacedPathHeader = "X-Replaced-Path"
)
// ReplacePath is a middleware used to replace the path of a URL request // ReplacePath is a middleware used to replace the path of a URL request
type ReplacePath struct { type ReplacePath struct {
@ -14,6 +20,7 @@ type ReplacePath struct {
} }
func (s *ReplacePath) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *ReplacePath) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(), ReplacePathKey, r.URL.Path))
r.Header.Add(ReplacedPathHeader, r.URL.Path) r.Header.Add(ReplacedPathHeader, r.URL.Path)
r.URL.Path = s.Path r.URL.Path = s.Path
r.RequestURI = r.URL.RequestURI() r.RequestURI = r.URL.RequestURI()

View file

@ -1,6 +1,7 @@
package middlewares package middlewares
import ( import (
"context"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
@ -30,6 +31,7 @@ func NewReplacePathRegexHandler(regex string, replacement string, handler http.H
func (s *ReplacePathRegex) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *ReplacePathRegex) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if s.Regexp != nil && len(s.Replacement) > 0 && s.Regexp.MatchString(r.URL.Path) { if s.Regexp != nil && len(s.Replacement) > 0 && s.Regexp.MatchString(r.URL.Path) {
r = r.WithContext(context.WithValue(r.Context(), ReplacePathKey, r.URL.Path))
r.Header.Add(ReplacedPathHeader, r.URL.Path) r.Header.Add(ReplacedPathHeader, r.URL.Path)
r.URL.Path = s.Regexp.ReplaceAllString(r.URL.Path, s.Replacement) r.URL.Path = s.Regexp.ReplaceAllString(r.URL.Path, s.Replacement)
r.RequestURI = r.URL.RequestURI() r.RequestURI = r.URL.RequestURI()

View file

@ -10,9 +10,6 @@ const (
// StripPrefixKey is the key within the request context used to // StripPrefixKey is the key within the request context used to
// store the stripped prefix // store the stripped prefix
StripPrefixKey key = "StripPrefix" StripPrefixKey key = "StripPrefix"
// StripPrefixSlashKey is the key within the request context used to
// store the stripped slash
StripPrefixSlashKey key = "StripPrefixSlash"
// ForwardedPrefixHeader is the default header to set prefix // ForwardedPrefixHeader is the default header to set prefix
ForwardedPrefixHeader = "X-Forwarded-Prefix" ForwardedPrefixHeader = "X-Forwarded-Prefix"
) )
@ -26,21 +23,20 @@ type StripPrefix struct {
func (s *StripPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *StripPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, prefix := range s.Prefixes { for _, prefix := range s.Prefixes {
if strings.HasPrefix(r.URL.Path, prefix) { if strings.HasPrefix(r.URL.Path, prefix) {
trailingSlash := r.URL.Path == prefix+"/" rawReqPath := r.URL.Path
r.URL.Path = stripPrefix(r.URL.Path, prefix) r.URL.Path = stripPrefix(r.URL.Path, prefix)
if r.URL.RawPath != "" { if r.URL.RawPath != "" {
r.URL.RawPath = stripPrefix(r.URL.RawPath, prefix) r.URL.RawPath = stripPrefix(r.URL.RawPath, prefix)
} }
s.serveRequest(w, r, strings.TrimSpace(prefix), trailingSlash) s.serveRequest(w, r, strings.TrimSpace(prefix), rawReqPath)
return return
} }
} }
http.NotFound(w, r) http.NotFound(w, r)
} }
func (s *StripPrefix) serveRequest(w http.ResponseWriter, r *http.Request, prefix string, trailingSlash bool) { func (s *StripPrefix) serveRequest(w http.ResponseWriter, r *http.Request, prefix string, rawReqPath string) {
r = r.WithContext(context.WithValue(r.Context(), StripPrefixSlashKey, trailingSlash)) r = r.WithContext(context.WithValue(r.Context(), StripPrefixKey, rawReqPath))
r = r.WithContext(context.WithValue(r.Context(), StripPrefixKey, prefix))
r.Header.Add(ForwardedPrefixHeader, prefix) r.Header.Add(ForwardedPrefixHeader, prefix)
r.RequestURI = r.URL.RequestURI() r.RequestURI = r.URL.RequestURI()
s.Handler.ServeHTTP(w, r) s.Handler.ServeHTTP(w, r)

View file

@ -39,16 +39,14 @@ func (s *StripPrefixRegex) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Error("Error in stripPrefix middleware", err) log.Error("Error in stripPrefix middleware", err)
return return
} }
rawReqPath := r.URL.Path
trailingSlash := r.URL.Path == prefix.Path+"/"
r.URL.Path = r.URL.Path[len(prefix.Path):] r.URL.Path = r.URL.Path[len(prefix.Path):]
if r.URL.RawPath != "" { if r.URL.RawPath != "" {
r.URL.RawPath = r.URL.RawPath[len(prefix.Path):] r.URL.RawPath = r.URL.RawPath[len(prefix.Path):]
} }
r = r.WithContext(context.WithValue(r.Context(), StripPrefixSlashKey, trailingSlash)) r = r.WithContext(context.WithValue(r.Context(), StripPrefixKey, rawReqPath))
r = r.WithContext(context.WithValue(r.Context(), StripPrefixKey, prefix.Path))
r.Header.Add(ForwardedPrefixHeader, prefix.Path) r.Header.Add(ForwardedPrefixHeader, prefix.Path)
r.RequestURI = r.URL.RequestURI() r.RequestURI = ensureLeadingSlash(r.URL.RequestURI())
s.Handler.ServeHTTP(w, r) s.Handler.ServeHTTP(w, r)
return return
} }