Spaces:
3SCError404
/
Paused

nexus / scratch /security_audit.py
dushyantsingh0
backend stabilization
4405ca9
Raw History Blame Contribute Delete
3.14 kB
import asyncio
import httpx
from core.database import async_session_factory
from core.config import get_settings
from models.user import User
BASE_URL = "http://localhost:8000/api/v1"
async def audit():
settings = get_settings()
# 1. DB Audit
async with async_session_factory() as db:
res = await db.execute(select(User).where(User.email.in_(["admin@a.com", "admin@3sc.com"])))
users = res.scalars().all()
for u in users:
print(f"DB CHECK: {u.email} -> Global Role: {u.global_role}")
# 2. API Audit
async with httpx.AsyncClient() as client:
# A. Login as Admin A
print("\n--- AUDITING ADMIN A (admin@a.com) ---")
login_a = await client.post(f"{BASE_URL}/auth/login/", data={"username": "admin@a.com", "password": "password123"})
if login_a.status_code != 200:
print(f"FAILED LOGIN ADMIN A: {login_a.text}")
return
token_a = login_a.json()["access_token"]
headers_a = {"Authorization": f"Bearer {token_a}"}
# Test A.1: Attempt switch to Ansell (Assuming Ansell exists)
# From previous logs, Ansell ID was something like 00078389-729d-4355-827c-9411681283c7
ansell_id = "00078389-729d-4355-827c-9411681283c7"
switch_res = await client.post(f"{BASE_URL}/auth/switch-workspace/?target_workspace_id={ansell_id}", headers=headers_a)
print(f"Switch to Ansell (Admin A): {switch_res.status_code} (Expected: 403)")
# Test A.2: Leak search (try to find Ansell tickets while in A context)
# Term "Ans-999" was used in a previous session for Ansell
search_res = await client.get(f"{BASE_URL}/tickets/search/?q=Ans-999", headers=headers_a)
results = search_res.json()["results"]
print(f"Search for 'Ans-999' as Admin A: {len(results)} results (Expected: 0)")
# Test A.3: User List (Should only see Workspace A users)
users_res = await client.get(f"{BASE_URL}/users/", headers=headers_a)
user_list = users_res.json()
print(f"User List as Admin A: {len(user_list)} users (Expected: ~few local users)")
# B. Login as Super Admin
print("\n--- AUDITING SUPER ADMIN (admin@3sc.com) ---")
login_s = await client.post(f"{BASE_URL}/auth/login/", data={"username": "admin@3sc.com", "password": "password123"})
token_s = login_s.json()["access_token"]
headers_s = {"Authorization": f"Bearer {token_s}"}
# Test S.1: Switch to Ansell
switch_s = await client.post(f"{BASE_URL}/auth/switch-workspace/?target_workspace_id={ansell_id}", headers=headers_s)
print(f"Switch to Ansell (Super Admin): {switch_s.status_code} (Expected: 200)")
# Test S.2: Global User List
global_users_res = await client.get(f"{BASE_URL}/users/?workspace_id=all", headers=headers_s)
global_list = global_users_res.json()
print(f"Global User List as Super Admin: {len(global_list)} users (Expected: All system users)")
from sqlalchemy import select
if __name__ == "__main__":
asyncio.run(audit())