Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add middleware with support proxy headers #2453

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,28 @@ const (
// advertised as supported by the target resource. Returning an Allow header is mandatory
// for status 405 (method not found) and useful for the OPTIONS method in responses.
// See RFC 7231: https://datatracker.ietf.org/doc/html/rfc7231#section-7.4.1
HeaderAllow = "Allow"
HeaderAuthorization = "Authorization"
HeaderContentDisposition = "Content-Disposition"
HeaderContentEncoding = "Content-Encoding"
HeaderContentLength = "Content-Length"
HeaderContentType = "Content-Type"
HeaderCookie = "Cookie"
HeaderSetCookie = "Set-Cookie"
HeaderIfModifiedSince = "If-Modified-Since"
HeaderLastModified = "Last-Modified"
HeaderLocation = "Location"
HeaderRetryAfter = "Retry-After"
HeaderUpgrade = "Upgrade"
HeaderVary = "Vary"
HeaderWWWAuthenticate = "WWW-Authenticate"
HeaderXForwardedFor = "X-Forwarded-For"
HeaderXForwardedProto = "X-Forwarded-Proto"
HeaderXForwardedProtocol = "X-Forwarded-Protocol"
HeaderAllow = "Allow"
HeaderAuthorization = "Authorization"
HeaderContentDisposition = "Content-Disposition"
HeaderContentEncoding = "Content-Encoding"
HeaderContentLength = "Content-Length"
HeaderContentType = "Content-Type"
HeaderCookie = "Cookie"
HeaderForwarded = "Forwarded"
HeaderSetCookie = "Set-Cookie"
HeaderIfModifiedSince = "If-Modified-Since"
HeaderLastModified = "Last-Modified"
HeaderLocation = "Location"
HeaderRetryAfter = "Retry-After"
HeaderUpgrade = "Upgrade"
HeaderVary = "Vary"
HeaderWWWAuthenticate = "WWW-Authenticate"
HeaderXForwardedFor = "X-Forwarded-For"
HeaderXForwardedHost = "X-Forwarded-Host"
HeaderXForwardedPrefix = "X-Forwarded-Prefix"
HeaderXForwardedProto = "X-Forwarded-Proto"
HeaderXForwardedProtocol = "X-Forwarded-Protocol"

HeaderXForwardedSsl = "X-Forwarded-Ssl"
HeaderXUrlScheme = "X-Url-Scheme"
HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
Expand Down
50 changes: 50 additions & 0 deletions middleware/proxy_headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package middleware

import (
"net/url"
"regexp"
"strings"

"github.com/labstack/echo/v4"
)

var (
protoRegex = regexp.MustCompile(`(?i)(?:proto=)(https|http)`)
)

func ProxyHeaders() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if fwd := c.RealIP(); fwd != "" {
c.Request().RemoteAddr = fwd
}

if scheme := getScheme(c); scheme != "" {
c.Request().URL.Scheme = scheme
}

if c.Request().Header.Get(echo.HeaderXForwardedHost) != "" {
c.Request().Host = c.Request().Header.Get(echo.HeaderXForwardedHost)
Copy link
Contributor

@aldas aldas May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting host from proxied request is little bit more complex and has security nuances. Please see https://github.com/labstack/echo/blob/master/ip.go

}

if prefix := c.Request().Header.Get(echo.HeaderXForwardedPrefix); prefix != "" {
c.Request().RequestURI, _ = url.JoinPath(prefix, c.Request().RequestURI)
c.Request().URL.Path, _ = url.JoinPath(prefix, c.Request().URL.Path)
}
return next(c)
}
}
}

func getScheme(c echo.Context) string {
var scheme string

if proto := c.Request().Header.Get(echo.HeaderXForwardedProto); proto != "" {
scheme = strings.ToLower(proto)
} else if proto = c.Request().Header.Get(echo.HeaderForwarded); proto != "" {
if match := protoRegex.FindStringSubmatch(proto); len(match) > 1 {
scheme = strings.ToLower(match[1])
}
}
return scheme
}