10
"""
11
12
import decimal
13
+import functools
14
+import importlib
15
import re
16
import types
17
import typing as t
437
"""
438
439
declared: t.FrozenSet[str]
438
- patterns: t.Tuple[t.Tuple[t.Pattern[str], _DynamicKeyValidator], ...]
440
+ # Key matchers come from `_linear_pattern_matcher`, never Python `re`: keys
441
+ # are attacker-influenced and the schema's patterns are untrusted.
442
+ patterns: t.Tuple[t.Tuple[t.Callable[[str], bool], _DynamicKeyValidator], ...]
443
additional: t.Optional[_DynamicKeyValidator]
444
rejects_unmatched: bool
445
817
)
818
819
816
-def _compile_pattern_property(pattern: str) -> t.Pattern[str]:
820
+def _compile_pattern_property(pattern: str) -> t.Callable[[str], bool]:
821
+ """Compile a `patternProperties` key on the linear-time engine only.
822
+
823
+ A key pattern decides which subschema validates a value, so it cannot be
824
+ dropped the way an unenforceable `pattern` constraint is. Patterns that
825
+ engine rejects (look-around, backreferences) fail conversion instead of
826
+ running on Python's backtracking `re` against attacker-chosen keys.
827
+ """
828
+ matcher = _linear_pattern_matcher(pattern)
829
+ if matcher is not None:
830
+ return matcher
831
try:
818
- return re.compile(pattern)
819
- except re.error as exc:
832
+ re.compile(pattern)
833
+ except (re.error, OverflowError, RecursionError, TypeError) as exc:
834
raise ValueError(
835
f"Invalid patternProperties regular expression: {pattern!r}"
836
) from exc
837
+ raise ValueError(_unsupported_pattern_property_message(pattern))
838
+
839
+
840
+def _unsupported_pattern_property_message(pattern: t.Any) -> str:
841
+ return (
842
+ f"Unsupported patternProperties regular expression: {pattern!r}; "
843
+ "only patterns the linear-time regex engine accepts are supported "
844
+ "(look-around and backreferences can backtrack catastrophically)"
845
+ )
846
847
848
def _validate_object_policy_schema(
850
root_schema: t.Dict[str, t.Any],
851
) -> None:
852
"""Validate a dynamic object policy without materializing defaults."""
830
- validator_type = jsonschema_validators.validator_for(
831
- root_schema,
832
- default=jsonschema_validators.Draft7Validator,
853
+ validator_type = _with_safe_regex_keywords(
854
+ jsonschema_validators.validator_for(
855
+ root_schema,
856
+ default=jsonschema_validators.Draft7Validator,
857
+ )
858
)
859
root_validator = validator_type(root_schema)
860
878
document_root = root_schema if root_schema is not None else schema
879
declared = frozenset(schema.get("properties") or {})
880
additional = schema.get("additionalProperties", _MISSING)
856
- validator_type = jsonschema_validators.validator_for(
857
- document_root,
858
- default=jsonschema_validators.Draft7Validator,
881
+ validator_type = _with_safe_regex_keywords(
882
+ jsonschema_validators.validator_for(
883
+ document_root,
884
+ default=jsonschema_validators.Draft7Validator,
885
+ )
886
)
887
root_validator = validator_type(document_root)
888
915
def _iter_reachable_schemas(
916
schema: t.Any,
917
root_schema: t.Dict[str, t.Any],
891
- visited: t.Optional[t.Set[int]] = None,
918
+ visited: t.Optional[t.Set[t.Tuple[int, t.Any]]] = None,
919
+ validator_class: t.Any = None,
920
) -> t.Iterator[t.Dict[str, t.Any]]:
893
- """Yield schema nodes reached through Draft 7 schema-valued keywords."""
921
+ """Yield schema nodes and local targets under their effective dialect."""
922
if visited is None:
923
visited = set()
924
+ if validator_class is None:
925
+ validator_class = jsonschema_validators.validator_for(
926
+ root_schema, default=jsonschema_validators.Draft7Validator
927
+ )
928
if isinstance(schema, list):
929
for item in schema:
898
- yield from _iter_reachable_schemas(item, root_schema, visited)
930
+ yield from _iter_reachable_schemas(
931
+ item, root_schema, visited, validator_class
932
+ )
933
return
900
- if not isinstance(schema, dict) or id(schema) in visited:
934
+ if not isinstance(schema, dict):
935
return
902
- visited.add(id(schema))
936
+ if "$schema" in schema:
937
+ validator_class = jsonschema_validators.validator_for(
938
+ schema, default=validator_class
939
+ )
940
+ visit_key = (id(schema), validator_class)
941
+ if visit_key in visited:
942
+ return
943
+ visited.add(visit_key)
944
yield schema
945
946
reference = schema.get("$ref")
953
# validated.
954
resolved = None
955
if isinstance(resolved, (dict, list)):
915
- yield from _iter_reachable_schemas(resolved, root_schema, visited)
956
+ yield from _iter_reachable_schemas(
957
+ resolved, root_schema, visited, validator_class
958
+ )
959
917
- for keyword in ("properties", "patternProperties"):
960
+ for keyword in ("properties", "patternProperties", "dependentSchemas"):
961
+ if keyword == "dependentSchemas" and keyword not in validator_class.VALIDATORS:
962
+ continue
963
values = schema.get(keyword)
964
if isinstance(values, dict):
965
for child in values.values():
921
- yield from _iter_reachable_schemas(child, root_schema, visited)
966
+ yield from _iter_reachable_schemas(
967
+ child, root_schema, visited, validator_class
968
+ )
969
923
- for keyword in _SCHEMA_VALUED_KEYWORDS:
970
+ value_keywords = _SCHEMA_VALUED_KEYWORDS | (
971
+ {"unevaluatedItems", "unevaluatedProperties"}
972
+ & validator_class.VALIDATORS.keys()
973
+ )
974
+ for keyword in value_keywords:
975
child = schema.get(keyword)
976
if isinstance(child, (dict, list)):
926
- yield from _iter_reachable_schemas(child, root_schema, visited)
977
+ yield from _iter_reachable_schemas(
978
+ child, root_schema, visited, validator_class
979
+ )
980
928
- for keyword in _SCHEMA_LIST_KEYWORDS:
981
+ list_keywords = _SCHEMA_LIST_KEYWORDS | (
982
+ {"prefixItems"} & validator_class.VALIDATORS.keys()
983
+ )
984
+ for keyword in list_keywords:
985
children = schema.get(keyword)
986
if isinstance(children, list):
931
- yield from _iter_reachable_schemas(children, root_schema, visited)
987
+ yield from _iter_reachable_schemas(
988
+ children, root_schema, visited, validator_class
989
+ )
990
991
items = schema.get("items")
992
if isinstance(items, (dict, list)):
935
- yield from _iter_reachable_schemas(items, root_schema, visited)
993
+ yield from _iter_reachable_schemas(items, root_schema, visited, validator_class)
994
995
dependencies = schema.get("dependencies")
996
if isinstance(dependencies, dict):
997
for child in dependencies.values():
998
if isinstance(child, (dict, bool)):
941
- yield from _iter_reachable_schemas(child, root_schema, visited)
999
+ yield from _iter_reachable_schemas(
1000
+ child, root_schema, visited, validator_class
1001
+ )
1002
1003
1004
def _has_dynamic_object_policy(schema: t.Dict[str, t.Any]) -> bool:
1100
unrecognized = []
1101
for key, item in value.items():
1102
matched = False
1043
- for regex, validator in policy.patterns:
1044
- if regex.search(key):
1103
+ for matches_key, validator in policy.patterns:
1104
+ if matches_key(key):
1105
matched = True
1106
validator.validate(item)
1107
1134
1135
for key, value in list(extra.items()):
1136
matched = False
1077
- for regex, validator in policy.patterns:
1078
- if regex.search(key):
1137
+ for matches_key, validator in policy.patterns:
1138
+ if matches_key(key):
1139
matched = True
1140
extra[key] = validator.materialize(extra[key])
1141
1407
return validate
1408
1409
1350
-def _compile_pattern(pattern: str) -> t.Optional[t.Pattern[str]]:
1351
- try:
1352
- return re.compile(pattern)
1353
- except re.error:
1354
- logger.warning(
1355
- "Ignoring JSON Schema pattern %r: not a valid Python regular expression",
1356
- pattern,
1357
- )
1358
- return None
1359
-
1360
-
1410
def _with_string_constraints(annotation: t.Any, schema: t.Dict[str, t.Any]) -> t.Any:
1411
field_constraints: t.Dict[str, t.Any] = {}
1412
if "minLength" in schema:
1421
1422
1423
def _with_pattern_constraint(annotation: t.Any, pattern: str) -> t.Any:
1375
- """Prefer pydantic's native pattern; fall back to Python `re` when the Rust
1376
- regex crate rejects an ECMA construct such as look-around (legal in
1377
- Draft 7, whose regex dialect is ECMA-262)."""
1424
+ """Prefer pydantic's native pattern; otherwise use the guarded matcher,
1425
+ which covers ECMA constructs such as look-around (legal in Draft 7, whose
1426
+ regex dialect is ECMA-262) only when they cannot backtrack catastrophically.
1427
+ """
1428
candidate = t.Annotated[annotation, Field(pattern=pattern)]
1429
try:
1430
TypeAdapter(candidate)
1432
except Exception: # noqa: BLE001 - pydantic raises SchemaError subclasses
1433
pass
1434
1385
- compiled = _compile_pattern(pattern)
1386
- if compiled is None:
1435
+ matcher = _string_pattern_matcher(pattern)
1436
+ if matcher is None:
1437
return annotation
1438
1439
def validate(value: t.Any) -> t.Any:
1390
- if isinstance(value, str) and compiled.search(value) is None:
1440
+ if isinstance(value, str) and not matcher(value):
1441
raise ValueError(f"string does not match pattern {pattern!r}")
1442
return value
1443
1503
if not _SCALAR_CONSTRAINT_KEYWORDS.intersection(schema):
1504
return None
1505
1456
- compiled_pattern = (
1457
- _compile_pattern(schema["pattern"]) if "pattern" in schema else None
1506
+ pattern_matcher = (
1507
+ _string_pattern_matcher(schema["pattern"]) if "pattern" in schema else None
1508
)
1509
1510
def validate(value: t.Any) -> t.Any:
1517
raise ValueError(
1518
f"string must contain at most {schema['maxLength']} characters"
1519
)
1470
- if compiled_pattern is not None and compiled_pattern.search(value) is None:
1520
+ if pattern_matcher is not None and not pattern_matcher(value):
1521
raise ValueError(f"string does not match pattern {schema['pattern']!r}")
1522
if _is_numeric(value):
1523
_check_numeric_keywords(schema, value)
1855
)
1856
1857
1808
-_PATTERN_MATCHER_CACHE: t.Dict[str, t.Optional[t.Callable[[str], bool]]] = {}
1858
+# Schema-supplied regular expressions.
1859
+#
1860
+# Tool schemas are third-party input, and so are the strings matched against
1861
+# their patterns: LLM tool arguments, including object keys. Python's `re`
1862
+# backtracks, so a schema pattern such as `^(a+)+$` can freeze a worker on a
1863
+# short argument. Every schema regex is evaluated through one of two helpers:
1864
+#
1865
+# - `_linear_pattern_matcher`: pydantic-core's Rust regex engine, which runs in
1866
+# time linear in the input. `patternProperties` keys use only this, because a
1867
+# key pattern decides routing and cannot be dropped.
1868
+# - `_string_pattern_matcher`: the same engine, plus a guarded Python `re`
1869
+# fallback for the ECMA constructs the Rust engine rejects (look-around,
1870
+# backreferences). The fallback runs only when `_fallback_pattern_is_safe`
1871
+# proves the pattern cannot backtrack catastrophically, and only on input of
1872
+# at most `_BACKTRACKING_FALLBACK_MAX_INPUT` characters. Anything else is not
1873
+# enforced. Widening is safe: a hostile schema could omit the pattern.
1874
+#
1875
+# The static check matches the TypeScript SDK's fallback check.
1876
+
1877
+_PATTERN_CACHE_SIZE = 1024
1878
+
1879
+# Longer input is treated as matching on the `re` fallback. This bounds the
1880
+# quadratic scan over start positions that unanchored search performs.
1881
+_BACKTRACKING_FALLBACK_MAX_INPUT = 1000
1882
+
1883
+# A quantifier is "large" when it is unbounded or spans more than this many
1884
+# repetitions (`m - n` in `{n,m}`).
1885
+_LARGE_QUANTIFIER_SPAN = 16
1886
+
1887
+# Polynomial degree budget: one implicit factor for unanchored search, plus
1888
+# the large quantifiers outside look-around, plus the most in one look-around.
1889
+_MAX_FALLBACK_DEGREE = 2
1890
+
1891
+# Budget for the product of small quantifier spans and alternation widths.
1892
+_MAX_FALLBACK_CHOICES = 64
1893
+
1894
+# Absolute explicit repeat bounds also limit zero-width assertion work.
1895
+_MAX_FALLBACK_REPEAT_COUNT = 64
1896
+
1897
+
1898
+@functools.lru_cache(maxsize=_PATTERN_CACHE_SIZE)
1899
+def _linear_pattern_matcher(pattern: str) -> t.Optional[t.Callable[[str], bool]]:
1900
+ """Unanchored (search) matcher on pydantic's linear-time Rust regex, or
1901
+ None when that engine cannot compile `pattern`."""
1902
+ try:
1903
+ adapter = TypeAdapter(t.Annotated[str, Field(pattern=pattern)])
1904
+ except Exception: # noqa: BLE001 - pydantic raises SchemaError subclasses
1905
+ return None
1906
1907
+ def match_with_pydantic(value: str) -> bool:
1908
+ try:
1909
+ adapter.validate_python(value, strict=True)
1910
+ except ValidationError:
1911
+ return False
1912
+ return True
1913
1811
-def _pattern_matcher(pattern: str) -> t.Optional[t.Callable[[str], bool]]:
1812
- """Match `pattern` with pydantic's linear-time Rust regex when it can
1813
- compile it, falling back to Python `re` only for ECMA-only constructs."""
1814
- if pattern in _PATTERN_MATCHER_CACHE:
1815
- return _PATTERN_MATCHER_CACHE[pattern]
1816
- matcher: t.Optional[t.Callable[[str], bool]] = None
1817
- if _pattern_supported_by_pydantic(pattern):
1818
- adapter = TypeAdapter(t.Annotated[str, Field(pattern=pattern)])
1914
+ return match_with_pydantic
1915
1820
- def match_with_pydantic(value: str) -> bool:
1821
- try:
1822
- adapter.validate_python(value, strict=True)
1823
- except ValidationError:
1824
- return False
1825
- return True
1916
1827
- matcher = match_with_pydantic
1828
- else:
1829
- compiled = _compile_pattern(pattern)
1830
- if compiled is not None:
1917
+def _pattern_supported_by_pydantic(pattern: str) -> bool:
1918
+ return _linear_pattern_matcher(pattern) is not None
1919
+
1920
+
1921
+def _load_regex_parser() -> t.Tuple[t.Any, t.Any]:
1922
+ try: # Python 3.11+
1923
+ return (
1924
+ importlib.import_module("re._parser"),
1925
+ importlib.import_module("re._constants"),
1926
+ )
1927
+ except ImportError: # Python 3.10; deprecated aliases from 3.11 on
1928
+ return (
1929
+ importlib.import_module("sre_parse"),
1930
+ importlib.import_module("sre_constants"),
1931
+ )
1932
+
1933
+
1934
+_REGEX_PARSER, _REGEX_CONSTANTS = _load_regex_parser()
1935
+_REGEX_REPEATS = frozenset(
1936
+ op
1937
+ for op in (
1938
+ _REGEX_CONSTANTS.MAX_REPEAT,
1939
+ _REGEX_CONSTANTS.MIN_REPEAT,
1940
+ getattr(_REGEX_CONSTANTS, "POSSESSIVE_REPEAT", None),
1941
+ )
1942
+ if op is not None
1943
+)
1944
+_REGEX_LOOKAROUNDS = frozenset({_REGEX_CONSTANTS.ASSERT, _REGEX_CONSTANTS.ASSERT_NOT})
1945
+_REGEX_ATOMS = frozenset(
1946
+ {
1947
+ _REGEX_CONSTANTS.ANY,
1948
+ _REGEX_CONSTANTS.AT,
1949
+ _REGEX_CONSTANTS.CATEGORY,
1950
+ _REGEX_CONSTANTS.IN,
1951
+ _REGEX_CONSTANTS.LITERAL,
1952
+ _REGEX_CONSTANTS.NOT_LITERAL,
1953
+ }
1954
+)
1955
+_REGEX_ATOMIC_GROUP = getattr(_REGEX_CONSTANTS, "ATOMIC_GROUP", None)
1956
+
1957
+
1958
+class _BacktrackingRisk(Exception):
1959
+ """The pattern may backtrack catastrophically under Python `re`."""
1960
+
1961
+
1962
+class _FallbackPatternCheck:
1963
+ """Conservative static check over a `re` parse tree.
1964
+
1965
+ False positives only leave a pattern unenforced, so every construct that
1966
+ is not clearly bounded counts as a risk.
1967
+ """
1968
+
1969
+ def __init__(self) -> None:
1970
+ self.max_large_in_lookaround = 0
1971
+ self.choices = 1
1972
+
1973
+ def multiply_choices(self, factor: int) -> None:
1974
+ self.choices *= factor
1975
+ if self.choices > _MAX_FALLBACK_CHOICES:
1976
+ raise _BacktrackingRisk("too many backtracking choices")
1977
+
1978
+ def visit(
1979
+ self,
1980
+ subpattern: t.Any,
1981
+ *,
1982
+ in_repeat: bool,
1983
+ in_lookaround: bool,
1984
+ ) -> int:
1985
+ """Return the large quantifiers in `subpattern` outside look-around."""
1986
+ large = 0
1987
+ for op, av in subpattern.data:
1988
+ if op in _REGEX_REPEATS:
1989
+ if in_repeat:
1990
+ raise _BacktrackingRisk("quantified group contains a quantifier")
1991
+ low, high, body = av
1992
+ if low > _MAX_FALLBACK_REPEAT_COUNT or (
1993
+ high != _REGEX_CONSTANTS.MAXREPEAT
1994
+ and high > _MAX_FALLBACK_REPEAT_COUNT
1995
+ ):
1996
+ raise _BacktrackingRisk("repeat count exceeds the work budget")
1997
+ if high == _REGEX_CONSTANTS.MAXREPEAT or (
1998
+ high - low > _LARGE_QUANTIFIER_SPAN
1999
+ ):
2000
+ large += 1
2001
+ elif high > low:
2002
+ self.multiply_choices(high - low + 1)
2003
+ large += self.visit(body, in_repeat=True, in_lookaround=in_lookaround)
2004
+ elif op == _REGEX_CONSTANTS.BRANCH:
2005
+ if in_repeat:
2006
+ raise _BacktrackingRisk("quantified group contains alternation")
2007
+ _, alternatives = av
2008
+ self.multiply_choices(len(alternatives))
2009
+ for alternative in alternatives:
2010
+ large += self.visit(
2011
+ alternative,
2012
+ in_repeat=in_repeat,
2013
+ in_lookaround=in_lookaround,
2014
+ )
2015
+ elif op in _REGEX_LOOKAROUNDS:
2016
+ if in_lookaround:
2017
+ raise _BacktrackingRisk("nested look-around")
2018
+ _, body = av
2019
+ self.max_large_in_lookaround = max(
2020
+ self.max_large_in_lookaround,
2021
+ self.visit(body, in_repeat=in_repeat, in_lookaround=True),
2022
+ )
2023
+ elif op == _REGEX_CONSTANTS.SUBPATTERN:
2024
+ large += self.visit(
2025
+ av[-1], in_repeat=in_repeat, in_lookaround=in_lookaround
2026
+ )
2027
+ elif _REGEX_ATOMIC_GROUP is not None and op == _REGEX_ATOMIC_GROUP:
2028
+ large += self.visit(
2029
+ av, in_repeat=in_repeat, in_lookaround=in_lookaround
2030
+ )
2031
+ elif op not in _REGEX_ATOMS:
2032
+ # Backreferences (GROUPREF, GROUPREF_EXISTS) and anything this
2033
+ # check does not model.
2034
+ raise _BacktrackingRisk(f"unsupported regex construct {op}")
2035
+ return large
2036
+
2037
1832
- def match_with_re(value: str) -> bool:
1833
- return compiled.search(value) is not None
2038
+def _fallback_pattern_is_safe(pattern: str) -> bool:
2039
+ """Whether Python `re` cannot backtrack catastrophically on `pattern`.
2040
+
2041
+ Rejects backreferences, quantified groups (look-around included) that
2042
+ contain a quantifier or alternation, excessive explicit repeat bounds,
2043
+ nested look-around, more than
2044
+ `_MAX_FALLBACK_DEGREE` nested polynomial factors, and more than
2045
+ `_MAX_FALLBACK_CHOICES` combined small-quantifier and alternation choices.
2046
+
2047
+ :raises re.error: `pattern` is not a valid Python regular expression.
2048
+ """
2049
+ parsed = _REGEX_PARSER.parse(pattern)
2050
+ check = _FallbackPatternCheck()
2051
+ try:
2052
+ large_outside = check.visit(parsed, in_repeat=False, in_lookaround=False)
2053
+ except _BacktrackingRisk:
2054
+ return False
2055
+ anchored = (
2056
+ bool(parsed.data)
2057
+ and parsed.data[0]
2058
+ == (
2059
+ _REGEX_CONSTANTS.AT,
2060
+ _REGEX_CONSTANTS.AT_BEGINNING,
2061
+ )
2062
+ and not parsed.state.flags & re.MULTILINE
2063
+ )
2064
+ degree = (0 if anchored else 1) + large_outside + check.max_large_in_lookaround
2065
+ return degree <= _MAX_FALLBACK_DEGREE
2066
1835
- matcher = match_with_re
1836
- _PATTERN_MATCHER_CACHE[pattern] = matcher
1837
- return matcher
2067
+
2068
+@functools.lru_cache(maxsize=_PATTERN_CACHE_SIZE)
2069
+def _string_pattern_matcher(pattern: str) -> t.Optional[t.Callable[[str], bool]]:
2070
+ """Search-semantics matcher for a `pattern` string constraint, or None
2071
+ (with a warning) when it cannot be enforced in bounded time."""
2072
+ linear = _linear_pattern_matcher(pattern)
2073
+ if linear is not None:
2074
+ return linear
2075
+
2076
+ try:
2077
+ safe = _fallback_pattern_is_safe(pattern)
2078
+ compiled = re.compile(pattern)
2079
+ except (re.error, OverflowError, RecursionError):
2080
+ logger.warning(
2081
+ "Ignoring JSON Schema pattern %r: not a valid Python regular expression",
2082
+ pattern,
2083
+ )
2084
+ return None
2085
+ if not safe:
2086
+ logger.warning(
2087
+ "Ignoring JSON Schema pattern %r: the linear-time regex engine does "
2088
+ "not support it and Python's backtracking engine could stall on it",
2089
+ pattern,
2090
+ )
2091
+ return None
2092
+
2093
+ def match_with_re(value: str) -> bool:
2094
+ if len(value) > _BACKTRACKING_FALLBACK_MAX_INPUT:
2095
+ return True
2096
+ return compiled.search(value) is not None
2097
+
2098
+ return match_with_re
2099
2100
2101
def _exact_validation_pattern(
2106
) -> t.Iterator[t.Any]:
2107
if not isinstance(instance, str) or not isinstance(pattern, str):
2108
return
1848
- matcher = _pattern_matcher(pattern)
2109
+ matcher = _string_pattern_matcher(pattern)
2110
if matcher is not None and not matcher(instance):
2111
yield jsonschema_exceptions.ValidationError(
2112
f"{instance!r} does not match {pattern!r}"
2113
)
2114
2115
2116
+def _safe_pattern_properties(
2117
+ validator: t.Any,
2118
+ pattern_properties: t.Any,
2119
+ instance: t.Any,
2120
+ _schema: t.Any,
2121
+) -> t.Iterator[t.Any]:
2122
+ """jsonschema's `patternProperties` with keys matched in linear time."""
2123
+ if not validator.is_type(instance, "object"):
2124
+ return
2125
+
2126
+ for pattern, subschema in pattern_properties.items():
2127
+ matches_key = _linear_pattern_matcher(pattern)
2128
+ if matches_key is None:
2129
+ # Conversion rejects these first; fail closed if one slips past.
2130
+ yield jsonschema_exceptions.ValidationError(
2131
+ _unsupported_pattern_property_message(pattern)
2132
+ )
2133
+ continue
2134
+ for key, value in instance.items():
2135
+ if matches_key(key):
2136
+ yield from validator.descend(
2137
+ value,
2138
+ subschema,
2139
+ path=key,
2140
+ schema_path=pattern,
2141
+ )
2142
+
2143
+
2144
+def _extras_msg(extras: t.Sequence[t.Any]) -> t.Tuple[str, str]:
2145
+ verb = "was" if len(extras) == 1 else "were"
2146
+ return ", ".join(repr(extra) for extra in extras), verb
2147
+
2148
+
2149
+def _safe_additional_properties(
2150
+ validator: t.Any,
2151
+ additional: t.Any,
2152
+ instance: t.Any,
2153
+ schema: t.Any,
2154
+) -> t.Iterator[t.Any]:
2155
+ """jsonschema's `additionalProperties` with keys matched in linear time."""
2156
+ if not validator.is_type(instance, "object"):
2157
+ return
2158
+
2159
+ properties = schema.get("properties", {})
2160
+ patterns = list(schema.get("patternProperties", {}))
2161
+ matchers = []
2162
+ # jsonschema searches the `|`-joined patterns, so a lone "" matches nothing.
2163
+ if "|".join(patterns):
2164
+ for pattern in patterns:
2165
+ matches_key = _linear_pattern_matcher(pattern)
2166
+ if matches_key is None:
2167
+ yield jsonschema_exceptions.ValidationError(
2168
+ _unsupported_pattern_property_message(pattern)
2169
+ )
2170
+ return
2171
+ matchers.append(matches_key)
2172
+ extras = {
2173
+ key
2174
+ for key in instance
2175
+ if key not in properties
2176
+ and not any(matches_key(key) for matches_key in matchers)
2177
+ }
2178
+
2179
+ if validator.is_type(additional, "object"):
2180
+ for extra in extras:
2181
+ yield from validator.descend(instance[extra], additional, path=extra)
2182
+ elif not additional and extras:
2183
+ if "patternProperties" in schema:
2184
+ verb = "does" if len(extras) == 1 else "do"
2185
+ joined = ", ".join(repr(each) for each in sorted(extras))
2186
+ joined_patterns = ", ".join(
2187
+ repr(each) for each in sorted(schema["patternProperties"])
2188
+ )
2189
+ yield jsonschema_exceptions.ValidationError(
2190
+ f"{joined} {verb} not match any of the regexes: {joined_patterns}"
2191
+ )
2192
+ else:
2193
+ error = "Additional properties are not allowed (%s %s unexpected)"
2194
+ yield jsonschema_exceptions.ValidationError(
2195
+ error % _extras_msg(sorted(extras, key=str))
2196
+ )
2197
+
2198
+
2199
+class _LinearRegexModule:
2200
+ """Stands in for `re` inside jsonschema's evaluated-property helpers, whose
2201
+ only regex use is `re.search(pattern, key)` over `patternProperties`."""
2202
+
2203
+ @staticmethod
2204
+ def search(pattern: t.Any, string: t.Any) -> t.Optional[bool]:
2205
+ matches_key = _linear_pattern_matcher(pattern)
2206
+ if matches_key is None:
2207
+ raise ValueError(_unsupported_pattern_property_message(pattern))
2208
+ return True if matches_key(string) else None
2209
+
2210
+
2211
+def _unsupported_unevaluated_properties(
2212
+ validator: t.Any,
2213
+ _unevaluated: t.Any,
2214
+ instance: t.Any,
2215
+ _schema: t.Any,
2216
+) -> t.Iterator[t.Any]:
2217
+ if validator.is_type(instance, "object"):
2218
+ yield jsonschema_exceptions.ValidationError(
2219
+ "unevaluatedProperties is not supported with the installed jsonschema"
2220
+ )
2221
+
2222
+
2223
+def _linear_unevaluated_properties(keyword: t.Any) -> t.Any:
2224
+ """Rebind jsonschema's `unevaluatedProperties` (2019-09 and 2020-12) so its
2225
+ `patternProperties` key matching uses `_LinearRegexModule`.
2226
+
2227
+ The keyword's semantics are long and version-specific, so the original
2228
+ code objects run unchanged against copied module globals; nothing global
2229
+ is patched. If the helper no longer reaches regexes through a module-level
2230
+ `re`, the keyword fails closed instead of silently using `re`.
2231
+ """
2232
+ module_globals = getattr(keyword, "__globals__", None)
2233
+ finder = (module_globals or {}).get("find_evaluated_property_keys_by_schema")
2234
+ finder_globals = getattr(finder, "__globals__", None)
2235
+ if (
2236
+ not isinstance(keyword, types.FunctionType)
2237
+ or not isinstance(finder, types.FunctionType)
2238
+ or keyword.__closure__ is not None
2239
+ or finder.__closure__ is not None
2240
+ or finder_globals is None
2241
+ or finder_globals.get("re") is not re
2242
+ or "re" not in finder.__code__.co_names
2243
+ or "re" in keyword.__code__.co_names
2244
+ ):
2245
+ return _unsupported_unevaluated_properties
2246
+
2247
+ linear_finder_globals = dict(finder_globals)
2248
+ linear_finder = types.FunctionType(
2249
+ finder.__code__,
2250
+ linear_finder_globals,
2251
+ finder.__name__,
2252
+ finder.__defaults__,
2253
+ )
2254
+ linear_finder_globals["re"] = _LinearRegexModule
2255
+ linear_finder_globals["find_evaluated_property_keys_by_schema"] = linear_finder
2256
+
2257
+ linear_keyword_globals = dict(keyword.__globals__)
2258
+ linear_keyword_globals["re"] = _LinearRegexModule
2259
+ linear_keyword_globals["find_evaluated_property_keys_by_schema"] = linear_finder
2260
+ return types.FunctionType(
2261
+ keyword.__code__,
2262
+ linear_keyword_globals,
2263
+ keyword.__name__,
2264
+ keyword.__defaults__,
2265
+ )
2266
+
2267
+
2268
+_SAFE_REGEX_VALIDATOR_CLASSES: t.Set[t.Any] = set()
2269
+
2270
+
2271
+@functools.lru_cache(maxsize=None)
2272
+def _with_safe_regex_keywords(validator_class: t.Any) -> t.Any:
2273
+ """Extend a jsonschema validator class so no keyword evaluates a schema
2274
+ regex with Python `re` on unbounded input.
2275
+
2276
+ jsonschema's `evolve` (used by `descend` for every subschema) switches to
2277
+ the stock class of any subschema that declares `$schema`, which would drop
2278
+ these overrides. The returned class keeps the dialect switch but re-applies
2279
+ the regex overrides to the class it switches to.
2280
+ """
2281
+ if validator_class in _SAFE_REGEX_VALIDATOR_CLASSES:
2282
+ return validator_class
2283
+ overrides = {
2284
+ "additionalProperties": _safe_additional_properties,
2285
+ "pattern": _exact_validation_pattern,
2286
+ "patternProperties": _safe_pattern_properties,
2287
+ }
2288
+ unevaluated = validator_class.VALIDATORS.get("unevaluatedProperties")
2289
+ if unevaluated is not None:
2290
+ overrides["unevaluatedProperties"] = _linear_unevaluated_properties(unevaluated)
2291
+ return _install_safe_regex_evolve(
2292
+ jsonschema_validators.extend(validator_class, overrides)
2293
+ )
2294
+
2295
+
2296
+@functools.lru_cache(maxsize=None)
2297
+def _exact_validator_class() -> t.Any:
2298
+ # Align multipleOf with the decimal-scaled checks in the Zod and Effect
2299
+ # converters instead of raw float modulo.
2300
+ return _install_safe_regex_evolve(
2301
+ jsonschema_validators.extend(
2302
+ _with_safe_regex_keywords(jsonschema_validators.Draft7Validator),
2303
+ {"multipleOf": _exact_validation_multiple_of},
2304
+ )
2305
+ )
2306
+
2307
+
2308
+def _install_safe_regex_evolve(validator_class: t.Any) -> t.Any:
2309
+ stock_evolve = validator_class.evolve
2310
+
2311
+ def evolve(self: t.Any, **changes: t.Any) -> t.Any:
2312
+ evolved = stock_evolve(self, **changes)
2313
+ evolved_class: t.Any = type(evolved)
2314
+ if evolved_class in _SAFE_REGEX_VALIDATOR_CLASSES:
2315
+ return evolved
2316
+ # Rebuild on the safe variant, copying init fields exactly as
2317
+ # jsonschema's own `evolve` does.
2318
+ safe_class = _with_safe_regex_keywords(evolved_class)
2319
+ return safe_class(
2320
+ **{
2321
+ field.alias: getattr(evolved, field.name)
2322
+ for field in evolved_class.__attrs_attrs__
2323
+ if field.init
2324
+ }
2325
+ )
2326
+
2327
+ validator_class.evolve = evolve
2328
+ _SAFE_REGEX_VALIDATOR_CLASSES.add(validator_class)
2329
+ return validator_class
2330
+
2331
+
2332
# A bare registry resolves only references inside the schema document and
2333
# raises on anything else; jsonschema's default registry falls back to
2334
# fetching unknown URIs over the network.
2419
return result
2420
2421
2422
+def _reject_unsupported_pattern_properties(
2423
+ schema: t.Any,
2424
+ root_schema: t.Dict[str, t.Any],
2425
+) -> None:
2426
+ """Fail at conversion time on any reachable `patternProperties` key the
2427
+ linear-time engine cannot match."""
2428
+ for candidate in _iter_reachable_schemas(schema, root_schema):
2429
+ pattern_properties = candidate.get("patternProperties")
2430
+ if isinstance(pattern_properties, dict):
2431
+ for pattern in pattern_properties:
2432
+ _compile_pattern_property(pattern)
2433
+
2434
+
2435
def _validate_json_schema(
2436
schema: t.Dict[str, t.Any],
2437
root_schema: t.Dict[str, t.Any],
2448
# The SDK contract is Draft 7 plus the OpenAPI 3.0 boolean spelling for
2449
# exclusive bounds. Falling the entire document back to Draft 4 would make
2450
# `contains`, `const`, `if`/`then`/`else`, and `propertyNames` disappear.
1961
- validator_class = jsonschema_validators.Draft7Validator
1962
- # Align multipleOf with the decimal-scaled checks in the Zod and Effect
1963
- # converters instead of raw float modulo.
1964
- # `pattern` goes through pydantic's Rust regex where possible so a
1965
- # backtracking pattern cannot stall validation (Python `re` is exponential
1966
- # on inputs such as `(a+)+$`).
1967
- validator_class = jsonschema_validators.extend(
1968
- validator_class,
1969
- {
1970
- "multipleOf": _exact_validation_multiple_of,
1971
- "pattern": _exact_validation_pattern,
1972
- },
1973
- )
2451
+ # Schema regexes (`pattern`, `patternProperties`, `additionalProperties`
2452
+ # key matching) never run on unbounded Python `re`, which is exponential
2453
+ # on inputs such as `(a+)+$`.
2454
+ _reject_unsupported_pattern_properties(validation_schema, validation_root)
2455
+ validator_class = _exact_validator_class()
2456
validator = validator_class(
2457
validation_root,
2458
registry=_LOCAL_ONLY_REGISTRY,
2596
)
2597
2598
2117
-_PYDANTIC_PATTERN_SUPPORT_CACHE: t.Dict[str, bool] = {}
2118
-
2119
-
2120
-def _pattern_supported_by_pydantic(pattern: str) -> bool:
2121
- cached = _PYDANTIC_PATTERN_SUPPORT_CACHE.get(pattern)
2122
- if cached is not None:
2123
- return cached
2124
- try:
2125
- TypeAdapter(t.Annotated[str, Field(pattern=pattern)])
2126
- supported = True
2127
- except Exception: # noqa: BLE001 - pydantic raises SchemaError subclasses
2128
- supported = False
2129
- _PYDANTIC_PATTERN_SUPPORT_CACHE[pattern] = supported
2130
- return supported
2131
-
2132
-
2599
def _normalize_schema_for_library(schema: t.Any) -> t.Any:
2600
"""Rewrite constructs the json-schema-to-pydantic library mishandles.
2601