mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
173 lines
7.6 KiB
Python
173 lines
7.6 KiB
Python
"""Dependency-free validation of the shipped Concept Pack v1 schema."""
|
|
|
|
import json
|
|
import math
|
|
from pathlib import Path
|
|
import re
|
|
|
|
import yaml
|
|
|
|
|
|
class PackError(ValueError):
|
|
"""An untrusted pack failed validation (CLI exit code 4)."""
|
|
|
|
|
|
SCHEMA_PATH = Path(__file__).with_name("schema") / "concept-pack.v1.schema.json"
|
|
|
|
|
|
def _check(value, schema, root, location):
|
|
"""Implement the small JSON Schema vocabulary used by our bundled schema.
|
|
|
|
jsonschema remains a development dependency. Tests compare this validator
|
|
with Draft202012Validator so installed readers need no optional dependency.
|
|
"""
|
|
if "$ref" in schema:
|
|
schema = root["$defs"][schema["$ref"].removeprefix("#/$defs/")]
|
|
if "oneOf" in schema:
|
|
matches = 0
|
|
for alternative in schema["oneOf"]:
|
|
try:
|
|
_check(value, alternative, root, location)
|
|
matches += 1
|
|
except PackError:
|
|
pass
|
|
if matches != 1:
|
|
raise PackError(f"{location}: requires exactly one bundled path or remote reference")
|
|
types = {
|
|
"object": lambda: isinstance(value, dict),
|
|
"array": lambda: isinstance(value, list),
|
|
"string": lambda: isinstance(value, str),
|
|
"integer": lambda: type(value) is int,
|
|
"number": lambda: type(value) is int or type(value) is float and math.isfinite(value),
|
|
"boolean": lambda: type(value) is bool,
|
|
"null": lambda: value is None,
|
|
}
|
|
expected = schema.get("type")
|
|
if expected:
|
|
expected = [expected] if isinstance(expected, str) else expected
|
|
if not any(types[kind]() for kind in expected):
|
|
raise PackError(f"{location}: expected {' or '.join(expected)}")
|
|
if "enum" in schema and value not in schema["enum"]:
|
|
raise PackError(f"{location}: unsupported value {value!r}")
|
|
if isinstance(value, str):
|
|
if len(value) < schema.get("minLength", 0):
|
|
raise PackError(f"{location}: must not be empty")
|
|
if len(value) > schema.get("maxLength", math.inf):
|
|
raise PackError(f"{location}: string exceeds size limit")
|
|
if "pattern" in schema and not re.search(schema["pattern"], value):
|
|
raise PackError(f"{location}: invalid format")
|
|
if type(value) in (int, float):
|
|
if value < schema.get("minimum", -math.inf) or value > schema.get("maximum", math.inf):
|
|
raise PackError(f"{location}: value outside permitted bounds")
|
|
if isinstance(value, dict):
|
|
if not (
|
|
schema.get("minProperties", 0) <= len(value) <= schema.get("maxProperties", math.inf)
|
|
):
|
|
raise PackError(f"{location}: object exceeds property bounds")
|
|
for name in schema.get("required", []):
|
|
if name not in value:
|
|
raise PackError(f"{location}: missing {name}")
|
|
for name, item in value.items():
|
|
if "propertyNames" in schema:
|
|
_check(name, schema["propertyNames"], root, location)
|
|
child = schema.get("properties", {}).get(name)
|
|
if child is None and schema.get("additionalProperties") is False:
|
|
raise PackError(f"{location}: unknown field {name}")
|
|
if child is None and isinstance(schema.get("additionalProperties"), dict):
|
|
child = schema["additionalProperties"]
|
|
if child is not None:
|
|
_check(item, child, root, f"{location}.{name}")
|
|
if isinstance(value, list):
|
|
if len(value) < schema.get("minItems", 0):
|
|
raise PackError(f"{location}: too few entries")
|
|
if len(value) > schema.get("maxItems", math.inf):
|
|
raise PackError(f"{location}: too many entries")
|
|
if schema.get("uniqueItems") and any(item in value[:i] for i, item in enumerate(value)):
|
|
raise PackError(f"{location}: duplicate entries")
|
|
for index, item in enumerate(value):
|
|
if "items" in schema:
|
|
_check(item, schema["items"], root, f"{location}[{index}]")
|
|
|
|
|
|
def validate_manifest(manifest):
|
|
version = manifest.get("schema_version") if isinstance(manifest, dict) else None
|
|
if not isinstance(version, str) or not re.fullmatch(r"1\.[0-9]+", version):
|
|
raise PackError("Unsupported schema_version; use a v1 pack or upgrade OBLITERATUS")
|
|
schema = json.loads(SCHEMA_PATH.read_text(encoding="utf-8"))
|
|
_check(manifest, schema, schema, "manifest")
|
|
if "entry_point" in manifest["recipe"] and int(version.split(".")[1]) < 1:
|
|
raise PackError("recipe.entry_point requires schema_version 1.1+")
|
|
if "entry_point" in manifest["recipe"]:
|
|
if ("special_token_policy" in manifest["recipe"]["entry_point"]
|
|
and int(version.split(".")[1]) < 2):
|
|
raise PackError("entry_point.special_token_policy requires schema_version 1.2+")
|
|
if manifest["recipe"].get("readout", {"position": "final_token", "suffix": ""}) != {
|
|
"position": "final_token", "suffix": "",
|
|
}:
|
|
raise PackError("entry_point replaces legacy readout; omit recipe.readout")
|
|
from obliteratus.concepts.entry_points import EntryPoint
|
|
try:
|
|
EntryPoint(**manifest["recipe"]["entry_point"])
|
|
except (TypeError, ValueError) as exc:
|
|
raise PackError(f"manifest.recipe.entry_point: {exc}") from exc
|
|
|
|
|
|
def load_manifest(data):
|
|
"""Reject ambiguous keys, custom tags, aliases and excessive YAML nesting."""
|
|
try:
|
|
if len(data) > 256 * 1024:
|
|
raise PackError("manifest.yaml exceeds the size limit")
|
|
for token in yaml.scan(data):
|
|
if isinstance(token, (yaml.tokens.AliasToken, yaml.tokens.AnchorToken,
|
|
yaml.tokens.TagToken)):
|
|
raise PackError("manifest.yaml: YAML aliases, anchors and tags are forbidden")
|
|
|
|
def inspect(node, depth=0):
|
|
if depth > 32:
|
|
raise PackError("manifest.yaml: excessive nesting")
|
|
if isinstance(node, yaml.nodes.MappingNode):
|
|
names = set()
|
|
for key, value in node.value:
|
|
if (not isinstance(key, yaml.nodes.ScalarNode)
|
|
or key.tag != "tag:yaml.org,2002:str"):
|
|
raise PackError("manifest.yaml: keys must be strings")
|
|
if key.value in names:
|
|
raise PackError(f"manifest.yaml: duplicate key {key.value}")
|
|
names.add(key.value)
|
|
inspect(value, depth + 1)
|
|
elif isinstance(node, yaml.nodes.SequenceNode):
|
|
for value in node.value:
|
|
inspect(value, depth + 1)
|
|
|
|
inspect(yaml.compose(data, Loader=yaml.SafeLoader))
|
|
manifest = yaml.safe_load(data)
|
|
validate_manifest(manifest)
|
|
return manifest
|
|
except (yaml.YAMLError, UnicodeError, RecursionError) as exc:
|
|
raise PackError(f"Invalid manifest.yaml: {exc}") from exc
|
|
|
|
|
|
def json_data(data, path):
|
|
def pairs(items):
|
|
result = {}
|
|
for key, value in items:
|
|
if key in result:
|
|
raise PackError(f"{path}: duplicate JSON key {key}")
|
|
result[key] = value
|
|
return result
|
|
|
|
def constant(value):
|
|
raise PackError(f"{path}: non-finite JSON number {value}")
|
|
|
|
def finite_float(value):
|
|
number = float(value)
|
|
if not math.isfinite(number):
|
|
constant(value)
|
|
return number
|
|
|
|
try:
|
|
return json.loads(data, object_pairs_hook=pairs, parse_constant=constant,
|
|
parse_float=finite_float)
|
|
except (ValueError, UnicodeError, RecursionError) as exc:
|
|
raise PackError(f"{path}: invalid JSON: {exc}") from exc
|