使用ajax加载post数据,并且在源中显示类别ID是不安全的

Is loading post data with ajax and having the category ID visible in the source unsafe?

本文关键字:显示 ID 不安全 post 加载 ajax 数据 使用      更新时间:2023-09-26

在WordPress中,我试图使用Ajax点击按钮从X类加载一些帖子。

为了知道从哪个帖子/类别加载,我在按钮上添加了一个"数据猫"属性,例如:

<a href="http://url-of-category-X" data-cat="123" class="loader">Category X</a>

然后,我有一个特殊的-file.php设置,它创建了一个new WP_Query来加载来自某个类别的帖子(无论文件中输入了什么类别ID)。

在Javascript中,我有这样的:

1- On '.loader' click use Ajax
2- get data-cat value
3- run special-file.php and feed it the data-cat value
4- get posts returned 
5- load post HTML data in the DOM 
6- End 

我开始怀疑这是否真的不安全,是否容易被黑客攻击?我的解决方案中有什么大的禁忌吗?

让我想知道的是,黑客可以使用firebug或类似工具编辑html,并将数据猫值更改为潜在的恶意内容,尽管我不知道输入恶意类别ID是否真的会造成任何伤害?

p.s special-file.php看起来像这样:

$thequery = new WP_Query(  array( 'cat' => $cat_id,  'post_status' => 'publish',  'posts_per_page' => 4,  'ignore_sticky_posts'=> 1 ) );
foreach post in $thequery
echo the post title
echo the featured image
end foreach  
wp_reset_postdata();

所以你只需要输入类别ID,它就会使用WordPress的基本函数(比如_title();以获得帖子标题等)。

只需确保始终彻底清除发布的值,例如:

$cat = isset( $_POST['cat'] ) && $_POST['cat'] ? intval( $_POST['cat'] ) : 0;

问题不是暴露data-cat。您需要关注的是清除从用户那里获得的任何请求数据。这意味着在将data-cat值插入数据库查询之前,请检查该值是否安全。