False positive - LogInjection (CWE 117) is not mitigated via Log4j2 %{encodeCRLF) pattern
Hello, I have a case where I sanitize user inputs using log4j2 Pattern rule to escape '\n' and '\r' using the encode pattern %encode as follows where the user provided messages to log4j2.log() methods are encoded seamlessly:
Log4j2.xml:
PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} -%encode{%msg}{CRLF}%n"
Code:
log("UserId: {}", userId);
...
in order to mitigate a LogInjection high vulnebarility (CWE-117) issue reported by CodeQL in Java. The problem is that CodeQL is not happy as it still reports the same set of LogInjection issues after the sanitization.
However, if I sanitize the user provided inputs in the log() messages, using the following method, CodeQL is happy.
private String escapeCRLF(String msg) {
return (msg == null ? null : msg.replace("\n", "\\n" ).replace("\r", "\\r" ));
}
...
log("UserId: {}", escapeCRLF(userId));
...
So, apparently, CodeQL has a rule to recognize the 2nd mitigation method but somehow it misses the sanitization provided via log4j2 encoding CRLF rule, which has the exact same functionality as the above escapeCRLF method. I like the 1st solution which is uniform throughout the code base, requiring no code change. So, I am wondering if you would know how to make CodeQL happy if I deploy the Log4j2 solution.
Thanks in advance!
Which query is giving you the alert?
Rule ID: java/log-injection LogInjection.ql
Thanks!
Thanks for letting us know. This is indeed a false positive and we're tracking it internally. I can't make any promises on if or when we'll fix it, but we're aware of this and are determining how to address it. We'll update this issue when there are changes.
Thank you for your prompt response and effort to fix this issue. Highly appreciated!
Hello Team!
Is there any news regarding this false-positive alert?
I have similar issue - java/log-injection query flags an alert even when global custom log sanitizer is implemented via Logback's ClassicConverter and conversionRule in logback-spring.xml.
Looks like the data flow analysis in CodeQL currently doesn't recognize this kind of transformation rules
Thank you in advance!
I'm afraid we don't currently have plans to work on fixing this.
I briefly thought about how we would implement a fix for it. We are already parsing xml files, so we can find these PatternLayout. There would be a bit of work in correctly parsing them to find if they definitely escape new lines. I think the hardest part would be determining which calls to log4j2 logging functions correspond to appenders with the sanitizing PatternLayout.
Ok, I understand) Thank you very much for the answer!
I briefly thought about how we would implement a fix for it. We are already parsing xml files, so we can find these
PatternLayout. There would be a bit of work in correctly parsing them to find if they definitely escape new lines. I think the hardest part would be determining which calls to log4j2 logging functions correspond to appenders with the sanitizingPatternLayout.
Would it be worthwhile, though, to be able to at least catch this if there is only one, root logger, and that has the pattern?
And/or to at least note in the finding that this pattern was found, but can't determine whether it's the active one?
Possibly. However, I think our time is better spent working on plans we have to reduce the FP rate in a much more general way, rather than fixing lots of highly specific cases.
Although all of us spend a whole lot of time, over and over, having to dismiss these same findings like on every PR.