Skip to content

Protect AI requests, enforce shared quotas and patch dependencies - #19

Draft
gobinathm wants to merge 1 commit into
mainfrom
security/harden-ai-and-dependencies
Draft

gobinathm wants to merge 1 commit into
mainfrom
security/harden-ai-and-dependencies

Conversation

@gobinathm

Copy link
Copy Markdown
Owner

The public study assistant accepted lookalike origins, parsed unbounded JSON, and relied on per-isolate counters to protect the Gemini quota. This change requires a fresh Turnstile token, verifies its hostname/action server-side, and reserves an attempt from one durable global daily budget before calling Gemini. Invalid configuration or service failures fail closed. The default cap is 200 attempts per UTC day; only an aggregate date/count is persisted.

  • Bound actual body bytes, validate JSON shapes and UTF-8 content length, expire/bound IP tracking, add upstream timeouts, and avoid raw upstream-error logging.
  • Update vulnerable site and Wrangler dependencies. Align Notes and its worker with npm, independent lockfiles, Node 24, a five-day release-age policy and explicit allowed install scripts. Both clean installs and npm audits pass with zero reported vulnerabilities.
  • Add document CSP for VitePress and standalone cheat sheets because GitHub Pages does not apply _headers; preserve AI challenges, embedded fonts, audio and PDF export. Inline script allowances remain for the current VitePress/standalone implementation. HTTP-only framing/nosniff controls still require a compatible serving edge and are not claimed fixed.
  • Update privacy disclosures and replace the old security guide with concrete setup and validation instructions.

Required before merge

No Turnstile widget exists yet. Create a managed widget for notes.gobinath.com, then add repository variable TURNSTILE_SITE_KEY and repository secret TURNSTILE_SECRET_KEY. Keep the existing Gemini Worker secret. The deployment workflows check the keys and upload the private Turnstile secret. Deploy the site and Worker together; the old client cannot satisfy the new verification. See worker/SECURITY.md.

This PR is intentionally a draft. No production deployment or real provider challenge has been performed.

Validation

  • Clean npm installs and npm audit in both packages: zero reported vulnerabilities.
  • 10 offline proxy tests: exact origins, malformed JSON, actual streamed limits, UTF-8 limits, challenge rejection/outages and persistent budget behavior.
  • Wrangler dry run bundles the Worker and SQLite Durable Object successfully.
  • Real local SQLite object concurrency test: cap 2, ten concurrent reservations → two accepted and eight rejected.
  • Full site build; privacy checks and no-JavaScript smoke checks for 151 pages. Private-term checks require the CI secret and were skipped locally.
  • Local Chromium with mocked external services: Q&A and flashcards each use fresh challenge tokens; PDF download succeeds; no page/CSP errors. Real Turnstile integration must be checked after widget setup.

The local machine was short on disk space; the final local build reused large worktree media as hard-linked disposable build output. CI uses the normal build. Existing edits in the original checkout were not included.

This branch has not been deployed

No deployments
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.

1 participant