From 025d1bbffefc2b0f8286e20bb8d20a249391a703 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 29 May 2018 15:46:11 -0700 Subject: [PATCH] Guard against double-reporting validation errors when sanitization rejected --- .../sanitizers/class-amp-base-sanitizer.php | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/includes/sanitizers/class-amp-base-sanitizer.php b/includes/sanitizers/class-amp-base-sanitizer.php index 5755c742528..43c6d1f8e70 100644 --- a/includes/sanitizers/class-amp-base-sanitizer.php +++ b/includes/sanitizers/class-amp-base-sanitizer.php @@ -77,6 +77,13 @@ abstract class AMP_Base_Sanitizer { */ protected $root_element; + /** + * Keep track of nodes that should not be removed to prevent duplicated validation errors since sanitization is rejected. + * + * @var array + */ + private $should_not_removed_nodes = array(); + /** * AMP_Base_Sanitizer constructor. * @@ -295,9 +302,17 @@ public function maybe_enforce_https_src( $src, $force_https = false ) { * @return bool Whether the node should have been removed, that is, that the node was sanitized for validity. */ public function remove_invalid_child( $node, $validation_error = array() ) { + + // Prevent double-reporting nodes that are rejected for sanitization. + if ( isset( $this->should_not_removed_nodes[ $node->nodeName ] ) && in_array( $node, $this->should_not_removed_nodes[ $node->nodeName ], true ) ) { + return false; + } + $should_remove = $this->should_sanitize_validation_error( $validation_error, compact( 'node' ) ); if ( $should_remove ) { $node->parentNode->removeChild( $node ); + } else { + $this->should_not_removed_nodes[ $node->nodeName ][] = $node; } return $should_remove; } @@ -404,7 +419,7 @@ public function prepare_validation_error( array $error = array(), array $data = /** * Get data-amp-* values from the parent node 'figure' added by editor block. * - * @param DOMNode $node Base node. + * @param DOMElement $node Base node. * @return array AMP data array. */ public function get_data_amp_attributes( $node ) { @@ -445,9 +460,9 @@ public function filter_data_amp_attributes( $attributes, $amp_data ) { /** * Set attributes to node's parent element according to layout. * - * @param DOMNode $node Node. - * @param array $new_attributes Attributes array. - * @param string $layout Layout. + * @param DOMElement $node Node. + * @param array $new_attributes Attributes array. + * @param string $layout Layout. * @return array New attributes. */ public function filter_attachment_layout_attributes( $node, $new_attributes, $layout ) {