mirror of
https://github.com/AgriciDaniel/claude-seo.git
synced 2026-11-25 07:30:08 +01:00
cluster-map.html builds SVG and tooltip markup by string concatenation and assigns it to innerHTML. link.from, link.to, link.type, the volume, word count and status went in unescaped, and the escape helper did not escape quotes, so a SERP title or keyword could run script in the file the user opens. - escapeHtml escapes & < > " and ' and accepts any value. - link ids and every tooltip field are escaped; word counts are coerced to numbers; link types are limited to mandatory, recommended and optional; cluster colors must be #RRGGBB (darken() parses exactly that form) and otherwise fall back to the palette. - SKILL.md Step 6 tells the model to serialize CLUSTER_DATA as JSON and write every < as <, so a title containing </script> cannot end the script block. Verified in headless Chromium with hostile CLUSTER_DATA (img onerror payloads in every field, a quote-breaking link type and color, and a </script> title): the previous template ran 5 payloads (word count, pillar volume, status, post volume, link type); this one runs none and still draws every node. New test: test_cluster_map_escaping.py (4) fails on the previous template and passes now. Gates: pytest 3.10 and 3.14 green, ruff clean, consistency 0 errors, portability ok. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit a985a4df3b538f403b9f552a1bdd04aa68b2c1be) (cherry picked from commit 66d588640daa395ed55695cc3f4954b8d4aa3dc4)
51 lines
2.0 KiB
Python
51 lines
2.0 KiB
Python
"""The cluster map template escapes every SERP-derived field it puts in HTML.
|
|
|
|
Titles, keywords, statuses and link ids come from search results the user
|
|
does not control; the template builds markup by string concatenation and
|
|
assigns it to innerHTML, so each such field must go through escapeHtml (or
|
|
be coerced to a number or checked against an allowlist).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from pathlib import Path
|
|
|
|
TEMPLATE = Path(__file__).resolve().parents[1] / "skills" / "seo-cluster" / "templates" / "cluster-map.html"
|
|
SKILL = TEMPLATE.parents[1] / "SKILL.md"
|
|
DATA_FIELD = re.compile(r"\b(?:link|info|post|cluster|data\.pillar)\.[A-Za-z]+")
|
|
|
|
|
|
def _concatenated_expressions(text: str) -> list[str]:
|
|
"""Every expression spliced between two string pieces: '...' + EXPR + '...'."""
|
|
marked = re.sub(r"'[^'\n]*'", "\x00", text) # each single-quoted literal -> one marker
|
|
return [m.strip() for m in re.findall(r"(?=\x00\s*\+\s*([^\x00]+?)\s*\+\s*\x00)", marked)]
|
|
|
|
|
|
def test_every_data_field_in_markup_is_escaped_or_coerced() -> None:
|
|
text = TEMPLATE.read_text(encoding="utf-8")
|
|
unsafe = [
|
|
expr for expr in _concatenated_expressions(text)
|
|
if DATA_FIELD.search(expr) and not expr.startswith(("escapeHtml(", "(Number("))
|
|
]
|
|
assert unsafe == []
|
|
|
|
|
|
def test_escape_helper_covers_attribute_quotes() -> None:
|
|
text = TEMPLATE.read_text(encoding="utf-8")
|
|
body = text[text.index("function escapeHtml"):text.index("function safeColor")]
|
|
for entity in ("&", "<", ">", """, "'"):
|
|
assert entity in body
|
|
|
|
|
|
def test_colors_and_link_types_are_allowlisted() -> None:
|
|
text = TEMPLATE.read_text(encoding="utf-8")
|
|
assert "safeColor(cluster.color" in text
|
|
assert "/^#[0-9a-fA-F]{6}$/" in text
|
|
assert 'LINK_TYPES.indexOf(link.type)' in text
|
|
assert '"link-line " + link.type' not in text
|
|
|
|
|
|
def test_skill_tells_the_model_to_escape_less_than_in_the_data() -> None:
|
|
assert "\\u003c" in SKILL.read_text(encoding="utf-8")
|