Eri*_*pak 22 security rest wordpress
我正在考虑提高我的Wordpress网站的安全性,并且这样做已经遇到了默认启用的WP REST API(因为WP 4.4如果我没有记错的话).
什么是禁用它的安全方法?
这里的"安全"是指它不会引起意外的副作用,例如不会破坏任何其他WP核心功能.
一种可能的方法是使用.htaccess重写规则,但令人惊讶的是我没有找到任何"正式"指令.
非常感谢任何帮助或建议:)
更新: 第三方插件不是我要找的解决方案.虽然我知道有很多可以解决这个问题的任务,但它们包含了许多减慢网站速度的额外功能.我希望有一个解决这个问题的单行解决方案,而不需要额外插件的开销.
更新2: 以下是Wordpress的官方意见:https://developer.wordpress.org/rest-api/using-the-rest-api/frequently-asked-questions/#can-i-disable-the-rest- API
据此,Wordpress团队希望未来的WP功能依赖于新的REST API.这意味着没有保证安全的方法来禁用REST API.
我们希望有足够的安全专家来处理WP安全问题.
更新3:
WordPress API手册中提供了一种解决方法 - 您可以要求对所有Reque sts进行身份验证
这样可以确保禁用对您网站的REST API的匿名访问,只有经过身份验证的请求才有效.
小智 16
从作者原始问题我选择了来自wordpress官方建议的选项2(https://developer.wordpress.org/rest-api/using-the-rest-api/frequently-asked-questions/#can-i -disable-the-rest-api).所以只需输入你的functions.php就可以让只有登录的用户使用其余的api:
add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
if ( ! is_user_logged_in() ) {
return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) );
}
return $result;
});
Run Code Online (Sandbox Code Playgroud)
Luc*_*nte 12
您可以在functions.php中为localhost以外的请求禁用它:
function restrict_rest_api_to_localhost() {
$whitelist = [ '127.0.0.1', "::1" ];
if( ! in_array($_SERVER['REMOTE_ADDR'], $whitelist ) ){
die( 'REST API is disabled.' );
}
}
add_action( 'rest_api_init', 'restrict_rest_api_to_localhost', 0 );
Run Code Online (Sandbox Code Playgroud)
Kuz*_*eko 10
毕竟,禁用REST API并不是一个坏主意.它实际上在所有网站上都打开了一个大洞!
在wordpress 4.4中有一种方法
在这里,我找到了一个可能的解决方案,.htaccess 但是应该结合.htaccess文件中的其他内容仔细测试(例如,wordpress本身添加的漂亮网址规则):
# WP REST API BLOCK JSON REQUESTS
# Block/Forbid Requests to: /wp-json/wp/
# WP REST API REQUEST METHODS: GET, POST, PUT, PATCH, DELETE
RewriteCond %{REQUEST_METHOD} ^(GET|POST|PUT|PATCH|DELETE) [NC]
RewriteCond %{REQUEST_URI} ^.*wp-json/wp/ [NC]
RewriteRule ^(.*)$ - [F]
Run Code Online (Sandbox Code Playgroud)
一个非常激烈的方法,也是404.html在你的根目录中有一个网页然后添加这一行:
# WP REST API BLOCK JSON REQUESTS
# Redirect to a 404.html (you may want to add a 404 header!)
RewriteRule ^wp-json.*$ 404.html
Run Code Online (Sandbox Code Playgroud)
请注意,除非您使用静态页面,即不涉及wordpress函数,否则如果您想404使用适当的错误页面返回错误,这是一个完全独立的主题,当涉及Wordpress时会出现很多问题
小智 7
接受的答案会禁用来自未经身份验证的用户的所有 API 调用,但现在很多插件都依赖于该 API 的功能。
禁用所有调用将导致意外的站点行为,在我使用此代码时也会发生这种情况。
例如,ContactForm7 使用此 API 将联系信息发送到 DB(我认为)和 ReCaptcha 验证。
我认为最好为未经身份验证的用户禁用一些(默认)端点,如下所示:
// Disable some endpoints for unauthenticated users
add_filter( 'rest_endpoints', 'disable_default_endpoints' );
function disable_default_endpoints( $endpoints ) {
$endpoints_to_remove = array(
'/oembed/1.0',
'/wp/v2',
'/wp/v2/media',
'/wp/v2/types',
'/wp/v2/statuses',
'/wp/v2/taxonomies',
'/wp/v2/tags',
'/wp/v2/users',
'/wp/v2/comments',
'/wp/v2/settings',
'/wp/v2/themes',
'/wp/v2/blocks',
'/wp/v2/oembed',
'/wp/v2/posts',
'/wp/v2/pages',
'/wp/v2/block-renderer',
'/wp/v2/search',
'/wp/v2/categories'
);
if ( ! is_user_logged_in() ) {
foreach ( $endpoints_to_remove as $rem_endpoint ) {
// $base_endpoint = "/wp/v2/{$rem_endpoint}";
foreach ( $endpoints as $maybe_endpoint => $object ) {
if ( stripos( $maybe_endpoint, $rem_endpoint ) !== false ) {
unset( $endpoints[ $maybe_endpoint ] );
}
}
}
}
return $endpoints;
}
Run Code Online (Sandbox Code Playgroud)
有了这个,现在唯一打开的端点是插件安装的端点。
有关您站点上活动的端点的完整列表,请参阅 https://YOURSITE.com/wp-json/
随意$endpoints_to_remove根据您的要求编辑数组。
如果您有自定义帖子类型,请确保也将它们全部添加到列表中。
就我而言,我还将默认端点前缀从更改wp-json为mybrand-api。这应该对发出数千个蛮力请求的机器人起到威慑作用。
这是我所做的:
// Custom rest api prefix (Make sure to go to Dashboard > Settings > Permalinks and press Save button to flush/rewrite url cache )
add_filter( 'rest_url_prefix', 'rest_api_url_prefix' );
function rest_api_url_prefix() {
return 'mybrand-api';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20813 次 |
| 最近记录: |