Opened 14 months ago
Last modified 4 weeks ago
#60347 new defect (bug)
wp_kses breaking text fragments links
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Formatting | Keywords: | good-first-bug has-patch has-unit-tests needs-testing |
Focuses: | Cc: |
Description
Hello.
It seems that wp_kses() (probably wp_kses_bad_protocol()) is breaking text fragments links (https://developer.mozilla.org/en-US/docs/Web/Text_fragments).
For example:
<a href="#:~:text=highlight>Link</a>
This issue became more prominent as recently ACF started escaping HTML using the wp_kses() function (https://www.advancedcustomfields.com/blog/acf-6-2-5-security-release/).
I confirmed the issue with ACF's support.
Attachments (1)
Change History (9)
#3
@
4 months ago
- Component changed from Security to Formatting
- Keywords needs-patch needs-unit-tests good-first-bug added
@
4 months ago
This patch fixes the issue where kses removes text fragment links (e.g., #:~:text) as invalid protocols. The fix adds a check in wp_kses_bad_protocol to allow such fragments. The patch also includes unit tests to verify the behavior.
#4
follow-up:
↓ 5
@
4 months ago
That's great. Thanks, @shanemuir.
Will this be a part of the next release?
#5
in reply to:
↑ 4
@
4 months ago
Replying to asafm7:
That's great. Thanks, @shanemuir.
Will this be a part of the next release?
The patch will first need to undergo a review. If no issues are identified, it will then be assigned to a milestone, which will determine the future release in which this patch will be included.
This ticket was mentioned in PR #7890 on WordPress/wordpress-develop by @shanemuir.
4 months ago
#7
This PR addresses the issue in Trac ticket #60347, where wp_kses() removes valid text fragment links (e.g., #:~:text=highlight) by treating them as invalid protocols.
Changes:
- Updated wp_kses_bad_protocol() in kses.php to explicitly allow text fragments (#:~:text).
- Added a unit test to verify that text fragments are preserved in the href attribute.
Testing:
- Run npm run test:php to confirm all tests pass.
- Apply the patch and manually test with an anchor tag containing #:~:text in the href attribute.
- Example: <a href="#:~:text=highlight">Link</a> should pass validation.
Impact:
This change ensures that text fragment links are correctly preserved by wp_kses(), aligning with modern URL standards and avoiding unnecessary sanitization.
Trac ticket: https://core.trac.wordpress.org/ticket/60347
I forgot to mention that it only happens to relative links, without a protocol.
Like in the example I provided:
<a href="#:~:text=highlight>Link</a>