Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 3 min read

Why I Lock Every Local Browser Tool with CSP connect-src 'none' (And How It Runs PDF.js & .docx Offline)

Almost every online PDF converter, contract diff site, and CSV cleaner has the same reassuring footer: "We respect your privacy. Uploaded files are automatically deleted after 1 hour." If you're a freelancer reviewin

Almost every online PDF converter, contract diff site, and CSV cleaner has the same reassuring footer:

"We respect your privacy. Uploaded files are automatically deleted after 1 hour."

If you're a freelancer reviewing an NDA, a consultant comparing client MSAs, or an indie maker reconciling Stripe and Gumroad revenue CSVs, that promise isn't good enough. Once a confidential file crosses the wire to someone else's server, you no longer control it.

Over the past few weeks, I built a small suite of single-file, 100% offline HTML utilitiesβ€”no backend, no build step for the end user, no Electron bloat, and no OS code-signing warningsβ€”and made one non-negotiable architectural rule:

Every tool must technically prove it cannot phone home, using a strict <meta> Content Security Policy.

1. One <meta> Tag Beats a 10-Page Privacy Policy

For pure-JS tools (like my offline multi-platform sales CSV reconciler), the entire security guarantee fits in a single line inside <head>:

<meta
  http-equiv="Content-Security-Policy"
  content="default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data:; connect-src 'none'; base-uri 'none'; form-action 'none';"
>

When you open this file in Chrome, Edge, Safari, or Firefoxβ€”even under file:// in airplane modeβ€”the browser engine itself blocks:

  • Every fetch(), XMLHttpRequest, WebSocket, and EventSource (connect-src 'none')
  • Every external script, CDN stylesheet, web font, or tracking pixel (default-src 'none')
  • Every sneaky form submission (form-action 'none')

Users don't have to take my word for it: they can open DevTools β†’ Network, drop a sensitive document, and verify 0 bytes sent.

2. The Hard Part: Keeping connect-src 'none' While Parsing PDFs and Word (.docx)

Pure CSV parsing is easy under strict CSP. Parsing PDF and Word (.docx) files completely offline inside one .html file is where things get interesting.

Trick A: Running Mozilla PDF.js Without External Worker Requests

Normally, pdf.js loads pdf.worker.min.js over HTTPβ€”which fails under file:// CORS rules and strict CSP.

The fix is to inline pdf.min.js, embed pdf.worker.min.js as Base64, convert it into an in-memory Blob URL at startup, and loosen CSP only for worker-src blob: and script-src 'unsafe-inline' blob: while keeping connect-src 'none':

const bin = atob(EMBEDDED_WORKER_BASE64);
const bytes = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
const blob = new Blob([bytes], { type: "application/javascript" });
window.pdfjsLib.GlobalWorkerOptions.workerSrc = URL.createObjectURL(blob);

Also remember to pass isEvalSupported: false to pdfjsLib.getDocument(...) so PDF.js never attempts eval() under a locked-down CSP!

Trick B: Zero-Dependency .docx Extraction via DecompressionStream

A .docx file is a PKZIP archive containing word/document.xml. Instead of bundling heavy ZIP/Office libraries, modern browsers ship with DecompressionStream("deflate-raw").

About 60 lines of PKZIP Central Directory parsing + raw deflate decompression extracts live contract paragraphs nativelyβ€”and stripping <w:del>...</w:del> tags before collecting <w:t> prevents Word's tracked-change deletions from silently leaking back into your diffs:

const cleaned = xmlText.replace(/<w:del\b[^>]*>[\s\S]*?<\/w:del>/g, "");

3. Why Single-File HTML Beats .exe / .dmg for Small Indie Utilities

Historically, offline desktop tools shipped as .exe or .dmg binaries. Today, independent makers hit two massive walls with native binaries:

  1. OS Gatekeepers: Unsigned .exe and .app files trigger scary Windows SmartScreen / macOS Gatekeeper warnings unless you pay hundreds of dollars a year for EV code signing + Apple Developer notarization.
  2. Zero-Friction Demos: With a single HTML architecture, the exact same engine doubles as a 1-click live browser demo on GitHub Pagesβ€”so users can test their own files before downloading anything.

Live 1-Click Offline Demos (Try With DevTools Open!)

All demos below run strictly inside your browser with CSP: connect-src 'none' and zero signup:

(Full disclosure: The live browser demos are free to use with no signup; I also offer downloadable perpetual offline Pro editions with unlimited length, printable Audit HTML/CSV reports, and QuickBooks/Xero/Wave exports as one-time purchases on Gumroadβ€”DiffLocal Pro ($39) and CreatorCSV Local Pro ($29). PDF parsing uses Mozilla PDF.js under Apache 2.0. Parts of the drafting and test scaffolding were assisted by AI tools and verified with automated tests.)

Have you shipped single-file HTML utilities with strict CSP locks? What browser edge cases surprised you most? Let's compare notes in the comments!

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.