【问题标题】:How to fix "Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting')"如何修复“HTTP 标头中 CRLF 序列的不正确中和('HTTP 响应拆分')”
【发布时间】:2014-03-26 10:36:27
【问题描述】:

运行 VeraCode 后,在以下代码片段中报错“Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting')”:

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); 可以使用什么修复程序来消除该错误?

谢谢

【问题讨论】:

  • 我知道这是一个老问题,但假设您找到了解决方案,最好将答案标记为已接受。 (如果没有合适的,那么您可以添加自己的并选择那个。)

标签: c# cookies veracode


【解决方案1】:

我相信问题是因为这条线

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# 程序员)。

另见

【讨论】:

    【解决方案2】:

    消除此问题的最简单方法是使用 esapi jar 中的 ESAPI httputilities。 你可以使用

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

    和其他任务的类似方法。您需要在类路径中设置 ESAPI.properrties。这是我们为 Java 实现的方式。其他语言也可以使用相同的功能。

    不需要额外的工作,它会在veracode中解决问题。

    【讨论】:

    • 这是java。 C# 不使用 ESAPI。
    • 是的,它是 Java,也许我们需要在这里做一个衍生。无论如何, addCookie 的当前签名是: ESAPI.httpUtilities().addCookie(response, cookie);
    • @devwebcl:我准备为 .NET 衍生库 :)
    • 请在 ESAPI.jar for Java 中找到最新的方法;1. addCookie(Cookie cookie) 2. addCookie(HttpServletResponse response, Cookie cookie)
    【解决方案3】:

    说明

    函数调用包含 HTTP 响应拆分缺陷。将未经处理的用户提供的输入写入 HTTP 标头允许攻击者操纵浏览器呈现的 HTTP 响应,从而导致缓存中毒和跨站点脚本攻击。

    建议

    从用户提供的用于构建 HTTP 响应的数据中删除意外的回车和换行。始终验证用户提供的输入以确保其符合预期格式,并尽可能使用集中式数据验证例程。

    问题代码

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

    固定代码

    DefaultHTTPUtilities httpUtilities = new DefaultHTTPUtilities(); 
    httpUtilities.setHeader(headerKey,headerValue); 
    httpUtilities.addHeader(response, headerKey,headerValue);
    

    【讨论】:

    • 什么是 DefaultHTTPUtilities 对象?它在哪个图书馆?它是第三方对象吗?开源?
    • 这里有个问题,setHeader和addHeader中headerKey的长度都是硬编码的,setHeader是50,addHeader是20。但是我想添加一个长度超过20的header。 ..,它总是失败。即使我覆盖了 addHeader,response.addHeader 也会触发验证码问题...知道吗?
    • 这个“答案”看起来像是从 VeraCode 的此扫描仪规则的文档中复制和粘贴的。您在这里提供了哪些新信息?
    【解决方案4】:

    使用 StringUtils 替换所有导致 CRLF 的字符。它对我有用

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

    【讨论】:

      【解决方案5】:

      当配置选项 EnableHeaderChecking 为真(默认值)时,ASP.Net 会自动检查响应标头并编码 CRLF 字符,这看起来像是误报。这从 2.0 版开始可用.Net 框架,还将保护响应标头免受 cookie 名称中存在的 CRLF 字符的影响。

      参考文献:

      我知道扫描仪不能相信服务器设置是正确的,所以我用一个函数进行了一些测试,该函数替换了 cookie 名称中使用的字符串中的任何 CRLF 字符,但 Veracode 根本不会接受它.

      扫描器似乎只接受预定义实用程序列表中的清理代码。我从一些批准的实用程序中使用 URLEncode(它将对 CRLF 字符进行编码)进行了很多测试,但没有运气。

      参考资料:

      【讨论】:

        【解决方案6】:

        在 Asp.Net 中,您必须首先检查两件事 cookie 必须是 httponly 。您可以在 webconfig 中指定

        <httpCookies httpOnlyCookies="true"/>
        

        然后确保您已经清理了您保存在 cookie 中的内容,例如

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

        这个消毒剂类来自 ANtixss 库。 有关详细信息,您可以查看此链接 Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting') (CWE ID 113)

        【讨论】:

          猜你喜欢
          • 2015-09-16
          • 2019-09-06
          • 2014-06-24
          • 2015-09-27
          • 2013-05-17
          • 2018-11-28
          • 2011-02-01
          • 2013-05-02
          • 1970-01-01
          相关资源
          最近更新 更多