feat: add security improvements and fixes to supabase schema #99
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.
Description
This PR stacks on top of
pr-63-schema-correctionsand adds security improvements, idempotency fixes, and additional enhancements to the Supabase database schema migration.Type of Change
What's Changed
Security Enhancements
conversations.user_id: Prevents potential RLS bypass scenarios where NULL user_id could be insertedSECURITY DEFINERto helper functions (get_conversation_with_messages,get_user_conversations) with explicitauth.uid()checks to ensure users can only access their own dataconversations:user:{userId}ormessages:user:{userId})GRANT EXECUTEpermissions for helper functionsIdempotency Improvements
ALTER PUBLICATIONin a DO block with existence checks to prevent errors on re-runsADD COLUMN IF NOT EXISTSfor user preference columnsBug Fixes
messages_changes()trigger: Added NULL check to handle CASCADE DELETE scenarios gracefully. When a conversation is deleted, messages are cascade-deleted, and the trigger would fail trying to broadcast for a non-existent conversation. Now it logs and skips the broadcast.token_cavalues to default UUID and set default value for future insertsNew Features
instant_buy_amount(NUMERIC) andinstayield_enabled(BOOLEAN) to users tablesupabase/migrations/README.mdwith installation methods, troubleshooting, and verification stepsDocumentation Improvements
tool_use,tool_result) is stored inmetadata.parts, not as role valuesFiles Changed
supabase/migrations/20250101000000_initial_schema.sql- Enhanced with security, idempotency, and bug fixessupabase/migrations/README.md- New comprehensive migration documentationTesting
The migration is idempotent and can be safely run multiple times. To test:
Run migration on fresh database:
Re-run migration (idempotency test):
Verify security:
Verify NULL handling:
Checklist