-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
padremortius
wants to merge
5
commits into
labstack:master
Choose a base branch
from
padremortius:pm/proxyheaders
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
10e734e
Add proxy headers: Forwarded, X-Forwarded-Host, X-Forwarded-Prefix
padremortius ef3bbb9
Add middleware with support proxy headers
padremortius a0b99c6
Switch func getScheme from echo.Context to http.Request.
padremortius 431d0fb
Add get ip address from header Forwarded (RFC-7239)
padremortius 513ada9
Merge branch 'labstack:master' into pm/proxyheaders
padremortius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
var ( | ||
protoRegex = regexp.MustCompile(`(?i)(?:proto=)(https|http)`) | ||
ipRegex = regexp.MustCompile("(?i)(?:for=)([^(;|,| )]+)") | ||
) | ||
|
||
func ProxyHeaders() echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
if fwd := c.Request().Header.Get(echo.HeaderForwarded); fwd != "" { | ||
if match := ipRegex.FindStringSubmatch(fwd); len(match) > 1 { | ||
c.Request().RemoteAddr = strings.Trim(match[1], `"`) | ||
} | ||
} else if fwd := c.RealIP(); fwd != "" { | ||
c.Request().RemoteAddr = fwd | ||
} | ||
|
||
if scheme := getScheme(c.Request()); scheme != "" { | ||
c.Request().URL.Scheme = scheme | ||
} | ||
|
||
if c.Request().Header.Get(echo.HeaderXForwardedHost) != "" { | ||
c.Request().Host = c.Request().Header.Get(echo.HeaderXForwardedHost) | ||
} | ||
|
||
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(r *http.Request) string { | ||
var scheme string | ||
|
||
if proto := r.Header.Get(echo.HeaderXForwardedProto); proto != "" { | ||
scheme = strings.ToLower(proto) | ||
} else if proto := r.Header.Get(echo.HeaderXForwardedProtocol); proto != "" { | ||
scheme = strings.ToLower(proto) | ||
} else if proto = r.Header.Get(echo.HeaderForwarded); proto != "" { | ||
if match := protoRegex.FindStringSubmatch(proto); len(match) > 1 { | ||
scheme = strings.ToLower(match[1]) | ||
} | ||
} | ||
return scheme | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func Test_getScheme(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
r *http.Request | ||
headerName string | ||
whenHeader string | ||
want string | ||
}{ | ||
{ | ||
name: "test only X-Forwarded-Proto: https", | ||
headerName: "X-Forwarded-Proto", | ||
whenHeader: "https", | ||
want: "https", | ||
}, | ||
{ | ||
name: "test only X-Forwarded-Proto: http", | ||
headerName: "X-Forwarded-Proto", | ||
whenHeader: "http", | ||
want: "http", | ||
}, | ||
{ | ||
name: "test only X-Forwarded-Proto: HTTP", | ||
headerName: "X-Forwarded-Proto", | ||
whenHeader: "HTTP", | ||
want: "http", | ||
}, | ||
{ | ||
name: "test only X-Forwarded-Protocol: https", | ||
headerName: "X-Forwarded-Protocol", | ||
whenHeader: "https", | ||
want: "https", | ||
}, | ||
{ | ||
name: "test only X-Forwarded-Protocol: http", | ||
headerName: "X-Forwarded-Protocol", | ||
whenHeader: "http", | ||
want: "http", | ||
}, | ||
{ | ||
name: "test only X-Forwarded-Protocol: HTTP", | ||
headerName: "X-Forwarded-Protocol", | ||
whenHeader: "HTTP", | ||
want: "http", | ||
}, | ||
{ | ||
name: "test only Forwarded https", | ||
headerName: "Forwarded", | ||
whenHeader: "proto=https", | ||
want: "https", | ||
}, | ||
{ | ||
name: "test only Forwarded: http", | ||
headerName: "Forwarded", | ||
whenHeader: "proto=http", | ||
want: "http", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := &http.Request{ | ||
Header: http.Header{ | ||
tt.headerName: []string{tt.whenHeader}, | ||
}, | ||
} | ||
|
||
if got := getScheme(req); got != tt.want { | ||
t.Errorf("getScheme() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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