Stop double-escaping editorial comment fields#942
Merged
Conversation
The ajax_insert_comment handler passed five of the comment-data fields through esc_sql() before handing the array to wp_insert_comment(). That function already escapes every value through $wpdb->prepare(), so the pre-escape was applied twice: the literal backslash-quote sequences from the first pass were passed through as data to the second, and stored verbatim in the comments table. A user whose display name contained an apostrophe would appear on their editorial comment as "O\'Brien", and the same corruption affected comment author email, URL, IP address and user agent. Removing esc_sql() from all five fields fixes the storage bug. The $_SERVER values retain a light sanitisation via sanitize_text_field() and wp_unslash() so we still satisfy phpcs without reintroducing the double escape.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a user added an editorial comment, five fields on the comment — author name, email, URL, IP address and user agent — were passed through
esc_sql()before being handed towp_insert_comment(). That function already escapes every value via$wpdb->prepare(), so the pre-escape was applied a second time and the literal backslash-quote sequences from the first pass were stored as data in the comments table.The most visible symptom was an author whose display name contained an apostrophe. "O'Brien" appeared on their editorial comment as "O'Brien", and the same corruption silently affected comment author email, URL, IP and user agent. The
WordPress.Security.ValidatedSanitizedInput.InputNotSanitizedphpcs notice on the two$_SERVERlines had gradually cemented the belief thatesc_sql()was a sanitisation step; it is not.Removing
esc_sql()from all five fields is the correct fix. The two$_SERVERvalues retain a light sanitisation viasanitize_text_field( wp_unslash( ... ) )so phpcs's input-not-sanitised rule is satisfied without reintroducing the double escape. A regression test pins the behaviour: a user whose display name contains an apostrophe must have that apostrophe stored verbatim on their editorial comment.Test plan
composer test:integration— the newtest_insert_comment_does_not_double_escape_author_namecase passes, and the existing editorial-comments AJAX tests continue to pass