CA5365:請勿停用 HTTP 標頭檢查

屬性
規則識別碼 CA5365
標題 請勿停用 HTTP 標頭檢查
類別 安全性
修正程式是中斷或非中斷 不中斷
預設在 .NET 8 中啟用 No

原因

EnableHeaderChecking 設定為 false

檔案描述

HTTP 標頭檢查可讓您編碼歸位字元和換行符, \r 以及 \n回應標頭中找到的 。 這種編碼有助於避免插入式攻擊,攻擊會利用響應標頭中未受信任數據的應用程式。

如何修正違規

EnableHeaderChecking 設定為 true。 或者,移除 的指派 false ,因為預設值為 true

隱藏警告的時機

HTTP 標頭接續依賴跨越多行的標頭,而且需要其中的新行。 如果您需要使用標頭接續,則必須將 EnableHeaderChecking 屬性設定為 false。 檢查標頭會對效能造成影響。 如果您確定您已經進行正確的檢查,請關閉此功能可以改善應用程式的效能。 停用此功能之前,請確定您已在此區域中採取正確的預防措施。

隱藏警告

如果您只想要隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。

#pragma warning disable CA5365
// The code that's violating the rule is on this line.
#pragma warning restore CA5365

若要停用檔案、資料夾或項目的規則,請在組態檔中將其嚴重性設定為 。none

[*.{cs,vb}]
dotnet_diagnostic.CA5365.severity = none

如需詳細資訊,請參閱 如何隱藏程式代碼分析警告

虛擬程式代碼範例

using System;
using System.Web.Configuration;

class TestClass
{
    public void TestMethod()
    {
        HttpRuntimeSection httpRuntimeSection = new HttpRuntimeSection();
        httpRuntimeSection.EnableHeaderChecking = false;
    }
}

解決方案

using System;
using System.Web.Configuration;

class TestClass
{
    public void TestMethod()
    {
        HttpRuntimeSection httpRuntimeSection = new HttpRuntimeSection();
        httpRuntimeSection.EnableHeaderChecking = true;
    }
}