Skip to content

Commit 56711ed

Browse files
committed
fix(service): validate read_only via pydantic instead of truthy set
- route OSW_READ_ONLY through Settings so an unparseable value raises - a typo like "ture" previously yielded False, silently enabling writes - "y"/"t" now parse as true; all documented spellings keep working - blank/whitespace-only still falls back to the default, as for the ints - drop the now-unused _TRUTHY set Follow-up to #143.
1 parent f4ef72c commit 56711ed

2 files changed

Lines changed: 56 additions & 5 deletions

File tree

‎src/osw/service/config.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
from osw.auth import CredentialManager
2323

24-
_TRUTHY = {"1", "true", "yes", "on"}
25-
2624
# Environment variable names. Each tuple lists the canonical ``OSW_*`` name
2725
# first, followed by every alias that must keep working. ``OSW_CRED_FILEPATH``
2826
# is canonical (rather than an ``OSW_MCP_``-prefixed name) because
@@ -417,11 +415,16 @@ def load(strict: bool = True) -> Settings:
417415
password=password,
418416
cred_filepath=cred_filepath,
419417
sparql_endpoint=_first_env(ENV_SPARQL_ENDPOINT),
420-
read_only=(_first_env(ENV_READ_ONLY) or "").lower() in _TRUTHY,
421418
state_dir=_first_env(ENV_STATE_DIR),
422419
)
423-
# An unset or blank/whitespace-only integer variable falls back to the
424-
# model default; pass the raw string only when there is one to validate.
420+
# An unset or blank/whitespace-only variable falls back to the model
421+
# default; pass the raw string only when there is one to validate. Letting
422+
# pydantic parse read_only rather than testing membership in a truthy set
423+
# matters because the default is fail-open: a typo like "ture" would
424+
# otherwise silently leave writes enabled on a server meant to be read-only.
425+
read_only_raw = _first_env(ENV_READ_ONLY)
426+
if read_only_raw is not None and read_only_raw.strip():
427+
kwargs["read_only"] = read_only_raw
425428
max_results_raw = _first_env(ENV_MAX_RESULTS)
426429
if max_results_raw is not None and max_results_raw.strip():
427430
kwargs["max_results"] = max_results_raw

‎tests/test_service_config.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,54 @@ def test_error_names_the_alias_that_was_set(monkeypatch):
588588
assert "OSW_MCP_MAX_RESULTS" in str(exc.value)
589589

590590

591+
def test_misspelled_read_only_raises(monkeypatch):
592+
# A typo must not silently enable writes: read_only is the one flag whose
593+
# fail-open default is dangerous, so an unparseable value has to be loud.
594+
monkeypatch.setenv("OSW_DOMAIN", "wiki.example.org")
595+
monkeypatch.setenv("OSW_USERNAME", "alice")
596+
monkeypatch.setenv("OSW_PASSWORD", "secret")
597+
monkeypatch.setenv("OSW_READ_ONLY", "ture")
598+
with pytest.raises(RuntimeError) as exc:
599+
config.load()
600+
assert "OSW_READ_ONLY" in str(exc.value)
601+
602+
603+
@pytest.mark.parametrize("raw", ["1", "true", "TRUE", "yes", "on", "y", "t"])
604+
def test_read_only_truthy_spellings(monkeypatch, raw):
605+
monkeypatch.setenv("OSW_DOMAIN", "wiki.example.org")
606+
monkeypatch.setenv("OSW_USERNAME", "alice")
607+
monkeypatch.setenv("OSW_PASSWORD", "secret")
608+
monkeypatch.setenv("OSW_READ_ONLY", raw)
609+
assert config.load().read_only is True
610+
611+
612+
@pytest.mark.parametrize("raw", ["0", "false", "FALSE", "no", "off", "n", "f"])
613+
def test_read_only_falsy_spellings(monkeypatch, raw):
614+
monkeypatch.setenv("OSW_DOMAIN", "wiki.example.org")
615+
monkeypatch.setenv("OSW_USERNAME", "alice")
616+
monkeypatch.setenv("OSW_PASSWORD", "secret")
617+
monkeypatch.setenv("OSW_READ_ONLY", raw)
618+
assert config.load().read_only is False
619+
620+
621+
def test_blank_read_only_falls_back_to_default(monkeypatch):
622+
monkeypatch.setenv("OSW_DOMAIN", "wiki.example.org")
623+
monkeypatch.setenv("OSW_USERNAME", "alice")
624+
monkeypatch.setenv("OSW_PASSWORD", "secret")
625+
monkeypatch.setenv("OSW_READ_ONLY", " ")
626+
assert config.load().read_only is False
627+
628+
629+
def test_read_only_error_names_the_alias_that_was_set(monkeypatch):
630+
monkeypatch.setenv("OSW_DOMAIN", "wiki.example.org")
631+
monkeypatch.setenv("OSW_USERNAME", "alice")
632+
monkeypatch.setenv("OSW_PASSWORD", "secret")
633+
monkeypatch.setenv("OSW_MCP_READ_ONLY", "disabled")
634+
with pytest.raises(RuntimeError) as exc:
635+
config.load()
636+
assert "OSW_MCP_READ_ONLY" in str(exc.value)
637+
638+
591639
def test_domain_with_whitespace_rejected():
592640
with pytest.raises(ValidationError):
593641
Settings(domain="wiki.example.org has a space")

0 commit comments

Comments
 (0)