51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

wordpress屏蔽REST API防止用户泄露

对于wordpress来说,很多站长都不陌生,但是你是否留意过REST API,具体REST API核心用途可分为以下几个层面。

一、远程数据交互

通过JSON格式实现跨平台数据操作,如内容获取

文章/页面:/wp/v2/posts、/wp/v2/pages
分类/标签:/wp/v2/categories、/wp/v2/tags
媒体文件:/wp/v2/media
用户数据:/wp/v2/users

二、古腾堡编辑器依赖

古腾堡编辑器依赖REST API实现实时预览和内容保存

三、多平台应用开发

小程序、APP、跨系统集成,依赖REST API获取WordPress内容等

接下来说下REST API是如何泄露用户信息的

访问https://域名/wp-json/wp/v2/users/

例如:https://www.whsir.com/wp-json/wp/v2/users/

可以看到如下类似信息,当前wordress中所有用户名都显示了出来,这样对于安全性有着极大隐患。


解决此问题需要禁用REST API或相关地址,但是需要注意,如果完全禁用REST API会导致古腾堡编辑器失效,依赖API的插件可能崩溃等问题,请做好充分测试。

方法一:通过代码方式,普通用户登录可访问该接口

在wp-content/themes主题目录下编辑functions.php文件
//禁用未登录用户访问REST API add_filter('rest_authentication_errors', function ($result) { if (!is_user_logged_in()) { return new WP_Error( 'rest_unauthorized', __('You must be logged in to access the API'), ['status' => 401] ); } return $result; }, 10, 1);

|-------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 | //禁用未登录用户访问REST API add_filter('rest_authentication_errors', function ($result) { if (!is_user_logged_in()) { return new WP_Error( 'rest_unauthorized', __('You must be logged in to access the API'), ['status' => 401] ); } return $result; }, 10, 1); |

方法二:通过代码方式,仅管理员用户登录可访问该接口

在wp-content/themes主题目录下编辑functions.php文件
//禁用非管理员用户访问REST API add_filter('rest_authentication_errors', function ($result) { if (!is_user_logged_in() || !current_user_can('administrator')) { return new WP_Error( 'rest_api_disabled', __('REST API is disabled for non-administrators', 'whsir'), ['status' => 403] ); } return $result; }, 10, 1);

|-------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 | //禁用非管理员用户访问REST API add_filter('rest_authentication_errors', function ($result) { if (!is_user_logged_in() || !current_user_can('administrator')) { return new WP_Error( 'rest_api_disabled', __('REST API is disabled for non-administrators', 'whsir'), ['status' => 403] ); } return $result; }, 10, 1); |

方法三:通过nginx反向代理规则屏蔽

通过nginx规则屏蔽所有以/wp-json/wp/v2/users/开头的URI地址
location ~* ^/wp-json/wp/v2/users { # 拦截所有HTTP方法 if ($request_method !~ ^(GET|POST|HEAD)$ ) { return 405; } deny all; return 403; }

|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 | location ~* ^/wp-json/wp/v2/users { # 拦截所有HTTP方法 if ($request_method !~ ^(GET|POST|HEAD)$ ) { return 405; } deny all; return 403; } |

方法四:通过安装插件方式

可在插件中心安装"禁用WP REST API"插件


赞(2)
未经允许不得转载:工具盒子 » wordpress屏蔽REST API防止用户泄露