Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine CORS configuration and add specs
This fixes a couple of bugs in the previous implementation of this. Using GovukContentSecurityPolicy::GOVUK_DOMAINS didn't work because it makes use of wildcard origins, which aren't supported by rack-cors. I chose to put together a simple regex as an alternative. It also had an incorrect path for the resource - I had a wildcard asterisk so it can handle with and without format as the Rails path is without a format, yet in our apps we've configured a .json format [1]. Since the original was written it was also discovered that this configuration would be needed for more than just development environments and would actually be needed in production. The situation where this is needed is CSV previews [2] which are GOV.UK pages with the layout_super_navigation_header component hosted on the assets.publishing.service.gov.uk. In order to demonstrate CORS taking effect requests need to be provided with an origin header e.g: ``` ➜ ~ curl -Is -H "Origin: https://www.gov.uk" \ http://127.0.0.1:3062/api/search/autocomplete.json\?q\=test | grep access-control access-control-allow-origin: https://www.gov.uk access-control-allow-methods: GET access-control-expose-headers: access-control-max-age: 7200 ``` An absence of any access-control-* headers indicates a CORS fail and in a browser a request will be blocked e.g: ``` ➜ ~ curl -Is -H "Origin: https://example.com" \ http://127.0.0.1:3062/api/search/autocomplete.json\?q\=test | grep access-control ``` I've wrote request specs that demonstrate these behaviours. [1]: https://github.com/alphagov/govuk_publishing_components/blob/171e814b327bcfa0f2437fff0514ff086e31c96b/app/views/govuk_publishing_components/components/_layout_super_navigation_header.html.erb#L334 [2]: https://assets.publishing.service.gov.uk/media/663ca4da8603389a07a6d2f8/Malpractice_in_VTQ_-_Example_CSV_File.csv/preview
- Loading branch information