Review C: Fix unauthenticated export endpoint, readDocument pool, and pg_ regex - #18
Open
davidpomerenke wants to merge 1 commit into
Open
Review C: Fix unauthenticated export endpoint, readDocument pool, and pg_ regex#18davidpomerenke wants to merge 1 commit into
davidpomerenke wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three security/correctness bugs fixed from the code review:
1. Missing auth on
POST /api/export/entitiesFile:
src/app/api/export/entities/route.tsThis endpoint generated and returned a formatted Excel file but had no authentication check. Any unauthenticated internet caller could POST arbitrary row data and receive a well-formatted spreadsheet.
Fix: Added
getCurrentUser()check at the top of the handler; returns 401 if no session. This matches the auth pattern used by the parallelPOST /api/export/surveyendpoint.2.
readDocument()used the main admin DB poolFile:
src/lib/chat-tools.tsreadDocument()was callingquery()(the main admin connection pool) instead ofchatQuery()(the restricted read-only pool). Since the AI chat system is supposed to operate with reduced DB privileges for defense-in-depth, all chat tool functions should usechatQuery. Both pools set the samesearch_path, so the query result is identical — only the privilege level changes.Fix: Changed
query<...>(...)→chatQuery<...>(...)inreadDocument().3.
isQuerySafe()regex didn't blockpg_*functionsFile:
src/lib/chat-tools.tsThe
pg_entry in thedangerouskeywords array used\bpg_\bas the regex. Because_is a word character in JavaScript regex,\bdoes not match betweengand_. As a result,pg_read_file,pg_ls_dir,pg_largeobjectetc. all passed the safety check.Fix: Moved
pg_out of the word-boundary regex loop and added a dedicatedincludes('pg_')check on the lowercased SQL string before the loop. Defense-in-depth still applies (thechat_readonlyDB user lackspg_*privileges), but the application-layer filter now correctly blocks these patterns too.Test plan
curl -X POST /api/export/entitieswithout a session cookie → expect 401isQuerySafe('SELECT pg_read_file(\'/etc/passwd\')')now returns{ safe: false }isQuerySafe('SELECT * FROM documents')still returns{ safe: true }🤖 Generated with Claude Code
Generated by Claude Code