Skip to content
Closed
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
4 changes: 2 additions & 2 deletions python/fast_mlsirm/multilevel/_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def exact_integer(

def membership_weight(value: Any) -> float:
"""Return one finite membership weight in the interval ``(0, 1]``."""
if isinstance(value, bool) or not isinstance(value, (int, float)):
if type(value) not in (int, float):
raise contract_error(
"invalid_membership_weight",
"$.membership_weight",
Expand All @@ -132,7 +132,7 @@ def membership_weight(value: Any) -> float:

def autoregressive_coefficient(value: Any) -> float:
"""Return one finite stationary AR(1) coefficient strictly inside unity."""
if isinstance(value, bool) or not isinstance(value, (int, float)):
if type(value) not in (int, float):
raise contract_error(
"invalid_autoregressive_coefficient",
"$.autoregressive_coefficient",
Expand Down
53 changes: 53 additions & 0 deletions tests/test_multilevel_hostile_numeric_callbacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Hostile numeric callback boundaries for multilevel contracts."""

from __future__ import annotations

import pytest

from fast_mlsirm.multilevel import (
LongitudinalStateKind,
MultilevelContractError,
build_context_membership,
build_longitudinal_state_spec,
)


class ExplosiveFloat(float):
"""Float subclass that exposes whether validation dispatches callbacks."""

def __float__(self) -> float:
"""Raise an attacker-controlled exception instead of coercing."""
raise RuntimeError("sensitive_numeric_callback")


def test_membership_weight_rejects_numeric_subclass_without_callback() -> None:
"""Membership validation fails closed before untrusted numeric coercion."""
value = ExplosiveFloat(1.0)

with pytest.raises(MultilevelContractError) as caught:
build_context_membership(
observation_id="observation_alpha",
context_dimension_id="school_context",
context_id="school_north",
membership_weight=value,
membership_revision_fingerprint="a" * 64,
)

assert caught.value.code == "invalid_membership_weight"
assert caught.value.path == "$.membership_weight"
assert "sensitive_numeric_callback" not in str(caught.value)


def test_autoregressive_coefficient_rejects_numeric_subclass_without_callback() -> None:
"""AR-state validation fails closed before untrusted numeric coercion."""
value = ExplosiveFloat(0.5)

with pytest.raises(MultilevelContractError) as caught:
build_longitudinal_state_spec(
state_kind=LongitudinalStateKind.STATIONARY_AUTOREGRESSIVE,
autoregressive_coefficient=value,
)

assert caught.value.code == "invalid_autoregressive_coefficient"
assert caught.value.path == "$.autoregressive_coefficient"
assert "sensitive_numeric_callback" not in str(caught.value)
Loading