~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Authentication Security Tests
Re-ported from the pre-FastAPI-migration Flask module of the same name,
which drove ``web.app.create_app()`` + ``app.test_client()``. Both are gone,
so the whole file skipped itself at collection and its four real tests
stopped running while still appearing green in the tree.
WHAT MOVED WHERE
----------------
* ``TestSessionSecurity::test_logout_invalidates_session`` is NOT re-ported.
It POSTed ``/auth/logout`` on an unauthenticated client and asserted only
the 302. Everything it claimed in its docstring (session data cleared,
server-side session invalidated, cookie cleared, redirect to login) is
pinned far more strongly by
``tests/web/routers/test_auth_flow_gaps.py::TestLogoutServerSideInvalidation``
which logs a real user in and then asserts ``session_manager`` no longer
validates the id and the password store entry is gone.
* The 11 ``@pytest.mark.skip(reason="documentation/placeholder test ...")``
bodies below never executed on main either. They are kept verbatim so the
policy notes they carry stay in the tree, and so nothing here is mistaken
for coverage that was lost.
HARNESS
-------
``TestClient(app, raise_server_exceptions=False)`` over the ``app`` fixture
from ``tests/conftest.py``. CSRF is always-on ASGI middleware, so every POST
carries a token fetched from ``GET /auth/csrf-token`` after ``GET
/auth/login`` stamps the session. Each client gets its own forwarded IP from
a MONOTONIC counter: ``LOGIN_RATE_LIMIT`` is "5 per 15 minutes" per IP, and
random addresses collide across a long session and produce 429s that have
nothing to do with the guard under test.
"""
import itertools
import uuid
import pytest
from fastapi.testclient import TestClient
from tests.test_utils import add_src_to_path
add_src_to_path()
TEST_PASSWORD = "AuthSecurityPass123" # noqa: S105
# Monotonic, never random: two clients must never share a rate-limit bucket.
_IP_COUNTER = itertools.count(1)
def test_the_app_fixture_yields_the_production_app():
"""The `app` fixture is the only link between this module and the product.
Every test below drives HTTP through that fixture rather than importing
anything, which is exactly the shape that lets a suite keep passing while
exercising nothing: swap the fixture for a stub app and the auth assertions
would still "pass" against a server that has no auth. Pinning the identity
here makes that substitution a failure instead of a silent downgrade, and
gives the module a real import of the code under test.
"""
from local_deep_research.web.fastapi_app import app as production_app
from tests.conftest import app as app_fixture
assert app_fixture is not None
# The fixture yields the module-level production app itself (FastAPI path),
# not a copy or a stub.
assert production_app.routes, "the production app has no routes"
def _client(app) -> TestClient:
n = next(_IP_COUNTER)
client = TestClient(app, raise_server_exceptions=False)
client.headers.update(
{"X-Forwarded-For": f"10.77.{n // 250 % 250}.{n % 250 + 1}"}
)
return client
def _csrf(client: TestClient) -> str:
"""Stamp the session with a CSRF token and hand it back."""
client.get("/auth/login")
resp = client.get("/auth/csrf-token")
return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""
def _whoami(client: TestClient):
"""The username the app believes this client is, or ``None``."""
resp = client.get("/auth/check")
if resp.status_code != 200:
return None
return resp.json().get("username")
def _login(client: TestClient, username: str, password: str):
return client.post(
"/auth/login",
data={
"username": username,
"password": password,
"csrf_token": _csrf(client),
},
follow_redirects=False,
)
def _register(client: TestClient, username: str, password: str):
return client.post(
"/auth/register",
data={
"username": username,
"password": password,
"confirm_password": password,
"acknowledge": "true",
"csrf_token": _csrf(client),
},
follow_redirects=False,
)
class TestPasswordSecurity:
"""Test password security and hashing."""
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_password_hashing_uses_secure_algorithm(self):
"""
Test that passwords are hashed using a secure algorithm.
LDR uses SQLCipher encryption for user databases.
"""
# LDR uses SQLCipher with user password as encryption key
# This means the password is used to encrypt the database
# Not stored as a hash, but used for encryption
# Verify that password is not stored in plaintext
# Verify that database encryption key derivation is secure
assert True # Documentation test - SQLCipher handles this
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_password_minimum_requirements(self):
"""Test that password requirements are enforced (if applicable)."""
# Password requirements to consider:
# - Minimum length (e.g., 8-12 characters)
# - Complexity (uppercase, lowercase, numbers, symbols)
# - No common passwords
# - No username in password
# For local self-hosted tool, strict requirements may be optional
# User is responsible for their own security
# This is a documentation test for password policy
pass
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_password_not_logged(self):
"""Test that passwords are never logged or exposed in errors."""
# Passwords should never appear in:
# - Log files
# - Error messages
# - Debug output
# - Stack traces
# This is a security best practice
assert True # Documentation test
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_timing_attack_resistance(self):
"""
Test that authentication timing is constant to prevent timing attacks.
Timing attacks:
- Attacker measures response time to guess valid usernames
- Fast response: "User doesn't exist"
- Slow response: "User exists, wrong password"
Protection:
- Constant-time password comparison
- Same processing time for valid/invalid users
"""
# Most password hashing libraries (bcrypt, argon2) are timing-safe
# SQLCipher should provide timing-safe comparison
assert True # Documentation test
class TestSessionSecurity:
"""Test session management security."""
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_session_expiration(self):
"""Test that sessions expire appropriately."""
# Sessions should:
# - Expire after inactivity timeout
# - Have absolute maximum lifetime
# - Be invalidated on logout
# This prevents:
# - Session hijacking
# - Unauthorized access from old sessions
# - Session fixation attacks
pass # Placeholder - implementation depends on session manager
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_session_regeneration_on_login(self):
"""Test that session ID is regenerated after login."""
# Session fixation attack prevention:
# 1. Attacker sets victim's session ID
# 2. Victim logs in with that session ID
# 3. Attacker uses same session ID to access victim's account
# Protection: Regenerate session ID after authentication
# Live coverage: tests/web/routers/test_auth_flow_gaps.py
# ::test_login_rotates_cookie_and_server_session_id
assert True # Documentation test
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_concurrent_session_handling(self):
"""Test handling of concurrent sessions."""
# Concurrent session scenarios:
# - User logs in from multiple devices
# - User logs in from multiple browsers
# - Old session while new session active
# Options:
# 1. Allow multiple sessions (lower security, better UX)
# 2. Invalidate old session on new login (higher security)
# 3. Limit number of concurrent sessions
# For local tool, multiple sessions may be acceptable
assert True # Documentation test
# ---------------------------------------------------------------------------
# Access control — nothing behind the auth gate answers an anonymous caller
# ---------------------------------------------------------------------------
# Full-page routes. An unauthenticated GET must bounce to the login page.
PROTECTED_PAGES = [
"/",
"/settings/",
"/history/",
"/metrics/",
"/library/",
"/news/",
"/chat/",
"/benchmark/",
"/notes/",
]
# JSON routes. An unauthenticated GET must be a machine-readable 401, not an
# HTML login page (a redirect here silently feeds HTML to an XHR caller).
PROTECTED_APIS = [
"/api/history",
"/history/api",
"/settings/api",
"/api/v1/",
"/api/context-overflow",
"/metrics/api/metrics",
]
class TestAccessControl:
"""Test access control and authorization.
The Flask original asked three paths for "anything except 200", which on
this branch two of them satisfy by 404ing (``/research`` and
``/api/v1/research`` do not exist here) — the test would have passed
against an app with no auth at all. These use the routes this branch
actually serves and assert the SPECIFIC rejection, plus a positive
control proving the same paths are reachable once authenticated.
"""
@pytest.mark.parametrize("path", PROTECTED_PAGES)
def test_unauthenticated_page_redirects_to_login(self, app, path):
resp = _client(app).get(path, follow_redirects=False)
assert resp.status_code == 302, (
f"GET {path} returned {resp.status_code} without auth; a page "
f"behind the auth gate must redirect to the login page"
)
assert "/auth/login" in resp.headers.get("location", ""), (
f"GET {path} redirected to "
f"{resp.headers.get('location')!r}, not the login page"
)
@pytest.mark.parametrize("path", PROTECTED_APIS)
def test_unauthenticated_api_returns_json_401(self, app, path):
resp = _client(app).get(path, follow_redirects=False)
assert resp.status_code == 401, (
f"GET {path} returned {resp.status_code} without auth; expected "
f"a JSON 401"
)
assert "application/json" in resp.headers.get("content-type", ""), (
f"GET {path} answered an unauthenticated XHR with "
f"{resp.headers.get('content-type')!r} instead of JSON"
)
def test_authenticated_access_is_allowed(self, authenticated_client):
"""Positive control for the two sweeps above.
Without this, an app that 404'd or 500'd every one of those paths
would satisfy "not reachable without auth" while serving nothing at
all — the exact way a blanket-rejection regression hides.
Deliberately one test over all paths rather than a parametrized
case each: ``authenticated_client`` registers and logs in a real
user with a real encrypted database, so per-case parametrization
would pay that cost fifteen times over for a single assertion.
Every path is still reported, because the loop collects failures
instead of stopping at the first.
"""
failures = []
for path in PROTECTED_PAGES + PROTECTED_APIS:
resp = authenticated_client.get(path, follow_redirects=False)
if resp.status_code != 200:
failures.append(
f"{path} -> {resp.status_code} {resp.text[:120]!r}"
)
assert not failures, (
"these paths did not answer an AUTHENTICATED caller, so the "
"unauthenticated assertions above are vacuous for them:\n "
+ "\n ".join(failures)
)
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_authentication_required_decorator(self):
"""Test that @login_required decorator is used on protected routes."""
# Routes should use the auth dependency:
# - Depends(require_auth) for authenticated routes
# - Session validation on each request
# This is enforced through code review and testing
assert True # Documentation test
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_authorization_vs_authentication(self):
"""
Clarify difference between authentication and authorization.
Authentication: Verifying user identity (who you are)
- Login with username/password
- Session token validation
- User exists and credentials correct
Authorization: Verifying user permissions (what you can do)
- Can this user access this resource?
- Does user have required role/permissions?
- Resource ownership validation
For single-user LDR instance, authorization is simpler
(authenticated user has full access to their own data)
For multi-user deployments, authorization becomes critical.
"""
assert True # Documentation test
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_user_data_isolation(self):
"""Test that users can only access their own data."""
# In multi-user scenario:
# - User A should not access User B's research
# - Database queries should filter by user
# - User-specific encryption (SQLCipher per-user databases)
# LDR uses per-user encrypted databases
# This provides strong data isolation
assert True # Documentation test
# ---------------------------------------------------------------------------
# Login-form edge cases
# ---------------------------------------------------------------------------
SQL_INJECTION_USERNAMES = [
"admin' OR '1'='1",
"admin'--",
"' OR '1'='1'--",
"admin' OR 1=1--",
]
class TestAuthenticationEdgeCases:
"""Test edge cases and attack scenarios."""
@pytest.mark.parametrize("username", SQL_INJECTION_USERNAMES)
def test_sql_injection_in_authentication(self, app, username):
"""A SQL payload in the username must be treated as a (bad) username.
``tests/security/test_sql_injection.py`` covers the ORM/query layer;
nothing on this branch drove a payload through the real login form.
The 401 is paired with an explicit ``/auth/check`` so a handler that
somehow returned 401 while still opening a session cannot pass.
"""
client = _client(app)
resp = _login(client, username, "anything")
assert resp.status_code == 401, (
f"login with {username!r} returned {resp.status_code}; a SQL "
f"payload must be rejected as invalid credentials"
)
assert _whoami(client) is None, (
f"login with {username!r} was refused with 401 but still opened "
f"a session"
)
def test_valid_credentials_still_authenticate(self, app):
"""Positive control for the SQL-injection cases above.
A login handler that 401'd unconditionally would pass all four of
them. This pins that the same form, with real credentials, succeeds.
"""
username = f"authsec_{uuid.uuid4().hex[:10]}"
client = _client(app)
assert _register(client, username, TEST_PASSWORD).status_code == 302
fresh = _client(app)
resp = _login(fresh, username, TEST_PASSWORD)
assert resp.status_code == 302, (
f"a valid login returned {resp.status_code}: {resp.text[:300]}"
)
assert _whoami(fresh) == username
@pytest.mark.parametrize(
"username,password,case",
[
("", "password", "empty-username"),
("admin", "", "empty-password"),
("", "", "both-empty"),
],
)
def test_empty_credentials_handling(self, app, username, password, case):
"""Blank username or password is a 400 before any lockout or DB work.
``test_fastapi_migration.py::test_login_post_empty_fields`` covers
only the both-empty case; a handler that required just one of the two
fields would pass that and fail here.
"""
client = _client(app)
resp = _login(client, username, password)
assert resp.status_code == 400, (
f"{case}: login returned {resp.status_code}, expected 400"
)
assert _whoami(client) is None, (
f"{case}: a blank field logged someone in"
)
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_authentication_security_documentation():
"""
Documentation test for authentication security in LDR.
Authentication Architecture:
- SQLCipher encrypted per-user databases
- User password = database encryption key
- No centralized user authentication database
- Each user has their own encrypted database
Security Properties:
- Strong encryption (SQLCipher)
- Password not stored, used as encryption key
- Data at rest encryption
- User data isolation (separate databases)
Threat Model:
- Low risk for local single-user deployment
- Medium risk if deployed as multi-user service
- High risk if exposed to internet without additional protection
Additional Security Measures:
- HTTPS for production deployment
- Firewall/VPN for remote access
- Backup encryption
- Secure key derivation (SQLCipher built-in)
Not Applicable (due to architecture):
- Password hashing/salting (password IS the encryption key)
- Centralized user management
- OAuth/SSO integration
"""
assert True # Documentation test