Skip to content

Commit

Permalink
fix: use root URL to generate absolute proxy URL
Browse files Browse the repository at this point in the history
When using `BASE_URL` with a subfolder, the root URL must be used to
avoid base folder appearing twice in the generated URL.
  • Loading branch information
fguillot committed Aug 20, 2024
1 parent 3e0e8dd commit 701f47f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions internal/mediaproxy/media_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestAbsoluteProxyFilterWithHttpsAlways(t *testing.T) {
}
}

func TestAbsoluteProxyFilterWithCustomPortInBaseURL(t *testing.T) {
func TestAbsoluteProxyFilterWithCustomPortAndSubfolderInBaseURL(t *testing.T) {
os.Clearenv()
os.Setenv("BASE_URL", "http://example.org:88/folder/")
os.Setenv("MEDIA_PROXY_PRIVATE_KEY", "test")
Expand All @@ -198,11 +198,20 @@ func TestAbsoluteProxyFilterWithCustomPortInBaseURL(t *testing.T) {
t.Fatalf(`Unexpected base URL, got "%s"`, config.Opts.BaseURL())
}

r := mux.NewRouter()
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
if config.Opts.RootURL() != "http://example.org:88" {
t.Fatalf(`Unexpected root URL, got "%s"`, config.Opts.RootURL())
}

router := mux.NewRouter()

if config.Opts.BasePath() != "" {
router = router.PathPrefix(config.Opts.BasePath()).Subrouter()
}

router.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")

input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
output := RewriteDocumentWithAbsoluteProxyURL(r, input)
output := RewriteDocumentWithAbsoluteProxyURL(router, input)
expected := `<p><img src="http://example.org:88/folder/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`

if expected != output {
Expand Down
4 changes: 2 additions & 2 deletions internal/mediaproxy/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func ProxifyAbsoluteURL(router *mux.Router, mediaURL string) string {
return proxifyURLWithCustomProxy(mediaURL, customProxyURL)
}

// Note that the proxyified URL is relative to the root URL.
proxifiedUrl := ProxifyRelativeURL(router, mediaURL)

absoluteURL, err := url.JoinPath(config.Opts.BaseURL(), proxifiedUrl)
absoluteURL, err := url.JoinPath(config.Opts.RootURL(), proxifiedUrl)
if err != nil {
return mediaURL
}
Expand Down

0 comments on commit 701f47f

Please sign in to comment.