Harden config: no credentials in code (REVIEW SEC-001, SEC-006)
SEC-006: database_url had a working default carrying a real username and password. Removed. Absence is now reported by 'poorbird check' as a problem (exit 1) and raised by db.py with a message that names the variable and the example file, instead of SQLAlchemy's "Could not parse URL from string ''" three frames down. SEC-001: enroll_secret stays environment-only and keeps its empty default so the Phase 1 commands still run on a fresh clone. The device endpoints will refuse to serve without it when they land. Both fields now reject unedited placeholders (CHANGE_ME and friends), so a copied .example cannot boot as though it were configured. Tests: 12 -> 17. New cases cover the absent default, the readable failure, and placeholder rejection for both fields. Note: 6 files unrelated to this change are not clean under 'ruff format' (the repo is wrapped at 88, pyproject sets line-length 100). Left alone to keep this diff reviewable. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2188bed633
commit
3344a9e9b1
5 files changed
+125
-17
No files matched your search
@@ -1,5 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from poorbird.config import Settings
|
||||
|
||||
|
||||
@@ -18,8 +21,9 @@ def test_every_setting_reads_from_the_environment(monkeypatch):
|
||||
assert s.enroll_secret_is_set
|
||||
|
||||
|
||||
def test_database_url_is_tcp_not_a_unix_socket():
|
||||
def test_database_url_is_tcp_not_a_unix_socket(monkeypatch):
|
||||
"""A Unix socket is the one genuinely container-hostile choice."""
|
||||
monkeypatch.setenv("POORBIRD_DATABASE_URL", "postgresql+psycopg://u:p@127.0.0.1:5432/poorbird")
|
||||
s = Settings(_env_file=None)
|
||||
assert "@" in s.database_url and ":" in s.database_url.rpartition("@")[2]
|
||||
assert "/var/run/postgresql" not in s.database_url
|
||||
@@ -28,3 +32,52 @@ def test_database_url_is_tcp_not_a_unix_socket():
|
||||
def test_enroll_secret_is_not_leaked_by_repr():
|
||||
s = Settings(_env_file=None, enroll_secret="hunter2")
|
||||
assert "hunter2" not in repr(s)
|
||||
|
||||
|
||||
# --- SEC-006: no credentials baked into code --------------------------------
|
||||
|
||||
|
||||
def test_no_database_credentials_are_baked_in(monkeypatch):
|
||||
"""A default URL in code is a password in code, however harmless it looks."""
|
||||
monkeypatch.delenv("POORBIRD_DATABASE_URL", raising=False)
|
||||
s = Settings(_env_file=None)
|
||||
assert s.database_url == ""
|
||||
assert not s.database_url_is_set
|
||||
|
||||
|
||||
def test_unset_database_url_fails_with_a_readable_message(monkeypatch):
|
||||
"""Graceful, not a SQLAlchemy URL parse error from three frames down."""
|
||||
monkeypatch.delenv("POORBIRD_DATABASE_URL", raising=False)
|
||||
monkeypatch.setattr("poorbird.config.get_settings", lambda: Settings(_env_file=None))
|
||||
|
||||
import importlib
|
||||
import sys
|
||||
|
||||
sys.modules.pop("poorbird.db", None)
|
||||
with pytest.raises(RuntimeError, match="POORBIRD_DATABASE_URL is not set"):
|
||||
importlib.import_module("poorbird.db")
|
||||
sys.modules.pop("poorbird.db", None)
|
||||
|
||||
|
||||
def test_placeholder_database_url_is_rejected(monkeypatch):
|
||||
monkeypatch.setenv(
|
||||
"POORBIRD_DATABASE_URL", "postgresql+psycopg://poorbird:CHANGE_ME@127.0.0.1:5432/poorbird"
|
||||
)
|
||||
with pytest.raises(ValidationError, match="placeholder"):
|
||||
Settings(_env_file=None)
|
||||
|
||||
|
||||
# --- SEC-001: enroll secret comes from the environment only -----------------
|
||||
|
||||
|
||||
def test_enroll_secret_has_no_value_baked_in(monkeypatch):
|
||||
monkeypatch.delenv("POORBIRD_ENROLL_SECRET", raising=False)
|
||||
s = Settings(_env_file=None)
|
||||
assert s.enroll_secret.get_secret_value() == ""
|
||||
assert not s.enroll_secret_is_set
|
||||
|
||||
|
||||
def test_placeholder_enroll_secret_is_rejected(monkeypatch):
|
||||
monkeypatch.setenv("POORBIRD_ENROLL_SECRET", "CHANGE_ME")
|
||||
with pytest.raises(ValidationError, match="placeholder"):
|
||||
Settings(_env_file=None)
|
||||
Reference in new issue
Block a user