Skip to content

fix(seo): escape the endpoint's title before it reaches the title element - #24

Merged
Ardnived merged 5 commits into
mainfrom
claude/fix-22-escape-seo-title
Sep 13, 2026
Merged

Ardnived merged 5 commits into
mainfrom
claude/fix-22-escape-seo-title

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • esc_html() on the endpoint's title in sahaj_atlas_seo_title(), the last sink that lacked it.
  • A title carrying < no longer closes the <title> element and injects into the document.
  • jsonLd is untouched — still echoed byte for byte, as its <script> sink requires.

Changes

  • includes/seo.php — one line of code, plus the ⚠ docblock carrying the short-circuit mechanism and why the other branch stays raw.
  • tests/seo.php — a group driving wp_get_document_title() with a hostile and an entity-carrying title.
  • AGENTS.md — the escaping convention widened past attributes, and trap 20.

Test results

  • Lean gate: ✓ pnpm test:all — syntax lint, 234 assertions in WordPress 6.7 / PHP 7.4, render checks on both theme kinds
  • Targeted specs: 234 passed, 0 failed
  • Contract step: ✓ tests/atlas-url-contract.json matches SahajCloud main
  • Build: N/A

Watched failing first. With esc_html() removed, a hostile title opens no element and and closes none either both fail; with it restored, both pass. Re-verified after the cleanup pass trimmed the group, and again after the review pass.

Manual verification

Nothing needed. The trust boundary is SahajCloud's operator-written copy, so there is no live route that produces a hostile title today — this is defence in depth, and the suite reaches the sink through core's real wp_get_document_title().

Notes for reviewer

  • The escape is in a filter, which usually leaks. It does not here: core's own non-short-circuit path in wp_get_document_title() ends with esc_html(), so every other caller already receives entity-encoded text on every non-atlas page. This restores parity rather than imposing something new.
  • AGENTS.md was the actual gap. Its Conventions bullet said "pass every attribute reaching markup through esc_attr() or esc_url()". <title> is text, so the rule never covered the sink this PR closes. Widening it is the change most likely to prevent the next one.
  • One decision waits on you. SahajCloud types this field as "Page title, ready for <title>" (responseTypes.ts:154) — the wording that invited this bug. A one-line change there, matching the explicit contract jsonLd already carries, stops the next consumer repeating it. SahajCloud's guide needs your go-ahead before I file an issue there, so the thread stays open.
  • The add_query_arg() note in fix(seo): the endpoint's title is echoed into <title> unescaped #22 is not fixed here. The ticket files it as lower-value and scoped to the caller's own request. It stays for whoever next touches sahaj_atlas_seo_request().
  • Four findings were rejected during review, with reasons — see Detail.
Detail

Sink inventory. Every field of the SEO answer was traced to its sink to confirm this was the only unescaped one. description → esc_attr(); canonical, alternates[].href, images[].url, website, events[].url → esc_url(); alternates[].hreflang, openGraph key and value (where og:title lives), images[].alt → esc_attr(); every body text field, including title in the root's <h1> and the settings panel's row → esc_html(); jsonLd → deliberately raw, with its phpcs:ignore and the AGENTS.md carve-out. Nothing else was left open.

Accepted from the adversarial review (commit 5): the docblock gave double encoding as the reason the else branch returns $title untouched, while the paragraph directly above it says esc_html() never double encodes — _wp_specialchars() defaults $double_encode to false. The two contradicted each other. The reason is ownership: $title there is whatever core or an earlier pre_get_document_title callback composed, so escaping it would edit another plugin's output. That holds whether or not the escape would double encode. Comment only, no behaviour change.

Rejected: escape centrally, at fetch or parse time. It breaks jsonLd, which arrives pre-escaped for a <script> sink and whose existing and never re-escaped assertion would fail. sahaj_atlas_seo_get() is also the wrong lever — sahaj_atlas_seo_boot() reads type and canonical through it for control flow, so HTML-escaping there would corrupt the trap-15 domain check before any rendering happens. And the sinks want three different escapes, so one escape upstream would double-escape the body renderers that already escape at their own sink.

Rejected: drop and closes none either as implied by the < assertion. True of esc_html(), not of the regression it guards: a half-escape replacing < alone passes the first assertion and leaves every > standing.

Rejected: read the title through sahaj_atlas_seo_get(). The (string) cast predates this branch, and the helper would not survive the is_array() guard the ternary already carries. Out of scope for a one-line fix.

Deferred, not rejected: the producer's type comment. It is a SahajCloud change, not one this PR can make, and that repo's guide needs a human's go-ahead before an issue is filed. The thread stays open for that decision.

Accepted from the cleanup pass (commit 3): wp_specialchars_decode( …, ENT_QUOTES ) instead of html_entity_decode() — it is esc_html()'s own inverse, so the round-trip assertion pins the escape under test rather than the whole HTML5 entity table, and it was the only html_entity_decode() in first-party code. Two duplicate assertions were dropped: the plain-title case, already pinned byte for byte 300 lines up, and a second jsonLd-echoed-raw check, already pinned 45 lines up. The docblock's claim that og:title applies "the same escape" was wrong — og:title goes through esc_attr() — and was replaced with the core-parity fact above.

No fixture change. Both hostile titles are array_merge() overlays on the shared root answer, so tests/fixtures/seo-answer.php stays the one copy the PHP suite and both render blueprints read. Verified against that file: it supplies root with a plain title and an unrelated jsonLd, and array_merge replaces only the title key.

One commit per behaviour: the fix, its tests, the cleanup pass, the docs sync, the review pass.

Closes #22

🤖 Written by the sydevs autonomous loop — see what this is

🤖 Generated with Claude Code

https://claude.ai/code/session_01X6wCPpNabER3kVtBVv4Pq4

`sahaj_atlas_seo_title()` returned the endpoint's `title` into
`pre_get_document_title` unescaped, and nothing downstream escapes it. A
non-empty return from that filter short-circuits
`wp_get_document_title()` before every sanitising step below it, and
`_wp_render_title_tag()` echoes the result raw. A `title` carrying `<`
therefore closed the `<title>` element and put whatever followed into
the document.

Defence in depth rather than an open door: the value is SahajCloud's
operator-written copy, not a visitor's. But it was the one unescaped
sink in a file whose Conventions entry names exactly one deliberate
exception, and `jsonLd` is that exception. Every other place this same
field is rendered already escapes it — `esc_html()` in the crawlable
body, `esc_attr()` for `og:title`, `esc_html()` in the settings panel.

`esc_html()` does not double encode, so a title that already carries an
entity survives unchanged. The other branch is left alone: it returns
what core or another plugin composed, and escaping a value we did not
fetch would double encode somebody else's already-escaped title.

`jsonLd` is untouched. It arrives pre-escaped for a `<script>` sink and
is still echoed byte for byte — re-encoding that one is the defect, not
the fix.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017hy4doZb3QkvY4yuVrzge3
Six assertions, through `wp_get_document_title()` rather than the
callback alone — the short-circuit is the half that makes the sink
reachable, so a test that called `sahaj_atlas_seo_title()` directly
would not show that nothing sanitises the value afterwards.

Watched failing first, against the unescaped version restored: `a
hostile title opens no element` and `and closes none either` both fail,
and pass with `esc_html()` in place. The decode assertion beside them
passes either way by construction, and is there to pin that the title is
still readable text rather than only harmless.

The entity case is what separates an escape from a double escape. The
decode assertion cannot: decoding `&amp;lt;` once still returns the
hostile string. A title arriving as `Cours &amp; ateliers` must come
back byte for byte, because `&amp;amp;` in a browser tab is how a double
escape shows on a real site.

The last one guards the opposite direction. `jsonLd` is escaped by its
producer for a `<script>` sink, so a fix reaching for "escape everything
the endpoint sends" would break structured data no assertion above would
notice.

No fixture change. The hostile and entity titles are `array_merge()`
overlays on the shared root answer, so `tests/fixtures/seo-answer.php`
stays the one copy all three lanes read.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017hy4doZb3QkvY4yuVrzge3
Four cleanup lenses over the branch diff. Applied:

- `wp_specialchars_decode( …, ENT_QUOTES )` replaces `html_entity_decode()`
  in the round-trip assertion. It is `esc_html()`'s own documented inverse,
  so the assertion pins the escape under test rather than the whole HTML5
  entity table — and it was the only `html_entity_decode()` in first-party
  code.
- Dropped the "ordinary title passes through untouched" case. `tests/seo.php`
  already asserts the plain root title byte for byte through this same
  function, so the new one could not fail without that one failing first.
- Dropped the second `jsonLd`-echoed-raw assertion. The group 45 lines above
  already pins the same script tag byte for byte, and its `&quot;` assertion
  is what an "escape everything the endpoint sends" over-fix would trip.
- Corrected the docblock: it claimed `og:title` applies the same escape, but
  `og:title` goes through `esc_attr()`. Replaced with the load-bearing fact
  instead — core's own non-short-circuit path ends with `esc_html()`, so
  escaping in the filter restores parity with what every other caller of
  `wp_get_document_title()` already receives, rather than imposing something
  new on them.
- Cut the short-circuit story from the test comment, which repeated the
  docblock near-verbatim. Its test-specific half stays.

Rejected, with reasons:

- "Drop `and closes none either`, since it cannot fail without the `<`
  assertion failing." Not true of the regression it guards: a half-escape
  replacing `<` alone passes the first and leaves every `>` standing.
- "Read the title through `sahaj_atlas_seo_get()`, as the other renderers
  do." The cast predates this branch, and the reader would not survive the
  `is_array()` guard the ternary already carries.
- "Escape centrally, at fetch or parse time." That breaks `jsonLd`, corrupts
  the trap-15 canonical check that reads the same answer for control flow,
  and double-escapes every body renderer that already escapes at its sink.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017hy4doZb3QkvY4yuVrzge3
…p 20

The Conventions bullet said "pass every attribute reaching markup through
`esc_attr()` or `esc_url()`". That is the gap this branch's defect fell
through: the `<title>` element is text, not an attribute, so the rule as
written never covered it and the reviewer who wrote the sink had nothing to
check against. It now names the escape each sink wants, `esc_html()` for
text included. The `jsonLd` carve-out is unchanged.

Trap 20 carries the WordPress mechanism, which is the part nobody guesses: a
non-empty `pre_get_document_title` return short-circuits
`wp_get_document_title()` before every sanitising step below it, core's own
`esc_html()` included, and `_wp_render_title_tag()` echoes it raw.

`CLAUDE.md` is a symlink to this file, so both tools read the change.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017hy4doZb3QkvY4yuVrzge3
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 13, 2026
@sydevs-bot

sydevs-bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running

recent dispatches
  • 17:06Z address-review · attempt 1 · session · done
  • 17:06Z address-review · attempt 1 · session
  • 16:58Z adversarial-review · attempt 1 · session · done
  • 16:58Z adversarial-review · attempt 1 · session

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧐 Adversarial review

The diff is the ticket and no larger: one line of code, four assertions, one trap. All three acceptance criteria are pinned. esc_html() is the right escape in the right place — core's own wp_get_document_title() ends with it, so this restores parity rather than inventing a rule. The sink sweep re-traced clean. No blockers.

Findings

  • consider — seo.php:193. The docblock says the else branch avoids double encoding. The paragraph above says esc_html() never does.
  • consider — seo.php:202. SahajCloud documents title as "ready for <title>" — the wording that invited this bug.

What was checked

#22 vs the diff, 4 commits, includes/seo.php whole. Sink sweep re-run: openGraph → esc_attr (244), jsonLd raw (263), <h1> → esc_html (317), diagnostics escapes at its own sink (diagnostics.php:416). _wp_specialchars() defaults $double_encode false, so the entity assertion is real. Producer read: SahajCloud responseTypes.ts:154,169. document_title_parts weighed and rejected: it texturizes operator copy.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Comment thread includes/seo.php Resolved
Comment thread includes/seo.php
$seo = $GLOBALS['sahaj_atlas_seo'];

return ( is_array( $seo ) && ! empty( $seo['title'] ) ) ? (string) $seo['title'] : $title;
return ( is_array( $seo ) && ! empty( $seo['title'] ) ) ? esc_html( (string) $seo['title'] ) : $title;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SahajCloud types this field as /** Page title, ready for <title>. */ (responseTypes.ts:154). Read literally that says drop it straight in, which is how this bug arrived. jsonLd states its contract explicitly 15 lines down, so the precedent is there.

Should the producer's comment say title is plain text, escaped at the consumer's sink? A one-line ticket there stops the next consumer repeating this.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, and deferred rather than rejected. responseTypes.ts:154 does read as "drop it straight in", and jsonLd at :169 is the precedent.

The consumer side is closed here — trap 20 plus the widened convention. The wording is a SahajCloud change, and that repo's guide needs a human's go-ahead before I file an issue there. Ardnived decides, and I file it.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 13, 2026
The docblock gave double encoding as the reason `sahaj_atlas_seo_title()`
returns `$title` untouched. The paragraph directly above it says
`esc_html()` never double encodes — `_wp_specialchars()` defaults
`$double_encode` to false — so the two contradicted each other, and a reader
weighing whether to widen the escape got no usable rule.

The reason is ownership, not encoding. `$title` on that branch is whatever
core or an earlier `pre_get_document_title` callback composed. Escaping it
would edit another plugin's output. That holds whether or not the escape
would double encode, so it survives the next reader who checks.

Comment only. No behaviour change.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01X6wCPpNabER3kVtBVv4Pq4
@sydevs-bot sydevs-bot changed the title fix(seo): the endpoint's title is echoed into <title> unescaped fix(seo): escape the endpoint's title before it reaches the title element Sep 13, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Both critic threads answered. One commit pushed: be918e0.

  • Adopted — seo.php:193. The docblock blamed double encoding, which the paragraph above it rules out. It now gives ownership as the reason: that branch returns another plugin's title, not ours. Comment only. Thread resolved.
  • Deferred, waiting on you — seo.php:202. The producer's type comment reads "ready for <title>". Fixing it is a SahajCloud issue, and that repo's guide needs your go-ahead before I file one. Thread left open.

Lean gate re-run after the commit: pnpm test:all green, 234 assertions, both theme kinds. Contract step matches SahajCloud main. PR title and body refreshed — the old title was truncated mid-word.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 13, 2026
@sydevs-bot
sydevs-bot marked this pull request as ready for review September 13, 2026 17:13
@sydevs-bot
sydevs-bot requested a review from Ardnived September 13, 2026 17:13
@Ardnived
Ardnived merged commit 219bee6 into main Sep 13, 2026
28 of 30 checks passed
@Ardnived
Ardnived deleted the claude/fix-22-escape-seo-title branch September 13, 2026 21:15
@sydevs-bot sydevs-bot moved this from Revising to Done in SYDevelopers Technical Roadmap Sep 13, 2026
@sydevs-bot sydevs-bot mentioned this pull request Sep 18, 2026
Ardnived pushed a commit that referenced this pull request Sep 18, 2026
* chore(release): v0.2.0

Three merged changes ship in the plugin zip since v0.1.0, and a volunteer sees
none of them until a tag builds one: #21, #23 and #24. Everything else merged
since c1fec89 is CI, docs or tests, all of which `.distignore` keeps out of the
zip, and #10 and #13 touched shipping files in comments only.

Minor, not patch, and pre-1.0 either way: #23 adds a capability and a second
site setting, so the version has to move more than a fix would.

Four declarations, not two. `release.yml` compares the tag against the plugin
header alone, so it would have passed with `SAHAJ_ATLAS_VERSION` left at 0.1.0
— and that constant is the cache-buster on `atlas-page.css`, `atlas-page.js`
and the editor assets. Every updated site would have kept serving the 0.1.0
copies. `blocks/embed/block.json` declares the same version to the editor.
`package.json` declares none: it is dev tooling, and `.distignore` excludes it.

The changelog is written for the one non-technical volunteer who runs each
site, so it names what they will see rather than what changed in the code.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QNEAgEV5tDoLsB9nyLUv26

* fix(readme): the 0.1.0 entry claimed a 0.2.0 row

#23 edited the shipped `= 0.1.0 =` changelog entry, adding "and which
side describes the Atlas page" to the status-panel bullet. That row did
not exist at v0.1.0 — `git show v0.1.0:readme.txt` has the panel covering
four things, not five.

Plugin Update Checker merges readme.txt's sections into the WordPress
update modal, so a volunteer on 0.1.0 deciding whether to update reads
the fifth row credited to the version they already run. The 0.2.0 entry
announcing that same row then reads as a repeat, and the release that
actually ships it loses the credit.

A shipped release's entry describes what that release shipped.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Jg5XawkJdwdSyhgVGdkvQu

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

fix(seo): the endpoint's title is echoed into <title> unescaped

3 participants