Spaces:
Paused
Paused
dushyantsingh0 commited on
Commit ·
eb692fb
1
Parent(s): 812e16e
backend stabilization
Browse files- api/v1/messages.py +2 -7
- api/v1/users.py +2 -8
- scratch/investigate_user.py +19 -0
- scratch/verify_isolation.py +75 -0
api/v1/messages.py
CHANGED
|
@@ -42,16 +42,11 @@ async def parse_mentions(content: str, workspace_id: UUID, db: AsyncSession) ->
|
|
| 42 |
from models.user_workspace import UserWorkspace
|
| 43 |
# Broadened query: Match if in workspace OR has SC staff role
|
| 44 |
# This aligns the mention parser with the visibility rules in list_users
|
| 45 |
-
SC_ROLES = [UserRole.SC_ADMIN, UserRole.SC_LEAD, UserRole.SC_AGENT]
|
| 46 |
-
|
| 47 |
stmt = (
|
| 48 |
select(User)
|
| 49 |
-
.
|
| 50 |
.where(
|
| 51 |
-
|
| 52 |
-
UserWorkspace.workspace_id == workspace_id,
|
| 53 |
-
User._legacy_role.in_(SC_ROLES)
|
| 54 |
-
),
|
| 55 |
func.lower(User.email).in_(emails),
|
| 56 |
User.deleted_at.is_(None)
|
| 57 |
)
|
|
|
|
| 42 |
from models.user_workspace import UserWorkspace
|
| 43 |
# Broadened query: Match if in workspace OR has SC staff role
|
| 44 |
# This aligns the mention parser with the visibility rules in list_users
|
|
|
|
|
|
|
| 45 |
stmt = (
|
| 46 |
select(User)
|
| 47 |
+
.join(UserWorkspace, User.id == UserWorkspace.user_id)
|
| 48 |
.where(
|
| 49 |
+
UserWorkspace.workspace_id == workspace_id,
|
|
|
|
|
|
|
|
|
|
| 50 |
func.lower(User.email).in_(emails),
|
| 51 |
User.deleted_at.is_(None)
|
| 52 |
)
|
api/v1/users.py
CHANGED
|
@@ -146,15 +146,9 @@ async def list_users(
|
|
| 146 |
# Platform-wide view
|
| 147 |
pass
|
| 148 |
else:
|
| 149 |
-
#
|
| 150 |
target_ws = workspace_id if (workspace_id and current_user.is_platform_admin) else current_user.workspace_id
|
| 151 |
-
|
| 152 |
-
stmt = stmt.where(
|
| 153 |
-
or_(
|
| 154 |
-
UserWorkspace.workspace_id == target_ws,
|
| 155 |
-
UserWorkspace.role.in_([UserRole.SC_AGENT, UserRole.SC_LEAD, UserRole.SC_ADMIN])
|
| 156 |
-
)
|
| 157 |
-
)
|
| 158 |
|
| 159 |
stmt = stmt.where(User.deleted_at.is_(None)).distinct(User.id).offset(skip).limit(limit)
|
| 160 |
result = await db.execute(stmt)
|
|
|
|
| 146 |
# Platform-wide view
|
| 147 |
pass
|
| 148 |
else:
|
| 149 |
+
# STRICT isolation: ONLY users associated with this workspace
|
| 150 |
target_ws = workspace_id if (workspace_id and current_user.is_platform_admin) else current_user.workspace_id
|
| 151 |
+
stmt = stmt.where(UserWorkspace.workspace_id == target_ws)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
stmt = stmt.where(User.deleted_at.is_(None)).distinct(User.id).offset(skip).limit(limit)
|
| 154 |
result = await db.execute(stmt)
|
scratch/investigate_user.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from core.database import async_session_factory
|
| 3 |
+
from sqlalchemy import select
|
| 4 |
+
from models.user import User
|
| 5 |
+
from models.user_workspace import UserWorkspace
|
| 6 |
+
from uuid import UUID
|
| 7 |
+
|
| 8 |
+
async def check():
|
| 9 |
+
async with async_session_factory() as db:
|
| 10 |
+
# Check Admin A
|
| 11 |
+
stmt = select(User.email, User.full_name, UserWorkspace.role, UserWorkspace.workspace_id).outerjoin(UserWorkspace).where(User.email == 'admin@a.com')
|
| 12 |
+
res = await db.execute(stmt)
|
| 13 |
+
print("Admin A data:", res.all())
|
| 14 |
+
|
| 15 |
+
if __name__ == "__main__":
|
| 16 |
+
import sys
|
| 17 |
+
if sys.platform == "win32":
|
| 18 |
+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
| 19 |
+
asyncio.run(check())
|
scratch/verify_isolation.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import sys
|
| 3 |
+
from uuid import UUID
|
| 4 |
+
from sqlalchemy import select, func
|
| 5 |
+
|
| 6 |
+
# Add project root to sys.path
|
| 7 |
+
import os
|
| 8 |
+
sys.path.append(os.getcwd())
|
| 9 |
+
|
| 10 |
+
from core.database import async_session_factory
|
| 11 |
+
from models.user import User, UserRole
|
| 12 |
+
from models.user_workspace import UserWorkspace
|
| 13 |
+
|
| 14 |
+
async def verify_isolation():
|
| 15 |
+
# We'll use the 'Ansell' workspace context if found, else a mock one
|
| 16 |
+
async with async_session_factory() as db:
|
| 17 |
+
# 1. Find Ansell Workspace
|
| 18 |
+
from models.workspace import Workspace
|
| 19 |
+
ws_res = await db.execute(select(Workspace).where(Workspace.name.ilike('%Ansell%')))
|
| 20 |
+
ansell_ws = ws_res.scalar_one_or_none()
|
| 21 |
+
|
| 22 |
+
if not ansell_ws:
|
| 23 |
+
print("Ansell workspace not found. Verification might be incomplete.")
|
| 24 |
+
return
|
| 25 |
+
|
| 26 |
+
print(f"Verifying Isolation for Workspace: {ansell_ws.name} ({ansell_ws.id})")
|
| 27 |
+
|
| 28 |
+
# 2. Check list_users logic (Simulated)
|
| 29 |
+
# Goal: admin@a.com should NOT be in the result
|
| 30 |
+
target_ws = ansell_ws.id
|
| 31 |
+
stmt = select(User).outerjoin(UserWorkspace, User.id == UserWorkspace.user_id)
|
| 32 |
+
stmt = stmt.where(UserWorkspace.workspace_id == target_ws)
|
| 33 |
+
stmt = stmt.where(User.deleted_at.is_(None)).distinct(User.id)
|
| 34 |
+
|
| 35 |
+
res = await db.execute(stmt)
|
| 36 |
+
users = res.scalars().all()
|
| 37 |
+
|
| 38 |
+
emails = [u.email for u in users]
|
| 39 |
+
print(f"Users found in Ansell: {emails}")
|
| 40 |
+
|
| 41 |
+
if 'admin@a.com' in emails:
|
| 42 |
+
print("[FAIL]: admin@a.com leaked into Ansell user list.")
|
| 43 |
+
else:
|
| 44 |
+
print("[PASS]: admin@a.com is correctly hidden from Ansell user list.")
|
| 45 |
+
|
| 46 |
+
# 3. Check parse_mentions logic (Simulated)
|
| 47 |
+
# Goal: mentioning admin@a.com should return 0 results
|
| 48 |
+
mention_emails = ['admin@a.com', 'admin@ansell.com']
|
| 49 |
+
|
| 50 |
+
mention_stmt = (
|
| 51 |
+
select(User)
|
| 52 |
+
.join(UserWorkspace, User.id == UserWorkspace.user_id)
|
| 53 |
+
.where(
|
| 54 |
+
UserWorkspace.workspace_id == target_ws,
|
| 55 |
+
func.lower(User.email).in_([e.lower() for e in mention_emails]),
|
| 56 |
+
User.deleted_at.is_(None)
|
| 57 |
+
)
|
| 58 |
+
.distinct()
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
mention_res = await db.execute(mention_stmt)
|
| 62 |
+
resolved_mentions = mention_res.scalars().all()
|
| 63 |
+
|
| 64 |
+
resolved_emails = [u.email for u in resolved_mentions]
|
| 65 |
+
print(f"Resolved mentions for Ansell: {resolved_emails}")
|
| 66 |
+
|
| 67 |
+
if 'admin@a.com' in resolved_emails:
|
| 68 |
+
print("[FAIL]: admin@a.com resolved as a mention in Ansell.")
|
| 69 |
+
else:
|
| 70 |
+
print("[PASS]: admin@a.com correctly failed to resolve in Ansell.")
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
if sys.platform == "win32":
|
| 74 |
+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
| 75 |
+
asyncio.run(verify_isolation())
|