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

feat: Add support for X-Forwarded-Proto header #665

Merged
merged 5 commits into from
Oct 13, 2021

Conversation

amoshydra
Copy link
Contributor

@amoshydra amoshydra commented Mar 16, 2021

Related issue

Addresses #153
Context: #153 (comment)

Proposed changes

Check X-Forwarded-Proto header inside proxy.(*Proxy) EnrichRequestedURL using the same logic in #638

Checklist

  • I have read the contributing guidelines.
  • I have read the security policy.
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security. vulnerability, I
    confirm that I got green light (please contact
    security@ory.sh) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature
    works.
  • I have added or changed the documentation.

Further comments

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

Thank you, this looks great!

Could you please add a test or two to ensure this doesn't regress in future changes?

@aeneasr aeneasr self-assigned this Mar 29, 2021
@aeneasr
Copy link
Member

aeneasr commented Apr 23, 2021

Are you still up for the changes? :) If you need any help, let us know!

@amoshydra
Copy link
Contributor Author

amoshydra commented May 1, 2021

Hi @aeneasr I am very sorry for the lack of update. I hadn't been spending time on using oathkeeper in the past weeks.


Could you please add a test or two to ensure this doesn't regress in future changes?

I have originally assumed that I could add additional test onto test/reload/run.sh script. But I later realise it is meant for testing the reload functionality of oathkeeper.

Where should I add the test to? Is it appropriate to create a new folder for this? For example:

test/forwarded-for-header/run.sh

@amoshydra amoshydra force-pushed the x-forwarded-proto-in-proxy branch from 07876ee to 0a089d9 Compare May 2, 2021 15:51
@aeneasr
Copy link
Member

aeneasr commented May 2, 2021

No problem! The best place to test this is probably here: https://github.com/ory/oathkeeper/blob/0a089d9f513160c9792027bd2057a2180a1be3a6/proxy/proxy_test.go#L47

@amoshydra
Copy link
Contributor Author

Hi @aeneasr, I have added some new tests that add X-Forwarded-Proto header. The tests are added in test/forwarded-header/run.sh


Before commit: 62e657e

PASS: 4
- Executing request against a HTTP rule -> 200
- Executing request against a HTTP rule with forwrded proto HTTP -> 200
- Executing request against a HTTPS rule -> 404
- Executing request against a HTTPS rule with forwarded proto HTTP -> 404
FAILED: 2
- Executing request against a HTTP rule with forwrded proto HTTPS -> 404
- Executing request against a HTTPS rule with forwarded proto HTTPS -> 200

After commit: 62e657e

PASS: 6
- Executing request against a HTTP rule -> 200
- Executing request against a HTTP rule with forwrded proto HTTP -> 200
- Executing request against a HTTP rule with forwrded proto HTTPS -> 404
- Executing request against a HTTPS rule -> 404
- Executing request against a HTTPS rule with forwarded proto HTTP -> 404
- Executing request against a HTTPS rule with forwarded proto HTTPS -> 200

However,

(1)
in all of these tests, r.TLS is always nil. I am not sure how to setup oathkeeper in such a way that r.TLS is not nil.

Reference

func EnrichRequestedURL(r *http.Request) {
	r.URL.Scheme = "http"
	r.URL.Host = r.Host
	if r.TLS != nil || strings.EqualFold(r.Header.Get("X-Forwarded-Proto"), "https") {
        // ^^^^^
        //
        // In the tests, r.TLS is always `nil`.

		r.URL.Scheme = "https"
	}
}

@aeneasr
Copy link
Member

aeneasr commented May 2, 2021

Cool!

Yeah, to enable HTTPS you would need to use httptest.NewTLSServer similar to here. This will set up a TLS server with a self-signed certificate. To avoid errors, you can use the Client method.

If you want, you can set up a minimal new test case just for that function with both TLS enabled and disabled!

@aeneasr aeneasr added the pending reply Awaiting reply of author or contributor. Issue will be closed on inactivity. label May 10, 2021
@aeneasr
Copy link
Member

aeneasr commented May 11, 2021

Are you still up for the changes? 🧐 If you need any help, let us know!

I'll convert this to a draft in the meanwhile. When it's ready for review again, please remove the draft indicator! 👀

@aeneasr aeneasr marked this pull request as draft May 11, 2021 09:11
@vallahaye
Copy link

Hi @aeneasr, do you really think testing is necessary for such a simple feature?
It's literally a one-line change that seems to have little impact on the behavior of the proxy.
Also, the same functionality was added to the decision API and merged without adding additional tests (#638).
Let me know what you think.

@aeneasr
Copy link
Member

aeneasr commented Sep 7, 2021

Tests are important to open source projects because the allow reviewers to better judge if the changes are doing what was intended. They also ensure that your changes never regress / break when someone else works on code that is related to this.

If you need help with the tests feel free to push your work in progress and ping myself or another maintainer 🙋

@dwmunster
Copy link

Does the ./test/forwarded-header/run.sh file not sufficiently test the intended behavior? After all, the context of this request is when Oathkeeper is behind an SSL-terminating proxy (e.g. the request to Oathkeeper is HTTP but represents a forwarded HTTPS connection), which is exactly the behavior that file tests.

@aeneasr
Copy link
Member

aeneasr commented Sep 29, 2021

Added the test I was looking for: 67221fb

Will merge when tests pass :)

@aeneasr aeneasr marked this pull request as ready for review September 29, 2021 10:26
@aeneasr aeneasr removed the pending reply Awaiting reply of author or contributor. Issue will be closed on inactivity. label Sep 29, 2021
@codecov
Copy link

codecov bot commented Oct 12, 2021

Codecov Report

Merging #665 (cb3cded) into master (057293f) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #665      +/-   ##
==========================================
+ Coverage   62.47%   62.51%   +0.04%     
==========================================
  Files         102      102              
  Lines        4813     4813              
==========================================
+ Hits         3007     3009       +2     
+ Misses       1531     1530       -1     
+ Partials      275      274       -1     
Impacted Files Coverage Δ
proxy/proxy.go 72.34% <100.00%> (+2.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c89737b...cb3cded. Read the comment docs.

@aeneasr aeneasr merged commit a8c9354 into ory:master Oct 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants