Harden Optimization Detective XPath data against HTML-mangling plugins - #2694
Draft
sarthak-19 wants to merge 1 commit into
Draft
sarthak-19 wants to merge 1 commit into
sarthak-19 wants to merge 1 commit into
Conversation
Replace the data-od-xpath attribute, which embeds raw XPath text directly in HTML, with an auto-incremented data-od-id attribute. The id-to-xpath mapping is shipped once as a gzip-compressed, base64-encoded JSON SCRIPT tag (falling back to plain JSON when gzip is unavailable) and decoded by detect.js at initialization. This prevents third-party "HTML minifier" plugins that use naive regular expressions to strip JS/CSS comments from mistaking an XPath's /* for the start of a comment and corrupting the page, while also shrinking the HTML payload and avoiding search engines treating XPath strings as crawlable URLs. Embed Optimizer's detect.js, which previously read data-od-xpath directly off the DOM, now resolves XPaths through a new getElementXPath() function exposed to extensions via the initialize()/finalize() args. Fixes WordPress#1947
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #2694 +/- ##
==========================================
- Coverage 70.50% 70.49% -0.01%
==========================================
Files 91 91
Lines 7869 7888 +19
==========================================
+ Hits 5548 5561 +13
- Misses 2321 2327 +6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1947.
Some plugins implement HTML "minification" using naive regular expressions that strip JS/CSS comments (e.g.
preg_replace('@/\*(.*?)\*/@s', ...)). Because Optimization Detective writes raw XPath strings like/HTML/BODY/*[1][self::DIV]directly intodata-od-xpathattributes, such plugins can mistake the/*in an XPath for the start of a JS/CSS comment and corrupt the page by swallowing everything up to the next*/-like sequence.Per the direction from the issue thread, this takes the larger approach rather than a simple
base64_encode()/atob()of the attribute value:data-od-idattribute instead of the raw XPath text.id → xpathmapping is shipped once in aSCRIPTtag as gzip-compressed, base64-encoded JSON (application/gzip+json;base64), falling back to plain JSON whengzencode()is unavailable server-side.detect.jsdecompresses/parses this mapping at initialization and resolvesdata-od-idback to the original XPath.This removes XPath text from the HTML entirely, which:
Extension API change
embed-optimizer/detect.jspreviously readdata-od-xpathdirectly off the DOM viaelement.dataset.odXpath. Since the attribute no longer carries the XPath, a newgetElementXPath(element)function is now exposed to extensions via theinitialize()/finalize()args (added toInitializeArgs/FinalizeArgsintypes.ts), and Embed Optimizer uses it instead.image-prioritizerneeded no changes — its tag visitors call$processor->get_xpath()directly during the PHP-side tag walk, independent of the HTML attribute.AI TOOL