CA5365:請勿停用 HTTP 標頭檢查

屬性 值
規則識別碼 CA5365
職稱 請勿停用 HTTP 標頭檢查
類別 安全性
修正是造成中斷還是不中斷 不中斷
在 .NET 10 中預設啟用 否
適用語言 C# 與 Visual Basic

原因

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