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

Auto-reject excessive CSS validation errors #3346

Conversation

schlessera
Copy link
Collaborator

@schlessera schlessera commented Sep 25, 2019

The AMP_Validation_Manager::is_sanitization_auto_accepted() now accepts an $error as argument and auto-rejects the specific case of an excessive CSS validation error.

In addition, the Tag & Attribute sanitizer specifically ignores the <style amp-custom> tag when it comes to the max_bytes rule in the spec (introduced in #3084).

Finally, to ensure that the resulting <style amp-custom> is not stripped itself for excessive size, the tag & attribute sanitizer treats this tag as a special case for enforcing the 'max_bytes' spec rule.

Fixes #2326

The `AMP_Validation_Manager::is_sanitization_auto_accepted()` now accepts an `$error` as argument and auto-rejects the specific case of an excessive CSS validation error.

In addition, the Tag & Attribute sanitizer specifically ignores the `<style amp-custom>` tag when it comes to the `max_bytes` rule in the spec.
@googlebot googlebot added the cla: yes Signed the Google CLA label Sep 25, 2019
Co-Authored-By: Weston Ruter <westonruter@google.com>
@schlessera
Copy link
Collaborator Author

Lots of broken tests for this change, will have to go through them and see what exact Pandora's box I actually opened here... ;)

@westonruter
Copy link
Member

westonruter commented Sep 25, 2019

Lots of broken tests for this change, will have to go through them and see what exact Pandora's box I actually opened here... ;)

It may be just a matter of replacing add_theme_support('amp') with add_theme_support('amp', ['paired' => true]), in the tests.

@westonruter
Copy link
Member

Alternatively, if the return value of is_sanitization_auto_accepted can be filtered, then the tests could could be updated to just __return_true for that filter.

@schlessera
Copy link
Collaborator Author

if the return value of is_sanitization_auto_accepted can be filtered

Is that something we should add anyway?

@westonruter
Copy link
Member

I think so. I think it makes sense to be able to give the ability to filter whether a given validation error is auto-accepted. This would be needed when removing the auto-accept checkbox, ultimately.

@schlessera
Copy link
Collaborator Author

There's only one failing test now: https://travis-ci.org/ampproject/amp-wp/jobs/589550895#L1035-L1044

The 'invalid_element' that is not being stripped anymore is very specifically is the <style amp-custom> in here:

$bad_dev_mode_document = sprintf(
'
<html amp data-ampdevmode>
<head>
<meta charset="utf-8">
<style amp-custom data-ampdevmode>%s</style>
</head>
<body>
<amp-state id="something">
<script type="application/json" data-ampdevmode>%s</script>
</amp-state>
<button data-ampdevmode onclick="alert(\'Hello!\')"></button>
<script data-ampdevmode>document.write("Hello World!")</script>
</body>
</html>
',
'button::before { content:"' . str_repeat( 'a', 50001 ) . '";"}',
'{"foo":"' . str_repeat( 'b', 100001 ) . '"}'
);

The current logic is ignoring 'max_bytes' for <style amp-custom> tags so they don't get removed: https://github.com/ampproject/amp-wp/pull/3346/files#diff-ee41b32e64c90445a71328ca93cfa799R676

I could of course change the test now, but I wonder if there's no way to make the edge case I'm overriding more specific so that the test wouldn't break here. I can't think of a way though.

@westonruter Thoughts?

@schlessera
Copy link
Collaborator Author

The only way I can think of here without modifying the tests is to check from within the Tag & Attribute sanitizer whether we have NEW_REJECTED validation errors in the current document. However:

  • I'm not yet sure this would reliably work in terms of timing.
  • It would tightly couple the Tag & Attribute sanitizer to the current WordPress data model, which we want to avoid.

So for now I'm going ahead and changing that test instead, but let's discuss further as needed.

@schlessera
Copy link
Collaborator Author

schlessera commented Sep 26, 2019

I think so. I think it makes sense to be able to give the ability to filter whether a given validation error is auto-accepted. This would be needed when removing the auto-accept checkbox, ultimately.

As I don't actually need it here, I'll open a separate issue for that.

Link: #3350

@schlessera
Copy link
Collaborator Author

schlessera commented Sep 26, 2019

I added tests for:

  • asserting that excessive_style is rejected by default (validation manager)
  • <style amp-custom> is not removed if it exceeds 'max_bytes' (tag & attribute sanitizer)

I think this should cover the current behavior. The style sanitizer did not require any changes. However, right now, the CSS manifest comment does not contain any added information. I'm not sure if we should add something there or not.

@kienstra
Copy link
Contributor

Looks Good

Hi @schlessera,
This looks good.

On testing this with Query Monitor to ensure the CSS is above 50 KB, the excessive_css errors were auto-rejected in Standard mode:

new-r

...and the <amp-custom> didn't have any styling removed:

amp-attribute-not-present

Before this PR, the Query Monitor CSS was removed, as it put the CSS over the 50 KB limit.

* @return bool Whether sanitization is forcibly accepted.
*/
public static function is_sanitization_auto_accepted() {
return amp_is_canonical() || AMP_Options_Manager::get_option( 'auto_accept_sanitization' );
public static function is_sanitization_auto_accepted( $error = null ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea to accept an optional $error parameter.

@schlessera schlessera changed the title [WIP] Auto-reject excessive CSS validation errors Auto-reject excessive CSS validation errors Sep 26, 2019
@westonruter westonruter added this to the v1.3.1 milestone Oct 8, 2019
@westonruter westonruter merged commit e564bb8 into develop Oct 8, 2019
@westonruter westonruter deleted the fix/2326-discontinue-excluding-excessive-css-in-standard-mode branch October 8, 2019 02:20
@swissspidy swissspidy modified the milestones: v1.3.1, v1.4 Oct 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA CSS Sanitizers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discontinue excluding excessive CSS by default on Standard mode sites
5 participants