Skip to content

Commit

Permalink
Guard against double-reporting validation errors when sanitization re…
Browse files Browse the repository at this point in the history
…jected
  • Loading branch information
westonruter committed May 29, 2018
1 parent 0e3c5c5 commit 025d1bb
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions includes/sanitizers/class-amp-base-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down

0 comments on commit 025d1bb

Please sign in to comment.