-
Notifications
You must be signed in to change notification settings - Fork 383
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
[WIP] Add dynamic handling of validation errors #1063
Conversation
…zation where relevant * Move debug code into AMP_Validation_Utils class. * Remove disable_invalid_removal sanitizer arg in favor of validation_error_callback
615ff1f
to
73f4373
Compare
@pbakaus Are there other such validation errors that come to mind which would leave a site worse off than sanitizing them from the response? |
As of the current state of this PR the following is how a theme could on its own opt-in to allow illegal CSS properties (such as in keyframe animations): add_filter( 'amp_content_sanitizers', function( $sanitizer_classes ) {
foreach ( array_keys( $sanitizer_classes ) as $sanitizer_class ) {
$original_callback = null;
if ( isset( $sanitizer_classes[ $sanitizer_class ]['validation_error_callback'] ) ) {
$original_callback = $sanitizer_classes[ $sanitizer_class ]['validation_error_callback'];
}
$sanitizer_classes[ $sanitizer_class ]['validation_error_callback'] = function( $validation_error ) use ( $original_callback ) {
$retval = call_user_func( $original_callback, $validation_error );
if ( isset( $validation_error['code'] ) && 'illegal_css_property' === $validation_error['code'] ) {
return false;
}
return $retval;
};
}
return $sanitizer_classes;
}, 20 ); Naturally this could be made more user friendly, such as with a filter. UPDATE: Now it looks like this: add_filter( 'amp_content_sanitizers', function( $sanitizer_classes ) {
foreach ( $sanitizer_classes as &$args ) {
$args['validation_error_callback'] = function( $validation_error ) {
if ( isset( $validation_error['code'] ) && 'illegal_css_property' === $validation_error['code'] ) {
return false;
}
return true;
};
}
return $sanitizer_classes;
} ); |
* Run amp_content_sanitizers filter late to apply after sanitizers have been added. * Wrap any existing validation_error_callback functions which have been supplied to do the handling of the validation error. * Pass flag to style sanitizer to locate_sources to ensure sources are available for reporting when they'll be needed. * Add handle_validation_error and set_current_node methods to reduce duplication. * Fix inclusion of property_value in validation_error. * Pass debug flag to AMP_Validation_Utils upon init.
👉 Update: On second thought, the ability for a validation callback to prevent the removal of something that is invalid should not be allowed. Rather, when validation errors occur and these validation errors are not acknowledged as being ignorable, then AMP should be prevented from being served altogether. See #1003 (comment) |
Ah, rather than letting the validation callback suppress sanitization of the error, the callback could instead be used to determine whether or not the validation error is “critical”, and when the first such error is encountered, switch to serving non-AMP. |
Commits cherry-picked into #1093. |
Follow up on #1048 (comment)
There are a few validation errors that should specifically be not sanitized by default since they could seriously impact a site:
In addition to allowing the user to manually acknowledge whether a given validation error should be considered critical or be ignored (#1003), there also needs to be a way to dynamically make this decision based on a user-supplied validation callback, a capability which would also be used in #1087 to check whether a validation error is a deal breaker for serving AMP for the current page.
Todo
$node
to methods).Decide on which validation errors should not result in sanitization by default (if any).Consider having a AMP admin setting for whether to allow select validation errors when they occur.Look at factoring in sanitization-skipping to paired mode, to servecanonical
dirty AMP documents but valid non-canonical (amphtml
) ones. This is similar to incorporating prerendering into the postprocessing flow (Enable generation of Optimized (aka Transformed) AMP pages #958).Closes #956