Opened 9 years ago
Last modified 4 weeks ago
#43946 new defect (bug)
Inconsistent encoding of comment_content
| Reported by: | leewillis77 | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Comments | Version: | 4.9.5 |
| Severity: | normal | Keywords: | needs-patch |
| Cc: | Focuses: |
Description
The contents of the comments.comment_content field in the WordPress database are stored inconsistently depending on whether the user creating the comment was logged in or not.
> select comment_ID, comment_content, comment_type from wp_comments where comment_ID > 7; +------------+-----------------------------------------------------+--------------+ | comment_ID | comment_content | comment_type | +------------+-----------------------------------------------------+--------------+ | 8 | This is a good & strong comment (comment, anon) | | | 9 | This is a good & strong comment (comment, auth) | | +------------+-----------------------------------------------------+--------------+
In the example above, comment 8 was left by an anonymous user, and comment 9 was left by an authenticated user. Notice how the comment_content field has been HTML-escaped for ID 8, but not for ID 9.
Steps to reproduce:
- Install a fresh copy of WordPress
- As the site administrator, navigate to the default "hello-world" post
- Leave a comment including characters that should be escaped when output to HTML, e.g. &
- Log out
- Navigate to the default "hello-world" post
- Leave a comment including characters that should be escaped when output to HTML, e.g. &
Compare the contents of the two comments in the wp_comments table.
Attachments (2)
Change History (17)
This ticket was mentioned in Slack in #core-comments by presskopp. View the logs.
9 years ago
#4
follow-up:
↓ 5
@
9 years ago
Thanks for the reproduction, and the proposed patch. However, I'm not sure that patch really addresses the issue. In general, best practice is to store content "raw" and escape it as appropriate on usage (bearing in mind that different output schemes will have different encoding needs, e.g. XML vs HTML vs JSON, so ideally any such patch should do that (ensuring that it *is* properly escaped on usage).
I also think any patch needs to deal somehow with legacy data.
#5
in reply to: ↑ 4
@
9 years ago
So, basically what you mean is "NOT" to escape while storing and rather keep it raw. Escape the value the way it is required when outputted. Meaning you want it to be other way round. Am I right?
Replying to leewillis77:
Thanks for the reproduction, and the proposed patch. However, I'm not sure that patch really addresses the issue. In general, best practice is to store content "raw" and escape it as appropriate on usage (bearing in mind that different output schemes will have different encoding needs, e.g. XML vs HTML vs JSON, so ideally any such patch should do that (ensuring that it *is* properly escaped on usage).
I also think any patch needs to deal somehow with legacy data.
#6
@
9 years ago
- Keywords reporter-feedback added
I tried to reproduce the same issue today with a fresh WP install and twentysebenteen activated. For both Logged-In and Non-Logged-In statuses, the& changed to & in the database. The way it store the data (for both user_id 0 and user_id > 0 ) today is different from yesterdaty! Straneg!!
The patch is not applied yet.
Here is a screenshot of the database records in wp_comments table.
https://tinyurl.com/y83q25ym
#7
@
9 years ago
Hmm, it depends what you write:
"This is great & awesome" < triggers the error
"This is great & awesome" < does not - the & in & does not get escaped
As a side affect, the latter comment also gets displayed in a browser as "This is great & awesome" rather than "This is great & awesome" - a prime example of why the current scenario is broken....
#8
@
13 months ago
- Keywords needs-testing added; reporter-feedback removed
Hey all,
I have just taken a look at this in 6.8 and this is still a persisting issue today. As of such I have modernised the original patch into a PR as this solves the issue absolutely perfectly, and then we can check this against the current test suite! Props to @subrataemfluence for the original patch!
I really don't believe there is much of a barrier to getting this into core, so hopefully we can get this over the line in the next release! 😃
This ticket was mentioned in PR #10374 on WordPress/wordpress-develop by @callumbw95.
13 months ago
#9
Update comment content to pass it through html_esc() regardless of being logged in / not.
Trac ticket: #43946
#10
@
13 months ago
- Keywords needs-patch added; has-patch removed
The issue is that when a user is logged-in, namely as an administrator, then they can do unfiltered_html. This means they can add arbitrary HTML. Even a non-administrator, however, is allowed to use a subset of HTML which is allow-listed by Kses. Passing all submitted comment text through esc_html() will break the ability for users to format any of their comments with HTML. So unfortunately the proposed patch isn't the right solution.
#11
@
13 months ago
If the comment needs to normalize entities like the ampersand before saving it to the database, wp_kses_normalize_entities() might fit.
#12
@
10 months ago
- Keywords needs-testing removed
Removing needs-testing - awaiting new patch.
@
3 months ago
Normalizes HTML entities in comment_content on save for users with the unfiltered_html capability (admin front-end, REST, XML-RPC), who currently bypass KSES entirely. Hooks wp_kses_normalize_entities on pre_comment_content at priority 9, ahead of wp_filter_kses (priority 10), so entity normalization happens once regardless of which path a comment takes. wp_insert_comment() remains unfiltered. Includes unit tests in tests/phpunit/tests/comment/encoding.php covering guest/subscriber/admin inserts, REST inserts, updates, idempotency, and preservation of allowed HTML for privileged users.
#13
@
3 months ago
The inconsistency is that pre_comment_content only normalizes
entities when KSES runs. Users with unfiltered_html skip KSES, so
the same text is stored as AT&T for guests and AT&T for
privileged REST/admin/XML-RPC inserts. I hooked
wp_kses_normalize_entities on pre_comment_content at priority 9 so
wp_filter_comment() is the single encoding path and it runs before
wp_filter_kses (priority 10). This matches what KSES already does,
so existing KSES comments do not change, valid entities are not
double-encoded, and wp_insert_comment() stays unfiltered.
Manual check as administrator (unfiltered_html) on trunk: with the
filter removed, front-end wp-comments-post.php stored
43946-BEFORE AT&T and REST POST /wp/v2/comments stored
43946-REST-BEFORE AT&T. With the filter at priority 9, the same
paths stored 43946-AFTER AT&T and 43946-REST-AFTER AT&T.
HEX of the ampersand is 26 before the fix and 26616D703B (&)
after.
This takes a different approach from the earlier 43946.diff, which
applied esc_html() directly inside wp_insert_comment(). That would
affect every caller of that low-level API (including programmatic
inserts that expect pre-sanitized content) and would break already-
allowed HTML for users with unfiltered_html, since esc_html()
encodes all tags rather than just normalizing stray ampersands.
Hooking wp_kses_normalize_entities on pre_comment_content keeps the
fix scoped to the same entry point wp_filter_comment() already uses,
so it only affects new/edited comment submissions, matches KSES's
existing normalization behavior, and leaves wp_insert_comment()
untouched.
Added unit tests in tests/phpunit/tests/comment/encoding.php.
#15
@
4 weeks ago
Tested the implementation in 43946.2.diff against trunk at 4b2b478948bb.
The ampersand discrepancy is addressed, but unconditional normalization introduces regressions for users with unfiltered_html. For example, JavaScript left && right becomes left && right inside <script>, and CSS string contents also change. HTML entities are not decoded in those contexts.
On this checkout, which enables the newer KSES parser, a guest comment containing ∳ also changes from displaying ∳ to displaying literal entity notation.
The existing WP_Test_REST_Comments_Controller::test_comment_roundtrip_as_superadmin() fails with the hook enabled. The supplied tests cover basic inserts and updates, but contain no REST/XML-RPC cases or repeated-save idempotency check.
I think this needs further design discussion before landing. Please add coverage for raw-text elements and HTML5 entities, and review the intended REST raw-content compatibility.
I confirm that following the steps mentioned I could reproduce the issue!