Skip to content

Sitemaps: Remove characters that XML doesn't allow - #55616

Open
kraftbj wants to merge 2 commits into
trunkfrom
fix/sitemap-invalid-xml-characters
Open

kraftbj wants to merge 2 commits into
trunkfrom
fix/sitemap-invalid-xml-characters

Conversation

@kraftbj

@kraftbj kraftbj commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #55582

Proposed changes

  • Remove characters XML 1.0 doesn't allow from sitemap text values before writing them. Escaping can't make these characters valid, so one bad filename or title could make search engines reject the whole file.
  • Replace malformed UTF-8 with U+FFFD, using core's wp_scrub_utf8(). It also makes the file unparseable.
  • Do this in all three writers: XMLWriter, DOMDocument, and the string fallback (which the issue doesn't mention).

If a URL really contains one of these characters, the cleaned-up URL may 404 or, rarely, match a different file. That affects one entry instead of the whole file.

Core doesn't have a function that does all of this. wp_scrub_utf8() fixes malformed UTF-8 but keeps these characters. wp_kses_no_null() keeps U+FFFE and U+FFFF, and by default also removes some valid text. esc_xml() keeps control characters, and its escaping would be doubled by the sitemap writers.

The reported failure depends on the server's libxml2 version. 2.9.14 writes control characters as is, while 2.15.2 (in the current dev container) replaces them with �. 2.15.2 still writes U+FFFE and malformed UTF-8 as is, and still cuts values off at a null byte. The new test compares the parsed values, so it also fails without the fix on newer versions.

Related product discussion/links

  • None beyond the issue.

Does this pull request change what data or activity we track or use?

No.

Testing instructions

Use a site whose PHP uses libxml2 2.9.x to see the reported failure. Check with php -r 'echo LIBXML_DOTTED_VERSION;'.

  • Turn on SEO and Sitemaps (Sitemaps doesn't load while SEO is off), and make sure search engines are allowed to index the site.
  • Publish a post and upload two images attached to it.
  • Put a control character in one image's filename: wp post meta update <attachment_id> _wp_attached_file "2019/07/test$(printf '\001').jpg"
  • Run wp jetpack sitemap rebuild --purge.
  • Run curl -s https://example.com/image-sitemap-1.xml | xmllint --noout -, using your site's URL.
  • On trunk, parsing fails with PCDATA invalid Char value 1. On this branch, it passes. Both images are listed, and the changed one ends in test.jpg.
  • To check the DOMDocument writer, add add_filter( 'jetpack_sitemap_use_xmlwriter', '__return_false' );, then rebuild and parse again.

I ran these steps on WordPress 7.1.5 with PHP 8.4.27 and libxml2 2.9.14, parsing with PHP instead of xmllint. XMLWriter failed before the fix and passed after it. It also handled a post title containing U+0001 in the news sitemap (checked after the fix only). I only checked the DOMDocument writer after the fix. The string fallback is covered by unit tests only.

Escaping can't make these characters valid, so one bad value made a whole sitemap file unparseable. Clean text values in all three buffer types before writing them.
@kraftbj kraftbj added Bug When a feature is broken and / or not performing as intended [Status] Needs Review This PR is ready for review. [Feature] Sitemaps labels Nov 30, 2026
@kraftbj kraftbj self-assigned this Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/sitemap-invalid-xml-characters branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/sitemap-invalid-xml-characters

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions github-actions Bot added [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests labels Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: December 8, 2026
    • Code freeze: December 7, 2026

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@jp-launch-control

jp-launch-control Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/modules/sitemaps/sitemap-constants.php 5/77 (6.49%) 6.49% 0 💚
projects/plugins/jetpack/modules/sitemaps/sitemap-buffer-fallback.php 29/42 (69.05%) 69.05% -29 💚

Full summary · PHP report · JS report

Matching bytes in scrubbed text doesn't depend on PCRE's Unicode support. Image, video and news values take a separate path in the DOMDocument buffer that the test didn't cover.
@kraftbj kraftbj added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels Nov 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug When a feature is broken and / or not performing as intended [Feature] Sitemaps [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] Ready to Merge Go ahead, you can push that green button! [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sitemaps: control characters in values are written unescaped, making the whole sitemap file invalid XML

1 participant