CORS origins setup does not work

AI-generated summary

A user, mymario, is trying to embed the latest updates from their Discourse installation (https://community.schulminator.com/) on their website (https://www.schulminator.com/). Despite setting up CORS origins in the admin panel and updating app.yml, they encounter a CORS policy error.

mymario shares their configuration and error message, which indicates the absence of the ‘Access-Control-Allow-Origin’ header. barreeeiroo reports experiencing the same issue.

johanricher suggests that a trailing slash in the CORS origin link might be the cause of the problem. They propose a change to the CORS setting documentation to make this clearer.

codinghorror suggests that the code should auto-remove trailing slashes. sam requests that vinothkannans implement this change, which is now fixed in a PR (#10996).

I want to embed the latest updates from our community on my website:

To accomplish this, I am trying to access the latest.json and will parse that appropriately and embed on my site:

I went to admin and setup the CORS origins:

I even updated app.yml with the following lines and rebuild the whole thing:
DISCOURSE_ENABLE_CORS: true
DISCOURSE_CORS_ORIGIN: ‘https://www.schulminator.com/’
I don’t know if this is needed but it does work with this setting or without.

But still same issue as you check on this site:
https://www.schulminator.com/test?123

Access to XMLHttpRequest at ‘https://community.schulminator.com/latest.json’ from origin ‘https://www.schulminator.com’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

I am really stuck here…

Same problem here
Has someone been able to get it work?

If anyone has a similar problem with a CORS configuration, please check that you removed the trailing slash from your link. A stupid mistake that cost me a couple of hours of my time. :sweat_smile:

I think the CORS setting tip should definitely be made clearer:

Current Text

Allowed origins for cross-origin requests (CORS). Each origin must include http:// or https://. The DISCOURSE_ENABLE_CORS env variable must be set to true to enable CORS.

Draft Proposition

Allowed origins for cross-origin requests (CORS). Each origin must include http:// or https:// and no trailing slash. The DISCOURSE_ENABLE_CORS env variable must be set to true to enable CORS.

Btw, how would one make a PR for something like this? Should it change only the English locale here and the translations will be managed elsewhere (e.g. through Transifex), or should the translations be also covered by the PR?

If the trailing slash is a problem, our code should auto-remove it @sam.

@vinothkannans can you change it so we auto strip trailing slash here?

This is now fixed in the PR below

https://github.com/discourse/discourse/pull/10996