只能使用replaceAll(" \ r"," _")传递Veracode CWE 117(日志输出中和不当).replaceAll(" \ n",&#34 ; _&#34)

时间:2017-10-04 12:11:05

标签: java logging audit veracode

我在一些论坛上读到了一个神话,即通过做这样的事情就足以通过Veracode CWE 117(日志的不正确输出中和)问题。 有人可以确认是否是这种情况吗?

 message.replaceAll("\r", "_").replaceAll("\n", "_");

从这个主题How to fix Veracode CWE 117 (Improper Output Neutralization for Logs) ,我知道我需要做这样的事情

ESAPI.encoder().encodeForHTML(message);

4 个答案:

答案 0 :(得分:2)

消息需要针对其所在的上下文进行转义.ESAPI记录器会替换\r\n字符以及如果配置为html进行编码。

目前这段代码给了我一个来自Veracode的CWE 117:

log.log(Level.WARNING, System.getenv("unsafe"));

此代码不会:

log.log(Level.WARNING, ESAPI.encoder().encodeForHTML(System.getenv("unsafe")));

encodeForHTML分别将\r\n编码为&#xd;&#xa;,但下划线更清晰,如果你解码了html,你可能会得到意想不到的新行。< / p>

答案 1 :(得分:1)

如果您不想直接使用ESAPI,您可以编写自己的函数来执行类似的操作:

  • 逃避新行和
  • 编码html。

我在这里给出了一个这样的函数的例子(基于ESAPI)作为答案:security flaw - veracode report - crlf injection

答案 2 :(得分:1)

我们可以选择任何一种方式。

message.replaceAll("\r", "_").replaceAll("\n", "_");

ESAPI.encoder().encodeForHTML(message);

HtmlUtils.htmlEscape(input)

答案 3 :(得分:1)

您可以使用 StringEscapeUtilsescapeJava 方法在 Veracode 中传递 CWE-117。我能够使用 2.6 的 commons-lang https://mvnrepository.com/artifact/commons-lang/commons-lang/2.6

通过 CWE-177
StringEscapeUtils.escapeJava(message)