Skip to content

Commit 70dd34e

Browse files
committed
fix: [ocisdev-266] code scan #27
1 parent 1078612 commit 70dd34e

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

services/proxy/pkg/command/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func loadMiddlewares(logger log.Logger, cfg *config.Config,
333333
chimiddleware.RequestID,
334334
middleware.AccessLog(logger),
335335
middleware.ContextLogger(logger),
336-
middleware.HTTPSRedirect,
336+
middleware.HTTPSRedirect(cfg.Commons.OcisURL),
337337
middleware.Security(cspConfig),
338338
router.Middleware(serviceSelector, cfg.PolicySelector, cfg.Policies, logger),
339339
middleware.Authentication(
Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
package middleware
22

33
import (
4-
"fmt"
54
"net/http"
5+
"net/url"
6+
"strings"
67
)
78

8-
// HTTPSRedirect redirects insecure requests to https
9-
func HTTPSRedirect(next http.Handler) http.Handler {
10-
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
11-
proto := req.Header.Get("x-forwarded-proto")
12-
if proto == "http" || proto == "HTTP" {
13-
http.Redirect(res, req, fmt.Sprintf("https://%s%s", req.Host, req.URL), http.StatusPermanentRedirect)
14-
return
9+
// HTTPSRedirect creates middleware that redirects insecure requests to HTTPS using a trusted base URL.
10+
func HTTPSRedirect(trustedBaseURL string) func(http.Handler) http.Handler {
11+
var trustedHost string
12+
if trustedBaseURL != "" {
13+
if u, err := url.Parse(trustedBaseURL); err == nil {
14+
trustedHost = u.Host
1515
}
16+
}
1617

17-
next.ServeHTTP(res, req)
18-
})
18+
return func(next http.Handler) http.Handler {
19+
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
20+
proto := req.Header.Get("x-forwarded-proto")
21+
if proto == "http" || proto == "HTTP" {
22+
if strings.TrimSpace(trustedHost) != "" {
23+
target := &url.URL{
24+
Scheme: "https",
25+
Host: trustedHost,
26+
Path: req.URL.Path,
27+
RawQuery: req.URL.RawQuery,
28+
}
29+
http.Redirect(res, req, target.String(), http.StatusPermanentRedirect)
30+
return
31+
}
32+
// No trusted host configured; do not perform a redirect
33+
}
34+
35+
next.ServeHTTP(res, req)
36+
})
37+
}
1938
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package middleware
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"gotest.tools/v3/assert"
9+
)
10+
11+
func TestHTTPSRedirect_UsesTrustedHost(t *testing.T) {
12+
downstream := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
13+
w.WriteHeader(http.StatusOK)
14+
})
15+
mw := HTTPSRedirect("https://trusted.ocis.local")(downstream)
16+
17+
req := httptest.NewRequest(http.MethodGet, "/foo?bar=1", nil)
18+
req.Host = "non-trusted.example"
19+
req.Header.Set("X-Forwarded-Proto", "http")
20+
21+
rr := httptest.NewRecorder()
22+
mw.ServeHTTP(rr, req)
23+
24+
assert.Equal(t, rr.Code, http.StatusPermanentRedirect)
25+
location := rr.Header().Get("Location")
26+
assert.Equal(t, location, "https://trusted.ocis.local/foo?bar=1")
27+
}

0 commit comments

Comments
 (0)