diff --git a/docs/examples/auth/oauth-external-auth/README.md b/docs/examples/auth/oauth-external-auth/README.md index c151be340..9199a6dcf 100644 --- a/docs/examples/auth/oauth-external-auth/README.md +++ b/docs/examples/auth/oauth-external-auth/README.md @@ -25,7 +25,7 @@ metadata: name: application annotations: nginx.ingress.kubernetes.io/auth-url: "https://$host/oauth2/auth" - nginx.ingress.kubernetes.io/auth-signin: "https://$host/oauth2/start?rd=$request_uri" + nginx.ingress.kubernetes.io/auth-signin: "https://$host/oauth2/start?rd=$escaped_request_uri" ... ``` diff --git a/docs/examples/auth/oauth-external-auth/dashboard-ingress.yaml b/docs/examples/auth/oauth-external-auth/dashboard-ingress.yaml index 743f6b49e..17a222939 100644 --- a/docs/examples/auth/oauth-external-auth/dashboard-ingress.yaml +++ b/docs/examples/auth/oauth-external-auth/dashboard-ingress.yaml @@ -3,7 +3,7 @@ kind: Ingress metadata: annotations: nginx.ingress.kubernetes.io/auth-url: "https://$host/oauth2/auth" - nginx.ingress.kubernetes.io/auth-signin: "https://$host/oauth2/start?rd=$request_uri" + nginx.ingress.kubernetes.io/auth-signin: "https://$host/oauth2/start?rd=$escaped_request_uri" name: external-auth-oauth2 namespace: kube-system spec: diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index 1e69a0077..c3d5ba5c9 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -815,14 +815,14 @@ func buildAuthSignURL(input interface{}) string { u, _ := url.Parse(s) q := u.Query() if len(q) == 0 { - return fmt.Sprintf("%v?rd=$pass_access_scheme://$http_host$request_uri", s) + return fmt.Sprintf("%v?rd=$pass_access_scheme://$http_host$escaped_request_uri", s) } if q.Get("rd") != "" { return s } - return fmt.Sprintf("%v&rd=$pass_access_scheme://$http_host$request_uri", s) + return fmt.Sprintf("%v&rd=$pass_access_scheme://$http_host$escaped_request_uri", s) } var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") diff --git a/internal/ingress/controller/template/template_test.go b/internal/ingress/controller/template/template_test.go index f42514c9a..256b5ae0f 100644 --- a/internal/ingress/controller/template/template_test.go +++ b/internal/ingress/controller/template/template_test.go @@ -697,8 +697,8 @@ func TestBuildAuthSignURL(t *testing.T) { cases := map[string]struct { Input, Output string }{ - "default url": {"http://google.com", "http://google.com?rd=$pass_access_scheme://$http_host$request_uri"}, - "with random field": {"http://google.com?cat=0", "http://google.com?cat=0&rd=$pass_access_scheme://$http_host$request_uri"}, + "default url": {"http://google.com", "http://google.com?rd=$pass_access_scheme://$http_host$escaped_request_uri"}, + "with random field": {"http://google.com?cat=0", "http://google.com?cat=0&rd=$pass_access_scheme://$http_host$escaped_request_uri"}, "with rd field": {"http://google.com?cat&rd=$request", "http://google.com?cat&rd=$request"}, } for k, tc := range cases { diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index c57272d72..35516c4bc 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -977,6 +977,7 @@ stream { {{ end }} {{ if $location.ExternalAuth.SigninURL }} + set_escape_uri $escaped_request_uri $request_uri; error_page 401 = {{ buildAuthSignURL $location.ExternalAuth.SigninURL }}; {{ end }}