繁体   English   中英

如何修复“HTTP标头中的CRLF序列的不正确中和('HTTP响应拆分')”

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM