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

Drop Origin & Accept from Access-Control-Allow-Headers value #3225

Conversation

sideshowbarker
Copy link
Contributor

@sideshowbarker sideshowbarker commented May 30, 2021

This change drops the Origin and Accept header names from the recommended value for the CORS Access-Control-Allow-Headers header. Per the CORS protocol, it’s not necessary or useful to include them.

Details:

Per-spec at https://fetch.spec.whatwg.org/#forbidden-header-name, Origin is a “forbidden header name” set by the browser and that frontend JavaScript code is never allowed to set.

So the value of Access-Control-Allow-Headers isn’t relevant to Origin or in general to other headers set by the browser itself — the browser never ever consults the Access-Control-Allow-Headers value to confirm that it’s OK for the request to include an Origin header.

And per-spec at https://fetch.spec.whatwg.org/#cors-safelisted-request-header, Accept is a “CORS-safelisted request-header”, which means that browsers allow requests to contain the Accept header regardless of whether the Access-Control-Allow-Headers value contains "Accept".*

So it’s unnecessary for the Access-Control-Allow-Headers to explicitly include Accept. Browsers will not perform a CORS preflight for requests containing an Accept request header.

Related: matrix-org/synapse#10114

Signed-off-by: Michael[tm] Smith <[email protected]>


* There is actually one edge case in which browsers won’t allow an Accept header in a request: If the value contains a “CORS-unsafe request-header byte” https://fetch.spec.whatwg.org/#cors-unsafe-request-header-byte. But in that case, the Accept header is anyway malformed and is therefore not going to have its intended effect. So that edge case doesn’t merit including Accept in the Access-Control-Allow-Headers value.

@turt2live turt2live requested a review from a team May 31, 2021 13:34
@richvdh
Copy link
Member

richvdh commented Jun 2, 2021

sounds plausible to me.

For the record, this was added in #1365, based on Synapse's implementation. The presence of Accept and Origin there dates way back to the first git commit of Synapse.

However, I'm going to leave this for a review from someone who knows more about CORS headers than me.

We should also ask ourselves whether this requires an MSC, but given it's just a recommendation, my inclination is to say not.

@sideshowbarker: do you know if this change has been implemented in any server implementations? I'd be happy to see a PR for Synapse.

sideshowbarker added a commit to sideshowbarker/synapse that referenced this pull request Jun 3, 2021
This change drops the Origin and Accept header names from the value of the
Access-Control-Allow-Headers response header sent by Synapse. Per the CORS
protocol, it’s not necessary or useful to include those header names.

Details:

Per-spec at https://fetch.spec.whatwg.org/#forbidden-header-name, Origin
is a “forbidden header name” set by the browser and that frontend
JavaScript code is never allowed to set.

So the value of Access-Control-Allow-Headers isn’t relevant to Origin or
in general to other headers set by the browser itself — the browser
never ever consults the Access-Control-Allow-Headers value to confirm
that it’s OK for the request to include an Origin header.

And per-spec at https://fetch.spec.whatwg.org/#cors-safelisted-request-header,
Accept is a “CORS-safelisted request-header”, which means that browsers
allow requests to contain the Accept header regardless of whether the
Access-Control-Allow-Headers value contains "Accept".

So it’s unnecessary for the Access-Control-Allow-Headers to explicitly
include Accept. Browsers will not perform a CORS preflight for requests
containing an Accept request header.

Related: matrix-org/matrix-spec-proposals#3225

Signed-off-by: Michael[tm] Smith <[email protected]>
@sideshowbarker
Copy link
Contributor Author

@sideshowbarker: do you know if this change has been implemented in any server implementations?

It’s not been — I started just with raising it here.

I'd be happy to see a PR for Synapse.

OK, raised matrix-org/synapse#10114

This change drops the Origin and Accept header names from the
recommended value for the CORS Access-Control-Allow-Headers header. Per
the CORS protocol, it’s not necessary or useful to include them.

Per-spec at https://fetch.spec.whatwg.org/#forbidden-header-name, Origin
is a “forbidden header name” set by the browser and that frontend
JavaScript code is never allowed to set.

So the value of Access-Control-Allow-Headers isn’t relevant to Origin or
in general to other headers set by the browser itself — the browser
never ever consults the Access-Control-Allow-Headers value to confirm
that it’s OK for the request to include an Origin header.

And per-spec at https://fetch.spec.whatwg.org/#cors-safelisted-request-header,
Accept is a “CORS-safelisted request-header”, which means that browsers
allow requests to contain the Accept header regardless of whether the
Access-Control-Allow-Headers value contains "Accept".

So it’s unnecessary for the Access-Control-Allow-Headers to explicitly
include Accept. Browsers will not perform a CORS preflight for requests
containing an Accept request header.

Related: Related: matrix-org/synapse#10114

Signed-off-by: Michael[tm] Smith <[email protected]>
@sideshowbarker sideshowbarker force-pushed the client-server-api-Access-Control-Allow-Headers-drop-Options-Accept branch from dbd1ef7 to 2481074 Compare June 3, 2021 05:14
@turt2live turt2live requested review from turt2live and removed request for a team June 7, 2021 16:29
@turt2live
Copy link
Member

I don't think this sort of change needs an MSC by the way: if it becomes a problem then we can MSC it, but for the time being it's a clarification-style change.

Copy link
Member

@turt2live turt2live left a comment

Choose a reason for hiding this comment

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

Thanks!

@turt2live turt2live merged commit 56a86e4 into matrix-org:master Jun 23, 2021
turt2live added a commit that referenced this pull request Jun 23, 2021
richvdh pushed a commit to matrix-org/synapse that referenced this pull request Jun 23, 2021
* Drop Origin & Accept from Access-Control-Allow-Headers value

This change drops the Origin and Accept header names from the value of the
Access-Control-Allow-Headers response header sent by Synapse. Per the CORS
protocol, it’s not necessary or useful to include those header names.

Details:

Per-spec at https://fetch.spec.whatwg.org/#forbidden-header-name, Origin
is a “forbidden header name” set by the browser and that frontend
JavaScript code is never allowed to set.

So the value of Access-Control-Allow-Headers isn’t relevant to Origin or
in general to other headers set by the browser itself — the browser
never ever consults the Access-Control-Allow-Headers value to confirm
that it’s OK for the request to include an Origin header.

And per-spec at https://fetch.spec.whatwg.org/#cors-safelisted-request-header,
Accept is a “CORS-safelisted request-header”, which means that browsers
allow requests to contain the Accept header regardless of whether the
Access-Control-Allow-Headers value contains "Accept".

So it’s unnecessary for the Access-Control-Allow-Headers to explicitly
include Accept. Browsers will not perform a CORS preflight for requests
containing an Accept request header.

Related: matrix-org/matrix-spec-proposals#3225

Signed-off-by: Michael[tm] Smith <[email protected]>
richvdh pushed a commit that referenced this pull request Aug 23, 2021
…-Control-Allow-Headers-drop-Options-Accept

Drop Origin & Accept from Access-Control-Allow-Headers value
richvdh pushed a commit that referenced this pull request Aug 23, 2021
richvdh pushed a commit that referenced this pull request Aug 27, 2021
…-Control-Allow-Headers-drop-Options-Accept

Drop Origin & Accept from Access-Control-Allow-Headers value
richvdh pushed a commit that referenced this pull request Aug 27, 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.

3 participants