-
-
Notifications
You must be signed in to change notification settings - Fork 18
feat support reverse proxy #178
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
base: v2
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ func (logtoClient *LogtoClient) HandleSignInCallback(request *http.Request) erro | |
| } | ||
|
|
||
| callbackUri := GetOriginRequestUrl(request) | ||
| if logtoClient.trustForwardedHeader { | ||
| callbackUri = getForwaredRequestUrl(request) | ||
| } | ||
|
Comment on lines
19
to
+22
|
||
| code, retrieveCodeErr := core.VerifyAndParseCodeFromCallbackUri(callbackUri, signInSession.RedirectUri, signInSession.State) | ||
| if retrieveCodeErr != nil { | ||
| return retrieveCodeErr | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,26 @@ func GetOriginRequestUrl(request *http.Request) string { | |||||||||||||||||||||||||
| return getRequestProtocol(request) + "://" + request.Host + request.RequestURI | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func getForwaredRequestUrl(request *http.Request) string { | ||||||||||||||||||||||||||
| proto := getRequestProtocol(request) | ||||||||||||||||||||||||||
| host := getForwaredRequestHost(request) | ||||||||||||||||||||||||||
| uri := getForwaredRequestRequestUri(request) | ||||||||||||||||||||||||||
| return proto + "://" + host + uri | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+15
to
+20
|
||||||||||||||||||||||||||
| func getForwaredRequestHost(request *http.Request) string { | ||||||||||||||||||||||||||
| host := request.Header.Get("X-Forwarded-Host") | ||||||||||||||||||||||||||
| if host != "" { | ||||||||||||||||||||||||||
| return host | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return request.Host | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| func getForwaredRequestRequestUri(request *http.Request) string { | ||||||||||||||||||||||||||
| uri := request.Header.Get("X-Forwarded-Url") | ||||||||||||||||||||||||||
| if uri != "" { | ||||||||||||||||||||||||||
| return uri | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+29
to
+32
|
||||||||||||||||||||||||||
| uri := request.Header.Get("X-Forwarded-Url") | |
| if uri != "" { | |
| return uri | |
| } | |
| uri := request.Header.Get("X-Original-URI") | |
| if uri != "" { | |
| return uri | |
| } | |
| uri = request.Header.Get("X-Forwarded-Path") | |
| if uri != "" { | |
| return uri | |
| } |
Copilot
AI
Jan 18, 2026
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.
There's a spelling error in the function names throughout the code. "Forwared" should be spelled "Forwarded" (with two 'd's). This affects:
getForwaredRequestUrlgetForwaredRequestHostgetForwaredRequestRequestUri- Test function names:
TestGetForwaredRequestUrlShouldReturnXForwardedIfPresent,TestGetForwaredRequestHostShouldFallbackToRequestHost, andTestGetForwaredRequestRequestUriShouldFallbackToRequestURI
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,3 +130,43 @@ func createTestToken(resource string) (string, error) { | |
|
|
||
| return token, nil | ||
| } | ||
|
|
||
| func TestGetForwaredRequestUrlShouldReturnXForwardedIfPresent(t *testing.T) { | ||
| req, err := http.NewRequest("GET", "http://example.com/path?query=1", nil) | ||
| assert.Nil(t, err) | ||
| // Ensure RequestURI is set like in real servers | ||
| req.RequestURI = "/path?query=1" | ||
|
|
||
| req.Header.Add("X-Forwarded-Host", "forwarded.example.com") | ||
| req.Header.Add("X-Forwarded-Url", "/forwarded-path?query=2") | ||
| req.Header.Add("X-Forwarded-Proto", "https") | ||
|
|
||
| url := getForwaredRequestUrl(req) | ||
|
|
||
| assert.Equal(t, "https://forwarded.example.com/forwarded-path?query=2", url) | ||
| } | ||
|
|
||
| func TestGetForwaredRequestHostShouldFallbackToRequestHost(t *testing.T) { | ||
| req, _ := http.NewRequest("GET", "http://example.com/", nil) | ||
| req.RequestURI = "/" | ||
|
|
||
| host := getForwaredRequestHost(req) | ||
|
|
||
| assert.Equal(t, "example.com", host) | ||
|
|
||
| req.Header.Add("X-Forwarded-Host", "proxied.example.com") | ||
| host = getForwaredRequestHost(req) | ||
| assert.Equal(t, "proxied.example.com", host) | ||
| } | ||
|
|
||
| func TestGetForwaredRequestRequestUriShouldFallbackToRequestURI(t *testing.T) { | ||
| req, _ := http.NewRequest("GET", "http://example.com/api", nil) | ||
| req.RequestURI = "/api" | ||
|
|
||
| uri := getForwaredRequestRequestUri(req) | ||
| assert.Equal(t, "/api", uri) | ||
|
|
||
| req.Header.Add("X-Forwarded-Url", "/proxied/api?x=1") | ||
| uri = getForwaredRequestRequestUri(req) | ||
| assert.Equal(t, "/proxied/api?x=1", uri) | ||
| } | ||
|
Comment on lines
+134
to
+172
|
||
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.
Security concern: When
trustForwardedHeaderis enabled, the forwarded headers are trusted without any validation. This could be exploited if the application is not actually behind a properly configured reverse proxy, or if the proxy doesn't strip client-provided X-Forwarded-* headers.Consider adding: