Repository navigation
Improve lazy loading for iframe and video content replacement - #1244
Conversation
There was a problem hiding this comment.
🟢 Approved
This approves the fix. One inline comment asks for a preload test, and two notes ask about malformed iframes and image attribute rewrites.
inc/lazyload_replacer.php:283—noteThe image paths still rewrite attribute text without parsing attributes. This applies to the nativeloading=replace here, the/<img/iminserts ininc/tag_replacer.php:463-491, and the GIF-to-videostr_replaceininc/tag_replacer.php:177-188. An img whose title holdsloading='x' onmouseover=alert(1) y='getsonmouseoveras a real attribute. Core, comment and WooCommerce output encode those quotes. Do builder integrations (Divi, Elementor, Beaver Builder, Jetpack) ever pass such raw attribute values toreplace_content()? If no, this is safe. If yes, users withoutunfiltered_htmlcan inject attributes.
Validation details
- Files reviewed: 2/2 changed files. One history question stays open: the precedent of issue #1184, where an attribute-aware rewrite skips malformed markup.
- PHPUnit 9.6.22 on PHP 8.3.36 with the WordPress test suite: the four new cases pass at HEAD.
cd /repo && set -o pipefail; timeout 600 vendor/bin/phpunit --do-not-cache-result --filter 'Test_Lazyload::test_lazyload…printedOK (4 tests, 5 assertions)(run R4) - The same cases fail at pr-base on the changed assertion. The A/B helper printed no run record.
- Existing iframe and video tests pass at HEAD.
cd /repo && set -o pipefail; timeout 600 vendor/bin/phpunit --do-not-cache-result --filter 'Test_Lazyload::.*(video|ifra…printedOK (10 tests, 21 assertions)(run R5) - An isolated run compared HEAD and pr-base patterns on oEmbed, Maps, multi-line and 500-iframe pages. The results were identical, except for iframes with no space between attributes. [R6]
test_dam_lazyloadingfails at HEAD and at pr-base alike.
Untested areas
- The precedent of issue #1184 stays open: no source shows real embeds with malformed iframe start tags.
- Builder integration outputs (Divi, Elementor, Beaver Builder, Jetpack, FacetWP, JetEngine) were not traced.
- Other PHPUnit files, Jest and Playwright were not run.
🤖 Automated review · run code-review-agent_6afebc6863a256.99596185.
🤖 Review agent — review posted ✅ on 4ef19155 · approved · 1 finding · 31 min
Run code-review-agent_6afebc6863a256.99596185 · trail
| $videos = []; | ||
| preg_match_all( '#(?:<noscript\s*>\s*)?<iframe(.*?)></iframe>(?:\s*</noscript\s*>)?#is', $content, $iframes ); | ||
| // Only iframes with no fallback content; one with fallback text is left as written. | ||
| preg_match_all( '#(?:<noscript\s*>\s*)?<iframe(?>(?:\s+' . self::TAG_ATTRIBUTE . ')*)\s*/?></iframe>(?:\s*</noscript\s*>)?#i', $content, $iframes ); |
There was a problem hiding this comment.
note The new iframe pattern needs a space before each attribute and a value without stray quotes. An iframe like <iframe src="x"width="5"></iframe>, an unquoted value with an apostrophe, or slashed quotes \" no longer matches. Such an iframe is not lazy-loaded any more and loads at page start; pr-base lazy-loaded it. Do embeds your users paste emit such markup? If no, this is safe. If yes, accept those start tags or document the change.
There was a problem hiding this comment.
Slashed \" values are deliberately left unmatched. That markup is JSON-escaped HTML. If it were read as HTML, title=\"a src=x …\" would expose the src=x text to the rewrite again, which is the issue's own attack. At development the rewrite inserted a raw " there, which produced invalid JSON under JSON_UNESCAPED_SLASHES, so skipping it removes a breakage. test_lazyload_iframe_leaves_json_escaped_markup_unchanged pins this.
There was a problem hiding this comment.
The two HTML cases are fixed. <iframe src="…"width="5"></iframe>, <iframe src=… title=it's></iframe> and <iframe title="a"src="…"></iframe> are lazy-loaded again at b147deb, as at pr-base. At 4ef1915 they were returned unchanged. The slashed-quote case is conceded. At pr-base the JSON round trip returned null, and the new test passes at HEAD.
There was a problem hiding this comment.
🟢 Approved
This approves the new commit. One inline comment asks to narrow the docblock about JSON-escaped markup to iframes.
Validation details
- Files reviewed: 2/2 changed files since the prior review at 4ef1915.
- PHPUnit 9.6.22 on PHP 8.3.36 with the WordPress test suite: the two new tests pass at HEAD.
cd /repo && set -o pipefail; timeout 600 vendor/bin/phpunit --do-not-cache-result --filter 'Test_Lazyload::test_lazyload…printedOK (2 tests, 3 assertions)(run R3) - Existing iframe and video tests pass at HEAD.
cd /repo && set -o pipefail; timeout 600 vendor/bin/phpunit --do-not-cache-result --filter 'Test_Lazyload::.*(video|ifra…printedOK (12 tests, 24 assertions)(run R4) - Both new tests fail at pr-base on their assertions [R6].
- Iframes with no space between attributes or an apostrophe in an unquoted value are lazy-loaded again [R8].
- A vertical tab in an attribute does not reach the rewrite after
wp_kses_post[R12].
Untested areas
- The variant pass was not rerun. It is reused from run code-review-agent_6afebc6863a256.99596185.
- The A/B helper failed on the base side, so the base run was done by hand.
- Only
wp_kses_postfor an author was probed.
🤖 Automated review · run code-review-agent_6afec878d16e38.57223985.
🤖 Review agent — review posted ✅ on b147debf · approved · 1 finding · 14 min
Run code-review-agent_6afec878d16e38.57223985 · trail
There was a problem hiding this comment.
🟢 Approved
This approves the new commit. It narrows the docblock about JSON-escaped markup to iframes, as the earlier comment asked.
Validation details
- Files reviewed: 1/1 changed file since the prior review at b147deb.
- The new sentence matches the iframe pattern. A value that starts with a backslash matches no value branch, so such an iframe is skipped.
- Tests from run code-review-agent_6afec878d16e38.57223985 at b147deb still apply, not rerun: R3, R4, R6 and R8. The new commit changes no executable line.
Untested areas
- The variant pass was not rerun. It is reused from run code-review-agent_6afebc6863a256.99596185.
- No sandbox runs were made. The new commit changes only a comment.
🤖 Automated review · run code-review-agent_6afecd0362c009.48241585.
🤖 Review agent — review posted ✅ on bc28cec0 · approved · 0 findings · 3 min
Run code-review-agent_6afecd0362c009.48241585 · trail
All Submissions:
Changes proposed in this Pull Request:
Refactored iframe and video lazy-loading logic in
Optml_Lazyload_Replacerto improve attribute replacement reliability and security. Scoped URL rewrites to the intended attributes, preventing unintended modifications, and improved handling of elements containing fallback content. Added comprehensive tests covering attribute rewriting, fallback content, and edge cases.Closes https://github.com/Codeinwp/optimole-service/issues/1980
Other information: