-
Notifications
You must be signed in to change notification settings - Fork 0
Bound versioned retrieval RPC match_count to prevent unbounded results
#756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+105
−0
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a11ea87
docs: record retrieval RPC review
BigSimmo b9197a7
Merge branch 'main' into codex/review-and-improve-supabase/postgres-l…
BigSimmo ed1f71b
fix: rename migration to 20260717174000 to avoid timestamp collision …
cursoragent 21c0720
fix: move migration to 20260717162000 to preserve reassert-as-last co…
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
supabase/migrations/20260717162000_bound_versioned_retrieval_match_count.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| -- Bound the result cardinality of the two versioned owner-plus-public retrieval | ||
| -- entry points. PostgREST permits explicitly-null RPC arguments; PostgreSQL | ||
| -- interprets LIMIT NULL (and a negative LIMIT) as no limit. Before this guard, | ||
| -- a malformed or compromised service-role caller could turn the final LIMIT | ||
| -- into an unbounded result within each helper's candidate window, multiplying | ||
| -- JSON hydration and response size on a hot retrieval path. | ||
| -- | ||
| -- Keep the public signatures and defaults intact for generated types and | ||
| -- callers. The inner helpers already cap candidate work; this outer clamp | ||
| -- makes the exposed result contract deterministic: 1..96 rows. | ||
| -- | ||
| -- Rollback / forward repair: re-create these wrappers from | ||
| -- 20260717160000_optimize_owner_public_retrieval.sql to restore the former | ||
| -- pass-through count behaviour. No data is changed. | ||
|
|
||
| create or replace function public.match_document_chunks_text_v2( | ||
| query_text text, | ||
| match_count integer default 12, | ||
| document_filters uuid[] default null, | ||
| owner_filter uuid default '00000000-0000-0000-0000-000000000000'::uuid, | ||
| include_public boolean default true | ||
| ) | ||
| returns table ( | ||
| id uuid, document_id uuid, title text, file_name text, page_number integer, | ||
| chunk_index integer, section_heading text, content text, retrieval_synopsis text, | ||
| image_ids uuid[], source_metadata jsonb, document_labels jsonb, document_summary text, | ||
| similarity double precision, text_rank double precision, hybrid_score double precision, | ||
| lexical_score double precision, images jsonb | ||
| ) | ||
| language sql | ||
| stable | ||
| set search_path = public, extensions, pg_temp | ||
| as $$ | ||
| select * | ||
| from public.match_document_chunks_text_scoped( | ||
| $1, | ||
| least(greatest(coalesce($2, 12), 1), 96), | ||
| $3, | ||
| $4, | ||
| $5 | ||
| ); | ||
| $$; | ||
|
|
||
| create or replace function public.match_document_index_units_hybrid_v2( | ||
| query_embedding extensions.vector(1536), | ||
| query_text text, | ||
| match_count integer default 24, | ||
| min_similarity double precision default 0.1, | ||
| document_filters uuid[] default null, | ||
| owner_filter uuid default '00000000-0000-0000-0000-000000000000'::uuid, | ||
| include_public boolean default true | ||
| ) | ||
| returns table ( | ||
| id uuid, document_id uuid, source_chunk_id uuid, source_image_id uuid, unit_type text, title text, | ||
| content text, page_start integer, page_end integer, heading_path text[], normalized_terms text[], | ||
| source_span jsonb, quality_score real, extraction_mode text, similarity double precision, | ||
| text_rank double precision, hybrid_score double precision, metadata jsonb | ||
| ) | ||
| language sql | ||
| stable | ||
| set search_path = public, extensions, pg_temp | ||
| as $$ | ||
| select * | ||
| from public.match_document_index_units_hybrid_scoped( | ||
| $1, | ||
| $2, | ||
| least(greatest(coalesce($3, 24), 1), 96), | ||
| $4, | ||
| $5, | ||
| $6, | ||
| $7 | ||
| ); | ||
| $$; | ||
|
|
||
| -- CREATE OR REPLACE preserves privileges in PostgreSQL, but re-state the | ||
| -- service-role-only contract so future function changes cannot reopen it. | ||
| revoke all on function public.match_document_chunks_text_v2(text, integer, uuid[], uuid, boolean) | ||
| from public, anon, authenticated; | ||
| grant execute on function public.match_document_chunks_text_v2(text, integer, uuid[], uuid, boolean) | ||
| to service_role; | ||
| revoke all on function public.match_document_index_units_hybrid_v2( | ||
| extensions.vector, text, integer, double precision, uuid[], uuid, boolean | ||
| ) from public, anon, authenticated; | ||
| grant execute on function public.match_document_index_units_hybrid_v2( | ||
| extensions.vector, text, integer, double precision, uuid[], uuid, boolean | ||
| ) to service_role; |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
Assert the complete ACL contract.
These prefix checks still pass if
authenticatedis omitted fromREVOKE, or if theservice_rolegrants are removed. Assert each full revoke clause and bothGRANT ... TO service_rolestatements.🤖 Prompt for AI Agents