Scenario
We have some Javascript hosted by a third-party CMS service, and we have some data on a server under our control. When a user is accessing the CMS via a browser (and executing our Javascript), we would like to make a cross-origin GET
request to our server and have the server respond with the correct Access-Control-Allow-Origin
header so that the browser accepts the response.
Problem
When we inspect requests going from the CMS to our server, the request's Origin
header is set to null
. So we can't set a value for Access-Control-Allow-Origin
other than *
to make the browser accept the response. We're not really sure why the value is null
. The JS is executing in an environment that we don't fully control.
Potential Solution
We're working under the assumption that the Origin will always be null, and there's nothing we can do to change it. The best solution we can come up with is defining a pre-shared secret. When the browser makes the cross-origin request, it adds a custom header with the secret. The server looks for that header, and if the secret matches, the server adds the response header Access-Control-Allow-Origin: *
.
Is there a better, more secure way to handle this situation?