Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(revisions,router): a LAYA_SHA256_DIGESTS the JSON reader will not walk is refused, not raised

Aashish8b9511b8bc68
diff --git a/laya/revisions.py b/laya/revisions.py
index cb306d36..48be2ca5 100644
--- a/laya/revisions.py
+++ b/laya/revisions.py
@@ -114,8 +114,9 @@ def verify_digests(
"""Verify SHA-256 digests of files under `model_dir` against {relpath: hexdigest}.
Raises FileNotFoundError when a listed file is absent and ValueError on a digest
- mismatch or an unsafe (absolute or escaping) relative path. Verification runs before
- any weight is parsed or executed, so a tampered artifact never reaches the runtime.
+ mismatch, an unsafe (absolute or escaping) relative path, or a `LAYA_SHA256_DIGESTS`
+ the JSON reader cannot walk. Verification runs before any weight is parsed or
+ executed, so a tampered artifact never reaches the runtime.
"""
if expected is None:
raw = os.environ.get("LAYA_SHA256_DIGESTS", "").strip()
@@ -123,7 +124,13 @@ def verify_digests(
return
try:
expected = json.loads(raw)
- except json.JSONDecodeError as exc:
+ except (json.JSONDecodeError, RecursionError) as exc:
+ # `RecursionError` is a third way this variable is not a document: `json` reports a
+ # nesting depth it will not walk as a stack overflow, which is neither a `ValueError`
+ # decode failure nor anything the caller catches. `laya/_eval_policy.py`,
+ # `laya/evidence.py` and `laya/cli.py` read an unreadable JSON the same way, and here
+ # the leak escaped `load()` and `Router.load()` -- which both document the ValueError
+ # and mean it as "this pinning configuration is wrong" -- as an interpreter traceback.
raise ValueError("LAYA_SHA256_DIGESTS must be a JSON object of artifact->sha256") from exc
if not isinstance(expected, dict):
raise ValueError("expected_sha256 must be a mapping of artifact paths to digests")
diff --git a/laya/router.py b/laya/router.py
index f538d589..6e027996 100644
--- a/laya/router.py
+++ b/laya/router.py
@@ -441,7 +441,12 @@ def _digests_from_env(models: Dict[str, Any], resolve=normalise_name) -> Dict[st
return {}
try:
data = json.loads(raw)
- except ValueError:
+ except (ValueError, RecursionError):
+ # `RecursionError` is a `RuntimeError`, so `except ValueError` did not cover the one shape
+ # of unparseable this variable can have that is not a decode error: `json` answers a
+ # nesting depth it will not walk with a stack overflow. It names nothing, so it names no
+ # checkpoint here either -- `laya.revisions` reports it, in the words this docstring
+ # already promises are the only ones this layer uses.
return {}
if not isinstance(data, dict) or not data:
return {}
diff --git a/tests/test_revision_pinning.py b/tests/test_revision_pinning.py
index ed906f4d..5155fb53 100644
--- a/tests/test_revision_pinning.py
+++ b/tests/test_revision_pinning.py
@@ -173,6 +173,20 @@ class VerifyDigestsTests(unittest.TestCase):
with patch.dict(os.environ, {"LAYA_SHA256_DIGESTS": json.dumps({"weights.bin": self.digest})}):
verify_digests(self.dir)
+ def test_an_unreadable_environment_map_is_a_value_error(self):
+ # `json` refuses a document two ways: a `ValueError` for text that is not JSON, and -- for a
+ # nesting depth past the scanner's own stack -- a `RecursionError`, which is a `RuntimeError`
+ # and so was never in the `except` here. The second escaped `load()` as an interpreter
+ # traceback instead of the refusal this function's docstring names, which is what
+ # `laya/_eval_policy.py`, `laya/evidence.py` and `laya/cli.py` already refuse to let a
+ # caller's JSON do.
+ for bad in ("{not json", "[" * 100000 + "]" * 100000):
+ with self.subTest(value=bad[:20] + ("..." if len(bad) > 20 else "")):
+ with patch.dict(os.environ, {"LAYA_SHA256_DIGESTS": bad}):
+ with self.assertRaises(ValueError) as cm:
+ verify_digests(self.dir)
+ self.assertIn("LAYA_SHA256_DIGESTS", str(cm.exception))
+
def test_external_onnx_digest_is_supported(self):
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "model.onnx")
@@ -625,7 +639,12 @@ class RouterEnvDigestTests(unittest.TestCase):
def test_unparseable_environment_is_not_this_layers_error(self):
# `laya.revisions.verify_digests` owns the message a malformed value gets; a Router
# that repeated it would be a second copy of a rule it does not enforce.
- for value in ("", " ", "{not json", "[]", '"digests"', "{}"):
+ #
+ # The last value is the shape `except ValueError` here did not cover: `json` answers a
+ # nesting depth past its own stack with a `RecursionError`, a `RuntimeError`, so building a
+ # Router on it crashed instead of naming nothing -- the one unparseable input this layer
+ # promises to hand on rather than report.
+ for value in ("", " ", "{not json", "[]", '"digests"', "{}", "[" * 100000 + "]" * 100000):
with self._env(value):
self.assertEqual(Router().sha256_digests, {}, value)