From fd7d46e9da45eceaca340de7734a7e3018c38f70 Mon Sep 17 00:00:00 2001 From: "Tim St. Clair" Date: Fri, 12 Jun 2015 17:23:31 -0700 Subject: [PATCH] Don't rewrite relative URLs when proxying HTTP content. --- pkg/util/proxy/transport.go | 21 ++++++++------------- pkg/util/proxy/transport_test.go | 12 ++++++------ 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/pkg/util/proxy/transport.go b/pkg/util/proxy/transport.go index f49ceed0ea7..23e76c53064 100644 --- a/pkg/util/proxy/transport.go +++ b/pkg/util/proxy/transport.go @@ -119,21 +119,17 @@ func (t *Transport) rewriteURL(targetURL string, sourceURL *url.URL) string { if err != nil { return targetURL } - if url.Host != "" && url.Host != sourceURL.Host { + + isDifferentHost := url.Host != "" && url.Host != sourceURL.Host + isRelative := !strings.HasPrefix(url.Path, "/") + if isDifferentHost || isRelative { return targetURL } url.Scheme = t.Scheme url.Host = t.Host origPath := url.Path - - if strings.HasPrefix(url.Path, "/") { - // The path is rooted at the host. Just add proxy prepend. - url.Path = path.Join(t.PathPrepend, url.Path) - } else { - // The path is relative to sourceURL. - url.Path = path.Join(t.PathPrepend, path.Dir(sourceURL.Path), url.Path) - } + url.Path = path.Join(t.PathPrepend, url.Path) if strings.HasSuffix(origPath, "/") { // Add back the trailing slash, which was stripped by path.Join(). @@ -144,10 +140,9 @@ func (t *Transport) rewriteURL(targetURL string, sourceURL *url.URL) string { } // updateURLs checks and updates any of n's attributes that are listed in tagsToAttrs. -// Any URLs found are, if they're relative, updated with the necessary changes to make -// a visit to that URL also go through the proxy. -// sourceURL is the URL of the page which we're currently on; it's required to make -// relative links work. +// Any URLs found are, if they share the source host, updated with the necessary changes +// to make a visit to that URL also go through the proxy. +// sourceURL is the URL of the page which we're currently on. func (t *Transport) updateURLs(n *html.Node, sourceURL *url.URL) { if n.Type != html.ElementNode { return diff --git a/pkg/util/proxy/transport_test.go b/pkg/util/proxy/transport_test.go index dac3069f737..e140407ea57 100644 --- a/pkg/util/proxy/transport_test.go +++ b/pkg/util/proxy/transport_test.go @@ -77,7 +77,7 @@ func TestProxyTransport(t *testing.T) { input: `
kubelet.loggoogle.log
`, sourceURL: "http://myminion.com/logs/log.log", transport: testTransport, - output: `
kubelet.loggoogle.log
`, + output: `
kubelet.loggoogle.log
`, contentType: "text/html", forwardedURI: "/proxy/minion/minion1:10250/logs/log.log", }, @@ -85,7 +85,7 @@ func TestProxyTransport(t *testing.T) { input: `
kubelet.loggoogle.log
`, sourceURL: "http://myminion.com/logs/log.log", transport: testTransport, - output: `
kubelet.loggoogle.log
`, + output: `
kubelet.loggoogle.log
`, contentType: "text/html", forwardedURI: "/proxy/minion/minion1:10250/logs/log.log", }, @@ -93,7 +93,7 @@ func TestProxyTransport(t *testing.T) { input: `
kubelet.loggoogle.log
`, sourceURL: "http://myminion.com/logs/log.log", transport: testTransport, - output: `
kubelet.loggoogle.log
`, + output: `
kubelet.loggoogle.log
`, contentType: "text/html; charset=utf-8", forwardedURI: "/proxy/minion/minion1:10250/logs/log.log", }, @@ -109,15 +109,15 @@ func TestProxyTransport(t *testing.T) { input: `kubelet.loggoogle.log`, sourceURL: "http://myminion.com/whatever/apt/somelog.log", transport: testTransport2, - output: `kubelet.loggoogle.log`, + output: `kubelet.loggoogle.log`, contentType: "text/html", forwardedURI: "/proxy/minion/minion1:8080/whatever/apt/somelog.log", }, "image": { - input: `
`, + input: `
`, sourceURL: "http://myminion.com/", transport: testTransport, - output: `
`, + output: `
`, contentType: "text/html", forwardedURI: "/proxy/minion/minion1:10250/", },