Skip to content

Use esc_url_raw() for avatar URLs in AJAX responses#4903

Open
GaryJones wants to merge 1 commit intobuddyboss:releasefrom
GaryJones:fix/avatar-ajax-url-encoding
Open

Use esc_url_raw() for avatar URLs in AJAX responses#4903
GaryJones wants to merge 1 commit intobuddyboss:releasefrom
GaryJones:fix/avatar-ajax-url-encoding

Conversation

@GaryJones
Copy link
Copy Markdown

Problem

The avatar upload and crop AJAX handlers in bp-core/bp-core-avatars.php use esc_url() to encode the avatar URL before returning it in a JSON response.

esc_url() is designed for HTML output contexts — it encodes ampersands as &, strips certain characters, and performs other transformations appropriate for embedding URLs in HTML attributes. When the URL is returned in a JSON response for JavaScript consumption, these transformations corrupt the URL. Query string parameters with & become &, and certain valid URL characters may be stripped entirely.

Fix

Switch both occurrences to esc_url_raw(), which performs the same sanitisation (protocol validation, invalid character removal) without the HTML-specific encoding. This is the correct function for URLs that will be used programmatically rather than output in HTML.

This follows the WordPress Codex guidance: use esc_url() for HTML output, esc_url_raw() for database storage, redirects, and HTTP API usage.

Test plan

  • Upload a new avatar — verify the returned avatar URL is valid and the avatar displays correctly
  • Crop an existing avatar — verify the cropped avatar URL is valid
  • Test with an avatar URL containing query string parameters — verify ampersands are not double-encoded

The avatar upload and crop AJAX handlers use esc_url() to encode
the avatar URL before returning it in a JSON response. esc_url()
is designed for HTML output and encodes ampersands and other
characters that are valid in raw URLs, which can break the URL
when JavaScript consumes it.

Switch to esc_url_raw() which sanitises without encoding for
display context, preserving the URL for programmatic use.
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