From f160558bd82ca35443197e52b35dbab576337ac6 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:28:02 -0600 Subject: [PATCH] Support RFC 9110 Forwarded header in scheme extraction --- echo.go | 1 + scheme.go | 27 +++++++++++++++++++++++++++ scheme_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/echo.go b/echo.go index adc76adf1..82ef40cfc 100644 --- a/echo.go +++ b/echo.go @@ -212,6 +212,7 @@ const ( HeaderUpgrade = "Upgrade" HeaderVary = "Vary" HeaderWWWAuthenticate = "WWW-Authenticate" + HeaderForwarded = "Forwarded" HeaderXForwardedFor = "X-Forwarded-For" HeaderXForwardedProto = "X-Forwarded-Proto" HeaderXForwardedProtocol = "X-Forwarded-Protocol" diff --git a/scheme.go b/scheme.go index 1ba4d642b..99a71990c 100644 --- a/scheme.go +++ b/scheme.go @@ -98,6 +98,33 @@ func isTrustedPeer(remoteAddr string, checker *ipChecker) bool { } func schemeFromHeaders(req *http.Request) string { + // RFC 9110 ยง7.6.1 defines a structured Forwarded header whose proto + // parameter carries the scheme used on the incoming connection. Like + // X-Forwarded-Proto, the last element is the one added by the nearest proxy. + if values := req.Header.Values(HeaderForwarded); len(values) > 0 { + proto := "" + for _, value := range values { + for _, element := range strings.Split(value, ",") { + for _, parameter := range strings.Split(element, ";") { + key, val, ok := strings.Cut(strings.TrimSpace(parameter), "=") + if !ok || !strings.EqualFold(strings.TrimSpace(key), "proto") { + continue + } + val = strings.TrimSpace(val) + if len(val) >= 2 && val[0] == '"' && val[len(val)-1] == '"' { + val = val[1 : len(val)-1] + } + proto = val + } + } + } + if proto != "" { + if scheme, ok := canonicalProto(proto); ok { + return scheme + } + return "http" + } + } // When X-Forwarded-Proto is present it is the only header used: a trusted proxy sets it, and the other headers // might be client-supplied values that the proxy did not remove. With several values (repeated header lines or a // comma-separated list) the last one was added by the nearest proxy. diff --git a/scheme_test.go b/scheme_test.go index 07298ec02..189f7a9fd 100644 --- a/scheme_test.go +++ b/scheme_test.go @@ -236,3 +236,33 @@ func TestNewWithConfig_SchemeExtractor(t *testing.T) { assert.Equal(t, "http", c.Scheme()) } + +func TestSchemeFromForwardedHeader(t *testing.T) { + var testCases = []struct { + name string + header string + expected string + }{ + {"simple proto", `proto=https`, "https"}, + {"case insensitive", `Proto=HTTPS`, "https"}, + {"quoted proto", `for=192.0.2.1;proto="https"`, "https"}, + {"last element wins", `proto=https, proto=http`, "http"}, + {"invalid proto rejected", `proto=javascript`, "http"}, + {"no proto falls through", `for=192.0.2.1`, "http"}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.RemoteAddr = "127.0.0.1:1234" + req.Header.Set(HeaderForwarded, tc.header) + assert.Equal(t, tc.expected, ExtractSchemeFromHeaders()(req)) + }) + } +} + +func TestForwardedHeaderIgnoredFromUntrustedPeer(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.RemoteAddr = "203.0.113.10:1234" + req.Header.Set(HeaderForwarded, "proto=https") + assert.Equal(t, "http", ExtractSchemeFromHeaders()(req)) +}