Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions analysis/business_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,21 @@ def load_business_context_file(path: Any) -> BusinessContext:
# An empty FILE is an empty CONTEXT, which is valid.
return BusinessContext()

# Same three escapes, and the same fix, as policy.load_policy_file().
# Same three escapes, and the same fix, as policy.load_policy_file(): only
# json.loads is guarded by `except ValueError` and RecursionError, because
# they are about the file only there. load_business_context() raises
# BusinessContextError for every problem with a file and does not recurse
# into nested values, so a bare ValueError from it is a bug and is left
# to show as one (#446 follow-up, Shubham's note).
try:
parsed = json.loads(text, object_pairs_hook=_refuse_duplicate_keys)
return load_business_context(parsed)
except json.JSONDecodeError as error:
raise BusinessContextError(
f"{file_path} is not valid JSON: line {error.lineno}, "
f"column {error.colno}: {error.msg}"
) from error
except BusinessContextError:
# _refuse_duplicate_keys() runs inside json.loads and raises this.
raise
except RecursionError as error:
raise BusinessContextError(
Expand All @@ -365,6 +370,8 @@ def load_business_context_file(path: Any) -> BusinessContext:
f"{file_path} holds a value that cannot be read: {error}"
) from error

return load_business_context(parsed)


def _refuse_duplicate_keys(pairs: List[Tuple[str, Any]]) -> Dict[str, Any]:
"""Build one JSON object, refusing a key that appears twice in it.
Expand Down
17 changes: 16 additions & 1 deletion analysis/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,21 +714,36 @@ def load_policy_file(path: Any) -> Policy:
# on upload rather than a refusal naming the file: an integer of more
# than 4,300 digits (ValueError), and nesting deeper than Python's
# recursion limit, hit by json.loads or by load_policy() walking it.
#
# THE TWO STEPS ARE GUARDED SEPARATELY (#446 follow-up, Shubham's note).
# The bare ValueError is json.loads's, and only json.loads's: it is the
# one that really is about the file. Wrapping load_policy() in the same
# `except ValueError` made a plain ValueError from a validation BUG
# reach the user as "holds a value that cannot be read", a refusal that
# blames their file for our mistake. load_policy() raises PolicyError
# for every problem with a file, so it is guarded only for the
# RecursionError a deeply nested rule list causes, and anything else it
# raises stays visible as the bug it is.
try:
parsed = json.loads(text, object_pairs_hook=_refuse_duplicate_keys)
return load_policy(parsed)
except json.JSONDecodeError as error:
raise PolicyError(
f"{file_path} is not valid JSON: line {error.lineno}, "
f"column {error.colno}: {error.msg}"
) from error
except PolicyError:
# _refuse_duplicate_keys() runs inside json.loads and raises this.
raise
except RecursionError as error:
raise PolicyError(f"{file_path} is nested too deeply to be a policy") from error
except ValueError as error:
raise PolicyError(f"{file_path} holds a value that cannot be read: {error}") from error

try:
return load_policy(parsed)
except RecursionError as error:
raise PolicyError(f"{file_path} is nested too deeply to be a policy") from error


def _refuse_duplicate_keys(pairs: List[Tuple[str, Any]]) -> Dict[str, Any]:
"""Build one JSON object, refusing a key that appears twice in it.
Expand Down
38 changes: 38 additions & 0 deletions tests/test_business_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import pytest

import analysis.business_context as context_module
from analysis.business_context import (
TIERS,
BusinessContext,
Expand Down Expand Up @@ -406,6 +407,43 @@ def test_a_file_json_cannot_hold_faithfully_is_refused_not_crashed_or_trimmed(
load_business_context_file(path)


def test_a_duplicate_key_refusal_keeps_its_own_message_not_a_generic_one(tmp_path):
"""Mirrors the policy loader's test: BusinessContextError is a ValueError,
so without `except BusinessContextError: raise` the duplicate-key refusal
raised inside json.loads would be re-wrapped as "holds a value that cannot
be read", which a substring match on "appears twice" still accepts."""
path = tmp_path / "business-context.json"
path.write_bytes(b'[{"device": "rtr-us5", "tier": "critical", "tier": "standard"}]')

with pytest.raises(BusinessContextError) as raised:
load_business_context_file(path)

assert "appears twice" in str(raised.value)
assert "cannot be read" not in str(raised.value)


def test_a_bug_in_the_validator_is_not_relabelled_as_a_bad_file(tmp_path, monkeypatch):
"""Mirrors the policy loader's test (#446 follow-up). Only json.loads
raises a bare ValueError that is really about the file, so only json.loads
is guarded by `except ValueError`. A bug in load_business_context() must
surface as itself, not as "holds a value that cannot be read"."""

def broken_validator(parsed):
raise ValueError("a bug in the validator, not in the file")

monkeypatch.setattr(context_module, "load_business_context", broken_validator)
path = tmp_path / "business-context.json"
path.write_text("[]", encoding="utf-8")

with pytest.raises(ValueError) as raised:
load_business_context_file(path)

assert not isinstance(raised.value, BusinessContextError), (
"a validator bug was turned into a refusal that blames the user's file"
)
assert "a bug in the validator" in str(raised.value)


def test_a_real_file_round_trips(tmp_path):
path = tmp_path / "business-context.json"
path.write_text(
Expand Down
42 changes: 42 additions & 0 deletions tests/test_policy_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import pytest

import analysis.policy as policy_module
from analysis.policy import (POLICY_SECTIONS, Policy, PolicyError, load_policy,
load_policy_file, _ALLOWED_VALUES, _LEGACY_KEYS,
_SECTION_REQUIRED)
Expand Down Expand Up @@ -242,6 +243,47 @@ def test_a_file_json_cannot_hold_faithfully_is_refused_not_crashed_or_trimmed(
load_policy_file(path)


def test_a_duplicate_key_refusal_keeps_its_own_message_not_a_generic_one(tmp_path):
"""_refuse_duplicate_keys() raises a PolicyError from inside json.loads,
and PolicyError is a ValueError. Without `except PolicyError: raise` the
bare `except ValueError` beside it re-wraps it as "holds a value that
cannot be read: ... appears twice", which a substring match on "appears
twice" still accepts, so the parametrized test above cannot tell. This
one can: the user is told what is actually wrong, in the loader's words."""
path = tmp_path / "policy.json"
path.write_bytes(b'{"policy_compliance": [' + _A_RULE + b'], "policy_compliance": []}')

with pytest.raises(PolicyError) as raised:
load_policy_file(path)

assert "appears twice" in str(raised.value)
assert "cannot be read" not in str(raised.value)


def test_a_bug_in_the_validator_is_not_relabelled_as_a_bad_file(tmp_path, monkeypatch):
"""The broad `except ValueError` used to wrap load_policy() too, so a plain
ValueError raised by a validation bug reached the user as "holds a value
that cannot be read", a refusal that blames their file for our mistake
(Shubham's note on #446). Only json.loads raises a bare ValueError that
really is about the file (an integer past 4,300 digits), so only
json.loads is guarded by it. A bug must stay a bug."""

def broken_validator(parsed):
raise ValueError("a bug in the validator, not in the file")

monkeypatch.setattr(policy_module, "load_policy", broken_validator)
path = tmp_path / "policy.json"
path.write_text('{"policy_compliance": []}', encoding="utf-8")

with pytest.raises(ValueError) as raised:
load_policy_file(path)

assert not isinstance(raised.value, PolicyError), (
"a validator bug was turned into a refusal that blames the user's file"
)
assert "a bug in the validator" in str(raised.value)


# ---------------------------------------------------------------------------
# D2 -- per entry, with an optional top-level default
# ---------------------------------------------------------------------------
Expand Down
Loading