Skip to content
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

Merged
merged 3 commits into from
Dec 12, 2024
Merged

Conversation

csutter
Copy link
Contributor

@csutter csutter commented Nov 22, 2024

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:

➜  ~ curl -Is -H "Origin: https://www.gov.uk/" \
  [http://127.0.0.1:3062/api/search/autocomplete.json\?q\=test](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](http://127.0.0.1:3062/api/search/autocomplete.json/?q\=test) | grep access-control

---

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.

@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3571 November 22, 2024 11:35 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3571 December 11, 2024 22:57 Inactive
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3571 December 11, 2024 23:03 Inactive
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.
Copy link
Contributor

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".

Copy link
Member

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
Copy link
Contributor

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, have sorted now

csutter and others added 3 commits December 12, 2024 10:50
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
@govuk-ci govuk-ci temporarily deployed to finder-frontend-pr-3571 December 12, 2024 10:55 Inactive
@kevindew kevindew merged commit 6abdcb0 into main Dec 12, 2024
12 checks passed
@kevindew kevindew deleted the cors-autocomplete branch December 12, 2024 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants