亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

安全性禁用 WP REST API
P粉463811100
P粉463811100 2024-01-10 16:27:31
0
2
743

我正在考慮提高我的 Wordpress 網(wǎng)站的安全性,在這樣做時(shí)發(fā)現(xiàn)預(yù)設(shè)啟用了 WP REST API(如果我沒記錯(cuò)的話,從 WP 4.4 開始)。

停用它的安全方法是什麼?

這裡的「安全」是指它不會(huì)導(dǎo)致意外的副作用,例如不會(huì)破壞任何其他 WP 核心功能。

一種可能的方法是使用 .htaccess 重寫規(guī)則,但令人驚訝的是我沒有找到任何這樣做的「官方」說明。

非常感謝任何幫助或建議:)

更新: 第三方插件不是我正在尋找的解決方案。儘管我知道有很多可以解決該任務(wù)的工具,但它們包含許多會(huì)減慢網(wǎng)站速度的額外功能。我希望有一個(gè)單行解決方案可以解決這個(gè)問題,而無需額外的插件開銷。

更新 2: 這是Wordpress的官方意見:https://developer.wordpress.org/rest-api/using-the-rest-api/frequently-asked-questions/#can-i-disable-the-rest-api

據(jù)此,Wordpress 團(tuán)隊(duì)希望未來的 WP 功能能依賴新的 REST API。這意味著沒有保證安全性的方法來停用 REST API。

我們只是希望有足夠的安全專家來負(fù)責(zé) WP 的安全。

更新3:

#

WordPress API 手冊(cè)中提供了一種解決方法 - 您可以要求對(duì)所有請(qǐng)求進(jìn)行身份驗(yàn)證

這可確保停用對(duì)網(wǎng)站 REST API 的匿名訪問,只有經(jīng)過驗(yàn)證的請(qǐng)求才有效。

P粉463811100
P粉463811100

全部回覆(2)
P粉478445671

接受的答案會(huì)停用未經(jīng)身份驗(yàn)證的使用者的所有 API 呼叫,但現(xiàn)在許多插件都依賴此 API 的功能。

停用所有呼叫將導(dǎo)致意外的網(wǎng)站行為,在我使用此程式碼時(shí)也發(fā)生過這種情況。

例如,ContactForm7 使用此 API 將聯(lián)絡(luò)資訊傳送到資料庫(我認(rèn)為)並進(jìn)行 ReCaptcha 驗(yàn)證。

我認(rèn)為最好為未經(jīng)身份驗(yàn)證的用戶禁用某些(預(yù)設(shè))端點(diǎn),如下所示:

// 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;
}

這樣,現(xiàn)在開啟的唯一端點(diǎn)是由外掛程式安裝的端點(diǎn)。

有關(guān)您網(wǎng)站上活動(dòng)的端點(diǎn)的完整列表,請(qǐng)參閱https://YOURSITE.com/wp-json/

#

您可以根據(jù)您的要求隨意編輯 $endpoints_to_remove 陣列。

如果您有自訂貼文類型,請(qǐng)確保將它們?nèi)啃略龅角鍐沃小?

就我而言,我還將預(yù)設(shè)端點(diǎn)前綴wp-json 更改為 mybrand-api。這應(yīng)該會(huì)對(duì)發(fā)出數(shù)千個(gè)暴力請(qǐng)求的機(jī)器人起到威懾作用。

這是我所做的:

// 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';
}
P粉512363233

根據(jù)作者最初的問題,我選擇了來自WordPress 官方建議的選項(xiàng)2(https://developer.wordpress.org/rest-api/using-the-rest-api/frequently-asked-questions /#can-i-disable-the-rest-api)。因此,只需放入你的functions.php,只讓登入的用戶使用其餘的API(但只需交叉檢查原始鏈接,以防我的程式碼區(qū)塊過時(shí);)): 更新(2021年10月1日):

#
add_filter( 'rest_authentication_errors', function( $result ) {
    // If a previous authentication check was applied,
    // pass that result along without modification.
    if ( true === $result || is_wp_error( $result ) ) {
        return $result;
    }

    // No authentication has been performed yet.
    // Return an error if user is not logged in.
    if ( ! is_user_logged_in() ) {
        return new WP_Error(
            'rest_not_logged_in',
            __( 'You are not currently logged in.' ),
            array( 'status' => 401 )
        );
    }

    // Our custom authentication check should have no effect
    // on logged-in requests
    return $result;
});
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板