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

Block API: Replace JSON-escaped quotation mark with unicode escape sequence #6619

Merged
merged 2 commits into from
Jun 21, 2018

Conversation

aduth
Copy link
Member

@aduth aduth commented May 7, 2018

Fixes #6181

This pull request seeks to resolve an issue where contributors who submit a block containing an (escaped) quotation mark in the serialized attributes would have the resulting post content become malformed. The specific behavior results during post sanitization, which for users without unfiltered_html capability includes a number more filters. In particular, the wp_kses_stripslashes function causes escaped JSON quotes to become unescaped, thus resulting in an invalid parse in the next editor session.

Implementation notes:

It was proposed at #6181 (comment) to use entity-encoding on the quotation mark. However, this can result in a jarring end-user experience, where the encoded version would be displayed on next load:

encoding

Testing instructions:

Repeat steps to reproduce from #6181 (comment) , verifying that the unicode escape sequence is saved to post content and that the post restores itself correctly upon refresh.

@aduth aduth added the [Feature] Block API API that allows to express the block paradigm. label May 7, 2018
@aduth aduth requested a review from dmsnell May 7, 2018 15:22
// escaping of quotation mark.
//
// See: https://developer.wordpress.org/reference/functions/wp_kses_stripslashes/
.replace( /\\"/g, '\\u0022' );
Copy link
Member

Choose a reason for hiding this comment

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

Can we find a corresponding translation in the parser? I'm nervous about introducing asymmetries in the parser/printer system that could confuse people or introduce inconsistencies. For example, what happens if we want to write \" in a code block? Would it be preserved or transformed into \u0022?

Is there a way we can transform the quotation mark on save so that it never gets mangled by the WordPress backend?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm nervous about introducing asymmetries in the parser/printer system that could confuse people or introduce inconsistencies.

To be fair, is it being introduced? What about the other replacements here? I ask partly because I was hoping to find precedent in the parser 😄

Is there a way we can transform the quotation mark on save so that it never gets mangled by the WordPress backend?

I don't have the knowledge to speak to whether it's viable, but the documentation of the wp_kses_stripslashes function reads like a hacky fix ("It’s really weird, but the quoting from preg_replace(//e) seems to require this") that could potentially do for a better solution more accommodating of the slash'd quote.

Copy link
Member

Choose a reason for hiding this comment

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

To be fair, is it being introduced? What about the other replacements here? I ask partly because I was hoping to find precedent in the parser 😄

Huh. Well in my head there was precedence. Maybe it was primarily the HTML itself which was the other half of the equation.

If we can store that value in a code block and have it remain the same through the whole cycle then I think we're fine.

/me digs around to find those unserializers…

Copy link
Member Author

Choose a reason for hiding this comment

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

By default this wouldn't impact a code block, since it's only relevant for the JSON-serialized attributes. The code block sources its content from the markup. Manually updating the block to use comment attributes, the escaped form becomes:

<!-- wp:code {"content":"$foo = \u0022my \\\u0022escaped\\\u0022 string\u0022;"} -->
<pre class="wp-block-code"><code>$foo = "my \"escaped\" string";</code></pre>
<!-- /wp:code -->

Which apparently the parser converts back to its non-unicode form when restored:

{
  "blockName": "core/code",
  "attrs": {
    "content": "$foo = \"my \\\"escaped\\\" string\";"
  },
  "innerBlocks": [],
  "innerHTML": "\n<pre class=\"wp-block-code\"><code>$foo = \"my \\\"escaped\\\" string\";</code></pre>\n"
}

Further interesting to note is that the original problem (slash stripping) doesn't exist with the code block as implemented currently. I think it has to do with the behavior of wp_kses_split which only operates on text within HTML comments (i.e. serialized block attributes) or within the opening tags, not the content between the opening and closing tag.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me see if it'll be simple enough to write a unit test for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in cc34b0e.

@aduth aduth force-pushed the fix/serialize-attributes-quote branch from a4561c9 to 218370a Compare May 31, 2018 20:56
@aduth aduth requested a review from a team May 31, 2018 20:56
Copy link
Member

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

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

Followed the testing steps locally—looks good! 👍

@aduth
Copy link
Member Author

aduth commented Jun 4, 2018

Thanks for reviewing @noisysocks ! As with #6620, I'll plan to land this shortly after the upcoming 3.0 release.

@aduth aduth added this to the 3.1 milestone Jun 4, 2018
@gziolo
Copy link
Member

gziolo commented Jun 21, 2018

Thanks for reviewing @noisysocks ! As with #6620, I'll plan to land this shortly after the upcoming 3.0 release.

I think I saw that one in the past. Not much time left before 3.1. Let's give it a spin before we start looking for a regression introduced after 3.0 :D

@gziolo gziolo merged commit f989b9c into master Jun 21, 2018
@gziolo gziolo deleted the fix/serialize-attributes-quote branch June 21, 2018 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PlainText block not properly escaping attributes object in db
4 participants