-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add CORS configuration for autocomplete API #3571
Conversation
71b33b5
to
bef01a6
Compare
bef01a6
to
5b55c75
Compare
config/initializers/cors.rb
Outdated
Rails.application.config.middleware.insert_before 0, Rack::Cors do | ||
# Allow the autocomplete API to be accessed from any GOV.UK domain, including non-production ones. | ||
# This allows autocomplete to be available on CSV previews, which are hosted on | ||
# assets.publishing.service.gov.uk. This allows allows for local development usage. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Little typo here with two "allows".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops thanks. Amended.
end | ||
end | ||
|
||
it "returns CORS headers when there is a format extension on the path" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec description says "a format extension on the path" but I'm not seeing one in the action get
request, am I missing something here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, have sorted now
Allow the autocomplete API to be accessed from any GOV.UK domain, including non-production ones. This lets us use the API in local development "live" stacks as well as the GOV.UK Publishing Components guide.
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
5b55c75
to
cf3002f
Compare
Update from @kevindew
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:
An absence of any access-control-* headers indicates a CORS fail and in
a browser a request will be blocked e.g:
---
Allow the autocomplete API to be accessed from any GOV.UK domain, including non-production ones. This lets us use the API in local development "live" stacks as well as the GOV.UK Publishing Components guide.