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

Viewer origins #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ an iframe.

NOTE: The helper will render a full HTML document and should not be used in a layout.

### CORS of the viewer

As the viewer does a cross origin check, you'll have specify the origins you
want to allow to use your pdf.js viewer using an ENV variable:

```
$ export PDFJS_VIEWER_ORIGINS=http://localhost:3000,http://example.com,http://production-site.com
```

## Development

Tests can be executed with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7141,11 +7141,11 @@ var PDFViewerApplication = {
};
window.PDFView = PDFViewerApplication; // obsolete name, using it as an alias


var HOSTED_VIEWER_ORIGINS = ['null',
'http://mozilla.github.io', 'https://mozilla.github.io'];
var HOSTED_VIEWER_ORIGINS = <%= ENV.fetch("PDFJS_VIEWER_ORIGINS", "").split(",").inspect %>;
function validateFileURL(file) {
try {
// IE workaround because window.location.origin
// is not available in < IE 11
var viewerOrigin = new URL(window.location.href).origin || 'null';
if (HOSTED_VIEWER_ORIGINS.indexOf(viewerOrigin) >= 0) {
// Hosted or local viewer, allow for any file locations
Expand Down
9 changes: 9 additions & 0 deletions test/integration/viewer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ class ViewerTest < ActionDispatch::IntegrationTest
assert_rendered_pdf output, screenshot: SANDBOX_PATH + "helper.png"
end

test "ENV variable is set in JS" do
visit "/"
click_on "minimal viewer"
assert_equal [
"http://example.com",
"http://random.example.com"
], page.evaluate_script("HOSTED_VIEWER_ORIGINS")
end

private
def assert_rendered_pdf(output, screenshot:)
puts output.scan(/Warning:.+$/)
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Configure Rails Environment
ENV["RAILS_ENV"] = "test"

ENV["PDFJS_VIEWER_ORIGINS"] = "http://example.com,http://random.example.com"
Copy link
Owner

Choose a reason for hiding this comment

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

This should be set in the respective test-case and be reset after it (in an ensure)


require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
# ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
# ActiveRecord::Migrator.migrations_paths << File.expand_path('../../db/migrate', __FILE__)
Expand Down