WordPress安全:禁用REST API功能,移除REST API链接

编辑于:2023年05月23日

WordPress安全:禁用REST API功能,移除REST API链接

使用 Chrome 浏览器检查 WordPress 页面的源代吗,你可能会发现一些类似的链接,这是 WordPress 默认开启的 REST API 自动生成的链接。它能够提供一些功能,但也会带来一些 WordPress安全 风险。

1. REST API介绍

1.1 功能作用

  • ⭕访问 WordPress 数据:REST API 可以访问 WordPress 的文章、页面、评论、用户、分类目录等数据,允许开发人员通过外部应用程序获取和管理这些数据。
  • ⭕扩展 WordPress 功能:REST API 可以为 WordPress 添加新的功能。例如,开发人员可以使用 REST API 创建自定义的 WordPress 插件和主题,添加自己的路由和控制器。
  • ⭕增加 WordPress 网站的互操作性:REST API 可以允许其他应用程序与 WordPress 网站交互。例如,开发人员可以使用 REST API 将 WordPress 与其他 CMS 平台和应用程序集成。

1.2 安全风险

  • ⭕恶意爬取:REST API 默认无权限公开数据,所有访问者都可以随意查看网站全部的文章、页面、评论、用户、分类目录等数据。
  • ⭕CSRF攻击:由于REST API使用基于Cookie的身份验证,攻击者可以使用跨站请求伪造(CSRF)攻击来伪造请求,执行未经授权的操作。
  • ⭕未经授权的数据访问:如果未正确配置REST API权限,攻击者可以访问并修改WordPress网站上的数据。
  • ⭕DDos攻击:攻击者可以使用REST API来进行分布式拒绝服务(DDoS)攻击,对WordPress网站造成影响。

2. 禁用或限制 REST API

对于不需要REST API功能或者想要对REST API做一些权限限制,如仅限网站管理员或登录用户使用,可以通过在 WordPress 网站的功能文件(functions.php)中添加以下代码实现。

2.1 彻底禁用 REST API:

add_filter('rest_authentication_errors', 'disable_rest_api');
function disable_rest_api($access) {
  return new WP_Error('rest_disabled', __('The REST API has been disabled.'), array('status' => 403));
}

上述代码通过将返回一个错误来阻止对 REST API 的所有请求。在这种情况下,无论用户是否登录或具有管理权限,都无法使用 REST API。

add_filter('rest_endpoints', 'remove_json_api');
function remove_json_api($endpoints) {
  if (!is_admin()) {
    unset($endpoints['/wp/v2/users']);
    unset($endpoints['/wp/v2/users/(?P<id>[\d]+)']);
    unset($endpoints['/wp/v2/comments']);
    unset($endpoints['/wp/v2/comments/(?P<id>[\d]+)']);
    unset($endpoints['/wp/v2/posts']);
    unset($endpoints['/wp/v2/posts/(?P<id>[\d]+)']);
    unset($endpoints['/wp/v2/pages']);
    unset($endpoints['/wp/v2/pages/(?P<id>[\d]+)']);
  }
  return $endpoints;

此外,作为一个可选项,你还使用rest_endpoints过滤器来,对非管理员身份,从 REST API 中删除默认路由。这些路由包括:

  • ⭕/wp-json/wp/v2/posts
  • ⭕/wp-json/wp/v2/pages
  • ⭕/wp-json/wp/v2/comments
  • ⭕/wp-json/wp/v2/users

2.2 除管理员身份外禁用 REST API

add_filter('rest_authentication_errors', 'disable_rest_api_except_admins');
function disable_rest_api_except_admins($access) {
  if (!current_user_can('manage_options')) {
    return new WP_Error('rest_disabled', __('The REST API has been disabled.'), array('status' => 403));
  }
  return $access;
}

上述代码通过使用 current_user_can('manage_options')函数检查用户是否具有管理员权限来限制只有管理员才能访问 REST API。其他用户将收到一个错误消息,无法访问 REST API。

2.3 除登录用户外禁用REST API

add_filter('rest_authentication_errors', 'disable_rest_api_except_logged_in');
function disable_rest_api_except_logged_in($access) {
  if (!is_user_logged_in()) {
    return new WP_Error('rest_disabled', __('The REST API has been disabled.'), array('status' => 403));
  }
  return $access;
}

上述代码通过使用is_user_logged_in()函数检查用户是否已登录来限制只有登录用户才能访问REST API。未登录的用户将收到一个错误消息,无法访问REST API。

3 移除前台页面REST API的链接

在文章开头我们谈到了WordPress会在网站源码的头部显示REST API的链接,既然我们不需要或者对此功能做了权限限制,那么不如直接禁止在前台输出与REST API相关的链接。

remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action('template_redirect', 'rest_output_link_header', 11);

上述代码使用remove_action()函数来移除 REST API 链接。

  • ⭕第一行代码使用rest_output_link_wp_head动作来移除在 HTML 头部中添加的 REST API 链接。
  • ⭕第二行代码使用wp_oembed_add_discovery_links动作来移除 oEmbed API 的链接。
  • ⭕第三行代码使用rest_output_link_header动作来移除在 HTTP 标头中添加的 REST API 链接。

将上述代码添加到您的网站的主题功能文件(functions.php)中,即可移除 WordPress REST API 链接。请注意,在移除 REST API 链接之前,请务必确保了解其影响,并进行备份。

相关推荐

暂无评论