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

Allow cross-origin requests to access public content endpoint #1011

Merged
merged 1 commit into from
Nov 17, 2022

Conversation

kevindew
Copy link
Member

This addresses #1006

This sets a Access-Control-Allow-Origin: * header on HTTP responses to the show endpoint, this will allow requests to this API to originate (via JS) from hosts other than www.gov.uk.

The reason for adding this is resolve the issue raised on GitHub 1006 1. GOV.UK doesn't directly need this as it doesn't make use of client side requests to the Content Store and, if it did, they'd be from the same host. However this is added in to reflect that this is indeed a partially supported public API 2 and that we are not concerned with JS clients calling it from a different host.

I only put this on the one endpoint as there didn't seem to be any need to have it on endpoints other than ContentItems#show. I also didn't implement the HTTP OPTIONS method for the endpoint as I don't think it's strictly needed and can't see evidence of this enabled on other GOV.UK cross-origin endpoints:

➜ ~ curl -sI -X OPTIONS https://www.gov.uk/api/search.json\?fields\=publishing_app\&filter_publishing_app\=publisher\&filter_first_published_at\=from:2022-09-01,to:2022-09-30 | grep HTTP HTTP/2 404

⚠️ This repo is Continuously Deployed: make sure you follow the guidance ⚠️

This addresses #1006

This sets a `Access-Control-Allow-Origin: *` header on HTTP responses to
the show endpoint, this will allow requests to this API to originate
(via JS) from hosts other than www.gov.uk.

The reason for adding this is resolve the issue raised on GitHub 1006
[1]. GOV.UK doesn't directly need this as it doesn't make use of client
side requests to the Content Store and, if it did, they'd be from the
same host. However this is added in to reflect that this is indeed a
partially supported public API [2] and that we are not concerned with JS
clients calling it from a different host.

I only put this on the one endpoint as there didn't seem to be any need
to have it on endpoints other than ContentItems#show. I also didn't
implement the HTTP OPTIONS method for the endpoint as I don't think it's
strictly needed and can't see evidence of this enabled on other GOV.UK
cross-origin endpoints:

➜  ~ curl -sI -X OPTIONS https://www.gov.uk/api/search.json\?fields\=publishing_app\&filter_publishing_app\=publisher\&filter_first_published_at\=from:2022-09-01,to:2022-09-30 | grep HTTP
HTTP/2 404

[1]: #1006
[2]: https://content-api.publishing.service.gov.uk/
Copy link
Contributor

@BeckaL BeckaL left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@kevindew kevindew merged commit 5264e70 into main Nov 17, 2022
@kevindew kevindew deleted the cors-headers branch November 17, 2022 15:57
@NickColley
Copy link

Works a treat thank you! Now the exact same code works in Node.js and the Browser just depends where you import it 😎

@kevindew
Copy link
Member Author

Great, thanks for reporting back. I'd totally forgotten about this so glad to have confirmation it works.

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.

None yet

3 participants