Skip to content

Fix CSP blocking the PDF viewer when files are stored in a remote storage service - #134

Open
antopalidi wants to merge 2 commits into
mainfrom
bugfix/csp-connect-src-remote-storage
Open

antopalidi wants to merge 2 commits into
mainfrom
bugfix/csp-connect-src-remote-storage

Conversation

@antopalidi

@antopalidi antopalidi commented Aug 7, 2026 •

Copy link
Copy Markdown
Member

With remote storage (S3-compatible / CDN) the PDF is fetched from the storage host, which is not allowed by the default CSP, so the browser blocks the request and the viewer stays empty. Local disk storage is not affected.

  • Append the storage origin to the connect-src CSP directive in the PDF viewer responses (public pdf_viewer, admin pdf_viewer / edit_pdf).
  • Add controller specs

Summary by CodeRabbit

  • Bug Fixes

    • Improved PDF viewing and editing when documents are stored on external storage services.
    • Updated security policies to allow secure connections to valid external PDF storage locations.
    • Preserved existing behavior for locally stored files, missing attachments, invalid URLs, and storage errors.
  • Tests

    • Added coverage for remote, local, and unavailable document storage scenarios.

Copilot AI lite review requested due to automatic review settings August 7, 2026 13:12
@coderabbitai

coderabbitai Bot commented Aug 7, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@antopalidi, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 12 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: d4a60eef-44eb-40c0-a67b-60163b5932c9

📥 Commits

Reviewing files that changed from the base of the PR and between 5049382 and 70cd420.

📒 Files selected for processing (1)
  • .github/workflows/test.yml
📝 Walkthrough

Walkthrough

PDF controllers now add external Active Storage origins to connect-src before PDF viewing and editing. The implementation excludes local hosts and handles missing attachments, invalid URLs, and storage errors without changing CSP.

Changes

PDF storage CSP support

Layer / File(s) Summary
Storage origin CSP resolution
app/controllers/concerns/decidim/participatory_documents/needs_pdf_document.rb
The concern resolves the attached PDF blob origin, excludes local hosts, preserves non-default ports, and ignores URL or storage errors.
Controller callbacks and coverage
app/controllers/decidim/participatory_documents/documents_controller.rb, app/controllers/decidim/participatory_documents/admin/documents_controller.rb, spec/controllers/decidim/participatory_documents/*
The controllers invoke CSP handling before PDF actions. Specs cover remote storage, local storage, missing attachments, viewing, and editing.Estimated code review effort: 3 (Moderate)

Suggested reviewers: copilot

Sequence Diagram(s)

sequenceDiagram
  participant DocumentsController
  participant NeedsPdfDocument
  participant ActiveStorageBlob
  DocumentsController->>NeedsPdfDocument: append_storage_host_to_csp
  NeedsPdfDocument->>ActiveStorageBlob: resolve PDF blob URL
  ActiveStorageBlob-->>NeedsPdfDocument: blob origin
  NeedsPdfDocument-->>DocumentsController: update connect-src CSP
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: allowing remote storage hosts in the PDF viewer Content Security Policy.
✨ Finishing Touches 💡 2
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/csp-connect-src-remote-storage

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
spec/controllers/decidim/participatory_documents/admin/documents_controller_spec.rb (1)

24-52: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add the promised admin local-storage and no-file cases.

These specs only cover remote storage. Add contexts for local storage and for a document without an attachment for both pdf_viewer and edit_pdf. Assert that local storage keeps connect-src 'self' and that no attachment does not append a storage host.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@spec/controllers/decidim/participatory_documents/admin/documents_controller_spec.rb`
around lines 24 - 52, Add local-storage and no-attachment contexts to both the
GET pdf_viewer and GET edit_pdf specs. Stub a local service URL and assert the
CSP connect-src remains 'self'; create a document without an attachment and
assert no storage host is appended, preserving the existing remote-storage
coverage.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In
`@spec/controllers/decidim/participatory_documents/admin/documents_controller_spec.rb`:
- Around line 24-52: Add local-storage and no-attachment contexts to both the
GET pdf_viewer and GET edit_pdf specs. Stub a local service URL and assert the
CSP connect-src remains 'self'; create a document without an attachment and
assert no storage host is appended, preserving the existing remote-storage
coverage.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b48cd521-c3c1-4184-8f0a-eb1533f2f337

📥 Commits

Reviewing files that changed from the base of the PR and between 21501f8 and 5049382.

📒 Files selected for processing (5)
  • app/controllers/concerns/decidim/participatory_documents/needs_pdf_document.rb
  • app/controllers/decidim/participatory_documents/admin/documents_controller.rb
  • app/controllers/decidim/participatory_documents/documents_controller.rb
  • spec/controllers/decidim/participatory_documents/admin/documents_controller_spec.rb
  • spec/controllers/decidim/participatory_documents/documents_controller_spec.rb

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a CSP issue where the PDF viewer (pdf.js) cannot fetch PDFs when the underlying ActiveStorage blob redirects to a remote storage/CDN origin (e.g., S3-compatible), by allowing that storage origin in the connect-src CSP directive for the PDF viewer/editor responses.

Changes:

  • Add before_action hooks to adjust CSP for the public pdf_viewer and admin pdf_viewer / edit_pdf actions.
  • Implement origin detection in NeedsPdfDocument to append the remote storage origin to connect-src when applicable.
  • Add controller specs validating CSP behavior (remote/local/no-file in public; remote cases in admin).

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
spec/controllers/decidim/participatory_documents/documents_controller_spec.rb Adds public controller specs covering remote/local/no-file CSP behavior for pdf_viewer.
spec/controllers/decidim/participatory_documents/admin/documents_controller_spec.rb Adds admin controller specs for remote-storage CSP behavior in pdf_viewer and edit_pdf.
app/controllers/decidim/participatory_documents/documents_controller.rb Runs CSP adjustment before rendering the public pdf_viewer.
app/controllers/decidim/participatory_documents/admin/documents_controller.rb Runs CSP adjustment before rendering admin pdf_viewer and edit_pdf.
app/controllers/concerns/decidim/participatory_documents/needs_pdf_document.rb Adds logic to compute storage origin and append it to the connect-src CSP directive.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@antopalidi
antopalidi force-pushed the bugfix/csp-connect-src-remote-storage branch from 606328e to 70cd420 Compare August 7, 2026 14:00
@antopalidi
antopalidi requested a review from microstudi August 7, 2026 14:59

@microstudi microstudi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice work, I have just a couple of questions

env:
RUBY_VERSION: 3.2.6
NODE_VERSION: 18.17.1
NODE_OPTIONS: "--experimental-global-webcrypto"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Curious about this? what is it?


- name: Install imagemagick and wkhtmltopdf
run: sudo apt install imagemagick wkhtmltopdf
run: sudo apt-get update && sudo apt-get install -y imagemagick wkhtmltopdf

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
run: sudo apt-get update && sudo apt-get install -y imagemagick wkhtmltopdf
run: sudo apt-get update && sudo apt-get install -y imagemagick libu2f-udev

We don't need that anymore right?

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.

3 participants