From a1cd80e039fb589fe8eacc8b80d4df30c9b92ec3 Mon Sep 17 00:00:00 2001 From: VampiricCyborg Date: Sun, 20 Sep 2026 13:28:04 +0530 Subject: [PATCH 1/3] fix(config): refuse to start with the development auth secret auth_secret is the only input to the session cookie's HMAC, and it fell back to "dev-only-change-me" -- a constant committed to a public repository. Any deployment that never set AUTH_SECRET was signing sessions with a key anyone could read, so anyone could mint a cookie for any user id. Nothing forced the override, and AUTH_SECRET was absent from .env.example, so there was not even a prompt to set it. A model validator on Settings now rejects an empty or still-default secret whenever debug is off. It runs at Settings() construction, which app.main performs at import, so a misconfigured process dies before create_app() wires a single route rather than serving forgeable sessions. Debug builds stay exempt so local development needs no setup. Also documents AUTH_SECRET in .env.example alongside the three other AUTH_* variables, which were equally undocumented. Co-Authored-By: Claude Opus 5 --- backend/.env.example | 12 ++++++++++++ backend/app/core/config.py | 30 +++++++++++++++++++++++++++++- backend/tests/test_security.py | 19 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/backend/.env.example b/backend/.env.example index 3587a4b..d91f838 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -27,6 +27,18 @@ CHUNK_OVERLAP=120 EMBEDDING_MODEL=BAAI/bge-small-en-v1.5 EMBEDDING_BATCH_SIZE=32 +# Authentication +# REQUIRED whenever DEBUG=false - the app refuses to start without it. +# This value alone signs every session cookie; anyone who knows it can forge a +# session for any user. Generate one with: +# python -c "import secrets; print(secrets.token_urlsafe(48))" +AUTH_SECRET= +AUTH_COOKIE_NAME=docuquery_session +AUTH_SESSION_DAYS=30 +# Cookies are Secure + SameSite=None by default, for a cross-site Vercel -> Railway +# setup. Set to false only for local http development. +AUTH_COOKIE_SECURE=true + # LLM LLM_PROVIDER=groq LLM_MODEL=qwen/qwen3.8-27b diff --git a/backend/app/core/config.py b/backend/app/core/config.py index 5cd1c0e..17dfa86 100644 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -1,7 +1,14 @@ import json from functools import lru_cache +from pydantic import model_validator from pydantic_settings import BaseSettings, SettingsConfigDict +# The value auth_secret falls back to when nothing sets AUTH_SECRET. It is a public +# constant in a public repo, so a deployment still carrying it signs session cookies +# with a key anyone can read. _reject_insecure_auth_secret below refuses to build a +# non-debug Settings in that state. +DEV_AUTH_SECRET = "dev-only-change-me" + class Settings(BaseSettings): model_config = SettingsConfigDict(env_file=".env", extra="ignore") @@ -55,7 +62,7 @@ def async_database_url(self) -> str: ollama_base_url: str = "http://localhost:11434" # Authentication. Set a strong value in Railway/Vercel environments. - auth_secret: str = "dev-only-change-me" + auth_secret: str = DEV_AUTH_SECRET auth_cookie_name: str = "docuquery_session" auth_session_days: int = 30 # Production-safe default for Vercel -> Railway cross-site requests. @@ -98,6 +105,27 @@ def allowed_ext_set(self) -> set[str]: def max_file_size_bytes(self) -> int: return self.max_file_size_mb * 1024 * 1024 + @model_validator(mode="after") + def _reject_insecure_auth_secret(self) -> "Settings": + """Refuse to build production settings that sign sessions with a known key. + + auth_secret is the only input to the session cookie's HMAC, so the shipped + default lets anyone mint a cookie for any user id. Failing here — at + Settings() construction — means the process dies before create_app() wires a + single route, rather than serving forgeable sessions. Debug builds are exempt + so local development needs no setup. + """ + if self.debug: + return self + if not self.auth_secret.strip() or self.auth_secret == DEV_AUTH_SECRET: + raise ValueError( + "AUTH_SECRET is unset or still the development default. Set it to a " + "strong random value (e.g. `python -c \"import secrets; " + "print(secrets.token_urlsafe(48))\"`) in the deployment environment, " + "or set DEBUG=true for local development." + ) + return self + @lru_cache def get_settings() -> Settings: diff --git a/backend/tests/test_security.py b/backend/tests/test_security.py index bc61900..2c2d3e2 100644 --- a/backend/tests/test_security.py +++ b/backend/tests/test_security.py @@ -1,3 +1,6 @@ +import pytest + +from app.core.config import DEV_AUTH_SECRET, Settings from app.core.security import create_session, hash_password, read_session, verify_password @@ -12,3 +15,19 @@ def test_session_round_trip_and_tamper_rejection(): token = create_session("user-123") assert read_session(token) == "user-123" assert read_session(token + "tampered") is None + + +@pytest.mark.parametrize("secret", [DEV_AUTH_SECRET, "", " "]) +def test_production_settings_reject_a_guessable_auth_secret(secret): + """A non-debug build must not start with a session key anyone could know.""" + with pytest.raises(ValueError, match="AUTH_SECRET"): + Settings(debug=False, auth_secret=secret) + + +def test_production_settings_accept_a_real_auth_secret(): + assert Settings(debug=False, auth_secret="s3kr1t-from-the-deploy-env").auth_secret + + +def test_debug_settings_still_work_without_an_auth_secret(): + """Local development should need no setup, so debug builds keep the default.""" + assert Settings(debug=True, auth_secret=DEV_AUTH_SECRET).auth_secret == DEV_AUTH_SECRET From cc718d00879da2a29c812142b8176efafa485a0f Mon Sep 17 00:00:00 2001 From: VampiricCyborg Date: Sun, 20 Sep 2026 13:28:14 +0530 Subject: [PATCH 2/3] fix(landing): rewind the typewriter during render, not in an effect useTypewriter reset its character count inside the effect that starts the interval, so a changed line rendered once at the previous line's length before the effect snapped it back to zero. Moving the reset into render -- the adjusting-state-on-a-changed-input pattern -- removes that frame, and seeding the initial count from `active` removes the same flicker on mount. This was also the one error failing `npm run lint`, which the next commit turns into a required check. Co-Authored-By: Claude Opus 5 --- frontend/components/landing/useDemoLoop.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/frontend/components/landing/useDemoLoop.ts b/frontend/components/landing/useDemoLoop.ts index 0e273c3..db1248b 100644 --- a/frontend/components/landing/useDemoLoop.ts +++ b/frontend/components/landing/useDemoLoop.ts @@ -21,14 +21,17 @@ export function useDemoLoop(steps: number, { intervalMs = 1500, holdMs = 3200, a /** Reveals `text` one character at a time while `active`; shows it in full otherwise. */ export function useTypewriter(text: string, active: boolean, speedMs = 26) { - const [count, setCount] = useState(text.length) + const [count, setCount] = useState(active ? 0 : text.length) + // Rewinding belongs in render, not in an effect: an effect would let the browser + // paint the previous line's length for a frame before snapping back to zero. + const [inputs, setInputs] = useState({ text, active }) + if (inputs.text !== text || inputs.active !== active) { + setInputs({ text, active }) + setCount(active ? 0 : text.length) + } useEffect(() => { - if (!active) { - setCount(text.length) - return - } - setCount(0) + if (!active) return let typed = 0 const id = setInterval(() => { typed += 1 From 4f3f889f35efdf1d1e1d182eee09634c1000eeb4 Mon Sep 17 00:00:00 2001 From: VampiricCyborg Date: Sun, 20 Sep 2026 13:28:14 +0530 Subject: [PATCH 3/3] ci: gate pull requests on backend tests and frontend checks The repository had no automated checks at all. Two jobs, neither needing a live service: pytest for the backend, and typecheck, lint and build for the frontend. The backend job sets AUTH_SECRET because `from app.main import app` builds Settings at import time and a non-debug Settings now refuses the development default -- so this workflow's first run also exercises the guard added two commits ago. Co-Authored-By: Claude Opus 5 --- .github/workflows/ci.yml | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..36666ff --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,65 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + +# A new push to a PR makes the in-flight run irrelevant. +concurrency: + group: ci-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + backend: + name: Backend — pytest + runs-on: ubuntu-latest + defaults: + run: + working-directory: backend + env: + # The suite mocks the database and the embedding service, so no services are + # needed. AUTH_SECRET is required because `from app.main import app` builds + # Settings at import time, and a non-debug Settings refuses the dev default. + AUTH_SECRET: ci-only-secret-not-used-for-any-real-session + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.13" + cache: pip + cache-dependency-path: backend/requirements.txt + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Run tests + run: pytest -q + + frontend: + name: Frontend — typecheck, lint, build + runs-on: ubuntu-latest + defaults: + run: + working-directory: frontend + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: "22" + cache: npm + cache-dependency-path: frontend/package-lock.json + + - name: Install dependencies + run: npm ci + + - name: Typecheck + run: npx tsc --noEmit + + - name: Lint + run: npm run lint + + - name: Build + run: npm run build