[英]Fix for CWE-113: Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting')
[英]How to fix “Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting')”
運行VeraCode后,它在以下代碼片段中報告了以下錯誤“HTTP標頭中的CRLF序列的不正確中和('HTTP響應拆分')”:
protected override void InitializeCulture() {
//If true then setup the ability to have a different culture loaded
if (AppSettings.SelectLanguageVisibility) {
//Create cookie variable and check to see if that cookie exists and set it if it does.
HttpCookie languageCookie = new HttpCookie("LanguageCookie");
if (Request.Cookies["LanguageCookie"] != null)
languageCookie = Request.Cookies["LanguageCookie"];
//Check to see if the user is changing the language using a query string.
if (Server.UrlDecode(Request.QueryString["l"]) != null)
languageCookie.Value = Server.UrlDecode(Request.QueryString["l"]);
//Check to make sure the cookie isn't null and set the culture variable to auto if it is and the value of the cookie if it isn't.
if (languageCookie.Value == null)
languageCookie.Value = string.Empty;
string culture = languageCookie.Value.ToString();
if (string.IsNullOrEmpty(culture))
culture = "Auto";
//Use to set the Culture and UI Culture.
this.UICulture = culture;
this.Culture = culture;
if (culture != "Auto") {
//If culture is changed set the new Current Culture and CurrentUICulture.
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(culture);
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
}
//Update the cookie value with the new culture and initialize the culture.
Response.Cookies.Set(languageCookie);
Response.Cookies["LanguageCookie"].Expires = DateTime.Now.ToLocalTime().AddYears(1);
Response.Cookies["LanguageCookie"].HttpOnly = true;
}
else {
//Else keep language as English if localization is not enabled.
this.UICulture = "en";
this.Culture = "en";
}
base.InitializeCulture();
}
該報告指向包含以下代碼的行: Response.Cookies.Set(languageCookie); 可以使用什么修復來消除該錯誤?
謝謝
我相信問題是因為這條線
languageCookie.Value = Server.UrlDecode(Request.QueryString["l"]);
接受(不可信)用戶輸入(即Request.QueryString["l"]
)。 嘗試添加函數調用以從該查詢字符串參數中刪除任何回車符或換行符(包括其編碼的等效項,如%0d
和%0a
),然后將其存儲在languageCookie
。
例如,您可以嘗試將該行更改為:
languageCookie.Value = Server.UrlDecode(Request.QueryString["l"])
.Replace("\r", string.Empty)
.Replace("%0d", string.Empty)
.Replace("%0D", string.Empty)
.Replace("\n", string.Empty)
.Replace("%0a", string.Empty)
.Replace("%0A", string.Empty);
雖然這應該可以清理一下(我現在不是C#程序員)。
也可以看看
刪除此問題的最簡單方法是使用esapi jar中的ESAPI httputilities。 您可以使用
ESAPI.httpUtilities().setHeader(response,param,value);
ESAPI.httpUtilities().addCookies(response, param,value);
和其他任務的類似方法。 您需要在類路徑中設置ESAPI.properrties。 這是我們為Java實現的方式。 其他語言也可以使用相同的功能。
不需要額外的工作,它將解決veracode中的問題。
當配置選項EnableHeaderChecking為true(默認值)時,ASP.Net將自動檢查響應頭並編碼CRLF字符,這看起來像是誤報。這是從.Net框架的2.0版開始提供的,並且還將保護響應針對cookie名稱中存在的CRLF字符的標題。
參考文獻:
我知道掃描程序不能相信服務器設置是正確的所以我去做了一些測試,用一個函數替換cookie名稱中使用的字符串中的任何CRLF字符,但Veracode根本不接受它。
掃描儀似乎只接受來自預定義實用程序列表的清理代碼。 我用一些批准的實用程序對URLEncode(它將編碼CRLF字符)進行了不少測試,但沒有運氣。
參考文獻:
使用StringUtils替換導致CRLF的所有字符的一個襯墊。 這個對我有用
StringUtils.replaceEach(strForCRLF,new String [] {“\\ n”,“\\ r”,“%0d”,“%0D”,“%0a”,“%0A”},new String [] {“”, “”,“”,“”,“”,“}};
在Asp.Net中,您必須檢查兩件事,首先必須是httponly。您可以在webconfig中指定
<httpCookies httpOnlyCookies="true"/>
之后,請確保您已將您保存在Cookie中的內容視為已廢棄
HttpCookie cookies=new HttpCookies("key",Sanitizer.GetSafeHtml(value));
這個消毒劑類來自ANtixss庫。 有關詳細信息,請查看此鏈接HTTP標頭中的CRLF序列的不正確中和('HTTP響應拆分')(CWE ID 113)
描述
函數調用包含HTTP響應拆分缺陷。 將未經過授權的用戶提供的輸入寫入HTTP標頭允許攻擊者操縱瀏覽器呈現的HTTP響應,從而導致緩存中毒和crosssite腳本攻擊。
建議
從用戶提供的用於構造HTTP響應的數據中刪除意外的回車符和換行符。 始終驗證用戶提供的輸入,以確保它符合預期的格式,盡可能使用集中數據驗證例程。
問題代碼
response.setHeader(headerKey,headerValue);
response.addHeader(headerKey, headerValue);
固定代碼
DefaultHTTPUtilities httpUtilities = new DefaultHTTPUtilities();
httpUtilities.setHeader(headerKey,headerValue);
httpUtilities.addHeader(response, headerKey,headerValue);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.