簡體   English   中英

節點獲取:禁用 SSL 驗證

[英]Node-fetch: Disable SSL verification

我有以下代碼,它是從快速服務器運行的:

import fetch from 'node-fetch';

let formBody = [];

const dataLogin = {
      'username': 'myUser',
      'password': 'myPassword'
};

for (let p in dataLogin) {
   let encodedKey = encodeURIComponent(p);
   let encodedValue = encodeURIComponent(dataLogin[p]);
   formBody.push(encodedKey + "=" + encodedValue);
 }

 formBody = formBody.join("&");   

 const url = 'https://external-login-api.com';
 return fetch(url, {
          method: 'POST',
          headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Content-Length': formBody.length         
  },     
  body: formBody
 });

當我運行代碼時,我收到以下錯誤,盡管能夠在 Postman 中運行請求沒有問題。

{"message":"request to https://external-login-api.com failed, reason: write EPROTO 7316:error:141A318A:SSLroutines:tls_process_ske_dhe:dh key too small:openssl\\ssl\\statem\\statem_clnt.c :1472:\\n","type":"system","errno":"EPROTO","code":"EPROTO"}

如何為此請求禁用 SSL 驗證?

另一種方法是將您自己的代理設置為 fetch 調用。

const fetch = require('node-fetch');
const https = require('https');

const httpsAgent = new https.Agent({
      rejectUnauthorized: false,
    });

const response = await fetch(url, {
      method: 'POST',
      headers: headers,
      body: body,
      agent: httpsAgent,
    });
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

將確保您忽略任何被拒絕的 TLS 證書,或者您可以在運行節點服務時將其設置為環境變量。 然而,這可能無濟於事,而且可能是一個壞主意。 SSL 錯誤不是因為證書無效(例如自簽名證書),而是因為 SSL/TLS 配置中的 Diffie-Hellman 密鑰較弱。

如果這是您托管的服務,您應該考慮更正和改進您的 TLS/SSL 密碼。 有關更多信息,請參閱此答案

重要的部分是:

您應該使用 2048 位 Diffie-Hellman 組或更大的組。 您不應使用 512 位或 1024 位 Diffie-Hellman 組。

如果這是第三方服務,您應該考慮聯系他們或使用不同的服務,因為他們讓自己容易受到Logjam 攻擊,這也在上面鏈接的答案中進行了討論。

如果您想在使用 AXIOS 庫時禁用 SSL 檢查,請以這種方式將代理添加到其調用中

// At instance level
const instance = axios.create({
  httpsAgent: new https.Agent({  
    rejectUnauthorized: false
  })
});

instance.get('https://something.com/foo');

// At request level
 const agent = new https.Agent({  
 rejectUnauthorized: false
});

axios.get('https://something.com/foo', { httpsAgent: agent });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM