Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

7.8 KB
Raw Blame
"""Tests for Issue #687: Web UI authentication on read APIs and SSE streams."""

import pytest
from typer.testing import CliRunner

from soup_cli.cli import app as cli_app
from soup_cli.ui.app import create_app, get_auth_token
from tests.ui_helpers import ui_client


def _auth_headers():
    return {"Authorization": f"Bearer {get_auth_token()}"}


class TestReadAuthentication:
    """Verify that private read endpoints require authentication."""

    @pytest.mark.parametrize(
        "endpoint",
        [
            "/api/runs",
            "/api/runs/compare?ids=dummy",
            "/api/runs/dummy_id",
            "/api/runs/dummy_id/metrics",
            "/api/runs/dummy_id/eval",
            "/api/system",
            "/api/templates",
            "/api/train/status",
            "/api/train/progress",
            "/api/config/schema",
            "/api/recipes",
            "/api/tool-outputs",
        ],
    )
    def test_unauthenticated_reads_return_401(self, endpoint):
        """Unauthenticated requests to read APIs must return 401."""
        client = ui_client(create_app())
        resp = client.get(endpoint)
        assert resp.status_code == 401, f"{endpoint} allowed unauthenticated access"

    @pytest.mark.parametrize(
        "endpoint",
        [
            "/api/runs",
            "/api/system",
            "/api/templates",
            "/api/train/status",
            "/api/config/schema",
            "/api/recipes",
            "/api/tool-outputs",
        ],
    )
    def test_bad_token_reads_return_401(self, endpoint):
        """Requests with an invalid Bearer token must return 401."""
        client = ui_client(create_app())
        resp = client.get(endpoint, headers={"Authorization": "Bearer invalid_secret_token"})
        assert resp.status_code == 401

    def test_authenticated_reads_succeed(self):
        """Authenticated requests with valid Bearer token return 200."""
        client = ui_client(create_app())
        headers = _auth_headers()

        resp = client.get("/api/system", headers=headers)
        assert resp.status_code == 200

        resp = client.get("/api/templates", headers=headers)
        assert resp.status_code == 200

        resp = client.get("/api/train/status", headers=headers)
        assert resp.status_code == 200

        resp = client.get("/api/config/schema", headers=headers)
        assert resp.status_code == 200

        resp = client.get("/api/recipes", headers=headers)
        assert resp.status_code == 200

        resp = client.get("/api/tool-outputs", headers=headers)
        assert resp.status_code == 200


class TestPublicEndpoints:
    """Verify that public endpoints remain reachable without authentication."""

    def test_index_is_public(self):
        client = ui_client(create_app())
        resp = client.get("/")
        assert resp.status_code == 200
        assert "text/html" in resp.headers["content-type"]

    def test_health_is_public(self):
        client = ui_client(create_app())
        resp = client.get("/api/health")
        assert resp.status_code == 200
        assert resp.json() == {"status": "ok"}

    def test_static_files_are_public(self):
        client = ui_client(create_app())
        resp = client.get("/static/app.js")
        assert resp.status_code == 200


class TestSSEAuthenticationAndTickets:
    """Verify SSE streaming authentication via single-use tickets and headers."""

    @pytest.mark.parametrize(
        "endpoint",
        [
            "/api/train/logs",
            "/api/train/metrics/live",
            "/api/train/stream",
        ],
    )
    def test_unauthenticated_sse_returns_401(self, endpoint):
        """SSE endpoints without auth must return 401."""
        client = ui_client(create_app())
        resp = client.get(endpoint)
        assert resp.status_code == 401

    def test_bearer_header_on_sse_succeeds(self):
        """SSE endpoints accept direct Authorization Bearer header."""
        client = ui_client(create_app())
        resp = client.get("/api/train/logs", headers=_auth_headers())
        assert resp.status_code == 200

    def test_durable_token_in_query_rejected(self):
        """?token= query param must NOT be accepted on SSE endpoints (#687)."""
        client = ui_client(create_app())
        token = get_auth_token()
        resp = client.get(f"/api/train/logs?token={token}")
        assert resp.status_code == 401

    def test_auth_ticket_exchange_and_single_use(self):
        """POST /api/auth/ticket issues ticket, ticket is single-use only."""
        client = ui_client(create_app())

        # Unauthenticated ticket request fails
        resp = client.post("/api/auth/ticket")
        assert resp.status_code == 401

        # Authenticated ticket request succeeds
        resp = client.post("/api/auth/ticket", headers=_auth_headers())
        assert resp.status_code == 200
        data = resp.json()
        assert "ticket" in data
        ticket = data["ticket"]
        assert len(ticket) > 20

        # First connection with ticket succeeds
        resp_sse = client.get(f"/api/train/logs?ticket={ticket}")
        assert resp_sse.status_code == 200

        # Reusing the same ticket fails with 401 (single use)
        resp_reused = client.get(f"/api/train/logs?ticket={ticket}")
        assert resp_reused.status_code == 401

    def test_invalid_ticket_rejected(self):
        """Nonexistent ticket fails with 401."""
        client = ui_client(create_app())
        resp = client.get("/api/train/logs?ticket=nonexistent-ticket-value")
        assert resp.status_code == 401


class TestNonLoopbackBindingProtection:
    """Verify that binding to non-loopback requires a valid auth token."""

    def test_create_app_refuses_non_loopback_without_valid_token(self, monkeypatch):
        """create_app raises ValueError on non-loopback host if auth token is missing or invalid."""
        import soup_cli.ui.app as ui_app

        monkeypatch.setattr(ui_app, "_auth_token", "")
        with pytest.raises(ValueError, match="requires a valid authentication token"):
            create_app(host="0.0.0.0")

        with pytest.raises(ValueError, match="requires a valid authentication token"):
            create_app(host="192.168.1.15")

        monkeypatch.setattr(ui_app, "_auth_token", "short")
        with pytest.raises(ValueError, match="requires a valid authentication token"):
            create_app(host="0.0.0.0")

    def test_create_app_allows_loopback_default(self):
        """create_app allows loopback hosts with normal token."""
        app = create_app(host="127.0.0.1")
        client = ui_client(app)
        resp = client.get("/api/system", headers=_auth_headers())
        assert resp.status_code == 200

    def test_create_app_allows_loopback_variants(self):
        """create_app treats 127.0.0.2, ::1, [::1], and localhost as loopback."""
        for host in ["127.0.0.1", "127.0.0.2", "::1", "[::1]", "localhost"]:
            app = create_app(host=host)
            assert app is not None

    def test_cli_ui_refuses_startup_on_non_loopback_with_invalid_token(self, monkeypatch):
        """soup ui on non-loopback exits with code 2 when token is invalid or missing."""
        import soup_cli.ui.app as ui_app

        monkeypatch.setattr(ui_app, "_auth_token", "")
        runner = CliRunner()
        result = runner.invoke(cli_app, ["ui", "--host", "0.0.0.0"])
        assert result.exit_code == 2
        assert "binding non-loopback host" in result.output.lower()

    def test_cli_ui_refuses_public_with_invalid_token(self, monkeypatch):
        """soup ui --public exits with code 2 when token is invalid or missing."""
        import soup_cli.ui.app as ui_app

        monkeypatch.setattr(ui_app, "_auth_token", "")
        runner = CliRunner()
        result = runner.invoke(cli_app, ["ui", "--public"])
        assert result.exit_code == 2
        assert "binding non-loopback host" in result.output.lower()