Skip to content

Add contextual awareness to Tag & Attribute sanitizer#3206

Closed
schlessera wants to merge 7 commits intodevelopfrom
fix/3137-keep-attributes-with-mustache-placeholders-intact
Closed

Add contextual awareness to Tag & Attribute sanitizer#3206
schlessera wants to merge 7 commits intodevelopfrom
fix/3137-keep-attributes-with-mustache-placeholders-intact

Conversation

@schlessera
Copy link
Copy Markdown
Collaborator

This PR changes the way nodes are processed in the Tag & Attribute sanitizer from breadth-first to depth-first. This is mainly achieved by replacing the stack additions with an array_splice to replace the currently processed node.

It also adds a mechanism to track contextual information ("Are we currently within a template tag?") to make the processing aware of such context, and add the ability to conditionally act based on such context.

Change breakdown:

  • Adds AMP_Context class as a container to track the current context.
  • Adds AMP_Contextual_Node class as stack marker to track enter, toggle and leave operations for such context.
  • Switches node traversal from breadth-first to depth-first.
  • Wraps node children in contextual nodes if context information is needed.
  • Adds first contextual knowledge about whether we are within a template tag or not.
  • Disables attribute value sanitization when within a template tag and a template placeholder is detected.

Fixes #3137

@googlebot googlebot added the cla: yes Signed the Google CLA label Sep 7, 2019
@schlessera schlessera added Bug Something isn't working Sanitizers labels Sep 7, 2019
@schlessera
Copy link
Copy Markdown
Collaborator Author

As we currently don't have a single consistent iteration over the DOM for all sanitizers combined, we cannot easily share the contextual information across sanitizers.

For solving something like a <template> tag within a <template> tag (which seems to be invalid for AMP, as there's no unambiguous way of knowing what template tag is handling which placeholders), we should either wait for the v2 redesign, or extract the context handling into a trait that can be shared amongst sanitizers to deduplicate the code at least.

}

// The type attribute of a template tag defines what templating engine to use.
switch ( $type->nodeValue ) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This switch only has one case so far, but this properly communicates the intent that there are multiple possible values we need to consider here. Turning it into an if for the short-term would obfuscate this intent and probably lead to problems later on when extending.

* used.
*/
protected function needs_contextual_nodes( DOMNode $node ) {
switch ( $node->nodeName ) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This switch only has one case so far, but this properly communicates the intent that there are multiple possible values we need to consider here. Turning it into an if for the short-term would obfuscate this intent and probably lead to problems later on when extending.

@schlessera schlessera removed the request for review from swissspidy September 7, 2019 13:05
@westonruter westonruter self-assigned this Sep 11, 2019
@westonruter westonruter added this to the v1.3 milestone Sep 11, 2019
…keep-attributes-with-mustache-placeholders-intact

* 'develop' of github.com:ampproject/amp-wp: (113 commits)
  This converts the keyboard cut handler to equal a copy handler to avoid bugs
  Fix aria-label adding helper function
  Remove extra line I added in resolving merge conflict
  Fix alignment of arrow
  Fix custom CTA text for page attachment
  Fix cut handler by shortcut
  Cleanup of duplicated comment
  Add unit testing to `addVideoAriaLabel`
  Remove unused piece of code
  Remove Cloudflare AMP cache since deprecated
  Handle cut keyboard requests. (#3231)
  Page Attachment block (#3035)
  Keyboard & Right-Click Menu Copy + Paste (#3083)
  Animation Previews (#3104)
  Make internal methods inaccessible
  Omit the ecosystem link also when using a core theme
  Add skipped e2e test for the video block
  Add array_colum() pollyfill for PHP < 5.5
  Add asserts to make sure we are not enqueueing both versions of dashicons
  Remove useless variable
  ...
Copy link
Copy Markdown
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review my PR #3243 which builds on what you've done here to suggest the an alternative solution which I believe is simpler and more flexible (e.g. being able to be used also for checking if has_ancestor).

That PR targets this PR's branch, so once merged into this branch we can then wrap up this PR for 1.3. I updated the PR to point to develop.

@westonruter
Copy link
Copy Markdown
Member

Suggesting we close this PR in favor of #3243.

@swissspidy swissspidy deleted the fix/3137-keep-attributes-with-mustache-placeholders-intact branch September 24, 2019 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working cla: yes Signed the Google CLA Sanitizers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AMP_Allowed_Tags_Generated class is stripping amp-date-display {{iso}} format from amp-timeago component

3 participants