Spaces:
Paused
Paused
Download tests/test_security.py from 3SCError404/nexus: direct link, hf CLI and curl.
- Browser
- Download file 5.7 kB
-
https://huggingface.co/spaces/3SCError404/nexus/resolve/main/tests/test_security.py
- Command line
-
hf download hf://spaces/3SCError404/nexus/tests/test_security.py
-
curl -L -o test_security.py https://huggingface.co/spaces/3SCError404/nexus/resolve/main/tests/test_security.py
5.7 kB
| import pytest | |
| from httpx import AsyncClient | |
| async def test_workspace_isolation_bola(client: AsyncClient, token_a: str, token_b: str): | |
| """ | |
| CRITICAL: Test that User A (Workspace A) cannot access User B's (Workspace B) tickets. | |
| This is the ultimate test of our BOLA (Broken Object Level Authorization) hardening. | |
| """ | |
| # 1. Create a ticket as User A (Workspace A) | |
| headers_a = {"Authorization": f"Bearer {token_a}"} | |
| create_res = await client.post( | |
| "/api/v1/tickets/", | |
| json={"title": "Private Workspace A Ticket", "description": "Secret"}, | |
| headers=headers_a | |
| ) | |
| assert create_res.status_code == 201 | |
| ticket_id = create_res.json()["id"] | |
| # 2. Try to access it as User B (Workspace B) | |
| headers_b = {"Authorization": f"Bearer {token_b}"} | |
| get_res = await client.get(f"/api/v1/tickets/{ticket_id}", headers=headers_b) | |
| # Assert ACCESS DENIED (Isolation working) | |
| # The API returns 404 to hide the existence of the ticket (Standard Security Practice) | |
| assert get_res.status_code == 404 | |
| assert get_res.json()["detail"] == "Ticket not found or access denied" | |
| async def test_staff_portal_lockout_fails_on_customer_login(client: AsyncClient): | |
| """ | |
| Test that an administrative account is locked out of the standard customer login. | |
| """ | |
| response = await client.post( | |
| "/api/v1/auth/login", | |
| data={"username": "admin@a.com", "password": "password123"} | |
| # NO X-Nexus-Portal header! | |
| ) | |
| # Assert FAIL-SAFE TO CUSTOMER (Staff blocked) | |
| # Based on our auth.py: Staff must use the Operations Console | |
| assert response.status_code == 403 | |
| assert "Staff must use the Operations Console" in response.json()["detail"] | |
| async def test_cross_workspace_message_security(client: AsyncClient, admin_token_a: str, token_b: str): | |
| """ | |
| Test that an admin in Workspace A cannot access/mess with tickets from Workspace B. | |
| """ | |
| # 1. Create a ticket in Workspace B | |
| headers_b = {"Authorization": f"Bearer {token_b}"} | |
| create_res = await client.post( | |
| "/api/v1/tickets/", | |
| json={"title": "Private Workspace B Ticket", "description": "Secret"}, | |
| headers=headers_b | |
| ) | |
| ticket_b_id = create_res.json()["id"] | |
| # 2. Try to 'List Messages' as Admin A (different workspace) | |
| headers_admin_a = { | |
| "Authorization": f"Bearer {admin_token_a}", | |
| "X-Nexus-Portal": "internal" | |
| } | |
| list_msg_res = await client.get(f"/api/v1/tickets/{ticket_b_id}/messages", headers=headers_admin_a) | |
| # Assert ACCESS DENIED (isolation works) | |
| assert list_msg_res.status_code == 404 | |
| async def test_password_complexity_enforcement(client: AsyncClient): | |
| """ | |
| Test that the API rejects passwords that don't meet complexity requirements. | |
| """ | |
| # 1. Too short | |
| res_short = await client.post( | |
| "/api/v1/auth/register", | |
| json={"email": "weak@test.com", "password": "123", "full_name": "Weak User"} | |
| ) | |
| assert res_short.status_code == 422 | |
| assert "at least 8 characters" in str(res_short.json()) | |
| # 2. No digits/symbols | |
| res_simple = await client.post( | |
| "/api/v1/auth/register", | |
| json={"email": "simple@test.com", "password": "passwordonly", "full_name": "Simple User"} | |
| ) | |
| assert res_simple.status_code == 422 | |
| assert "digit or special character" in str(res_simple.json()) | |
| async def test_report_workspace_isolation(client: AsyncClient, admin_token_a: str, workspace_b_id: str): | |
| """ | |
| Test that an admin in Workspace A cannot see Workspace B's reports even by passing the ID. | |
| The API should now strictly ignore the 'workspace_id' parameter and use the caller's ID. | |
| """ | |
| headers = { | |
| "Authorization": f"Bearer {admin_token_a}", | |
| "X-Nexus-Portal": "internal" | |
| } | |
| # Try to fetch volume trends for Workspace B | |
| res = await client.get(f"/api/v1/reports/volume?workspace_id={workspace_b_id}", headers=headers) | |
| # It should succeed but return Workspace A's data (which we know from our isolated seeds) | |
| # The key is that it didn't fail with 500 and didn't return B's data. | |
| # In our test setup, Workspace A and B are seeded differently. | |
| assert res.status_code == 200 | |
| async def test_admin_password_randomness(client: AsyncClient, db_session): | |
| """ | |
| Test that the SC_Admin password is no longer predictable. | |
| """ | |
| # 1. Register a new workspace | |
| new_email = "owner_rand@random.com" | |
| reg_res = await client.post( | |
| "/api/v1/auth/register", | |
| json={"email": new_email, "password": "SecurePassword123!", "full_name": "Owner Rand"} | |
| ) | |
| assert reg_res.status_code == 201 | |
| # 2. Derive what the OLD predictable email would be | |
| import re | |
| ws_name = "Owner Rand's Workspace" | |
| ws_slug = re.sub(r'[^a-z0-9]', '_', ws_name.lower()) | |
| sc_email = f"admin_{ws_slug}@3sc.com" | |
| # 3. Derive what the OLD predictable password would have been | |
| from models.workspace import Workspace | |
| from sqlalchemy import select | |
| stmt = select(Workspace).where(Workspace.name == ws_name) | |
| ws = (await db_session.execute(stmt)).scalar_one() | |
| old_predictable_pass = f"SC_Admin_{str(ws.id)[:8]}!" | |
| # 4. Try to login with the OLD predictable password (should FAIL) | |
| login_res = await client.post( | |
| "/api/v1/auth/login", | |
| data={"username": sc_email, "password": old_predictable_pass}, | |
| headers={"X-Nexus-Portal": "internal"} | |
| ) | |
| assert login_res.status_code == 400 | |
| assert "Incorrect password" in login_res.json()["detail"] | |