Skip to content

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
sirreal added a commit that referenced this pull request Sep 4, 2025
Fix an issue in the block editor where attributes that terminate in the \ character are
mis-encoded and cause block attributes to be lost.

This change encodes `\` characters with their Unicode escape sequence `\u005c`
instead of their escaped form `\\`. This makes the replacement of escaped double quotes
`\"` much simpler because the preceding `\` character must be the escape character for the
quote and an escaped character itself.

Escaping of `\"` was originally introduced in #6619
to address an issue where `wp_kses_stripslashes()` would replace escaped double quotes `\"`
with plain quotes `"` and break JSON syntax (`{"str":"\""}` becomes `{"str":"""}`).

There is a companion ticket for WordPress Core: https://core.trac.wordpress.org/ticket/63917
And an associated PR to apply the same updated JSON encoding:
WordPress/wordpress-develop#9558

See a related ticket about `wp_kses_stripslashes()` and its purpose today:
https://core.trac.wordpress.org/ticket/63881

---

Unlinked contributors: ehti, Alexius08.

Co-authored-by: sirreal <[email protected]>
Co-authored-by: dmsnell <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: skorasaurus <[email protected]>
Co-authored-by: youknowriad <[email protected]>
peterwilsoncc pushed a commit to peterwilsoncc/gutenberg-build that referenced this pull request Sep 4, 2025
Fix an issue in the block editor where attributes that terminate in the \ character are
mis-encoded and cause block attributes to be lost.

This change encodes `\` characters with their Unicode escape sequence `\u005c`
instead of their escaped form `\\`. This makes the replacement of escaped double quotes
`\"` much simpler because the preceding `\` character must be the escape character for the
quote and an escaped character itself.

Escaping of `\"` was originally introduced in WordPress/gutenberg#6619
to address an issue where `wp_kses_stripslashes()` would replace escaped double quotes `\"`
with plain quotes `"` and break JSON syntax (`{"str":"\""}` becomes `{"str":"""}`).

There is a companion ticket for WordPress Core: https://core.trac.wordpress.org/ticket/63917
And an associated PR to apply the same updated JSON encoding:
WordPress/wordpress-develop#9558

See a related ticket about `wp_kses_stripslashes()` and its purpose today:
https://core.trac.wordpress.org/ticket/63881

---

Unlinked contributors: ehti, Alexius08.

Co-authored-by: sirreal <[email protected]>
Co-authored-by: dmsnell <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: skorasaurus <[email protected]>
Co-authored-by: youknowriad <[email protected]>

Source: WordPress/gutenberg@10453ab
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

5 participants