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(fetch): ignore charset= hidden in another meta attribute

Rudy Celekli77aa0c66f8da
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 648be42..ea725ad 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+
+- **An og:image query no longer sets the page encoding.** `charset=` inside
+ another meta attribute was treated as `<meta charset>`. A later
+ `<meta charset="utf-8">` was ignored, so a canonical
+ `https://example.com/café` was decoded as `https://example.com/café`.
+ A real `charset` attribute, or an `http-equiv="content-type"` pragma,
+ is still the encoding.
+
## [2.5.0] - 2026-10-22
Security and accuracy fixes from a full audit, and Claude Desktop installs again.
diff --git a/scripts/url_safety.py b/scripts/url_safety.py
index bd8a443..7d9d488 100644
--- a/scripts/url_safety.py
+++ b/scripts/url_safety.py
@@ -782,8 +782,10 @@ DEFAULT_REQUEST_HEADERS = {
_CONTENT_TYPE_CHARSET_RE = re.compile(r"charset\s*=\s*['\"]?([^;,'\"\s>]+)", re.IGNORECASE)
-_META_CHARSET_RE = re.compile(
- r"<meta[^>]+charset\s*=\s*['\"]?([^;,'\"\s/>]+)",
+# HTML's encoding prescan reads attributes, not the substring "charset=".
+_META_TAG_RE = re.compile(r"<meta\b([^>]*)>", re.IGNORECASE)
+_META_ATTR_RE = re.compile(
+ r"([^\s=/>]+)\s*=\s*(?:\"([^\"]*)\"|'([^']*)'|([^\s\"'=<>`/]+))",
re.IGNORECASE,
)
_BOMS = (
@@ -806,9 +808,44 @@ def _extract_charset_from_content_type(content_type: str) -> str | None:
def _extract_meta_charset(raw: bytes) -> str | None:
+ """Return a meta encoding, using HTML's prescan rules.
+
+ A ``charset`` attribute wins. A ``content`` attribute counts only when
+ the same tag's ``http-equiv`` is ``content-type``. ``charset=`` inside
+ another attribute, such as an og:image URL, is not the document encoding.
+ """
head = raw[:4096].decode("ascii", errors="ignore")
- match = _META_CHARSET_RE.search(head)
- return match.group(1).strip() if match else None
+ for match in _META_TAG_RE.finditer(head):
+ charset = None
+ got_pragma = False
+ need_pragma = None
+ seen: set[str] = set()
+ for attr in _META_ATTR_RE.finditer(match.group(1)):
+ name = attr.group(1).lower()
+ if name in seen:
+ continue
+ seen.add(name)
+ value = attr.group(2)
+ if value is None:
+ value = attr.group(3)
+ if value is None:
+ value = attr.group(4) or ""
+ if name == "http-equiv" and value.lower() == "content-type":
+ got_pragma = True
+ elif name == "content":
+ extracted = _extract_charset_from_content_type(value)
+ if extracted and charset is None:
+ charset = extracted
+ need_pragma = True
+ elif name == "charset":
+ charset = value.strip()
+ need_pragma = False
+ if not charset or need_pragma is None:
+ continue
+ if need_pragma and not got_pragma:
+ continue
+ return charset.strip()
+ return None
def decode_response_text(response) -> str:
diff --git a/tests/test_fetch_page_decoding.py b/tests/test_fetch_page_decoding.py
index feb48a1..e78606f 100644
--- a/tests/test_fetch_page_decoding.py
+++ b/tests/test_fetch_page_decoding.py
@@ -38,6 +38,33 @@ def test_meta_charset_is_used_when_header_has_no_charset() -> None:
assert "Smart quote: “" in fetch_page._decode_response_content(response)
+def test_og_image_query_does_not_override_a_later_charset_attribute() -> None:
+ """charset= inside another attribute is not the document encoding.
+
+ HTML's prescan honors a charset attribute, or a content attribute only
+ when http-equiv is content-type. An og:image query must not win over a
+ later <meta charset="utf-8">, or the canonical URL is decoded wrong.
+ """
+ import parse_html
+
+ html = (
+ '<meta property="og:image" content="https://cdn.example/a.jpg?charset=iso-8859-1">'
+ '<meta charset="utf-8">'
+ '<link rel="canonical" href="https://example.com/café">'
+ )
+ decoded = fetch_page._decode_response_content(FakeResponse(html.encode("utf-8"), "text/html"))
+ assert parse_html.parse_html(decoded)["canonical"] == "https://example.com/café"
+
+
+def test_charset_attribute_wins_when_it_is_the_encoding() -> None:
+ html = (
+ '<meta property="og:image" content="https://cdn.example/a.jpg?charset=utf-8">'
+ '<meta charset="windows-1252"><p>Smart quote: “</p>'
+ ).encode("windows-1252")
+ decoded = fetch_page._decode_response_content(FakeResponse(html, "text/html"))
+ assert "Smart quote: “" in decoded
+
+
def test_invalid_bytes_are_replaced_not_dropped() -> None:
response = FakeResponse(b"valid utf8 then invalid: \x8f")
decoded = fetch_page._decode_response_content(response)