From 153c046778a05aadac5dc89d0156019773a7f7c3 Mon Sep 17 00:00:00 2001
From: Pascal Bleser
Date: Wed, 25 Nov 2026 09:15:42 +0100
Subject: [PATCH] fix(proxy): redirect to HTTPS on the host of OC_URL
The proxy redirected requests forwarded as plain HTTP to https:// plus
the Host header of the request. The client controls that header, so the
redirect could send users to any other site (open redirect).
The redirect now uses the host of OC_URL, the URL where OpenCloud is
reachable for users, and keeps the path and query of the request as the
client escaped them. If OC_URL can't be parsed or has no host, requests
are passed on without a redirect, and the proxy logs why at startup.
Assisted-by: Claude Code:claude-opus-5-5
---
services/proxy/pkg/command/server.go | 7 +-
.../proxy/pkg/middleware/https_redirect.go | 66 +++++++++++---
.../pkg/middleware/https_redirect_test.go | 86 +++++++++++++++++++
3 files changed, 147 insertions(+), 12 deletions(-)
create mode 100644 services/proxy/pkg/middleware/https_redirect_test.go
diff --git a/services/proxy/pkg/command/server.go b/services/proxy/pkg/command/server.go
index aa91fc3652..91e540d9e9 100644
--- a/services/proxy/pkg/command/server.go
+++ b/services/proxy/pkg/command/server.go
@@ -393,6 +393,11 @@ func loadMiddlewares(logger log.Logger, cfg *config.Config,
logger.Fatal().Err(err).Msg("Failed to load CSP configuration.")
}
+ var baseURL string
+ if cfg.Commons != nil {
+ baseURL = cfg.Commons.OpenCloudURL
+ }
+
return alice.New(
pkgmiddleware.ClientIP(trustedProxies, cfg.ClientIP.Header),
chimiddleware.RequestID,
@@ -408,7 +413,7 @@ func loadMiddlewares(logger log.Logger, cfg *config.Config,
pkgmiddleware.TraceContext,
middleware.AccessLog(logger),
middleware.ContextLogger(logger),
- middleware.HTTPSRedirect, // redirect to https if enabled
+ middleware.HTTPSRedirect(logger, baseURL), // redirect to https if enabled
// 2. Rewrite datagateway url tokens to dataprovider endpoint and
// rewrite Location headers in TUS responses to signed datagateway urls
diff --git a/services/proxy/pkg/middleware/https_redirect.go b/services/proxy/pkg/middleware/https_redirect.go
index f5d94144cb..00eb1bd05b 100644
--- a/services/proxy/pkg/middleware/https_redirect.go
+++ b/services/proxy/pkg/middleware/https_redirect.go
@@ -1,19 +1,63 @@
package middleware
import (
- "fmt"
"net/http"
+ "net/url"
+
+ "github.com/opencloud-eu/opencloud/pkg/log"
)
-// HTTPSRedirect redirects insecure requests to https
-func HTTPSRedirect(next http.Handler) http.Handler {
- return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
- proto := req.Header.Get("x-forwarded-proto")
- if proto == "http" || proto == "HTTP" {
- http.Redirect(res, req, fmt.Sprintf("https://%s%s", req.Host, req.URL), http.StatusPermanentRedirect)
- return
- }
+// HTTPSRedirect returns a middleware that redirects requests forwarded as
+// plain HTTP (X-Forwarded-Proto: http) to HTTPS.
+//
+// The redirect target uses the host of baseURL, the URL where OpenCloud is
+// reachable for users (OC_URL), never the Host header of the request: the
+// client controls that header, so a redirect built from it could send users
+// to any other site. If baseURL can't be parsed or has no host, requests are
+// passed on without a redirect, and the reason is logged once.
+func HTTPSRedirect(logger log.Logger, baseURL string) func(http.Handler) http.Handler {
+ var host string
+ u, err := url.Parse(baseURL)
+ switch {
+ case err != nil:
+ logger.Error().Err(err).Str("url", baseURL).Msg("cannot parse the OpenCloud URL, requests forwarded as HTTP are not redirected to HTTPS")
+ case u.Host == "":
+ logger.Warn().Str("url", baseURL).Msg("the OpenCloud URL has no host, requests forwarded as HTTP are not redirected to HTTPS")
+ default:
+ host = u.Host
+ }
- next.ServeHTTP(res, req)
- })
+ return func(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
+ // A reverse proxy in front of OpenCloud that terminates TLS sets
+ // X-Forwarded-Proto to the protocol the client used to reach the
+ // reverse proxy, not the one between the reverse proxy and
+ // OpenCloud. "http" means the client connected without TLS, so it
+ // is sent to the HTTPS URL instead. When the client then connects
+ // over HTTPS, the reverse proxy sets the header to "https", and
+ // the request is passed on without another redirect. Requests
+ // without the header come straight from the client and are passed
+ // on as well.
+ proto := req.Header.Get("X-Forwarded-Proto")
+ if host != "" && (proto == "http" || proto == "HTTP") {
+ // Only the path and the query are taken from the request.
+ // RawPath keeps the path as the client escaped it, e.g. a %2F
+ // in a file name. The user info is left out on purpose: clients
+ // send credentials in the Authorization header, and a user info
+ // in the request URL would be echoed back in the Location
+ // header, including a password.
+ target := url.URL{
+ Scheme: "https",
+ Host: host,
+ Path: req.URL.Path,
+ RawPath: req.URL.RawPath,
+ RawQuery: req.URL.RawQuery,
+ }
+ http.Redirect(res, req, target.String(), http.StatusPermanentRedirect)
+ return
+ }
+
+ next.ServeHTTP(res, req)
+ })
+ }
}
diff --git a/services/proxy/pkg/middleware/https_redirect_test.go b/services/proxy/pkg/middleware/https_redirect_test.go
new file mode 100644
index 0000000000..157f7b6174
--- /dev/null
+++ b/services/proxy/pkg/middleware/https_redirect_test.go
@@ -0,0 +1,86 @@
+package middleware
+
+import (
+ "net/http"
+ "net/http/httptest"
+
+ . "github.com/onsi/ginkgo/v2"
+ . "github.com/onsi/gomega"
+
+ "github.com/opencloud-eu/opencloud/pkg/log"
+)
+
+var _ = Describe("HTTPSRedirect", func() {
+ next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ })
+
+ serve := func(baseURL, target, host, proto string) *httptest.ResponseRecorder {
+ req := httptest.NewRequest(http.MethodGet, target, nil)
+ req.Host = host
+ if proto != "" {
+ req.Header.Set("X-Forwarded-Proto", proto)
+ }
+ rr := httptest.NewRecorder()
+ HTTPSRedirect(log.NopLogger(), baseURL)(next).ServeHTTP(rr, req)
+ return rr
+ }
+
+ It("redirects to the host of the base URL, not to the Host header", func() {
+ rr := serve("https://cloud.example.com", "/remote.php/webdav?foo=bar", "evil.example.org", "http")
+ Expect(rr.Code).To(Equal(http.StatusPermanentRedirect))
+ Expect(rr.Header().Get("Location")).To(Equal("https://cloud.example.com/remote.php/webdav?foo=bar"))
+ })
+
+ It("keeps the port of the base URL", func() {
+ rr := serve("https://localhost:9200", "/", "localhost:9200", "HTTP")
+ Expect(rr.Code).To(Equal(http.StatusPermanentRedirect))
+ Expect(rr.Header().Get("Location")).To(Equal("https://localhost:9200/"))
+ })
+
+ It("keeps the escaping of the path", func() {
+ rr := serve("https://cloud.example.com", "/dav/files/a%2Fb%20c", "cloud.example.com", "http")
+ Expect(rr.Code).To(Equal(http.StatusPermanentRedirect))
+ Expect(rr.Header().Get("Location")).To(Equal("https://cloud.example.com/dav/files/a%2Fb%20c"))
+ })
+
+ It("stays on the host of the base URL for a path that starts with two slashes", func() {
+ rr := serve("https://cloud.example.com", "//evil.example.org/x", "cloud.example.com", "http")
+ Expect(rr.Code).To(Equal(http.StatusPermanentRedirect))
+ Expect(rr.Header().Get("Location")).To(Equal("https://cloud.example.com//evil.example.org/x"))
+ })
+
+ It("redirects when the request has no Host header", func() {
+ rr := serve("https://cloud.example.com", "/index.html", "", "http")
+ Expect(rr.Code).To(Equal(http.StatusPermanentRedirect))
+ Expect(rr.Header().Get("Location")).To(Equal("https://cloud.example.com/index.html"))
+ })
+
+ It("keeps a # in the request path as part of the path", func() {
+ // clients don't send fragments: a # in the request is part of the path
+ rr := serve("https://cloud.example.com", "/dav/files/a#b", "cloud.example.com", "http")
+ Expect(rr.Code).To(Equal(http.StatusPermanentRedirect))
+ Expect(rr.Header().Get("Location")).To(Equal("https://cloud.example.com/dav/files/a%23b"))
+ })
+
+ It("takes neither host nor user info from an absolute request URL", func() {
+ // the user info would put the password into the Location header
+ rr := serve("https://cloud.example.com", "http://user:secret@evil.example.org/x?y=1", "evil.example.org", "http")
+ Expect(rr.Code).To(Equal(http.StatusPermanentRedirect))
+ Expect(rr.Header().Get("Location")).To(Equal("https://cloud.example.com/x?y=1"))
+ })
+
+ DescribeTable("passes the request on",
+ func(baseURL, proto string) {
+ rr := serve(baseURL, "/", "cloud.example.com", proto)
+ Expect(rr.Code).To(Equal(http.StatusOK))
+ Expect(rr.Header().Get("Location")).To(BeEmpty())
+ },
+ Entry("when it was forwarded as HTTPS", "https://cloud.example.com", "https"),
+ Entry("when it has no X-Forwarded-Proto", "https://cloud.example.com", ""),
+ Entry("when the base URL is empty", "", "http"),
+ Entry("when the base URL has no host", "cloud.example.com", "http"),
+ Entry("when the base URL has an empty host", "https://", "http"),
+ Entry("when the base URL can't be parsed", "https://cloud example.com:abc", "http"),
+ )
+})