Skip to content

Conversation

@pbearne
Copy link

@pbearne pbearne commented Jul 29, 2025

Paul Bearne and others added 5 commits February 26, 2025 18:20
The primary goal of this diff is to enhance the `sanitize_term()` function and related code to make the sanitization of term data more robust, consistent, and efficient. It also fixes several bugs and edge cases in how terms were sanitized in various contexts.

**Key Changes**

1.  **`sanitize_term()` Enhancements:**
    *   **Context-Specific Sanitization:** The core of the changes revolves around ensuring that terms are correctly sanitized based on the specified `$context` ('edit', 'db', 'display', 'rss', etc.).
    *   **Filter Tracking:** A new `filter` property is added to the term object (or array) to keep track of the context in which it was last sanitized. This helps prevent redundant sanitization and ensures the correct filters are applied.
    * **Object/Array Handling** The code has been updated to handle both array and object more carefully.
    *   **Raw Context is now handled by WP_Term:** The new function `WP_Term->filter('raw')` now properly returns the raw term data.
    * **Slug Sanitization**: The slug is now sanitized using sanitize_title in multiple contexts.
    * **Description Sanitization in 'db' context:** The Description is now more carefully sanitized in 'db' context, it is not simply stripped of HTML, but content in scripts is removed.
    * **Sanitization in display context:** The code now escapes html when using the display context.
    * **Sanitization in 'rss' context:** The code has been modified to stop stripping html when in rss context.
    * **Improved Code Clarity:** The code within `sanitize_term()` and `sanitize_term_field()` is more organized and easier to understand.

2.  **`sanitize_term_field()` Refinements:**
    *   **Context-Aware Filters:** The logic for applying filters in `sanitize_term_field()` is improved to apply the appropriate filters for each context.
    *   **'db' Context:** The 'db' context now more aggressively strips HTML and potentially harmful content.
    *   **'edit' Context:** The 'edit' context HTML-encodes data to make it safe for display in input fields.
    *   **'display' Context:** Now does html escaping.
    * **Slug Sanitization** The slug is now sanitized in all contexts, which should help prevent a wide range of unexpected data.
    * **Rss Context**: Html is not stripped when in this context.

3.  **`WP_Term` Class Changes:**
    *   **`filter()` Method:** The `WP_Term` class now has a dedicated `filter()` method to apply sanitization to a `WP_Term` object.
    *   **`get_instance()` Improvements:** The logic in `get_instance()` is updated to handle term objects with different filter states.
    * **Filter Property**: This property now accurately reflects the state of the object.

4.  **New Unit Tests:**
    *   **`Tests_Term_SanitizeTerm`:** A comprehensive test suite is added to cover various sanitization scenarios, including different contexts, input types, and potential edge cases. This ensures that the changes are well-tested and don't introduce regressions.

5.  **Deprecation of `sanitize_category` and `sanitize_category_field`:**
    *   These functions are now deprecated in favor of the more general `sanitize_term` and `sanitize_term_field`.
    * The functions have been moved to deprecated.php.

6. **Remove redundant sanitization**: `WP_Term->filter()` now checks the current filter, to ensure that the same filters are not applied multiple times.

7. **Fix bug**: An edge case where the slug was not being sanitized when dealing with objects.

**Potential Implications**

*   **Enhanced Security:** The more rigorous sanitization, especially in the 'db' and 'edit' contexts, significantly improves security by preventing XSS vulnerabilities and data corruption.
*   **Improved Data Integrity:** Sanitizing the slug more thoroughly across contexts enhances data integrity and consistency.
*   **Less Unexpected Behavior:** The clear differentiation between contexts should result in more predictable behavior when working with term data.
*   **Code Maintainability:** The code structure is improved, making it easier to maintain and extend in the future.
*   **Breaking changes:** Deprecating `sanitize_category` and `sanitize_category_field` means that developers will need to start updating their code to use `sanitize_term` and `sanitize_term_field`.

**Code Review Notes**

*   **Unit Tests are Excellent:** The new unit tests are thorough and comprehensive. This is a huge plus for ensuring code quality.
*   **Code Clarity:** The code is generally well-structured and readable.
* **Slug Sanitization**: The decision to sanitize the slug in multiple contexts is a good one.
* **Description Sanitization**: The changes made to the sanitization of the description is a big improvement.
* **Rss context:** The changes made to the sanitization in the rss context is a big improvement.

**In summary**

This diff is a substantial improvement to term sanitization in WordPress. It addresses several security concerns, enhances data integrity, and makes the code more robust and maintainable. The added unit tests are a significant contribution to the project. I highly recommend merging these changes.
# Conflicts:
#	src/wp-includes/deprecated.php
…tion-calls' into 50568-Improve-WP_Term's-sanitization-calls

# Conflicts:
#	src/wp-includes/deprecated.php
@github-actions
Copy link

github-actions bot commented Jul 29, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @[email protected].

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

Core Committers: Use this line as a base for the props when committing in SVN:

Props pbearne, rollybueno.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@rollybueno
Copy link

Most of failed checks here are about unit testing..

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