Skip to content

Fix email PDF preview blocked by Content Security Policy - #154

Open
cgroschupp wants to merge 1 commit into
nextcloud:mainfrom
cgroschupp:feature/blob-to-Uint8Array
Open

cgroschupp wants to merge 1 commit into
nextcloud:mainfrom
cgroschupp:feature/blob-to-Uint8Array

Conversation

@cgroschupp

@cgroschupp cgroschupp commented Oct 30, 2026 •

Copy link
Copy Markdown

Opening an email failed because PDF.js tried to load the generated PDF through a Blob URL, which was blocked by the PDF viewer’s Content Security Policy:

Content-Security-Policy: The page's settings blocked the loading of a resource
(connect-src) at blob:https://cloud.example.com/...
because it violates the following directive: "connect-src 'self'"

PDF.js: Unexpected server response (0) while retrieving PDF.

I could only view an .eml file in Firefox 159 after adding the following to apps/files_pdfviewer/lib/Controller/DisplayController.php:

$policy->addAllowedConnectDomain('blob:');

This change removes the need for that workaround by passing the generated PDF directly to PDF.js as a Uint8Array through PDFViewerApplication.open({ data }) after initialization. No Blob URL or CSP change is required.

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI (N/A)

Signed-off-by: Christian Groschupp <christian@groschupp.org>
@cgroschupp
cgroschupp force-pushed the feature/blob-to-Uint8Array branch from ab7b6bd to a926fd6 Compare October 30, 2026 08:00
@kesselb
kesselb requested a review from odzhychko October 30, 2026 08:03
@odzhychko odzhychko self-assigned this Oct 30, 2026
@odzhychko odzhychko added 3. to review Waiting for reviews bug Something isn't working labels Oct 30, 2026
@odzhychko

Copy link
Copy Markdown
Contributor

FYI It worked when I (and reviewers) tested, because Nextcloud Talk or Assistant was installed. It widens the CSP to allow blob:.

See https://github.com/search?q=org%3Anextcloud+%22addAllowedConnectDomain%28%27blob%3A%27%29%22&type=code

I'd like to avoid just widening the CSP (using AddContentSecurityPolicyEvent) like in https://github.com/nextcloud/spreed/blob/3171f9029be25bf55ef60ca41f38b7bee2ff1465/lib/Listener/CSPListener.php#L20

The proper fix would be to directly call PDF.js and no rely on a PDF viewer being installed. In hindsight something else then files_pdfviewer might be installed to preview PDFs which might also not be compatible with blob:.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review Waiting for reviews bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants