Repository navigation
Encode user information attribute in user selector - #177
Conversation
|
No actionable comments were generated in the recent review. WalkthroughThe full-user-information option now applies JSON hex encoding flags and ChangesUser selector output
Estimated code review effort: 1 (Trivial) | ~3 minutes Change: Bug fix Suggested reviewers: Merge Risk:
|
There was a problem hiding this comment.
Approve with 1 non-blocking note. The one-line change fixes the single-quote breakout in data-user-information and I found no way around it.
What the change does: the old markup put wp_json_encode( $user ) inside a single-quoted attribute, and wp_json_encode does not escape '. The new markup uses a double-quoted attribute, esc_attr(), and the JSON_HEX_* flags, so quotes, <, > and & leave the JSON as \uXXXX escapes. $user->ID, the selected check and the option text are untouched. php -l is clean. The consumers ($user.data( 'user-information' ) in order-section-controller.js and edit-post.js, and JSON.parse( attr(...) ) in users-dropdown.js) read the attribute value after the browser has decoded it, so they get the same JSON as before. The sibling html-widget-users-dropdown.tpl.php already used esc_attr( wp_json_encode( $user ) ) in double quotes, so the two templates now agree.
Live before/after: I built both versions of the option tag for a user whose display_name is x' onmouseover='alert(1)' data-x=' and whose email contains ", <script> and &, then parsed the HTML with Python's HTML parser and decoded the attribute as JSON. Old: the parser sees extra onmouseover and data-x attributes on the option, and the JSON fails to parse (unterminated string). New: only value and data-user-information exist, and the JSON parses back to the exact original display name and email. Limits: this used a 3-line PHP stand-in for esc_attr (htmlspecialchars with ENT_QUOTES, no double-encode, which is what WordPress does) and plain json_encode, not a booted WordPress, and no browser. I did not see the Patchstack report (the awpcp issue only links it), so I can only say this closes the single-quote breakout, not that it covers everything the report describes.
CI at this head: PHPUnit (PHP 7.4 and 8) and PHPCS passed.
Non-blocking: the PR adds no test, and this is a security fix. tests/suite has a test for the sibling users-dropdown widget but none for components/user-selector.tpl.php. A test that renders the template with a user whose display_name is x' onmouseover='alert(1)' and asserts the output has no raw ' or < inside the attribute would keep this from regressing.
Not run: browser, the plugin's own PHPUnit suite locally (CI ran it), code-review / security-review / simplify skills (I read the whole one-line diff).
Fixes Strategy11/awpcp#3211
Encode the user information JSON on the user selector options so profile values with quotes or entities are output and parsed correctly.