Skip to content

fix(pdf): PDF previews by adding the missing preview endpoint and allowing same-origin blob URLs in iframe CSP#4225

Merged
icecrasher321 merged 4 commits intostagingfrom
fix/csp-pdf-worker
Apr 18, 2026
Merged

fix(pdf): PDF previews by adding the missing preview endpoint and allowing same-origin blob URLs in iframe CSP#4225
icecrasher321 merged 4 commits intostagingfrom
fix/csp-pdf-worker

Conversation

@icecrasher321
Copy link
Copy Markdown
Collaborator

Summary

PDF previews by adding the missing preview endpoint and allowing same-origin blob URLs in iframe CSP

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 18, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 18, 2026 3:52am

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 18, 2026

PR Summary

Medium Risk
Touches security-sensitive surfaces by loosening frame-src CSP to include blob: and by adding new authenticated endpoints that execute user-supplied code in the sandbox; failures could impact preview isolation or CSP protections.

Overview
Adds missing document preview API endpoints for PDF and DOCX and refactors the existing PPTX preview handler to reuse a shared createDocumentPreviewRoute factory.

The shared route enforces consistent auth + workspace membership checks, JSON/code validation, and a new 1MB MAX_DOCUMENT_PREVIEW_CODE_BYTES limit before calling runSandboxTask, and streams back the generated binary with correct MIME types.

Updates CSP frame-src to allow blob: (to support iframe-based PDF previews) and adds Vitest coverage for the three preview routes plus the new CSP expectation.

Reviewed by Cursor Bugbot for commit ef2b8b2. Configure here.

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

bugbot run

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

@greptile

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 18, 2026

Greptile Summary

This PR adds the missing PDF preview API endpoint (/api/workspaces/[id]/pdf/preview) and refactors the shared document-preview logic (PDF, DOCX, PPTX) into a single factory function createDocumentPreviewRoute. It also adds blob: to the frame-src CSP directive to unblock iframe-based PDF preview rendering, with a matching test asserting its presence.

The refactoring is clean: auth, membership verification, JSON parsing, code validation, size guard, and sandbox dispatch are all handled once in create-preview-route.ts, and each per-format route file reduces to a one-liner config call.

Confidence Score: 5/5

Safe to merge — clean refactor with comprehensive test coverage and no functional regressions.

All changed files follow project conventions (toError, createLogger, absolute imports, vi.hoisted mocking). The shared factory eliminates duplication across three routes. Test suites cover every response branch including auth failures previously flagged in review. The CSP change (blob: in frame-src) is the minimum required to unblock iframe PDF rendering and is acceptable since blob URLs are inherently same-origin. No P0 or P1 issues found.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/app/api/workspaces/[id]/_preview/create-preview-route.ts New shared factory: handles all auth/validation/sandbox dispatch for document preview routes; correctly uses toError, createLogger, and req.signal for cancellation.
apps/sim/app/api/workspaces/[id]/pdf/preview/route.ts New PDF preview endpoint; delegates fully to createDocumentPreviewRoute with correct taskId, content-type, and label.
apps/sim/app/api/workspaces/[id]/docx/preview/route.ts Refactored DOCX route to use shared factory; thin wrapper with correct MIME type.
apps/sim/app/api/workspaces/[id]/pptx/preview/route.ts Refactored PPTX route to use shared factory; thin wrapper with correct MIME type.
apps/sim/lib/core/security/csp.ts Adds blob: to STATIC_FRAME_SRC to allow iframe-based PDF previews; acceptable relaxation since blob URLs are inherently same-origin.
apps/sim/lib/execution/constants.ts Adds MAX_DOCUMENT_PREVIEW_CODE_BYTES (1 MB) constant for payload size guard; well-documented with reasoning.
apps/sim/app/api/workspaces/[id]/pdf/preview/route.test.ts Comprehensive test suite covering success, all validation error branches (400/401/403/413), and sandbox failure (500) — all previously noted gaps are now addressed.
apps/sim/lib/core/security/csp.test.ts Adds test asserting blob: is present in the frame-src directive of the runtime CSP; correctly placed in the generateRuntimeCSP describe block.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[POST to document preview endpoint] --> B[createDocumentPreviewRoute factory]
    B --> C{Authenticated?}
    C -- No --> D[401 Unauthorized]
    C -- Yes --> E{Workspace member?}
    E -- No --> F[403 Insufficient permissions]
    E -- Yes --> G{Valid JSON body?}
    G -- No --> H[400 Invalid JSON]
    G -- Yes --> I{code present and non-empty?}
    I -- No --> J[400 code is required]
    I -- Yes --> K{code within 1 MB?}
    K -- No --> L[413 code exceeds maximum size]
    K -- Yes --> M[runSandboxTask with ownerKey and abort signal]
    M -- Success --> N[200 binary document response]
    M -- Error --> O[500 with error message]
Loading

Reviews (2): Last reviewed commit: "follow nextjs route gen strat" | Re-trigger Greptile

Comment thread apps/sim/app/api/workspaces/[id]/pdf/preview/route.ts Outdated
Comment thread apps/sim/app/api/workspaces/[id]/pdf/preview/route.test.ts
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 6f693c1. Configure here.

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

bugbot run

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

@greptile

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit ef2b8b2. Configure here.

@icecrasher321 icecrasher321 merged commit 524f33c into staging Apr 18, 2026
14 checks passed
@icecrasher321 icecrasher321 deleted the fix/csp-pdf-worker branch April 18, 2026 03:59
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.

1 participant