繁体   English   中英

在WordPress中使用Ajax和update_post_meta()更新post_meta时,如何防止注入?

[英]How can i protect against injection when updating the post_meta using ajax and update_post_meta() in WordPress?

在WordPress中使用Ajax和update_post_meta()更新post_meta时,如何防止注入? 我担心我网站上的用户输入会使它容易受到注射和其他我无法想到的讨厌的东西的攻击。

我是WordPress开发和Web开发的新手。

我正在构建一个系统,用户可以登录并更新有关其业务的各种信息,然后将其保存到MySQL数据库(将来,我将从客户端应用程序使用WordPress REST API来获取此数据)。 因此,我构建了个人资料页面并为用户输入创建了表单,然后使用ajax和WordPress的update_post_meta()函数发送数据。 这是我的ajax回调:

/**
* AJAX Callback
* Always Echos and Exits
*/
function lla_update_profil_meta_callback() {

// Ensure we have the data we need to continue
if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {

    // If we don't - return custom error message and exit
    header( 'HTTP/1.1 400 Empty POST Values' );
    echo 'Could Not Verify POST Values.';
    exit;
}

// Get our    current post ID
$post_id        = lla_get_post_id();     
if($post_id != null){

    //get all the values from $_POST
    $um_val         = sanitize_text_field($_POST['kategori_id'] ) ;      
    $selected_term_data = get_term((int)$um_val);
    $new_post_title = sanitize_text_field($_POST['post_title']);

    //these are the values and im guessing it should be here i protect against injection?
    $new_beskrivelse = $_POST['beskrivelse'];
    $new_telefon = $_POST['telefon'];
    $new_email = $_POST['email'];
    $new_website = $_POST['website'];
    $new_vejnavn = $_POST['vejnavn'];
    $new_husnummer = $_POST['husnummer'];
    $new_salside = $_POST['salside'];
    $new_postnummer = $_POST['postnummer'];
    $new_by = $_POST['by'];


    update_post_meta( $post_id, 'kategori_id', $um_val);
    update_post_meta($post_id, 'beskrivelse', $new_beskrivelse); 
    update_post_meta($post_id, 'telefon', $new_telefon);
    update_post_meta($post_id, 'email', $new_email);
    update_post_meta($post_id, 'website', $new_website);
    update_post_meta($post_id, 'vejnavn', $new_vejnavn);
    update_post_meta($post_id, 'husnummer', $new_husnummer);
    update_post_meta($post_id, 'salside', $new_salside);
    update_post_meta($post_id, 'postnummer', $new_postnummer);
    update_post_meta($post_id, 'by', $new_by);

    //make sure the category chosen is updated in the backend aswell
    wp_set_object_terms($post_id, (int)$um_val, $selected_term_data->taxonomy, false);
    //updating post-title
    $my_post = array(
      'ID'           => $post_id,
      'post_title'   => $new_post_title,
      'post_name' => $new_post_title,
    );
    wp_update_post($my_post);

}else{
    echo "no post";
}
exit;
}
add_action( 'wp_ajax_nopriv_um_cb', 'lla_update_profil_meta_callback' );
add_action( 'wp_ajax_um_cb', 'lla_update_profil_meta_callback' );

如何使它更安全?

使用如下功能:

esc_sql();
esc_attr();
esc_html();

另请参阅验证清理和转义用户数据
例:

$new_beskrivelse = esc_sql($_POST['beskrivelse']);
$new_telefon = esc_sql($_POST['telefon']);
$new_email = esc_sql($_POST['email']);
$new_website = esc_sql($_POST['website']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM