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

Use UsePortInRedirects only if enabled #3787

Merged
merged 1 commit into from
Feb 21, 2019

Conversation

aledbf
Copy link
Member

@aledbf aledbf commented Feb 20, 2019

What this PR does / why we need it:

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #

letsencrypt/pebble#200

Special notes for your reviewer:

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 20, 2019
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 20, 2019
@@ -1133,7 +1133,7 @@ stream {

return 497;
{{ else }}
return {{ $all.Cfg.HTTPRedirectCode }} https://$best_http_host$request_uri;
return {{ $all.Cfg.HTTPRedirectCode }} https://$host$request_uri;
Copy link
Member

Choose a reason for hiding this comment

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

I think we should stick to $best_http_host and trip :<port> instead if it exists.

Otherwise requests coming through an another reverse proxy with X-Forwarded-Host will get an incorrect Location.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we should stick to $best_http_host and trip :<port> instead if it exists.

Ok but that should be done in lua (runtime value). That's fine to you?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like

--- a/rootfs/etc/nginx/template/nginx.tmpl
+++ b/rootfs/etc/nginx/template/nginx.tmpl
@@ -1021,6 +1021,25 @@ stream {
             {{ end }}

             rewrite_by_lua_block {
+              {{/* redirect to HTTPS can be achieved forcing the redirect or having a SSL Certificate configured for the server */}}
+              {{ if (or $location.Rewrite.ForceSSLRedirect (and (not (empty $server.SSLCert.PemFileName)) $location.Rewrite.SSLRedirect)) }}
+              {{ if not (isLocationInLocationList $location $all.Cfg.NoTLSRedirectLocations) }}
+              # enforce ssl on server side
+              if ngx.var.redirect_to_https == 1 {
+                  {{ if $location.UsePortInRedirects }}
+                  -- using custom ports require a different rewrite directive
+                  {{ local redirect_port = (printf ":%v" $all.ListenPorts.HTTPS) }}
+                  -- I'm not sure about following
+                  -- error_page 497 ={{ $all.Cfg.HTTPRedirectCode }} https://$host{{ $redirect_port }}$request_uri;
+
+                  ngx.exit(497)
+                  {{ else }}
+                  return ngx.redirect("full url without port", {{ $all.Cfg.HTTPRedirectCode }})
+                  {{ end }}
+              }
+              {{ end }}
+              {{ end }}
+
                 balancer.rewrite()
             }

@@ -1121,24 +1140,6 @@ stream {
             set $proxy_upstream_name    "{{ buildUpstreamName $location }}";
             set $proxy_host             $proxy_upstream_name;

-            {{/* redirect to HTTPS can be achieved forcing the redirect or having a SSL Certificate configured for the server */}}
-            {{ if (or $location.Rewrite.ForceSSLRedirect (and (not (empty $server.SSLCert.PemFileName)) $location.Rewrite.SSLRedirect)) }}
-            {{ if not (isLocationInLocationList $location $all.Cfg.NoTLSRedirectLocations) }}
-            # enforce ssl on server side
-            if ($redirect_to_https) {
-                {{ if $location.UsePortInRedirects }}
-                # using custom ports require a different rewrite directive
-                {{ $redirect_port := (printf ":%v" $all.ListenPorts.HTTPS) }}
-                error_page 497 ={{ $all.Cfg.HTTPRedirectCode }} https://$host{{ $redirect_port }}$request_uri;
-
-                return 497;
-                {{ else }}
-                return {{ $all.Cfg.HTTPRedirectCode }} https://$best_http_host$request_uri;
-                {{ end }}
-            }
-            {{ end }}
-            {{ end }}
-

@aledbf aledbf force-pushed the port-in-redirect branch 3 times, most recently from e6c3e9c to 53de0ea Compare February 21, 2019 15:10
@aledbf
Copy link
Member Author

aledbf commented Feb 21, 2019

@ElvinEfendi ready for another review

return host_port[0]
else
return host_port[1]
end;
Copy link
Member

Choose a reason for hiding this comment

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

Why's the else condition here? Why not only return host_port[0]?

Copy link
Member Author

@aledbf aledbf Feb 21, 2019

Choose a reason for hiding this comment

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

Using return host_port[0]; returns ""

Copy link
Member

Choose a reason for hiding this comment

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

that's because in Lua indexes start from 1, so you need to return host_port[1]

Expect(resp.Header.Get("Location")).Should(Equal(fmt.Sprintf("https://%v/", host)))
})

It("should not use ports or X-Forwarded-Host during the HTTP to HTTPS redirection", func() {
Copy link
Member

Choose a reason for hiding this comment

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

I think it should consider X-Forwarded-Host during redirection. This test is passing only because you aren't trusted x-forwarded-headers.

@aledbf aledbf force-pushed the port-in-redirect branch 2 times, most recently from 9885958 to eb8f929 Compare February 21, 2019 16:31
@ElvinEfendi
Copy link
Member

@ElvinEfendi
Copy link
Member

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 21, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aledbf, ElvinEfendi

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit f04361c into kubernetes:master Feb 21, 2019
@aledbf aledbf deleted the port-in-redirect branch February 22, 2019 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants