Back to ShipLog
🚀 Feature DropFebruary 22, 20264 min read

Universal Secure Document Viewer: PDF, DOCX, XLSX Preview with Safe Mode

Canvas-based PDF rendering, Office document previews, extension blacklist, and a Safe Mode that blocks printing, copying, and adds confidentiality watermarks.

By TradeStance Engineering
securitydocument-viewerpdf.jssafe-mode

The Problem

SecureFilePreview could only render images, PDFs (via browser iframe), CSV, and plain text. DOCX, XLSX, and PPTX files showed a generic unsupported fallback. The iframe-based PDF viewer relied on the browser's built-in renderer and was blocked by strict Content-Security-Policy headers. There was no way to prevent users from printing or copying sensitive documents, and no extension blacklist to block executable uploads.

Canvas-Based PDF Rendering

We replaced the sandboxed iframe with pdf.js, Mozilla's open-source PDF engine. Every page renders to an HTML5 canvas element at 1.5× scale for Retina clarity. Unlike iframes, pdf.js does not execute embedded PDF JavaScript — it's a pure parser/renderer. The pdf.js web worker is served as a static file from /pdf.worker.min.mjs, keeping the main thread responsive even for large documents.

Office Document Previews

Three new viewers, all dynamically imported (code-split):

  • DOCX — docx-preview parses the document.xml inside the ZIP container and renders formatted HTML (headings, paragraphs, tables, embedded images). VBA macros (vbaProject.bin) and OLE objects are completely ignored.
  • XLSX — SheetJS reads cell values from the spreadsheet XML and renders them as a styled HTML table. Multi-sheet workbooks get tab selectors. Macros, external data connections, and DDE links are not loaded.
  • PPTX — No reliable client-side renderer exists, so we show a styled fallback card with a Presentation icon and download button.

Safe Mode

A new toggle in the viewer footer activates Safe Mode for confidential documents. When enabled: text selection is disabled (CSS user-select: none), right-click context menus are blocked, the Save/Download button is hidden, a @media print rule blanks the content when printing, and a semi-transparent diagonal "CONFIDENTIAL" watermark overlays the document at 6% opacity without blocking interaction.

Extension Blacklist

A shared BLOCKED_EXTENSIONS constant (18 dangerous extensions including .exe, .bat, .sh, .js, .ps1, .jar, .vbs) is now enforced at four layers: the Secure Vault upload route, the TradeDrive upload service, the frontend file explorer (with toast notification), and the preview modal itself (refuses to open blocked types).

Drive Download CSP

The /drive/files/:id/download endpoint now returns Content-Security-Policy headers. Inline mode (used by the zero-download viewer) gets a restrictive policy that allows styles and images but blocks all scripts, forms, and framing. Attachment mode gets an even stricter policy that blocks everything.

Related Help GuideDocument Viewer Guide
Open Guide

Was this update useful?

#security#document-viewer#pdf.js#safe-mode#office