mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-13 04:07:37 +00:00
feat(lib): add TrustForwardedURI to honor X-Forwarded-Uri for post-login redirect target
This commit is contained in:
+2
-2
@@ -69,7 +69,7 @@ func (t *TraefikOidc) prepareSessionForAuthentication(session *SessionData, csrf
|
||||
// - session: The session data to prepare for authentication.
|
||||
// - redirectURL: The pre-calculated callback URL (redirect_uri) for this middleware instance.
|
||||
func (t *TraefikOidc) defaultInitiateAuthentication(rw http.ResponseWriter, req *http.Request, session *SessionData, redirectURL string) {
|
||||
t.logger.Debugf("Initiating new OIDC authentication flow for request: %s", req.URL.RequestURI())
|
||||
t.logger.Debugf("Initiating new OIDC authentication flow for request: %s", t.originalRequestURI(req))
|
||||
|
||||
// Check and handle redirect limits
|
||||
if err := t.validateRedirectCount(session, rw, req); err != nil {
|
||||
@@ -98,7 +98,7 @@ func (t *TraefikOidc) defaultInitiateAuthentication(rw http.ResponseWriter, req
|
||||
}
|
||||
|
||||
// Clear existing session data and set new authentication state
|
||||
t.prepareSessionForAuthentication(session, csrfToken, nonce, codeVerifier, req.URL.RequestURI())
|
||||
t.prepareSessionForAuthentication(session, csrfToken, nonce, codeVerifier, t.originalRequestURI(req))
|
||||
|
||||
session.MarkDirty()
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package traefikoidc
|
||||
|
||||
import "net/http"
|
||||
|
||||
// originalRequestURI returns the request URI that should be used as the
|
||||
// post-login redirect target. When TrustForwardedURI is enabled and the
|
||||
// X-Forwarded-Uri header is present, that header wins — this lets the
|
||||
// oidcgate forward-auth daemon recover the real URL when nginx/Caddy/etc
|
||||
// proxied a stripped subrequest to /oauth2/auth or /oauth2/start.
|
||||
func (t *TraefikOidc) originalRequestURI(req *http.Request) string {
|
||||
if t.trustForwardedURI {
|
||||
if v := req.Header.Get("X-Forwarded-Uri"); v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return req.URL.RequestURI()
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package traefikoidc
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOriginalRequestURI_DefaultOff(t *testing.T) {
|
||||
tr := &TraefikOidc{trustForwardedURI: false}
|
||||
req := httptest.NewRequest(http.MethodGet, "/protected?x=1", nil)
|
||||
req.Header.Set("X-Forwarded-Uri", "/spoofed")
|
||||
if got := tr.originalRequestURI(req); got != "/protected?x=1" {
|
||||
t.Fatalf("default-off: want /protected?x=1, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOriginalRequestURI_TrustEnabled(t *testing.T) {
|
||||
tr := &TraefikOidc{trustForwardedURI: true}
|
||||
req := httptest.NewRequest(http.MethodGet, "/protected?x=1", nil)
|
||||
req.Header.Set("X-Forwarded-Uri", "/real?y=2")
|
||||
if got := tr.originalRequestURI(req); got != "/real?y=2" {
|
||||
t.Fatalf("trust-on with header: want /real?y=2, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOriginalRequestURI_TrustEnabledNoHeader(t *testing.T) {
|
||||
tr := &TraefikOidc{trustForwardedURI: true}
|
||||
req := httptest.NewRequest(http.MethodGet, "/protected", nil)
|
||||
if got := tr.originalRequestURI(req); got != "/protected" {
|
||||
t.Fatalf("trust-on no header: want /protected, got %q", got)
|
||||
}
|
||||
}
|
||||
@@ -201,6 +201,7 @@ func NewWithContext(ctx context.Context, config *Config, next http.Handler, name
|
||||
}(),
|
||||
forceHTTPS: config.ForceHTTPS,
|
||||
enablePKCE: config.EnablePKCE,
|
||||
trustForwardedURI: config.TrustForwardedURI,
|
||||
overrideScopes: config.OverrideScopes,
|
||||
strictAudienceValidation: config.StrictAudienceValidation,
|
||||
allowOpaqueTokens: config.AllowOpaqueTokens,
|
||||
|
||||
@@ -125,6 +125,13 @@ type Config struct {
|
||||
// ClientAssertionAlg is the JWS signing algorithm. Defaults to RS256.
|
||||
// Supported: RS256/384/512, PS256/384/512, ES256/384/512.
|
||||
ClientAssertionAlg string `json:"clientAssertionAlg,omitempty"`
|
||||
|
||||
// TrustForwardedURI, when true, makes the middleware prefer the
|
||||
// X-Forwarded-Uri request header (set by an upstream reverse proxy)
|
||||
// over req.URL when capturing the "where was the user going" target
|
||||
// stored for post-login redirect. Used by the oidcgate standalone
|
||||
// daemon. Default false preserves the Traefik plugin behavior exactly.
|
||||
TrustForwardedURI bool `json:"trustForwardedURI,omitempty"`
|
||||
}
|
||||
|
||||
// loadCACertPool assembles an x509.CertPool from CACertPath and CACertPEM.
|
||||
|
||||
Reference in New Issue
Block a user