CA2262:正確設定 'MaxResponseHeadersLength'
財產 | 價值 |
---|---|
規則標識碼 | CA2262 |
標題 | 將 MaxResponseHeadersLength 正確設置 |
類別 | 使用量 |
修正是中斷或非中斷的 | 非中斷 |
預設在 .NET 9 中啟用 | 建議 |
原因
HttpClientHandler.MaxResponseHeadersLength 屬性會設定為大於128的值。
規則描述
HttpClientHandler.MaxResponseHeadersLength 屬性是以 KB 為單位來測量,而不是以位元組為單位。 默認的最大長度為 64 KB,應該足以容納大部分的使用案例。 如果您將 屬性設定為大於 128 KB 的值,可能是因為對此屬性的單位有誤解。
如何修正違規
如果您打算設定較小的值,請將它更新為以 KB 為單位的所需值。
例
HttpClientHandler handler = new()
{
// Violation
MaxResponseHeadersLength = 512
// Fix (it is not possible to specify a limit lower than 1 KB)
MaxResponseHeadersLength = 1
};
Dim handler As New HttpClientHandler With {
' Violation
.MaxResponseHeadersLength = 512
' Fix (it is not possible to specify a limit lower than 1 KB)
.MaxResponseHeadersLength = 1
}
隱藏錯誤的時機
如果很大的值是預期的,那麼忽略這個警告是安全的。