-
Notifications
You must be signed in to change notification settings - Fork 17
[Feature]: Implement Row-Level Security (RLS) for Tenant Isolation #101
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
Merged
ankittroy-21
merged 4 commits into
ankittroy-21:ESSoC'26
from
ErebAsh:feat/rls-tenant-isolation
Jul 28, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5068f96
feat(security): Implement Row-Level Security (RLS) for Tenant Isolation
ErebAsh 6d5799d
chore: fix ruff formatting issues in test and service
ErebAsh 635399a
chore: fix remaining ruff lint issues
ErebAsh 841318b
Merge branch 'ESSoC'26' into feat/rls-tenant-isolation
ErebAsh 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| SUPABASE_URL="https://your-project.supabase.co" | ||
| SUPABASE_KEY="your-service-role-key" | ||
| SUPABASE_JWT_SECRET="your-jwt-secret" | ||
| TELEGRAM_BOT_TOKEN="your-telegram-bot-token" | ||
| GITHUB_CLIENT_ID="your-github-client-id" | ||
| ENVIRONMENT="development" | ||
| WEBHOOK_URL="https://your-webhook-url.ngrok-free.dev" |
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 |
|---|---|---|
|
|
@@ -8,3 +8,4 @@ python-dotenv>=1.0.0,<2.0.0 | |
| pydantic>=2.13.4,<3.0.0 | ||
| httpx>=0.28.1,<1.0.0 | ||
| slowapi>=0.1.9 | ||
| PyJWT>=2.8.0,<3.0.0 | ||
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
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,66 @@ | ||
| import os | ||
| import sys | ||
| import uuid | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) | ||
|
|
||
| # Load env vars before importing get_client | ||
| load_dotenv(os.path.join(os.path.dirname(__file__), '..', '.env')) | ||
|
|
||
| from supabase_service import get_client # noqa: E402 | ||
|
|
||
|
|
||
| def test_rls(): | ||
| print("--- Testing RLS Isolation ---") | ||
|
|
||
| user_a = "test_user_a_" + str(uuid.uuid4())[:8] | ||
| user_b = "test_user_b_" + str(uuid.uuid4())[:8] | ||
|
|
||
| if not os.environ.get("SUPABASE_JWT_SECRET"): | ||
| print("WARNING: SUPABASE_JWT_SECRET is not set. The client will fall back to service_role, and this test will fail.") | ||
| print("Please set SUPABASE_JWT_SECRET in your backend/.env file to run this test properly.") | ||
| return | ||
|
ErebAsh marked this conversation as resolved.
|
||
|
|
||
| # 1. Create a dummy user and staged file for User A using the global service_role client (bypasses RLS) | ||
| service_client = get_client() | ||
|
|
||
| print(f"Creating User A ({user_a}) and User B ({user_b}) via service_role...") | ||
| service_client.table("users").insert([ | ||
| {"telegram_id": user_a, "github_token": "dummy", "default_repo": "dummy/repo"}, | ||
| {"telegram_id": user_b, "github_token": "dummy", "default_repo": "dummy/repo"} | ||
| ]).execute() | ||
|
|
||
| # Get User A's internal UUID | ||
| user_a_record = service_client.table("users").select("id").eq("telegram_id", user_a).execute() | ||
| user_a_id = user_a_record.data[0]["id"] | ||
|
|
||
| print("Inserting a staged file for User A...") | ||
| service_client.table("staged_files").insert({ | ||
| "user_id": user_a_id, | ||
| "telegram_id": user_a, | ||
| "filepath": "secret.txt", | ||
| "diff": "+ secret data", | ||
| "base_sha": "abcdef123" | ||
| }).execute() | ||
|
|
||
| # 2. Query using User B's JWT context | ||
| print("Attempting to query User A's staged file using User B's JWT context...") | ||
| client_b = get_client(user_b) | ||
|
|
||
| # We explicitly ask for User A's data | ||
| result = client_b.table("staged_files").select("*").eq("telegram_id", user_a).execute() | ||
|
|
||
| if len(result.data) == 0: | ||
| print("SUCCESS: RLS is working! User B was denied access to User A's data (0 rows returned).") | ||
| else: | ||
| print(f"FAIL: RLS failed! User B retrieved User A's data: {result.data}") | ||
|
|
||
| # Cleanup | ||
| print("Cleaning up test data...") | ||
| service_client.table("users").delete().in_("telegram_id", [user_a, user_b]).execute() | ||
| print("Done.") | ||
|
|
||
| if __name__ == "__main__": | ||
| test_rls() | ||
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,17 @@ | ||
| -- ============================================================ | ||
| -- MIGRATION: ENABLE ROW LEVEL SECURITY (RLS) | ||
| -- Run this in your Supabase SQL editor to apply RLS to existing tables | ||
| -- ============================================================ | ||
|
|
||
| ALTER TABLE users ENABLE ROW LEVEL SECURITY; | ||
| -- Drop policy if it exists (for idempotency in reruns) | ||
| DROP POLICY IF EXISTS "User isolation" ON users; | ||
| CREATE POLICY "User isolation" ON users FOR ALL USING (telegram_id = current_setting('request.jwt.claim.sub', true)); | ||
|
|
||
| ALTER TABLE staged_files ENABLE ROW LEVEL SECURITY; | ||
| DROP POLICY IF EXISTS "User isolation" ON staged_files; | ||
| CREATE POLICY "User isolation" ON staged_files FOR ALL USING (telegram_id = current_setting('request.jwt.claim.sub', true)); | ||
|
|
||
| ALTER TABLE commit_log ENABLE ROW LEVEL SECURITY; | ||
| DROP POLICY IF EXISTS "User isolation" ON commit_log; | ||
| CREATE POLICY "User isolation" ON commit_log FOR ALL USING (telegram_id = current_setting('request.jwt.claim.sub', true)); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.