Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DOM exception "Not Found Error" when cleaning up after invalid attribute removal #3682

Merged
merged 3 commits into from
Nov 7, 2019

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Nov 4, 2019

Summary

Defer cleanup removal of invalid attributes until determining if they are actually removed. It turns out that calling \AMP_Base_Sanitizer::clean_up_after_attribute_removal() inside of \AMP_Base_Sanitizer::remove_invalid_attribute() can cause subsequent calls to \AMP_Base_Sanitizer::remove_invalid_attribute() to throw a DOMException “Not Found Error” if the former removed an attribute that needs to actually be removed by the latter.

For example, if there is a <a href="…" target="…"> element, and both href and target are causing validation errors, then if the target attribute gets cleaned up at the same time as href is removed, then when target tries to get removed as well, it will cause an error because it no longer is in the DOM.

Fixes issues reported on support forum:

Regression introduced 1.3.0 via #3151.

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@westonruter westonruter added Bug Something isn't working P0 High priority labels Nov 4, 2019
@westonruter westonruter added this to the v.1.4.1 milestone Nov 4, 2019
@googlebot googlebot added the cla: yes Signed the Google CLA label Nov 4, 2019
@westonruter westonruter force-pushed the fix/dom-exception-attribute-not-found-error branch from 04ee74d to 88cb213 Compare November 4, 2019 21:33
Co-Authored-By: Alain Schlesser <[email protected]>
@kienstra kienstra self-assigned this Nov 7, 2019
@kienstra
Copy link
Contributor

kienstra commented Nov 7, 2019

Approved

Hi @westonruter,
This looks good. It prevents the error, as you mentioned.

Before

On adding Weston's test snippet to a Custom HTML block and saving:

<a href=“%E2%80%9Chttps://example.com/path/to/post/%E2%80%9D“ target=“_blank“ rel=“noopener“>Whatever</a>

...there was a fatal error at:

$element->removeAttributeNode( $node );

Error log:

$ tail -f /path/to/log/php7.2_errors.log
#2 /path/to/wp-content/plugins/amp/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php(304): AMP_Tag_And_Attribute_Sanitizer->process_node(Object(DOMElement))
#3 /path/to/wp-content/plugins/amp/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php(287): AMP_Tag_And_Attribute_Sanitizer->sanitize_element(Object(DOMElement))
#4 /path/to/wp-content/plugins/amp/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php(287): AMP_Tag_And_ in path/to/wp-content/plugins/amp/includes/sanitizers/class-amp-base-sanitizer.php on line 489
[07-Nov-2019 18:13:23 UTC] PHP Fatal error:  Uncaught DOMException: Not Found Error in /path/to/wp-content/plugins/amp/includes/sanitizers/class-amp-base-sanitizer.php:489
Stack trace:
#0 /path/to/wp-content/plugins/amp/includes/sanitizers/class-amp-base-sanitizer.php(489): DOMElement->removeAttributeNode(Object(DOMAttr))

There was also an indication of a fatal error in the validator:

showing-error

After

With this PR, there was no fatal error. Validation worked as expected, without the message shown above.

validation-errors-appear

Also, quickly doing wp amp validation run didn't show any issue.

@westonruter
Copy link
Member Author

I've fleshed out the error message to make request checking the error log in the future when something like this happens again:

image

@westonruter westonruter merged commit eda41f1 into develop Nov 7, 2019
@westonruter westonruter deleted the fix/dom-exception-attribute-not-found-error branch November 7, 2019 23:56
westonruter added a commit that referenced this pull request Nov 7, 2019
…tribute removal (#3682)

* Defer cleanup removal of invalid attributes until determining if they are actually all removed

* Improve phpdoc comments

Co-Authored-By: Alain Schlesser <[email protected]>

* Add explanation to check server error log when errors occur
@kienstra
Copy link
Contributor

kienstra commented Nov 8, 2019

Ah, good idea!

@kienstra
Copy link
Contributor

Testing Steps

  1. Create a new post
  2. Add a Custom HTML block with Weston's snippet:
<a href=“%E2%80%9Chttps://example.com/path/to/post/%E2%80%9D“ target=“_blank“ rel=“noopener“>Whatever</a>
  1. Save
  2. There should be a notice of a validation error, click to see the details
  3. Expected: The validator screen should display without a 'URL validation failed' notice:

validation-errors-appear

@csossi
Copy link

csossi commented Nov 12, 2019

Verified in QA

@csossi csossi added the QA passed Has passed QA and is done label Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working cla: yes Signed the Google CLA P0 High priority QA passed Has passed QA and is done
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants