Repository navigation
Fatal error in wp_insert_term() when the term name is not a string #13521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
0890e0c
0f1e84c
99cded9
10a1d92
a21ad67
402aaaa
f216122
5f65187
4188078
1f8bcc4
19ef32b
51ab927
f406733
cb770c2
88f20e1
7ac3e7e
7f728ca
7a115d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -374,6 +374,10 @@ public function login_pass_ok( | |
| * @param string|array $data Escape single string or array of strings. | ||
| * @return string|null Returns with string if passed, alters by-reference | ||
| * when array is passed. | ||
| * | ||
| * @phpstan-template T of string|array | ||
| * @phpstan-param T $data | ||
| * @phpstan-param-out T $data | ||
| */ | ||
| public function escape( &$data ) { | ||
| if ( ! is_array( $data ) ) { | ||
|
|
@@ -387,6 +391,8 @@ public function escape( &$data ) { | |
| $v = wp_slash( $v ); | ||
| } | ||
| } | ||
|
|
||
| // @phpstan-ignore paramOut.type (The loop only slashes string values in place, so the shape of T is preserved.) | ||
| return null; | ||
| } | ||
|
|
||
|
|
@@ -1658,7 +1664,11 @@ protected function _insert_post( $user, $content_struct ) { | |
| return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) ); | ||
| } | ||
|
|
||
| $term_ids = $post_data['terms'][ $taxonomy ]; | ||
| $term_ids = $post_data['terms'][ $taxonomy ]; | ||
| if ( ! is_array( $term_ids ) ) { | ||
| return new IXR_Error( 403, __( 'Invalid term ID.' ) ); | ||
| } | ||
|
|
||
| $terms[ $taxonomy ] = array(); | ||
| foreach ( $term_ids as $term_id ) { | ||
| $term = get_term_by( 'id', $term_id, $taxonomy ); | ||
|
|
@@ -1705,11 +1715,23 @@ protected function _insert_post( $user, $content_struct ) { | |
| // Filter out non-ambiguous term names. | ||
| $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one' ) ); | ||
|
|
||
| $ambiguous_terms = array_keys( $ambiguous_tax_term_counts ); | ||
| // Cast back to strings, since array keys turn numeric names into integers. | ||
| $ambiguous_terms = array_map( 'strval', array_keys( $ambiguous_tax_term_counts ) ); | ||
| } | ||
|
|
||
| $term_names = $post_data['terms_names'][ $taxonomy ]; | ||
| if ( ! is_array( $term_names ) ) { | ||
| return new IXR_Error( 401, __( 'Invalid term name.' ) ); | ||
| } | ||
|
|
||
| foreach ( $term_names as $term_name ) { | ||
| // Numeric names may arrive as <int> or <double> values. | ||
| if ( is_int( $term_name ) || is_float( $term_name ) ) { | ||
| $term_name = (string) $term_name; | ||
| } elseif ( ! is_string( $term_name ) ) { | ||
| return new IXR_Error( 401, __( 'Invalid term name.' ) ); | ||
| } | ||
|
|
||
| if ( in_array( $term_name, $ambiguous_terms, true ) ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Comment by Claude Opus 5.5 Ambiguous numeric term names cannot be detected here.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Comment by Claude Opus 5.5 Addressed in 5f65187. |
||
| return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) ); | ||
| } | ||
|
|
@@ -2198,6 +2220,10 @@ public function wp_newTerm( $args ) { | |
| // Hold the data of the term. | ||
| $term_data = array(); | ||
|
|
||
| if ( isset( $content_struct['name'] ) && ! ( is_string( $content_struct['name'] ) || is_int( $content_struct['name'] ) || is_float( $content_struct['name'] ) ) ) { | ||
| return new IXR_Error( 403, __( 'Invalid term name.' ) ); | ||
| } | ||
|
|
||
| $term_data['name'] = trim( $content_struct['name'] ); | ||
| if ( empty( $term_data['name'] ) ) { | ||
| return new IXR_Error( 403, __( 'The term name cannot be empty.' ) ); | ||
|
|
@@ -2316,6 +2342,10 @@ public function wp_editTerm( $args ) { | |
| } | ||
|
|
||
| if ( isset( $content_struct['name'] ) ) { | ||
| if ( ! ( is_string( $content_struct['name'] ) || is_int( $content_struct['name'] ) || is_float( $content_struct['name'] ) ) ) { | ||
| return new IXR_Error( 403, __( 'Invalid term name.' ) ); | ||
| } | ||
|
|
||
| $term_data['name'] = trim( $content_struct['name'] ); | ||
|
|
||
| if ( empty( $term_data['name'] ) ) { | ||
|
|
@@ -3541,6 +3571,10 @@ public function wp_newCategory( $args ) { | |
| return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a category.' ) ); | ||
| } | ||
|
|
||
| if ( isset( $category['name'] ) && ! ( is_string( $category['name'] ) || is_int( $category['name'] ) || is_float( $category['name'] ) ) ) { | ||
| return new IXR_Error( 403, __( 'Invalid term name.' ) ); | ||
| } | ||
|
|
||
| /* | ||
| * If no slug was provided, make it empty | ||
| * so that WordPress will generate one. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 Comment by Claude Opus 5.5
$term_namesitself is not checked to be an array. Sendingterms_names => array( 'post_tag' => 'foo' )makes theforeachemit a "foreach() argument must be of type array|object" warning, and the terms are silently ignored instead of returning an error. The$term_idsbranch has the same gap. Possibly out of scope, but it sits right next to the new per-item guard.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 Comment by Claude Opus 5.5
Addressed in cb770c2. Both the
termsandterms_namesbranches now check that the per-taxonomy value is an array, returning the existing "Invalid term ID." (403) or "Invalid term name." (401) error respectively, instead of emitting aforeachwarning and silently dropping the terms.test_terms_not_arraycovers a string and an int fortermsand a string forterms_names; all three cases hit the warning before the fix. Sincewp.editPostshares_insert_post(), it is covered too.