8000 Add Vary: Origin to CORS response headers by pbrisbin · Pull Request #164 · freckle/freckle-app · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add Vary: Origin to CORS response headers #164

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

Merged
merged 1 commit into from
May 10, 2024
Merged

Add Vary: Origin to CORS response headers #164

merged 1 commit into from
May 10, 2024

Conversation

pbrisbin
Copy link
Member

Because the middleware is designed to allow multiple possible origins,
by sending back the origin given as Access-Control-Allowed-Origin (if
it's valid), we need to ensure those responses aren't cached and served
to other origins, otherwise they will get CORS errors. Practically
speaking, the request Origin should factor into any cache-key.
According to the HTTP spec, you do this by including that header in
Vary, so that's what this change does.

Unfortunately, this may not always work. Fastly is known to work
correctly1 but CloudFront does not2. For CloudFront, you also have
to configure your distribution itself to include Origin in the cache
key via your CachePolicy, and this Vary header is ignored.

Footnotes

  1. https://www.fastly.com/blog/getting-most-out-vary-fastly

  2. https://stackoverflow.com/a/16546364

Because the middleware is designed to allow multiple possible origins,
by sending back the origin given as `Access-Control-Allowed-Origin` (if
it's valid), we need to ensure those responses aren't cached and served
to other origins, otherwise they will get CORS errors. Practically
speaking, the request `Origin` should factor into any cache-key.
According to the HTTP spec, you do this by including that header in
`Vary`, so that's what this change does.

Unfortunately, this may not always work. Fastly is known to work
correctly[^1] but CloudFront does not[^2]. For CloudFront, you also have
to configure your distribution itself to include `Origin` in the cache
key via your `CachePolicy`, and this `Vary` header is ignored.

[^1]: https://www.fastly.com/blog/getting-most-out-vary-fastly
[^2]: https://stackoverflow.com/a/16546364
@pbrisbin pbrisbin requested review from a team and jason-lieb and removed request for a team May 10, 2024 14:22
@pbrisbin pbrisbin merged commit 6ce7642 into main May 10, 2024
7 checks passed
@pbrisbin pbrisbin deleted the pb/vary-origin branch May 10, 2024 15:24
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.

2 participants
0