sektron-gate 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ """sektron-gate: Zero-dependency, fail-closed tool policy engine."""
2
+
3
+ from .canonical import CanonicalizationError, canonical_json, sha256_canonical
4
+ from .policy import (
5
+ APPROVAL_SMUGGLE_KEY,
6
+ ArgConstraint,
7
+ Decision,
8
+ DefinitionChangedError,
9
+ GateEngine,
10
+ Policy,
11
+ ReasonCode,
12
+ ToolDriftResult,
13
+ ToolPinGuard,
14
+ definition_digest,
15
+ verify_tools_list,
16
+ )
17
+
18
+ __all__ = [
19
+ "APPROVAL_SMUGGLE_KEY",
20
+ "ArgConstraint",
21
+ "CanonicalizationError",
22
+ "Decision",
23
+ "DefinitionChangedError",
24
+ "GateEngine",
25
+ "Policy",
26
+ "ReasonCode",
27
+ "ToolDriftResult",
28
+ "ToolPinGuard",
29
+ "canonical_json",
30
+ "definition_digest",
31
+ "sha256_canonical",
32
+ "verify_tools_list",
33
+ ]
@@ -0,0 +1,66 @@
1
+ """Deterministic canonical JSON serialization (fail-closed on NaN/Infinity)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ import json
7
+ import math
8
+ from typing import Any
9
+
10
+
11
+ class CanonicalizationError(ValueError):
12
+ """Raised when an input value cannot be canonicalized safely."""
13
+
14
+
15
+ def _validate_str(value: str) -> None:
16
+ # Lone surrogates (U+D800-U+DFFF) cannot be encoded as UTF-8:
17
+ # json.dumps(ensure_ascii=False) emits them raw and the
18
+ # .encode("utf-8") step in callers then raises UnicodeEncodeError
19
+ # outside any CanonicalizationError handler. Reject fail-closed.
20
+ # (A real emoji is a single code point, not a surrogate pair.)
21
+ if any(0xD800 <= ord(ch) <= 0xDFFF for ch in value):
22
+ raise CanonicalizationError("string contains lone surrogate code point(s)")
23
+
24
+
25
+ def _validate_json_value(value: Any, *, depth: int = 0, max_depth: int = 32) -> None:
26
+ if depth > max_depth:
27
+ raise CanonicalizationError(f"maximum nesting depth ({max_depth}) exceeded")
28
+ if value is None or isinstance(value, (bool, int)):
29
+ return
30
+ if isinstance(value, str):
31
+ _validate_str(value)
32
+ return
33
+ if isinstance(value, float):
34
+ if not math.isfinite(value):
35
+ raise CanonicalizationError("non-finite float (NaN/Infinity) rejected fail-closed")
36
+ return
37
+ if isinstance(value, (list, tuple)):
38
+ for item in value:
39
+ _validate_json_value(item, depth=depth + 1, max_depth=max_depth)
40
+ return
41
+ if isinstance(value, dict):
42
+ for k, v in value.items():
43
+ if not isinstance(k, str):
44
+ raise CanonicalizationError(f"object keys must be strings, got {type(k).__name__}")
45
+ _validate_str(k)
46
+ _validate_json_value(v, depth=depth + 1, max_depth=max_depth)
47
+ return
48
+ raise CanonicalizationError(f"unsupported JSON type: {type(value).__name__}")
49
+
50
+
51
+ def canonical_json(value: Any, *, max_depth: int = 32) -> str:
52
+ """Serializes `value` into deterministic compact UTF-8 JSON with sorted keys."""
53
+ _validate_json_value(value, depth=0, max_depth=max_depth)
54
+ return json.dumps(
55
+ value,
56
+ sort_keys=True,
57
+ separators=(",", ":"),
58
+ ensure_ascii=False,
59
+ allow_nan=False,
60
+ )
61
+
62
+
63
+ def sha256_canonical(value: Any, *, max_depth: int = 32) -> str:
64
+ """Returns `sha256:<hex>` of the canonical UTF-8 JSON representation."""
65
+ encoded = canonical_json(value, max_depth=max_depth).encode("utf-8")
66
+ return "sha256:" + hashlib.sha256(encoded).hexdigest()