Fix Race Condition in Hint Points Deduction & Secure Team Sessions RLS Policies#76
Merged
Merged
Conversation
|
@Ayaanshaikh12243 is attempting to deploy a commit to the csxark's projects Team on Vercel. A member of the Team first needs to authorize it. |
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.
close #71 # Fix Race Condition in Hint Points Deduction & Secure Team Sessions RLS Policies
Overview
This PR addresses two critical issues affecting data integrity and security:
team_sessionstable allowing public accessIssues Fixed
Issue #70: Race Condition in Points Deduction for Hints
Problem:
Solution:
reveal-hintEdge Function with point validationWHERE points = currentPoints) for optimistic lockingIssue #71: Public RLS Policies on Team Sessions
Problem:
Solution:
user_idcolumn toteam_sessionstable for ownership trackingauth.uid()checksChanges Made
Database Migrations
Migration:
20260201_add_user_id_to_team_sessions.sqluser_id(uuid) column toteam_sessionstableprofiles.user_idwith cascading deleteuser_idfor query performanceMigration:
20260202_secure_team_sessions_rls.sqlAllow public read team sessionsAllow public insert team sessionsAllow public update team sessionsAllow public delete team sessionsUsers can read own team sessions(SELECT withuser_id = auth.uid())Users can insert own team sessions(INSERT withuser_id = auth.uid())Users can update own team sessions(UPDATE withuser_id = auth.uid())Frontend Changes
File:
src/components/ChallengePage.tsxFunction:
revealNextHint()reveal-hintEdge Function with error handlingEdge Functions
New:
supabase/functions/reveal-hint/index.tsAtomic hint reveal with race condition prevention:
Features:
New:
supabase/functions/create-session/index.tsSecure session creation with device restriction:
New:
supabase/functions/end-session/index.tsSecure session termination:
Documentation
Updated:
Docs/ADMIN_SETUP.mduser_idcolumn documentation to team_sessions tableNew:
Docs/SECURITY_FIX_TEAM_SESSIONS.mdComprehensive security fix documentation:
Technical Details
Optimistic Locking Pattern
The
reveal-hintfunction uses PostgreSQL optimistic locking:This ensures:
User Ownership Validation
All session operations now validate ownership:
Testing Recommendations
Issue #70 - Hint Points Race Condition
# Test rapid hint clicks 1. Multiple simultaneous hint reveal requests 2. Verify only one point deduction succeeds 3. Verify error on concurrent modification 4. Verify client refreshes and retries 5. Verify points never go below zeroIssue #71 - Team Sessions Security
Edge Function Tests
Migration Path
For Existing Deployments:
Deploy migrations in sequence:
20260201_add_user_id_to_team_sessions.sql- Adds column (nullable, allows existing rows)20260202_secure_team_sessions_rls.sql- Updates policiesUpdate application code:
revealNextHint()create-session,end-session,reveal-hint)Migrate existing sessions (optional):
Update session creation code to pass
user_idFor New Deployments:
Breaking Changes
team_sessionstable operations now require authenticationteam_idmust be unique per active user (device restriction)end-sessionfunction instead)Non-Breaking Changes
Security Compliance
Issues Addressed:
Performance Implications
Rollback Plan
If needed:
Checklist
Related Issues
Files Changed
Review Notes
auth.uid()for authenticated checksThis PR improves both data integrity and security without compromising user experience.