Skip to content

v1.10.6: Content Security Policy + SRI on every CDN script - #22

Open
gitluis wants to merge 1 commit into
masterfrom
csp-and-sri
Open

gitluis wants to merge 1 commit into
masterfrom
csp-and-sri

Conversation

@gitluis

@gitluis gitluis commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

Two security hardening changes, both in index.html:

  1. Content Security Policy. A CSP <meta> tag limits what the page can load and, more importantly, where it can send data. connect-src allows only https://api.anthropic.com and the page's own origin, so code that got into the page (a tampered library, an injection) can't post your data to another server with fetch() or XHR. Remote images are blocked too, which closes image beacons.
  2. SRI on every CDN script. Chart.js, PapaParse and Day.js had no integrity hash, so a tampered or changed CDN copy would have run with full access to IndexedDB, localStorage and the API key. All five libraries are now pinned and hashed.

To make the hashes verifiable, Chart.js, PapaParse and Day.js moved from cdnjs to the same versions on jsDelivr's npm mirror, which serves the npm files byte for byte. Every hash was computed from the npm package (npm pack + openssl dgst -sha384). As a cross-check, the existing marked and DOMPurify hashes match their npm packages exactly.

The policy

default-src 'none'; script-src 'self' 'unsafe-inline' <5 exact CDN file URLs>; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src https://fonts.gstatic.com; img-src 'self' data: blob:; connect-src 'self' https://api.anthropic.com; object-src 'none'; base-uri 'none'; form-action 'none'

Limits (honest scope)

  • 'unsafe-inline' stays in script-src. About 85 inline onclick handlers in the views need it. The CSP limits where injected script can send data, not whether it runs. Removing it means converting those handlers to addEventListener, which is a larger refactor for its own PR.
  • A <meta> CSP can't set frame-ancestors or report violations, and navigations (links, location) aren't covered. Those need HTTP headers, which a file:// page doesn't have.

Changes

  • index.html — CSP meta tag; SRI-pinned script tags from jsDelivr.
  • CLAUDE.md — §2 documents the CSP, its limits and how to recompute a hash; §15 says new CDN scripts need a hash and a CSP entry.
  • README.md, CHANGELOG.md, js/views-settings.js — v1.10.6.

Not verified

  • Loading from the real jsDelivr CDN (blocked in the sandbox). The hashes match the npm packages, and jsDelivr's /npm/ path serves those files unchanged, but please open the app once and check the console.
  • A real AI Analysis call and folder picking (File System Access needs a real click). Neither is affected by the CSP beyond connect-src, which allows the API.
  • Running FinLens served over HTTP (covered by 'self', but only file:// was tested).
Automated checks: earlier run, not re-run · commit not recorded

Moved unchanged from this PR's "Test results" comment on 2026-11-04; not re-run, so not in the new table format. Originally a description section titled "Testing".

Headless Chrome via Playwright, opening index.html as file:// (how FinLens is normally run). The sandbox can't reach the CDNs, so CDN requests were served from the npm packages, which is exactly what the hashes are checked against.

  • All libraries load with the CSP and hashes in place; 'self' matches FinLens's own scripts on file://.
  • Sample import + every view: imported the three files in samples/ (257 transactions) and opened all 25 views. No CSP violations and no errors, same as master.
  • Exfiltration blocked: fetch('https://example.com/…') → blocked by connect-src; a remote <img> → blocked by img-src. On master the same fetch went through.
  • Anthropic API still reachable: fetch('https://api.anthropic.com/v1/messages') passes the CSP.
  • SRI fails closed: serving Chart.js with one extra comment line → the browser blocks it with an integrity error.

Not verified:

  • Loading from the real jsDelivr CDN (blocked in the sandbox). The hashes match the npm packages, and jsDelivr's /npm/ path serves those files unchanged, but please open the app once and check the console.
  • A real AI Analysis call and folder picking (File System Access needs a real click). Neither is affected by the CSP beyond connect-src, which allows the API.
  • Running FinLens served over HTTP (covered by 'self', but only file:// was tested).
Manual QA steps

Setup

  1. Run git checkout csp-and-sri.
  2. Copy the three CSVs from samples/ (on this branch they are still named ally_checking_sample.csv, ally_savings_sample.csv, chase_card_sample.csv) into a new scratch folder (e.g. Desktop\finlens-test). This PR does not change saving, but test 5 writes finlens-state.json, so keep it away from your real folder.
  3. Open index.html from disk in Edge.
  4. Press F12 and open the Console tab. Keep DevTools open for all tests.

1. The page loads with no CSP or integrity errors

  1. With DevTools open, right-click the browser's reload button and choose Empty cache and hard reload.
  2. Read the Console tab.
  3. Open the Network tab, reload again, and type jsdelivr in the filter box.

Expected:

  • The sidebar footer reads v1.10.6 · FinLens.
  • The Console has no red lines mentioning "Content Security Policy", "Refused to load", or "integrity".
  • Network shows five requests to cdn.jsdelivr.net (chart.umd.js, papaparse.min.js, marked.min.js, purify.min.js, dayjs.min.js), all status 200.
  • Nothing loads from cdnjs.cloudflare.com any more.
  • The headings use the Sora font (rounded, geometric), not a plain system font. That confirms Google Fonts still pass style-src and font-src.

Before this fix: Chart.js, PapaParse and Day.js loaded from cdnjs with no integrity attribute.

2. Each library still works (import, dates, charts)

  1. Click ⚙️ Settings, then the 📥 Import tab.
  2. Click 📁 Pick a Folder and choose your scratch folder. Allow access when Edge asks.
  3. Assign the three files to accounts if asked, then click ▶ Import Data.
  4. In the top bar, set the range dropdown to All Time.
  5. Click 🏠 Overview.
  6. Click 📊 Budget, 🪣 Buckets, 📉 Debt, 📈 Investments, 🔮 Forecast and 🔍 Trends in turn.
  7. Click 💳 Transactions, then click any row.

Expected:

  • The Data Source card says ✓ 257 transactions loaded (PapaParse works).
  • Overview, Investments, Forecast and Trends show their charts, not empty boxes (Chart.js works).
  • Transaction dates show as e.g. "Oct 20" and the range label in the top bar updates (Day.js works).
  • The transaction drawer opens on the row click (inline handlers still run under 'unsafe-inline').
  • The Console stays free of CSP errors on every view.

3. Data can't be sent to another server

  1. In the Console tab, paste fetch('https://example.com/x').then(()=>console.log('SENT'),e=>console.log('BLOCKED',e.message)) and press Enter.
  2. Paste var i=new Image(); i.onload=()=>console.log('IMG LOADED'); i.onerror=()=>console.log('IMG BLOCKED'); i.src='https://example.com/a.png' and press Enter.
  3. Paste fetch('https://api.anthropic.com/v1/messages',{method:'POST'}).then(r=>console.log('API reachable',r.status),e=>console.log('API BLOCKED',e.message)) and press Enter.

Expected:

  • Step 1 prints a red "Refused to connect … violates … connect-src" line, then BLOCKED Failed to fetch.
  • Step 2 prints a "Refused to load the image … img-src" line, then IMG BLOCKED.
  • Step 3 prints API reachable with a status like 401 (no key sent). No CSP error.

Before this fix: step 1 would print SENT (or a CORS error, but the request still left the browser).

4. AI Analysis still works and renders Markdown

  1. Click ⚙️ Settings → ⚙️ General.
  2. Paste your API key in the Anthropic API Key box and click Save key.
  3. Click ✨ AI Analysis.
  4. Click the Summarize my finances quick analysis.
  5. Wait for the answer.
  6. Click 💾 Save as note.

Expected:

  • The answer appears with formatted headings, bold text and bullet lists (marked + DOMPurify work).
  • No CSP error in the Console during the call.
  • A finlens-analysis-<date>.md file downloads.

5. Saving to the folder still works

  1. Click ⚙️ Settings → 📥 Import.
  2. Find the 📦 Auto-Backup to Disk card and click 💾 Save Now.
  3. Open the scratch folder in File Explorer.

Expected:

  • A success message appears under the button and Last saved shows the current time.
  • finlens-state.json exists in the scratch folder with a current modified time.
  • No CSP error in the Console.

6. Regression: downloads and theme

  1. Click 💳 Transactions and click Export CSV.
  2. Click the ◐ button in the top-right corner.
  3. Click it again.

Expected:

  • A CSV file downloads (blob downloads are not blocked).
  • The theme switches dark/light and back, with no Console errors.

7. (Optional) A tampered library is blocked

  1. In DevTools open Sources → Overrides (in the left pane; use » if hidden). Click Select folder for overrides, pick any empty folder, and click Allow in the bar at the top.
  2. Open the Network tab, reload, right-click the dayjs.min.js request, and choose Override content.
  3. In the editor that opens, add //x on a new line at the end and press Ctrl+S.
  4. Reload the page.
  5. When done, go back to Sources → Overrides and untick Enable Local Overrides, then reload.

Expected:

  • After step 4 the Console shows "Failed to find a valid digest in the 'integrity' attribute for resource '…dayjs.min.js'".
  • The app fails to render (dates are needed everywhere), instead of running the changed file.
  • After step 5 the app loads normally again.

Before this fix: the changed Day.js file would run with no warning.

Please check before merging


Generated by Claude Code


Generated by Claude Code

- Add a CSP <meta> tag: connect-src allows only the Anthropic API (and the
  page's own origin), remote images are blocked, and scripts load only from
  FinLens's own files plus five exact CDN file URLs.
- Pin Chart.js, PapaParse and Day.js with SRI hashes, moving them from cdnjs
  to jsDelivr's npm mirror so every hash can be checked against npm.
- CLAUDE.md §2/§15, README, CHANGELOG, version bumped to v1.10.6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013Z8iBYnr2QiFrzchNxfCti
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

security Security hardening

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants