Fixes an issue with x-forwarded headers and checkOrigin'#15534
Open
qzio wants to merge 3 commits intowithastro:mainfrom
Open
Fixes an issue with x-forwarded headers and checkOrigin'#15534qzio wants to merge 3 commits intowithastro:mainfrom
qzio wants to merge 3 commits intowithastro:mainfrom
Conversation
🦋 Changeset detectedLatest commit: c4153ca The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
9db6877 to
270cd34
Compare
Contributor
|
We're talking about this on Discord, not ready yet for review. |
500b9d5 to
12c6d50
Compare
5e246b4 to
f5645cf
Compare
f5645cf to
42a1bb9
Compare
Contributor
|
I do think I understand the issue you're wanting to fix better now. Here's my feedback:
|
1 task
42a1bb9 to
978e6d3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
Before this change, the
validateForwardedHeadersonly used thematchPatternwith a hardcoded hostname ofexample.comwhen validating theX-Forwarded-Protoheader.This meant that NOT having
example.comin thesecurity.allowedDomainswould always result in an emptyresult.proto.Which meant that when having
security.checkOriginto true, it would make it impossible to get POST's through unless the host was localhost, example.com, or by addingexample.cominto thesecurity.allowedDomainslist.The middleware in
packages/astro/src/core/app/middlewares.tschecks the origin header against the parsed request, and includes the protocol in that check.So, unless the protocol are parsed correctly, the check will fail.
This PR changes the validateForwardedHeaders to require allowedDomains up front (since it was already semi-required).
It also changes to check the entry in allowedDomains (not against the hard coded
example.com value).It also expects only one entry in
security.allowedDomainsper hostname.However there's a bit of a chicken and the egg problem here since the host check would benifit (I guess) from the protocol and port check to come before, but it's tricky to get those unless we have the host beforehand...
Testing
The relevant tests was updated to always include a
x-forwarded-hostheader (otherwise we just return early).I also had to add
findRequestPorttonode.tsto get the testprefers port from hosttest to pass. However - this was a hack to get the test to pass, and I'm reluctant to the solution in this pr. I did not found a way to reliable get the port out of the request object...Docs
The docs are a bit confusing for checkOrigin.
It states that checkOrigin checks for
pathnamebut I would expect, and it does, checkorigin.And if the astro server is behind a reverse proxy,
security.allowedDomainswould be required to have a functionalcheckOrigin.NOTE
A smaller PR would be to change the
matchPatternintomatchProtocolin the previousvalidate-headers.tsbut this is something I realised after I refactored the function and made this bigger PR...Here's the smaller PR: #15544
If this PR is considered to big/scary/buggy/bad/etc I can make a small pr with just that funcion call changed, and it would solve the "bug" for now.
However, I do think it's a bit scary to allow multiple entries on
security.allowedDomainsfor a single hostname, and to check all of them for any matching host, protocol or port.-> I think this PR has some value.