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(seo-cluster): escape SERP data in the cluster map

Agrici Danielf22abcde957a
diff --git a/skills/seo-cluster/SKILL.md b/skills/seo-cluster/SKILL.md
index 97f3283..fdcd86b 100644
--- a/skills/seo-cluster/SKILL.md
+++ b/skills/seo-cluster/SKILL.md
@@ -166,7 +166,11 @@ Generate `cluster-map.html` using the template at `templates/cluster-map.html`.
meta: { totalPosts, totalClusters, totalLinks, estimatedWords }
}
```
-3. Replace the `CLUSTER_DATA` placeholder in the template with the actual JSON
+3. Replace the `CLUSTER_DATA` placeholder in the template with the actual JSON.
+ Serialize it as real JSON (for example `json.dumps`) and then write every
+ `<` as `\u003c`, so a SERP title containing `</script>` cannot end the
+ script block. Use `#RRGGBB` cluster colors; anything else falls back to the
+ default palette.
4. Write the completed HTML file to the output directory
5. Inform user: "Open `cluster-map.html` in a browser to explore the interactive cluster map."
diff --git a/skills/seo-cluster/templates/cluster-map.html b/skills/seo-cluster/templates/cluster-map.html
index c657dde..ea142d9 100644
--- a/skills/seo-cluster/templates/cluster-map.html
+++ b/skills/seo-cluster/templates/cluster-map.html
@@ -339,6 +339,7 @@
// === END CLUSTER DATA ===
const COLORS = ["#4E79A7", "#F28E2B", "#E15759", "#76B7B2", "#59A14F", "#EDC948", "#B07AA1", "#FF9DA7"];
+ const LINK_TYPES = ["mandatory", "recommended", "optional"];
(function render() {
const data = CLUSTER_DATA;
@@ -353,7 +354,7 @@
document.getElementById("stat-posts").textContent = data.meta.totalPosts;
document.getElementById("stat-clusters").textContent = data.meta.totalClusters;
document.getElementById("stat-links").textContent = data.meta.totalLinks;
- document.getElementById("stat-words").textContent = data.meta.estimatedWords.toLocaleString();
+ document.getElementById("stat-words").textContent = (Number(data.meta.estimatedWords) || 0).toLocaleString();
// Layout calculations
const numClusters = data.clusters.length;
@@ -406,12 +407,13 @@
const fromPos = nodePositions[link.from];
const toPos = nodePositions[link.to];
if (!fromPos || !toPos) return;
- const linkClass = "link-line " + link.type;
+ const linkType = LINK_TYPES.indexOf(link.type) >= 0 ? link.type : "optional";
+ const linkClass = "link-line " + linkType;
const linkId = "link-" + idx;
svgContent += '<line id="' + linkId + '" class="' + linkClass + '" '
+ 'x1="' + fromPos.x + '" y1="' + fromPos.y + '" '
+ 'x2="' + toPos.x + '" y2="' + toPos.y + '" '
- + 'data-from="' + link.from + '" data-to="' + link.to + '"/>';
+ + 'data-from="' + escapeHtml(link.from) + '" data-to="' + escapeHtml(link.to) + '"/>';
linkElements.push(linkId);
});
@@ -428,7 +430,7 @@
// Draw spoke nodes
data.clusters.forEach(function(cluster, ci) {
- const color = cluster.color || COLORS[ci % COLORS.length];
+ const color = safeColor(cluster.color, COLORS[ci % COLORS.length]);
cluster.posts.forEach(function(post, pi) {
const nodeId = "cluster-" + ci + "-post-" + pi;
const pos = nodePositions[nodeId];
@@ -471,11 +473,11 @@
let html = '<div class="tooltip-title">' + escapeHtml(info.title) + '</div>';
html += '<div class="tooltip-detail"><span>Keyword:</span><span>' + escapeHtml(info.keyword) + '</span></div>';
- html += '<div class="tooltip-detail"><span>Volume:</span><span>' + (info.volume || "N/A") + '</span></div>';
+ html += '<div class="tooltip-detail"><span>Volume:</span><span>' + escapeHtml(info.volume || "N/A") + '</span></div>';
html += '<div class="tooltip-detail"><span>Template:</span><span>' + escapeHtml(info.template) + '</span></div>';
- html += '<div class="tooltip-detail"><span>Words:</span><span>' + info.wordCount.toLocaleString() + '</span></div>';
+ html += '<div class="tooltip-detail"><span>Words:</span><span>' + (Number(info.wordCount) || 0).toLocaleString() + '</span></div>';
if (info.status) {
- html += '<div class="tooltip-detail"><span>Status:</span><span>' + info.status + '</span></div>';
+ html += '<div class="tooltip-detail"><span>Status:</span><span>' + escapeHtml(info.status) + '</span></div>';
}
tooltip.innerHTML = html;
@@ -567,14 +569,25 @@
})();
// Utility functions
+ // Escapes text and attribute values alike (quotes included): every
+ // field below comes from SERP data the page does not control.
function escapeHtml(str) {
- var div = document.createElement("div");
- div.textContent = str;
- return div.innerHTML;
+ return String(str == null ? "" : str)
+ .replace(/&/g, "&amp;")
+ .replace(/</g, "&lt;")
+ .replace(/>/g, "&gt;")
+ .replace(/"/g, "&quot;")
+ .replace(/'/g, "&#39;");
+ }
+
+ // Only #RRGGBB reaches fill/stroke: darken() parses exactly that form.
+ function safeColor(value, fallback) {
+ return /^#[0-9a-fA-F]{6}$/.test(value || "") ? value : fallback;
}
function truncate(str, maxLen) {
if (!str) return "";
+ str = String(str);
return str.length > maxLen ? str.substring(0, maxLen - 1) + "\u2026" : str;
}
diff --git a/tests/test_cluster_map_escaping.py b/tests/test_cluster_map_escaping.py
new file mode 100644
index 0000000..980a955
--- /dev/null
+++ b/tests/test_cluster_map_escaping.py
@@ -0,0 +1,50 @@
+"""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 ("&amp;", "&lt;", "&gt;", "&quot;", "&#39;"):
+ 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")