question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add CORS header to responses

See original GitHub issue

GitHub Pages adds access-control-allow-origin: * to all pages, so for static content hosted by Cloudflare Workers Sites, I’m guessing it should be safe to do the same.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Cherrycommented, May 17, 2021

I think adding Acess-Control-Allow-Origin: * to assets returned by kv-asset-handler would be totally fine, but should definitely be a SemVer major change. For static assets, I can’t think of any security issues doing this, but would encourage anyone with more info to correct me.

As others have mentioned, Workers are used for other things like APIs where you may very much want to restrict your CORS configuration, but for these use-cases you wouldn’t use kv-asset-handler to serve an asset, and would be manually controlling this anyway.

My concern with making this an optional configuration option would be for an ever-growing scope with headers: which headers do we decide should be first-party configuration options (security ones?), and which should be left to the user to maintain and configure? It’s pretty trivial to override headers set on the Response anyway, but this definitely adds a level of ambiguity.

As a comparison, Netlify seems to handle this with a _headers file: https://docs.netlify.com/routing/headers/. Something like this may make sense for Pages, but kv-asset-handler is definitely more code than config driven.

1reaction
GregBrimblecommented, Oct 27, 2020

Hi Aaron, thanks for your input. I would love to hear why you think that CORS should be disabled by default. Do you have an example of static content that would be unsafe to attach the access-control-allow-origin header onto? You’re totally correct in saying that it’s easy to add yourself (that’s what I’ve done in the interim), but my thinking is that Workers Sites is very similar to GitHub Pages which do add that header by default.

For anyone else looking to add the header manually, in your workers-site/index.js, handleEvent function, you just need to grab the response from getAssetFromKV and inject the header:

const response = await getAssetFromKV(event);
response.headers.set("access-control-allow-origin", "*");
return response;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cross-Origin Resource Sharing (CORS) - MDN Web Docs
Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, ...
Read more >
CORS and the Access-Control-Allow-Origin response header
The Access-Control-Allow-Origin header is included in the response from one website to a request originating from another website, and identifies the permitted ...
Read more >
How does the 'Access-Control-Allow-Origin' header work?
The Access-Control-Allow-Origin response header indicates whether the response can be shared with requesting code from ...
Read more >
Add a cross-origin resource sharing ... - AWS Documentation
The following example function adds an Access-Control-Allow-Origin HTTP header to the response if the response doesn't already contain this header.
Read more >
The Access-Control-Allow-Origin Header Explained
Access-Control-Allow-Origin is a CORS header. CORS, or Cross Origin Resource Sharing, is a mechanism for browsers to let a site running at ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found