Skip to content

Improve lazy loading for iframe and video content replacement - #1244

Merged
pirate-bot merged 3 commits into
developmentfrom
bugfix/optimole-service/1980
Nov 19, 2026
Merged

pirate-bot merged 3 commits into
developmentfrom
bugfix/optimole-service/1980

Conversation

@girishpanchal30

@girishpanchal30 girishpanchal30 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

All Submissions:

Changes proposed in this Pull Request:

Refactored iframe and video lazy-loading logic in Optml_Lazyload_Replacer to 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:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?

@pirate-bot

pirate-bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Plugin build for bc28cec is ready 🛎️!

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟢 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 — note The image paths still rewrite attribute text without parsing attributes. This applies to the native loading= replace here, the /<img/im inserts in inc/tag_replacer.php:463-491, and the GIF-to-video str_replace in inc/tag_replacer.php:177-188. An img whose title holds loading='x' onmouseover=alert(1) y=' gets onmouseover as 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 to replace_content()? If no, this is safe. If yes, users without unfiltered_html can 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… printed OK (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… printed OK (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_lazyloading fails 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

Comment thread tests/test-lazyload.php Resolved
Comment thread inc/lazyload_replacer.php Outdated
$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 );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

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.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟢 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… printed OK (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… printed OK (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_post for 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

Comment thread inc/lazyload_replacer.php Resolved

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟢 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

@pirate-bot
pirate-bot merged commit a6b3144 into development Nov 19, 2026
14 checks passed
@pirate-bot
pirate-bot deleted the bugfix/optimole-service/1980 branch November 19, 2026 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants