Skip to content

Harden data access, document uploads, and secret handling#42

Open
kveton wants to merge 1 commit intowillchen96:mainfrom
kveton:codex/security-hardening
Open

Harden data access, document uploads, and secret handling#42
kveton wants to merge 1 commit intowillchen96:mainfrom
kveton:codex/security-hardening

Conversation

@kveton
Copy link
Copy Markdown

@kveton kveton commented May 7, 2026

Summary

This PR implements a focused security hardening pass across the backend, frontend, migrations, and dependency locks.

What changed

  • Locks Supabase app data tables behind backend service-role APIs and adds a cleanup migration for invalid tabular cells.
  • Replaces direct frontend user_profiles access with safe backend profile APIs.
  • Encrypts user LLM API keys at rest and only returns has_*_api_key booleans to the browser.
  • Requires dedicated DOWNLOAD_SIGNING_SECRET and USER_API_KEYS_ENCRYPTION_KEY secrets.
  • Validates uploaded PDF/DOC/DOCX bytes and DOCX structure before storage/conversion.
  • Tightens tabular-review document authorization, project-review access, direct-share edit behavior, and chat project scoping.
  • Removes sensitive raw LLM/document logging and unused frontend server-secret utilities.
  • Updates vulnerable frontend/backend dependencies and adds targeted backend security tests.

Validation

  • backend: npm test passed, 9 tests.
  • backend: npm run build passed.
  • frontend: npm run build passed with placeholder public env values.
  • backend + frontend: npm audit --json reports 0 vulnerabilities.
  • Local smoke test:
    • Backend /health returned 200.
    • Protected backend routes returned 401 without auth.
    • Browser smoke verified /login and /signup render, protected routes redirect to /login, and no Next error overlays or material console errors appeared.

Notes

Authenticated Supabase/R2 flows were not smoke-tested locally because this workspace only has example env files, not real local secrets.

- lock Supabase app tables behind backend service-role APIs
- add safe profile endpoints and stop exposing user API keys to the browser
- encrypt user LLM API keys at rest
- require dedicated download signing and API-key encryption secrets
- validate uploaded PDF/DOC/DOCX bytes before storage
- tighten tabular-review and chat project authorization
- remove sensitive raw LLM/document logging
- update vulnerable dependencies and add focused backend security tests
@kveton kveton marked this pull request as ready for review May 7, 2026 19:37
Lef-F added a commit to Lef-F/mike that referenced this pull request May 8, 2026
PR willchen96#32 (commit 284890d) added the user_mcp_servers table holding MCP
connector configuration, including request "headers" (typically a
Bearer token) and "oauth_tokens" (refresh + access tokens) for OAuth
2.1 connectors. PR willchen96#42 (commit 9979566) introduced 004_security_
lockdown.sql which revokes anon and authenticated grants from every
user-data table — but user_mcp_servers was added between PRs and
slipped through.

The result: a user authenticated via Supabase could query their own
oauth_tokens / headers row directly through PostgREST, bypassing
the "backend service-role only" pattern PR willchen96#42 established. Worse,
any future regression of the per-row RLS policy would expose every
users credentials, not just app data.

Add the missing revoke to both 004 and the equivalent block at the
bottom of 000_one_shot_schema.sql so fresh deployments are locked
down too. Also alter table ... enable row level security for
explicitness even though migration 002 already enables it.
Lef-F added a commit to Lef-F/mike that referenced this pull request May 8, 2026
Threat model: a database compromise or stolen backup of user_mcp_servers
should not yield usable bearer tokens or refresh tokens for third-party
connectors. Migration 003 explicitly deferred encryption ("Per-row
encryption is intentionally deferred to a separate hardening PR"); this
is that PR.

Storage format: serialize the JSON value, encrypt the resulting string
with the existing AES-256-GCM "enc:v1:" envelope from apiKeys.ts, and
store the ciphertext as a JSON-string scalar in the existing jsonb
column (a JSON string is itself valid jsonb, so no schema change is
needed). Encrypting the whole blob keeps the format trivial, avoids
leaking shape ("this row has a refresh_token"), and minimizes cipher
operations vs per-leaf encryption. On read we sniff
typeof === "string" && startsWith("enc:v1:") to distinguish from legacy
plaintext objects.

New helpers encryptJsonBlob / decryptJsonBlob / needsJsonBlobUpgrade
live alongside the existing per-string helpers in apiKeys.ts and reuse
them underneath. Call sites updated: DbOAuthProvider.tokens() /
saveTokens() encrypt+decrypt oauth_tokens; saveCodeVerifier() /
codeVerifier() encrypt the PKCE verifier (using the per-string helper
since it's a single text column); mcpServers.ts POST/PATCH/test
encrypt+decrypt headers; loadEnabledMcpServersForUser decrypts each
row in place and fires off a best-effort UPDATE to upgrade legacy
plaintext rows in the background, mirroring the lazy-upgrade pattern
PR willchen96#42 introduced for LLM provider keys (commit 701535b). The
upgrade write is fire-and-forget so a failing-forever encryption
write cannot block the chat hot path on every turn, but errors are
logged so they can be detected. oauth_metadata stays plaintext —
it's discovery + DCR data, not secret. If
USER_API_KEYS_ENCRYPTION_KEY is unset, encryptApiKey throws and the
write fails closed, which is the correct behavior.
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