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 headers for file show method #1068

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

peetucket
Copy link
Member

@peetucket peetucket commented Dec 6, 2023

Basically adds the CORs header to any request to show a file (not just media auth requests, as is current).

Fixes #1066

@peetucket
Copy link
Member Author

@edsu this would add the CORs header to any request for a PDF (or any other file download for that matter)

Perhaps would allow the current PDF.js based viewer to work if the user is authed? Still doesn't deal with any messaging if they are not authed of course (as that is a different set of work).

@edsu
Copy link

edsu commented Dec 6, 2023

I think we only want to set Access-Control-Allow-Origin: embed.stanford.edu in cases where the file is Stanford Only? If we don't then any JavaScript running elsewhere won't be able to access any Stacks files? Those will need Access-Control-Allow-Origin: * as it currently is.

Admittedly there may not be many places that do this. I know that @peterchanws has put web archive links into items that point to replayweb.page that then fetch data from stacks. For example https://purl.stanford.edu/ww180gg9763 which links to:

https://replayweb.page/?source=https://stacks.stanford.edu/file/druid:ww180gg9763/the_quaranzine-20210609.wacz

Stanford University Press is doing something similar for some of its items, which use JavaScript to replay web archives stored in stacks: https://archive.supdigital.org/

@peetucket
Copy link
Member Author

I think we only want to set Access-Control-Allow-Origin: embed.stanford.edu in cases where the file is Stanford Only? If we don't then any JavaScript running elsewhere won't be able to access any Stacks files? Those will need Access-Control-Allow-Origin: * as it currently is.

Possibly. Adjusted PR for this situation. Did not test anything.

@peetucket
Copy link
Member Author

The new test passes locally for me...not sure why it fails in CI.

allow(Purl).to receive(:public_json).and_return(stanford_json)
end

it 'sends CORs header' do
Copy link

Choose a reason for hiding this comment

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

CORS :-)

@@ -7,7 +7,7 @@ class FileController < ApplicationController
render plain: 'File not found', status: :not_found
end

before_action :set_cors_headers, only: [:show]
before_action :set_cors_headers, only: [:show], if: proc { current_file.stacks_rights.stanford_restricted? }
Copy link

Choose a reason for hiding this comment

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

well that looks easy -- nicely done!

'Access-Control-Allow-Credentials' => 'true'
end
end

Copy link

Choose a reason for hiding this comment

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

Is it worth adding a test for when the file is not Stanford Only to make sure the header is *?

Copy link
Member Author

Choose a reason for hiding this comment

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

yup, can just add that check to an existing spec (basically ensure the header is not there).

are you interested in taking that on and seeing if you can sort why CI is failing?

Copy link

Choose a reason for hiding this comment

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

Sure, I can take a look.

Copy link

Choose a reason for hiding this comment

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

I see that CI is running the tests like this, which fails for me locally (unlike a regular rspec run):

SETTINGS__FEATURES__COCINA=true bin/rake spec

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh, yes. good catch. likely an issue with the mocking in the test not accounting for cocina rights computation

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed. Had a problem in my mocked cocina json (it wasn't internally consistent for stanford only).

@edsu edsu changed the title add cors headers for file show method Add cors headers for file show method Dec 7, 2023
@peetucket
Copy link
Member Author

I don't think any of this is needed, because it turns out we set the CORs header globally in stacks and allow any domain, see https://github.com/sul-dlss/stacks/blob/main/app/controllers/application_controller.rb#L11-L19

As to why it isn't respected in some cases, it may have been due to this recent change: #1096

Though that may not fully explain. Regardless, this code is likely duplicative of what already exists.

@edsu
Copy link

edsu commented Jan 14, 2024

The reason why it is needed is that when JavaScript does a cross-origin fetch with credentials the wildcard CORS header will not work.

@peetucket
Copy link
Member Author

Should this PR be reopened then?

@edsu
Copy link

edsu commented Jan 16, 2024

I guess it could be when/if we want to push sul-dlss/sul-embed#1944 forward.

@peetucket peetucket reopened this Jan 16, 2024
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.

Correctly set CORS header for Stanford Only files
2 participants