Skip to content

Commit

Permalink
Merge pull request ampproject#7894 from ampproject/fix/subscriptions-…
Browse files Browse the repository at this point in the history
…section

Add mandatory parent special case for amp-subscriptions
  • Loading branch information
westonruter authored Nov 12, 2024
2 parents 2d6d2f6 + 694810e commit a5a08dd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
29 changes: 24 additions & 5 deletions includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,11 +991,30 @@ private function get_json_error_code( $json_last_error ) {
*/
private function validate_tag_spec_for_node( DOMElement $node, $tag_spec ) {

if ( ! empty( $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ] ) && ! $this->has_parent( $node, $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ] ) ) {
return [
'code' => self::WRONG_PARENT_TAG,
'required_parent_name' => $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ],
];
if ( ! empty( $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ] ) ) {
$missing_required_parent = false;
if ( 'subscriptions-section content swg_amp_cache_nonce' === $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ] ) {
// Special case for mandatory parent name which doesn't follow the pattern where mandatory_parent is just a tag name.
if ( ! (
$this->has_parent( $node, 'section' )
&&
$node->parentNode instanceof DOMElement
&&
$node->parentNode->getAttribute( 'subscriptions-section' ) === 'content'
&&
$node->parentNode->hasAttribute( 'swg_amp_cache_nonce' )
) ) {
$missing_required_parent = true;
}
} elseif ( ! $this->has_parent( $node, $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ] ) ) {
$missing_required_parent = true;
}
if ( $missing_required_parent ) {
return [
'code' => self::WRONG_PARENT_TAG,
'required_parent_name' => $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ],
];
}
}

// Extension scripts must be in the head. Note this currently never fails because all AMP scripts are moved to the head before sanitization.
Expand Down
23 changes: 23 additions & 0 deletions tests/php/test-tag-and-attribute-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3268,6 +3268,29 @@ class="tiktok-embed"
null,
[ 'amp-wordpress-embed' ],
],

'subscriptions-section' => [
'
<section class="text" subscriptions-section="content" encrypted="" swg_amp_cache_nonce="">
<script type="application/octet-stream" ciphertext="">
</script>
</section>
<span swg_amp_cache_nonce="">
</span>
<section subscriptions-section="content">
<p class="text">
Lorem ipsum.
</p>
</section>
<section subscriptions-section="content-not-granted">
<p class="text">
<strong>Subscribe for more.</strong>
</p>
</section>
',
null,
[ 'amp-subscriptions' ],
],
];
}

Expand Down

0 comments on commit a5a08dd

Please sign in to comment.