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

Vary: Origin should be set on non-CORS request #330

Open
ehmicky opened this issue Oct 18, 2024 · 0 comments
Open

Vary: Origin should be set on non-CORS request #330

ehmicky opened this issue Oct 18, 2024 · 0 comments

Comments

@ehmicky
Copy link

ehmicky commented Oct 18, 2024

By default, the Vary: Origin response header is set, which is good. However, it is not set if the Origin request header is missing (i.e. on non-CORS requests).

cors/lib/index.js

Lines 220 to 222 in 53312a5

if (err2 || !origin) {
next(err2);
} else {

That's an error as mentioned in the standard.

In particular, consider what happens if Vary is not used and a server is configured to send Access-Control-Allow-Origin for a certain resource only in response to a CORS request. When a user agent receives a response to a non-CORS request for that resource (for example, as the result of a navigation request), the response will lack Access-Control-Allow-Origin and the user agent will cache that response. Then, if the user agent subsequently encounters a CORS request for the resource, it will use that cached response from the previous non-CORS request, without Access-Control-Allow-Origin.

But if Vary: Origin is used in the same scenario described above, it will cause the user agent to fetch a response that includes Access-Control-Allow-Origin, rather than using the cached response from the previous non-CORS request that lacks Access-Control-Allow-Origin.

Also in this blog post.

The rule here is simple: If your server makes a decision about what to return based on a what’s in a HTTP header, you need to include that header name in your Vary, even if the request didn’t include that header.

One thing to add here: if the Origin request header is ignored when computing any CORS response, then Vary: Origin should not be set (regardless of whether the Origin request header was used or not). In practice, this is when the origin option is false or a string (the default value), as opposed to when it is true, a regular expression, an array or a function. (see #332).

@ehmicky ehmicky added the bug label Oct 18, 2024
@ehmicky ehmicky changed the title Vary: Origin should be set even without Access-Control-Allow-Origin Vary: Origin should be set even without Origin Oct 19, 2024
@ehmicky ehmicky changed the title Vary: Origin should be set even without Origin Vary: Origin should be set on non-CORS request Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants