Repository navigation
fix(seo): escape the endpoint's title before it reaches the title element - #24
Conversation
`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 `&lt;` once still returns the hostile string. A title arriving as `Cours & ateliers` must come back byte for byte, because `&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 `"` 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
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 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 saysesc_html()never does.consider— seo.php:202. SahajCloud documentstitleas "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
| $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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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
<title> unescaped|
Both critic threads answered. One commit pushed: be918e0.
Lean gate re-run after the commit: 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
* 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]>
Summary
esc_html()on the endpoint'stitleinsahaj_atlas_seo_title(), the last sink that lacked it.titlecarrying<no longer closes the<title>element and injects into the document.jsonLdis 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 drivingwp_get_document_title()with a hostile and an entity-carrying title.AGENTS.md— the escaping convention widened past attributes, and trap 20.Test results
pnpm test:all— syntax lint, 234 assertions in WordPress 6.7 / PHP 7.4, render checks on both theme kindstests/atlas-url-contract.jsonmatches SahajCloudmainWatched failing first. With
esc_html()removed,a hostile title opens no elementandand closes none eitherboth 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
wp_get_document_title()ends withesc_html(), so every other caller already receives entity-encoded text on every non-atlas page. This restores parity rather than imposing something new.AGENTS.mdwas the actual gap. Its Conventions bullet said "pass every attribute reaching markup throughesc_attr()oresc_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.<title>" (responseTypes.ts:154) — the wording that invited this bug. A one-line change there, matching the explicit contractjsonLdalready 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.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 touchessahaj_atlas_seo_request().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,openGraphkey and value (whereog:titlelives),images[].alt→esc_attr(); every body text field, includingtitlein the root's<h1>and the settings panel's row →esc_html();jsonLd→ deliberately raw, with itsphpcs:ignoreand theAGENTS.mdcarve-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
$titleuntouched, while the paragraph directly above it saysesc_html()never double encodes —_wp_specialchars()defaults$double_encodeto false. The two contradicted each other. The reason is ownership:$titlethere is whatever core or an earlierpre_get_document_titlecallback 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 existingand never re-escapedassertion would fail.sahaj_atlas_seo_get()is also the wrong lever —sahaj_atlas_seo_boot()readstypeandcanonicalthrough 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 eitheras implied by the<assertion. True ofesc_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 theis_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 ofhtml_entity_decode()— it isesc_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 onlyhtml_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 secondjsonLd-echoed-raw check, already pinned 45 lines up. The docblock's claim thatog:titleapplies "the same escape" was wrong —og:titlegoes throughesc_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, sotests/fixtures/seo-answer.phpstays the one copy the PHP suite and both render blueprints read. Verified against that file: it suppliesrootwith a plaintitleand an unrelatedjsonLd, andarray_mergereplaces only thetitlekey.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