Skip to content

Security: Restrict leaderboard score updates to server-side functions - #1880

Open
anshul23102 wants to merge 4 commits into
durdana3105:mainfrom
anshul23102:fix/1871-leaderboard-validation
Open

Security: Restrict leaderboard score updates to server-side functions#1880
anshul23102 wants to merge 4 commits into
durdana3105:mainfrom
anshul23102:fix/1871-leaderboard-validation

Conversation

@anshul23102

Copy link
Copy Markdown
Contributor

Summary

Implement strict RLS policies and audit logging to prevent users from directly modifying their leaderboard scores through client-side code.

Changes

  • Restrict UPDATE policy to only profile fields (username, avatar_url)
  • Prevent direct updates to xp, streak, badges, sessions_joined
  • Require server-side functions for all score modifications
  • Add leaderboard_updates audit table for score change tracking
  • Create trigger for automatic audit logging
  • Users cannot access audit logs via RLS

Security Impact

Prevents score manipulation: users cannot arbitrarily boost XP, add unearned badges, or manipulate rankings through client-side Supabase calls.

Fixes #1871

Implement input sanitization for all message types to prevent stored XSS attacks:
- Add sanitize.ts utility module using DOMPurify for consistent sanitization
- Sanitize user messages before storing in useMessages.ts
- Sanitize room chat messages in useRoomChat.ts
- Sanitize chatbot messages in useChatbot.ts

This ensures malicious content cannot be stored in the database or rendered
to other users, protecting against stored XSS vulnerabilities.

Fixes durdana3105#1874

Signed-off-by: Anshul Jain <anshul23102@iiitd.ac.in>
Implement RLS policies and set buckets to private to prevent unauthorized
access to uploaded resources:

- Change 'resources' and 'avatars' buckets from public=true to public=false
- Ensure only authenticated users can upload files
- Ensure only authenticated users can read/download files
- Ensure only file owners can delete their resources
- Add comprehensive RLS policies for storage objects
- Add IF NOT EXISTS to policies to prevent conflicts

This prevents anonymous users from discovering or downloading shared
resources through direct storage bucket access.

Fixes durdana3105#1873

Signed-off-by: Anshul Jain <anshul23102@iiitd.ac.in>
Update Row-Level Security policies to prevent unauthorized enumeration of
study rooms and access to private sessions:

- Restrict study_rooms SELECT to: creator, participants, or public rooms
- Enforce message access control based on room accessibility
- Add participant lookup indexes for efficient RLS evaluation
- Document enumeration prevention mechanisms

Study room IDs already use UUID (non-sequential), but RLS policies were
too permissive, allowing any authenticated user to enumerate all rooms.

Fixes durdana3105#1872

Signed-off-by: Anshul Jain <anshul23102@iiitd.ac.in>
Implement RLS policies and audit logging to prevent users from directly
modifying their scores:

- Restrict UPDATE policy to only allow profile field changes (username, avatar)
- Prevent direct updates to xp, streak, badges, sessions_joined fields
- Require server-side functions (increment_xp, award_badge) for score changes
- Add leaderboard_updates audit table to log all score modifications
- Create trigger to automatically log score changes
- Users cannot access audit log (RLS prevents SELECT)

This prevents users from arbitrarily boosting their XP, adding unearned badges,
or manipulating their ranking through client-side code.

Fixes durdana3105#1871

Signed-off-by: Anshul Jain <anshul23102@iiitd.ac.in>
@vercel

vercel Bot commented Jul 29, 2026

Copy link
Copy Markdown

@anshul23102 is attempting to deploy a commit to the durdana3105's projects Team on Vercel.

A member of the Team first needs to authorize it.

@anshul23102

Copy link
Copy Markdown
Contributor Author

@durdana3105 Thank you for reviewing!

This PR secures the leaderboard by:

  1. Restricting direct updates to profile fields only (username, avatar)
  2. Requiring server-side functions for all score modifications
  3. Adding audit logging to detect and investigate suspicious changes
  4. Preventing users from arbitrarily manipulating their rankings

The gamification system's integrity depends on trusting server-side scoring, not client-side updates.

Could you please review and merge if it looks good? Also, please add the gssoc-approved label when ready.

Let me know if any changes are needed!

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@anshul23102, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 53 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 16a95888-c684-4142-a8af-e03dcbefc936

📥 Commits

Reviewing files that changed from the base of the PR and between 3565841 and 84d5d7f.

📒 Files selected for processing (10)
  • docs/LEADERBOARD_SECURITY.md
  • docs/SESSION_ENUMERATION_PREVENTION.md
  • docs/STORAGE_SECURITY.md
  • src/hooks/useChatbot.ts
  • src/hooks/useMessages.ts
  • src/hooks/useRoomChat.ts
  • src/utils/sanitize.ts
  • supabase/migrations/20260604000000_storage_size_policy.sql
  • supabase/migrations/20260730000000_prevent_session_enumeration.sql
  • supabase/migrations/20260730000001_secure_leaderboard_updates.sql

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

Leaderboard scores writable directly via Supabase client with no server-side validation

1 participant