简体   繁体   English

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

[英]How to fix “Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting')”

After running VeraCode, it reported a following error "Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting')" in the following code fragment: 运行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();
    }

The report points to the line containing following code: Response.Cookies.Set(languageCookie); 该报告指向包含以下代码的行: Response.Cookies.Set(languageCookie); What fix can be used to eliminate that error? 可以使用什么修复来消除该错误?

Thank's 谢谢

I believe the problem is because the line 我相信问题是因为这条线

languageCookie.Value = Server.UrlDecode(Request.QueryString["l"]);

accepts (untrusted) user input (ie Request.QueryString["l"] ). 接受(不可信)用户输入(即Request.QueryString["l"] )。 Try adding a function call to remove any carriage returns or line feed characters (including their encoded equivalents like %0d and %0a ) from that query string parameter before storing it in languageCookie . 尝试添加函数调用以从该查询字符串参数中删除任何回车符或换行符(包括其编码的等效项,如%0d%0a ),然后将其存储在languageCookie

For example, you might try changing that line to: 例如,您可以尝试将该行更改为:

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);

though that should probably be cleaned up a bit (I'm not a C# programmer at this time). 虽然这应该可以清理一下(我现在不是C#程序员)。

See also 也可以看看

The easiest way to remove this issue is to use ESAPI httputilities present in esapi jar. 删除此问题的最简单方法是使用esapi jar中的ESAPI httputilities。 You can use 您可以使用

ESAPI.httpUtilities().setHeader(response,param,value);
ESAPI.httpUtilities().addCookies(response, param,value);

and similar methods for other tasks. 和其他任务的类似方法。 You will need to have ESAPI.properrties set in you classpath. 您需要在类路径中设置ESAPI.properrties。 This is the way we implemented for Java. 这是我们为Java实现的方式。 Same features are available for other languages too. 其他语言也可以使用相同的功能。

No additional work is required and it will solve the issue in veracode. 不需要额外的工作,它将解决veracode中的问题。

It looks like a false positive as ASP.Net will automatically check the response headers and encode CRLF characters when the configuration option EnableHeaderChecking is true (the default value).This is available since version 2.0 of the .Net framework and will also protect the response header against CRLF chars present in the cookie name. 当配置选项EnableHeaderChecking为true(默认值)时,ASP.Net将自动检查响应头并编码CRLF字符,这看起来像是误报。这是从.Net框架的2.0版开始提供的,并且还将保护响应针对cookie名称中存在的CRLF字符的标题。

References: 参考文献:

I understand that the scanner cannot trust that the server settings will be correct so I went and did a few tests with a function that replaces any CRLF chars from the string used in the cookie name, but Veracode simply won't accept it. 我知道扫描程序不能相信服务器设置是正确的所以我去做了一些测试,用一个函数替换cookie名称中使用的字符串中的任何CRLF字符,但Veracode根本不接受它。

It seems like the scanner will only accept sanitization code from a pre-defined list of utilities. 扫描仪似乎只接受来自预定义实用程序列表的清理代码。 I did quite a few tests with URLEncode (which will encode the CRLF chars) from a few of the approved utilities but yet no luck. 我用一些批准的实用程序对URLEncode(它将编码CRLF字符)进行了不少测试,但没有运气。

References: 参考文献:

One liner to replace all character causing CRLF using StringUtils. 使用StringUtils替换导致CRLF的所有字符的一个衬垫。 It works for me 这个对我有用

StringUtils.replaceEach(strForCRLF, new String[] { "\\n", "\\r","%0d", "%0D", "%0a", "%0A" }, new String[] { "", "", "", "", "", "" }); StringUtils.replaceEach(strForCRLF,new String [] {“\\ n”,“\\ r”,“%0d”,“%0D”,“%0a”,“%0A”},new String [] {“”, “”,“”,“”,“”,“}};

In Asp.Net you have to check for two things first cookies must be httponly .You can specify that in your webconfig 在Asp.Net中,您必须检查两件事,首先必须是httponly。您可以在webconfig中指定

<httpCookies httpOnlyCookies="true"/>

and after that make sure you have santized the that you are saving in your cookies like 之后,请确保您已将您保存在Cookie中的内容视为已废弃

HttpCookie cookies=new HttpCookies("key",Sanitizer.GetSafeHtml(value));

This sanitizer class is from ANtixss library. 这个消毒剂类来自ANtixss库。 For details you can check this link Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting') (CWE ID 113) 有关详细信息,请查看此链接HTTP标头中的CRLF序列的不正确中和('HTTP响应拆分')(CWE ID 113)

Description 描述

A function call contains an HTTP response splitting flaw. 函数调用包含HTTP响应拆分缺陷。 Writing unsanitized user-supplied input into an HTTP header allows an attacker to manipulate the HTTP response rendered by the browser, leading to cache poisoning and crosssite scripting attacks. 将未经过授权的用户提供的输入写入HTTP标头允许攻击者操纵浏览器呈现的HTTP响应,从而导致缓存中毒和crosssite脚本攻击。

Recommendations 建议

Remove unexpected carriage returns and line feeds from user-supplied data used to construct an HTTP response. 从用户提供的用于构造HTTP响应的数据中删除意外的回车符和换行符。 Always validate user-supplied input to ensure that it conforms to the expected format, using centralized data validation routines when possible. 始终验证用户提供的输入,以确保它符合预期的格式,尽可能使用集中数据验证例程。

Issue Code 问题代码

response.setHeader(headerKey,headerValue); 
response.addHeader(headerKey, headerValue);

Fixed Code 固定代码

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