Skip to content

armgit / ARES

Public Imported from GitHub
0

ARES - authorized red-team engagement automation with dashboard, campaign scope, module orchestration, OPSEC controls, encrypted vault, and reporting.

2.8 KB
Raw Blame
"""
Security test for AUD-001: Verification of authentication enforcement on POST /modules/reload.
Ensures zero loopback bypass and strict operator RBAC.
"""

from starlette.testclient import TestClient

from ares.api.rbac import AuthenticatedUser, get_current_user
from ares.api.server import app


def test_modules_reload_unauthenticated_rejected_401():
    """Request tanpa token ke /modules/reload -> 401 Unauthorized."""
    with TestClient(app, base_url="http://127.0.0.1") as client:
        resp = client.post("/modules/reload")
        assert resp.status_code == 401
        assert resp.headers.get("www-authenticate") == "Bearer"


def test_modules_reload_loopback_unauthenticated_rejected_401():
    """Request dari 127.0.0.1 / localhost tanpa token -> 401 (bukan 200)."""
    # 1. base_url 127.0.0.1
    with TestClient(app, base_url="http://127.0.0.1") as client_ip:
        resp = client_ip.post("/modules/reload")
        assert resp.status_code == 401

    # 2. base_url localhost
    with TestClient(app, base_url="http://localhost") as client_host:
        resp = client_host.post("/modules/reload")
        assert resp.status_code == 401

    # 3. With X-Forwarded-For 127.0.0.1
    with TestClient(app, base_url="http://127.0.0.1") as client_fwd:
        resp = client_fwd.post("/modules/reload", headers={"X-Forwarded-For": "127.0.0.1"})
        assert resp.status_code == 401


def test_modules_reload_operator_authenticated_succeeds_200():
    """Request dengan token valid operator -> 200 OK."""
    from unittest.mock import MagicMock

    from ares.api.server import get_engine
    from ares.core.engine import AresEngine

    mock_engine = MagicMock(spec=AresEngine)
    mock_engine.load_modules.return_value = 15

    app.dependency_overrides[get_current_user] = lambda: AuthenticatedUser(
        username="ops_lead", role="operator"
    )
    app.dependency_overrides[get_engine] = lambda: mock_engine
    try:
        with TestClient(app, base_url="http://127.0.0.1") as client:
            resp = client.post("/modules/reload")
            assert resp.status_code == 200
            data = resp.json()
            assert data["status"] == "ok"
            assert data["reloaded"] is True
            assert data["module_count"] == 15
            mock_engine.load_modules.assert_called_once()
    finally:
        app.dependency_overrides.clear()


def test_modules_reload_insufficient_role_rejected_403():
    """Request dengan role non-operator (recon/reporter) -> 403 Forbidden."""
    try:
        app.dependency_overrides[get_current_user] = lambda: AuthenticatedUser(
            username="recon_agent", role="recon"
        )
        with TestClient(app, base_url="http://127.0.0.1") as client:
            resp = client.post("/modules/reload")
            assert resp.status_code == 403
    finally:
        app.dependency_overrides.clear()