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

Fix Access-Control-Request-Method header #4424

Merged
merged 2 commits into from
Jun 7, 2018
Merged

Fix Access-Control-Request-Method header #4424

merged 2 commits into from
Jun 7, 2018

Conversation

swissspidy
Copy link
Contributor

At least since #3578, the Access-Control-Request-Method HTTP header for preflight requests doesn't contain the actual request method, but instead the origin.

That's not correct.

server/utils.js Outdated
@@ -42,7 +42,7 @@ export function addCorsSupport (req, res) {
}

res.setHeader('Access-Control-Allow-Origin', req.headers.origin)
res.setHeader('Access-Control-Request-Method', req.headers.origin)
res.setHeader('Access-Control-Request-Method', req.method)
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
res.setHeader('Access-Control-Allow-Headers', req.headers.origin)
Copy link
Member

Choose a reason for hiding this comment

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

Interestingly this seems also wrong 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, you're right.

Copy link
Member

Choose a reason for hiding this comment

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

If you can fix this one too I'll merge the PR 👍

@timneutkens
Copy link
Member

I did some research into this and found that Access-Control-Request-Method is a request only header, we shouldn't reply with that header, instead, we have to reply with the correct allowed headers.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Request-Method

I've updated the PR to reflect this. cc @arunoda please review

@timneutkens timneutkens requested a review from arunoda May 25, 2018 10:32
@swissspidy
Copy link
Contributor Author

Thanks @timneutkens!

Copy link

@pranaygp pranaygp left a comment

Choose a reason for hiding this comment

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

Code LGTM.

Just to confirm intended behavior, the server now replies saying "Any header you request of me is allowed"? If so, can we not instead just use '*'? https://fetch.spec.whatwg.org/#http-new-header-syntax

@timneutkens
Copy link
Member

I should have linked to MDN as that's why I made the choice not to use *:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers

There's a compatibility note there saying * has not been implemented in many browsers.

The wildcard value (*) that is mentioned in the latest specification, is not yet implemented in browsers:

@timneutkens timneutkens merged commit a411b35 into vercel:canary Jun 7, 2018
@pranaygp
Copy link

pranaygp commented Jun 7, 2018

Nice catch @timneutkens 🙇‍♀️

@swissspidy swissspidy deleted the Access-Control-Request-Method branch June 7, 2018 18:48
lependu pushed a commit to lependu/next.js that referenced this pull request Jun 19, 2018
* Fix Access-Control-Request-Method header

* Make OPTIONS request work
@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants