Taxonomy: Check for empty term after DB sanitization in wp_insert_term().

When inserting a new term in the database, `wp_insert_term()` will check if the term is empty and return a corresponding error.

Afterwards the term is sanitized and inserted in the database. However, there is a chance the term is empty after the DB sanitization.

This commit adds a check for an empty term name after the term is sanitized, returning an error in that case.

Follow-up to [5726], [8393].

Props fgiannar, kraftbj.
Fixes #59995.

git-svn-id: https://develop.svn.wordpress.org/trunk@57251 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2024-01-08 22:42:49 +00:00
parent 71cb3f8615
commit 868f2ef1fd
2 changed files with 18 additions and 0 deletions

View File

@@ -2434,6 +2434,11 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) {
$description = wp_unslash( $args['description'] );
$parent = (int) $args['parent'];
// Sanitization could clean the name to an empty string that must be checked again.
if ( '' === $name ) {
return new WP_Error( 'invalid_term_name', __( 'Invalid term name.' ) );
}
$slug_provided = ! empty( $args['slug'] );
if ( ! $slug_provided ) {
$slug = sanitize_title( $name );

View File

@@ -895,6 +895,19 @@ class Tests_Term_WpInsertTerm extends WP_UnitTestCase {
$this->assertSame( '', $term_object->description );
}
/**
* @ticket 59995
*/
public function test_wp_insert_term_with_empty_name_after_db_sanitization() {
$term = wp_insert_term(
'<script>onclick=alert("hello")</script>',
'post_tag'
);
$this->assertWPError( $term );
$this->assertSame( 'invalid_term_name', $term->get_error_code() );
}
/** Helpers */
public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term, $object_ids ) {