Turn on the Content Security Policy (merge after #114, #115, #116) #117

Merged
CrowderSoup merged 3 commits from claude/csp into main 2026-11-17 23:03:13 +00:00
Owner

Merge after #114, #115 and #116. This branch is built on all three, because they add onclick handlers this policy would block, and it depends on #115's self-hosted scripts. Until they're merged, this PR's diff includes their commits too. After that, it's just the last commit.

Before

settings.py had a policy under CONTENT_SECURITY_POLICY["DIRECTIVES"], which is the setting name and shape the third-party django-csp package used. Django 6's built-in middleware reads SECURE_CSP, and it wasn't in MIDDLEWARE anyway. Production sent no CSP header.

Now

default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline';
img-src 'self' data: blob: https:; connect-src 'self' <VUPLOAD_UPLOAD_ORIGIN>;
font-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'
  • Scripts are vTodo's own files only. There are no CDNs and no 'unsafe-inline', so injected markup can't run code.
  • Inline style="" stays allowed: a few templates set colours that way, and EasyMDE sets styles as it draws.
  • There's no form-action. Sign-in (Google, OpenID Connect) and the MCP OAuth consent page redirect off-site, and Chrome applies form-action to those redirects.

What changed to make it fit

  • The theme bootstrap moves from an inline script to static/theme.js. It still loads before the page draws, so there's no flash of the wrong theme.
  • Every onclick=, onchange= and hx-on::after-request becomes a data-* attribute handled once in app.js:
    • data-close-panel, data-opens-panel, data-closes-panel, data-new-task, data-theme-choice
    • data-confirm (the Google Calendar disconnect), data-submit-on-change (member role), data-load-saved-view, data-reset-after (the agent form)
  • htmx-config sets allowEval: false, since nothing uses js: values or hx-on now, and turns off htmx's injected indicator <style>, which nothing uses.

Tests

  • tests/test_csp.py checks the header. It also fails if any template brings back an inline <script>, an on*= handler or hx-on, so a new one can't silently stop working.
  • tests/browser/test_csp_browser.py walks My work, List, Board, Calendar and Settings, then opens, closes and completes a task in the panel and switches the theme. It fails on any CSP violation in the console.
  • Attachment browser tests allow their fake bucket's origin, the way production allows vUpload's.
  • Playwright waits are now written as functions ("() => …"). A bare string expression is eval'd in the page, which the policy now blocks.
  • Full suite: 807 passed, browser tests included.

Checked by hand in the browser too: every page loads with no violations, and New task (board and My work), the panel close button, Mark complete and the theme switch all work.

After deploy, worth checking once: sign in with Google and with Pocket ID, and connect Claude over MCP OAuth. Those flows go through pages the tests don't fully drive.

🤖 Generated with Claude Code

**Merge after #114, #115 and #116.** This branch is built on all three, because they add `onclick` handlers this policy would block, and it depends on #115's self-hosted scripts. Until they're merged, this PR's diff includes their commits too. After that, it's just the last commit. ## Before `settings.py` had a policy under `CONTENT_SECURITY_POLICY["DIRECTIVES"]`, which is the setting name and shape the third-party django-csp package used. Django 6's built-in middleware reads `SECURE_CSP`, and it wasn't in `MIDDLEWARE` anyway. **Production sent no CSP header.** ## Now ``` default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; connect-src 'self' <VUPLOAD_UPLOAD_ORIGIN>; font-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none' ``` - **Scripts are vTodo's own files only.** There are no CDNs and no `'unsafe-inline'`, so injected markup can't run code. - Inline `style=""` stays allowed: a few templates set colours that way, and EasyMDE sets styles as it draws. - There's no `form-action`. Sign-in (Google, OpenID Connect) and the MCP OAuth consent page redirect off-site, and Chrome applies `form-action` to those redirects. ## What changed to make it fit - The theme bootstrap moves from an inline script to `static/theme.js`. It still loads before the page draws, so there's no flash of the wrong theme. - Every `onclick=`, `onchange=` and `hx-on::after-request` becomes a `data-*` attribute handled once in `app.js`: - `data-close-panel`, `data-opens-panel`, `data-closes-panel`, `data-new-task`, `data-theme-choice` - `data-confirm` (the Google Calendar disconnect), `data-submit-on-change` (member role), `data-load-saved-view`, `data-reset-after` (the agent form) - `htmx-config` sets `allowEval: false`, since nothing uses `js:` values or `hx-on` now, and turns off htmx's injected indicator `<style>`, which nothing uses. ## Tests - `tests/test_csp.py` checks the header. It also fails if any template brings back an inline `<script>`, an `on*=` handler or `hx-on`, so a new one can't silently stop working. - `tests/browser/test_csp_browser.py` walks My work, List, Board, Calendar and Settings, then opens, closes and completes a task in the panel and switches the theme. It fails on any CSP violation in the console. - Attachment browser tests allow their fake bucket's origin, the way production allows vUpload's. - Playwright waits are now written as functions (`"() => …"`). A bare string expression is `eval`'d in the page, which the policy now blocks. - Full suite: 807 passed, browser tests included. Checked by hand in the browser too: every page loads with no violations, and New task (board and My work), the panel close button, Mark complete and the theme switch all work. **After deploy, worth checking once:** sign in with Google and with Pocket ID, and connect Claude over MCP OAuth. Those flows go through pages the tests don't fully drive. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
My work: a New task button.
All checks were successful
tests / pytest (push) Successful in 1m50s
abd10942fd
My work is where vTodo opens, but it had no way to add a task. It now has
the same New task button as List, Board and Calendar. The task goes in the
active workspace, which the panel names ("New task in Personal"), and the
answer is My work's list, so the page stays put.

The button clears the workspace the panel was last pointed at, so opening
a Chores task from My work and then clicking New task doesn't file the new
one in Chores.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Serve htmx, EasyMDE and oat from vTodo, and draw the editor with vJournal's icons.
All checks were successful
tests / pytest (push) Successful in 1m23s
3e2f788df0
The page loaded four things from other people's servers: oat from unpkg
with no version (so any new release would change how vTodo looks),
htmx from unpkg, EasyMDE from jsdelivr, and Font Awesome from
bootstrapcdn, which EasyMDE fetched for its toolbar. They're now pinned
copies in static/vendor/, like vJournal serves its own.

The notes and comment editors drop Font Awesome for the toolbar vJournal's
editor has, with the same icons from packages/theme: bold, italic, heading,
bullets, checkbox, link, code.

Browser tests no longer need a stand-in for unpkg's htmx.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Theme: Auto, Light or Dark, and Auto follows the device.
All checks were successful
tests / pytest (push) Successful in 1m21s
92afde8520
The user menu had one "Dark mode" / "Light mode" button. Pressing it once
saved a choice for good: there was no way back to following the device,
and a device switching to dark at sunset didn't change vTodo. vJournal
always follows the device.

The menu now has a three-way switch, Auto, Light and Dark, with Auto the
default. Auto tracks the device as it changes. The browser's theme-color
follows too, so the phone's status bar matches the page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Turn the Content Security Policy on: scripts are vTodo's own files only.
All checks were successful
tests / pytest (push) Successful in 1m5s
1ef9bcdbfc
settings.py had a policy under CONTENT_SECURITY_POLICY["DIRECTIVES"],
django-csp's name and shape, but Django 6's built-in middleware reads
SECURE_CSP and wasn't installed. Production sent no policy at all.

Now ContentSecurityPolicyMiddleware is on, with script-src 'self' and
nothing else: no CDNs (static/vendor/ has them) and no 'unsafe-inline', so
markup that gets injected can't run code. object-src 'none', base-uri
'self', frame-ancestors 'none'. Inline style="" is still allowed. There's
no form-action, because sign-in and MCP OAuth consent redirect off-site.

That meant taking the code out of the templates:
- The theme bootstrap moves to static/theme.js, still loaded before the
  page draws.
- onclick=, onchange= and hx-on::after-request become data-* attributes
  (data-close-panel, data-opens-panel, data-closes-panel, data-new-task,
  data-theme-choice, data-confirm, data-submit-on-change,
  data-load-saved-view, data-reset-after), handled once in app.js.
- htmx-config turns off allowEval and its injected indicator styles.

tests/test_csp.py checks the header and fails if any template brings back
an inline script, an on*= handler or hx-on. A browser test walks the pages
and the task panel and fails on any CSP violation. Playwright waits are
written as functions, since a string expression is eval'd in the page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CrowderSoup deleted branch claude/csp 2026-11-17 23:03:13 +00:00
Sign in to join this conversation.
No description provided.