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

Add mandatory parent special case for amp-subscriptions #7894

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading