Skip to content

Conversation

@sirreal
Copy link
Member

@sirreal sirreal commented Aug 21, 2025

Companion to WordPress/gutenberg#71291 and #9558.

Prevent wp_kses_stripslashes() from stripping slashes that are escaped themselves and do no escape the ".

See WordPress/gutenberg#16508 and WordPress/gutenberg#6619.

[649] introduced this slash stripping in the original version of KSES introduced in Core. It's remained the same ever since:

In trac:

function wp_kses_stripslashes($string)
###############################################################################
# This function changes the character sequence \" to just "
# It leaves all other slashes alone. It's really weird, but the quoting from
# preg_replace(//e) seems to require this.
###############################################################################
{
return preg_replace('%\\\\"%', '"', $string);
} # function wp_kses_stripslashes

Trac ticket:


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@sirreal sirreal changed the title fix/kses stripslashes strip non slashed quote KSES: Prevent stripslashes from stripping escaped slashes Aug 21, 2025
@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@sirreal
Copy link
Member Author

sirreal commented Sep 1, 2025

#9610 proposes deprecating the function. See discussion in https://core.trac.wordpress.org/ticket/63881.

@sirreal
Copy link
Member Author

sirreal commented Sep 3, 2025

This is very difficult to reason about with all the levels of escaping, but I think I've confirmed that this is an issue. It's difficult to reason about additionally because the purpose of the function is not particularly clear.

$str = <<<'STR'
\"
\\"
\\\"
STR;

echo "Original:\n" . $str . "\n\n";
echo "KSES:\n" . wp_kses_stripslashes($str) . "\n\n";

Prints:

Original:
\"
\\"
\\\"

KSES:
"
\"
\\"

The interesting case is the second line. It appears to be an escaped \ followed by a " with no preceding \ (because the preceding \ was, itself, escaped).

However, \\" became \", the \" was replaced with " resulting in \". This appears to be incorrect behavior.

The first and last lines correctly replace \" with " where the preceding \ is, itself, not escaped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants