Skip to content

Commit 0d59cc4

Browse files
committed
Code Quality: Short-circuit Ajax handlers when the supplied post does not exist.
`get_post()` returns `null` for a nonexistent post, but four Ajax handlers dereferenced the result without checking: `wp_ajax_add_meta()`, `wp_ajax_inline_save()`, `wp_ajax_save_attachment()`, and `wp_ajax_save_attachment_compat()`. Each now bails when `null` is returned. Since these functions return `never`, subsequent references to `$post` do not cause static analysis errors. In `wp_ajax_add_meta()` the check is folded into the existing capability check, since `$post` is only dereferenced in the branch that adds meta. These paths are unreachable in core, as `current_user_can( 'edit_post', ... )` already blocks for a nonexistent post. The guards are defensive as a plugin filtering capabilities could still reach them, and they give static analysis the non-null narrowing it needs. Developed as subset of WordPress#12488. Follow-up to r62704, r62703. See #64898. git-svn-id: https://develop.svn.wordpress.org/trunk@62705 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1e521e7 commit 0d59cc4

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/wp-admin/includes/ajax-actions.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,7 @@ function wp_ajax_add_meta() {
16211621
$post = get_post( $post_id );
16221622

16231623
if ( isset( $_POST['metakeyselect'] ) || isset( $_POST['metakeyinput'] ) ) {
1624-
if ( ! current_user_can( 'edit_post', $post_id ) ) {
1624+
if ( ! $post || ! current_user_can( 'edit_post', $post_id ) ) {
16251625
wp_die( -1 );
16261626
}
16271627

@@ -2106,6 +2106,9 @@ function wp_ajax_inline_save() {
21062106
$data = &$_POST;
21072107

21082108
$post = get_post( $post_id, ARRAY_A );
2109+
if ( ! $post ) {
2110+
wp_die();
2111+
}
21092112

21102113
// Since it's coming from the database.
21112114
$post = wp_slash( $post );
@@ -3134,6 +3137,9 @@ function wp_ajax_save_attachment() {
31343137

31353138
$changes = $_REQUEST['changes'];
31363139
$post = get_post( $id, ARRAY_A );
3140+
if ( ! $post ) {
3141+
wp_send_json_error();
3142+
}
31373143

31383144
if ( 'attachment' !== $post['post_type'] ) {
31393145
wp_send_json_error();
@@ -3225,6 +3231,9 @@ function wp_ajax_save_attachment_compat() {
32253231
}
32263232

32273233
$post = get_post( $id, ARRAY_A );
3234+
if ( ! $post ) {
3235+
wp_send_json_error();
3236+
}
32283237

32293238
if ( 'attachment' !== $post['post_type'] ) {
32303239
wp_send_json_error();

0 commit comments

Comments
 (0)