osint-dashboard/tests/test_ingest_ssrf_and_sources.py

70 lines
2.2 KiB
Python
Raw Normal View History

"""SSRF guard on ingest triggers + PATCH /api/sources allowlist."""
from __future__ import annotations
import asyncio
import httpx
import pytest
from pydantic import ValidationError
from main import app
feat(auth): public read-only HUD, admin-only API keys and mutations Adds single-admin authentication to the dashboard. Anonymous visitors get a read-only site (map + news); the one admin unlocks everything else. - app/auth.py: deny-by-default ASGI middleware on /api/* with an explicit public read allowlist; /login page; /api/auth/{login,logout,me}; PBKDF2 password hashing; HMAC-signed HttpOnly SameSite=Lax session cookie (7d, Secure behind X-Forwarded-Proto: https); 5-fail/15-min per-IP login limit; Origin/Referer host check on state-changing admin requests. - Fails closed: missing ADMIN_PASSWORD_HASH or ADMIN_SESSION_SECRET -> admin routes 403 'admin auth not configured', public site unaffected. - app/main.py: mounts the middleware + router (6 lines, no per-route decorators). - app/static/index.html: hides Ingest/API Keys/Settings, the summarizer panel and geofence write controls until /api/auth/me says admin; adds a discreet SIGN IN link to /login. CSS gates fail closed without JS. - tests/test_auth.py: 42 tests (anonymous 401s, login/logout, tampered and expired cookies, fail-closed, rate limit, CSRF, HUD markup). - Existing API tests now send an admin session cookie for admin routes. Public allowlist extends the spec's list with the remaining read-only GETs the HUD calls (/api/stats, /api/conflicts, /api/place, /api/satellites, /api/infrastructure, /api/map/gpsjam, /api/map/times, /api/fire-incidents, /api/fire-perimeters, /api/weather-alerts, /api/storms, /api/geofences/{id}/at) so an anonymous HUD is not half-broken.
2026-11-13 20:56:07 -05:00
from conftest import admin_cookies
BASE = "http://test"
LINK_LOCAL_META = "http://169.254.169.254/latest/meta-data/"
LOOPBACK = "http://127.0.0.1/secret"
async def _req(method: str, path: str, **kw) -> httpx.Response:
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(transport=transport, base_url=BASE) as client:
feat(auth): public read-only HUD, admin-only API keys and mutations Adds single-admin authentication to the dashboard. Anonymous visitors get a read-only site (map + news); the one admin unlocks everything else. - app/auth.py: deny-by-default ASGI middleware on /api/* with an explicit public read allowlist; /login page; /api/auth/{login,logout,me}; PBKDF2 password hashing; HMAC-signed HttpOnly SameSite=Lax session cookie (7d, Secure behind X-Forwarded-Proto: https); 5-fail/15-min per-IP login limit; Origin/Referer host check on state-changing admin requests. - Fails closed: missing ADMIN_PASSWORD_HASH or ADMIN_SESSION_SECRET -> admin routes 403 'admin auth not configured', public site unaffected. - app/main.py: mounts the middleware + router (6 lines, no per-route decorators). - app/static/index.html: hides Ingest/API Keys/Settings, the summarizer panel and geofence write controls until /api/auth/me says admin; adds a discreet SIGN IN link to /login. CSS gates fail closed without JS. - tests/test_auth.py: 42 tests (anonymous 401s, login/logout, tampered and expired cookies, fail-closed, rate limit, CSRF, HUD markup). - Existing API tests now send an admin session cookie for admin routes. Public allowlist extends the spec's list with the remaining read-only GETs the HUD calls (/api/stats, /api/conflicts, /api/place, /api/satellites, /api/infrastructure, /api/map/gpsjam, /api/map/times, /api/fire-incidents, /api/fire-perimeters, /api/weather-alerts, /api/storms, /api/geofences/{id}/at) so an anonymous HUD is not half-broken.
2026-11-13 20:56:07 -05:00
client.cookies.update(admin_cookies())
return await client.request(method, path, **kw)
def test_rss_ingest_rejects_link_local_metadata_url(monkeypatch):
called = {"n": 0}
async def _boom(*_a, **_k):
called["n"] += 1
raise AssertionError("ingest_rss_feed must not run for a private URL")
monkeypatch.setattr("main.ingest_rss_feed", _boom)
resp = asyncio.run(_req("POST", "/api/ingest/rss", params={"feed_url": LINK_LOCAL_META}))
assert resp.status_code == 400
assert called["n"] == 0
def test_gdelt_ingest_rejects_private_query_url(monkeypatch):
called = {"n": 0}
async def _boom(*_a, **_k):
called["n"] += 1
raise AssertionError("ingest_gdelt must not run for a private URL query")
monkeypatch.setattr("main.ingest_gdelt", _boom)
resp = asyncio.run(_req("POST", "/api/ingest/gdelt", params={"query": LOOPBACK}))
assert resp.status_code == 400
assert called["n"] == 0
def test_update_source_rejects_unknown_fields():
sid = "00000000-0000-0000-0000-000000000001"
resp = asyncio.run(_req("PATCH", f"/api/sources/{sid}", json={"enabled": True, "source_type": "rss"}))
assert resp.status_code == 422
def test_feed_source_update_allowlist_only():
from schemas import FeedSourceUpdate
payload = FeedSourceUpdate(name="n", url="https://example.com/rss", config={"k": 1}, enabled=False)
assert payload.model_dump(exclude_unset=True) == {
"name": "n",
"url": "https://example.com/rss",
"config": {"k": 1},
"enabled": False,
}
with pytest.raises(ValidationError):
FeedSourceUpdate.model_validate({"enabled": True, "id": "00000000-0000-0000-0000-000000000001"})