Files
DECNET/decnet/ttp/impl/_state.py
anti d80e6aa6d1 fix(security): close MEDIUM ASVS findings — JWT pinning, SSE tickets, SSRF, mTLS pin, rate limits + correctness bugs
Auth (V2.1.1/V3.1.2, V2.1.3, V3.1.1):
- Pin JWT iss/aud/typ at mint and require+verify them at decode; revocation
  (jti denylist + tokens_valid_from) still enforced.
- Change-password now requires min_length=12.
- SSE auth moves off JWT-in-URL to a single-use 60s opaque ticket
  (POST /auth/sse-ticket); raw JWT in query no longer authenticates a stream.
  Removed dead fail-open get_stream_user helper.

Egress (V5.1.1, V9.1.1/V14.1.3):
- Webhook delivery + CRUD reject SSRF destinations (private/loopback/link-local/
  metadata, IPv4-mapped, multi-A-record) via resolved-IP validation, pin to the
  vetted IP, and never auto-follow redirects. Opt-out via DECNET_WEBHOOK_ALLOW_PRIVATE.
- UpdaterClient pins the worker leaf cert SHA-256 against the stored per-host
  fingerprint (fail closed on missing/mismatch); DECNET_VERIFY_HOSTNAME now
  defaults True.

Hardening (V13.1.3, V4.1.4, V13.1.2):
- Rate-limit change-password (5/min), enroll-bundle (10/min), webhook-create
  (20/min), host-delete (20/min) via the existing slowapi limiter.
- Correct false 'global auth middleware' comment; document enroll-bundle proxy
  trust.

Correctness (BUG-7..11):
- BUG-7 unbound bus in finally; BUG-8 apply_ceiling clamps to min(base,ceiling);
  BUG-9 commit before emit; BUG-10 multi-actor rearm for sub-threshold identities;
  BUG-11 normalize naive timestamps to UTC.

Already-closed (no change): V14.1.1, V2.1.2/V3.1.3, V5.1.2. Tests added for
every fix; unanimous adversarial review.
2026-06-10 12:32:15 -04:00

57 lines
2.0 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""Shared state-modulation helpers for rule consumers.
Both :class:`~decnet.ttp.impl.rule_engine.RuleEngine` and the per-source
lifters (E.3.9 onward) read :class:`~decnet.ttp.store.base.RuleState`
the same way: skip on ``disabled``, defense-in-depth re-check
``expires_at``, clamp confidence on ``clipped``. Single source of truth
so a future change to the state contract lands in one place.
"""
from __future__ import annotations
from datetime import datetime, timezone
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from decnet.ttp.store.base import RuleState
def is_active(state: "RuleState") -> bool:
"""Return ``True`` iff a rule with this state is allowed to fire.
``disabled`` rules never fire. ``clipped`` rules still fire — the
clip caps emitted confidence, doesn't suppress the emit. Expired
states act as ``disabled`` even though the store auto-reverts; the
re-check here is defense-in-depth against a racing read between
expiry and the store's revert pass.
"""
if state.state == "disabled":
return False
if state.expires_at is not None:
expires = state.expires_at
if expires.tzinfo is None:
expires = expires.replace(tzinfo=timezone.utc)
if expires < datetime.now(tz=timezone.utc):
return False
return True
def apply_ceiling(base: float, state: "RuleState") -> float:
"""Apply the operator's confidence ceiling, downward only.
A ``clipped`` state with ``confidence_max < 1.0`` clamps the emitted
confidence to ``min(base, ceiling)``. Any other state is a
no-op. The clamp is downward by construction — operator clips can
never raise a rule's confidence above its YAML-declared base, per
TTP_TAGGING.md §"Confidence model".
"""
if state.state != "clipped":
return base
ceiling = state.confidence_max
if ceiling is None or ceiling >= 1.0:
return base
return min(base, ceiling)
__all__ = ["is_active", "apply_ceiling"]