Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

fix(agentic): cap JSON depth before parsing so results do not depend on stack size

Agrici Daniel8b42cac6aea2
diff --git a/scripts/agentic_check.py b/scripts/agentic_check.py
index 201f006..bc0e80d 100644
--- a/scripts/agentic_check.py
+++ b/scripts/agentic_check.py
@@ -64,7 +64,13 @@ from urllib.parse import urljoin, urlparse
_SCRIPTS_DIR = os.path.dirname(os.path.abspath(__file__))
if _SCRIPTS_DIR not in sys.path:
sys.path.insert(0, _SCRIPTS_DIR)
-from url_safety import URLSafetyError, decode_body, safe_requests_get # noqa: E402
+from url_safety import ( # noqa: E402
+ MAX_JSON_DEPTH,
+ URLSafetyError,
+ decode_body,
+ json_depth_exceeds,
+ safe_requests_get,
+)
CHECKED_ON = "2026-09-23"
MAX_BODY = 2_000_000
@@ -646,6 +652,9 @@ def validate_ai_catalog(raw: str) -> dict:
The JSON Schema pass is not reproduced; ``lighthouse_agentic.py`` reports it.
"""
errors, warnings = [], []
+ if json_depth_exceeds(raw):
+ return {"errors": [f"malformed JSON: nested deeper than {MAX_JSON_DEPTH} levels"],
+ "warnings": [], "entries": 0}
try:
data = json.loads(raw)
except (ValueError, RecursionError) as exc: # RecursionError: absurdly deep nesting
@@ -760,6 +769,9 @@ def audit_well_known(root: str) -> tuple[list, dict]:
row["soft_404"] = True
status, fix = "warn", ("Returns HTML with 200. Serve a real 404 or the JSON "
"document; soft 404s mislead discovery clients.")
+ elif rec["status"] == 200 and json_depth_exceeds(rec["text"]):
+ row["valid_json"] = False
+ status, fix = "fail", f"Document is nested deeper than {MAX_JSON_DEPTH} levels."
elif rec["status"] == 200:
try:
json.loads(rec["text"])
diff --git a/scripts/url_safety.py b/scripts/url_safety.py
index d271dcd..b1a5cdc 100644
--- a/scripts/url_safety.py
+++ b/scripts/url_safety.py
@@ -876,6 +876,33 @@ def read_bounded(response, max_bytes: int) -> tuple[bytes, bool]:
return b"".join(chunks), too_large
+# Deepest JSON nesting a fetched document may have. Real discovery and
+# profile documents stay under 20 levels. From Python 3.14 the json module's
+# recursion limit follows the C stack size, so a 100k-deep document may parse
+# on one machine and raise on another; check depth first for one answer.
+MAX_JSON_DEPTH = 256
+
+_JSON_DEPTH_TOKEN_RE = re.compile(r'"(?:[^"\\]|\\.)*"|[\[\]{}]')
+
+
+def json_depth_exceeds(text: str, max_depth: int = MAX_JSON_DEPTH) -> bool:
+ """Return True when ``text`` nests arrays or objects past ``max_depth``.
+
+ Brackets inside JSON strings are skipped. The scan does not validate the
+ document; call it before ``json.loads`` and treat True as a finding.
+ """
+ depth = 0
+ for match in _JSON_DEPTH_TOKEN_RE.finditer(text):
+ token = match.group()
+ if token in "[{":
+ depth += 1
+ if depth > max_depth:
+ return True
+ elif token in "]}":
+ depth -= 1
+ return False
+
+
def _bound_redirect_body(response, *args, **kwargs): # type: ignore[no-untyped-def]
"""``response`` hook: read at most MAX_REDIRECT_BODY_BYTES of a 3xx body.
diff --git a/tests/test_hostile_inputs.py b/tests/test_hostile_inputs.py
index c3ce491..04ed99b 100644
--- a/tests/test_hostile_inputs.py
+++ b/tests/test_hostile_inputs.py
@@ -42,6 +42,27 @@ def test_deeply_nested_well_known_document_is_a_finding(monkeypatch):
assert all(c["status"] == "fail" for c in checks)
+def test_json_depth_limit_is_independent_of_stack_size(monkeypatch):
+ """300 levels fail and 200 levels pass on every Python and C stack size."""
+ too_deep = "[" * 300 + "]" * 300
+ shallow = "[" * 200 + "]" * 200
+ assert ac.validate_ai_catalog(too_deep)["errors"] == [
+ f"malformed JSON: nested deeper than {ac.MAX_JSON_DEPTH} levels"]
+ assert ac.validate_ai_catalog(shallow)["errors"] == ["root must be a JSON object"]
+ for body, valid in ((too_deep, False), (shallow, True)):
+ monkeypatch.setattr(ac, "fetch", lambda url, headers=None, body=body: _rec(
+ 200, body, {"content-type": "application/json"}))
+ checks, data = ac.audit_well_known("https://e.test")
+ assert all(row["valid_json"] is valid for row in data.values())
+
+
+def test_json_depth_scan_skips_brackets_inside_strings():
+ from url_safety import json_depth_exceeds
+ assert not json_depth_exceeds(json.dumps({"a": "[" * 1000, "b": "{\\\"[[["}))
+ assert json_depth_exceeds(json.dumps({"a": [[[]]]}), max_depth=3)
+ assert not json_depth_exceeds(json.dumps({"a": [[]]}), max_depth=3)
+
+
def test_ucp_profile_hostile_shapes_do_not_crash():
assert ucp_check.parse_profile(DEEP)["issues"]
profile = {"ucp": {"version": "2026-08-25", "services": {"dev.ucp.shopping": [