Checkmarx Java fix for Log Forging -sanitizing user input
Asked Answered
L

2

6

Can anyone suggest the proper sanitization/validation process required for the courseType variable in the following getCourses method. I am using that variable to write in a log file.

I've tried HtmlUtils.HtmlEscape() but didn't get expected results.

Thanks!

@RequestMapping(value = "/retriveCourses", method = RequestMethod.GET)
@ResponseBody
public List<Course> getCourses(@RequestParam(value = "courseType", required = false) String courseType) {

}
Lytton answered 26/3, 2019 at 19:5 Comment(2)
Can you add the part of the code where you write to the log?Globoid
Hi..thanks for the reply. I am writing the @RequestParam to the log as follows -logger.info("Course Type is "+HtmlUtils.HtmlEscape(courseType)). This enabling log forging.Lytton
G
7

it seems like the Checkmarx tool is correct in this case.

A "Log Forging" vulnerability means that an attacker could engineer logs of security-sensitive actions and lay a false audit trail, potentially implicating an innocent user or hiding an incident.

While using htmlEscape will escape some special characters:

  • &amplt; represents the < sign.
  • &ampgt; represents the > sign.
  • &ampamp; represents the & sign.
  • &ampquot; represents the " mark.

It will not escape or remove new-line/EOL/tab characters that must be avoided in order to keep logs integrity.

The best practice recommendations to avoid log forging are:

  1. Make sure to replace all relevant dangerous characters. example:

    cleanInput = input.replace('\t', '-').replace('\n', '-').replace('\r', '-');

  2. Validate all input, regardless of source. Validation should be based on a whitelist. Accept only data fitting a specified structure, rather than reject bad patterns. Check for: Data type, Size, Range, Format, Expected values.

Hopefully, that solves your problem.

Globoid answered 28/3, 2019 at 6:24 Comment(1)
I am reading an index value from database and then logging that value when some data referred by that index is incorrect or incomplete. Checkmarx complain about log forging but since I am only logging and index from a database I see nothing to sanitize. How could I solve this?Mucilage
F
6
  1. Have a look at the Logging - OWASP Cheat Sheet Series in the section 'Event Collection'

  2. The best encoder still OWASP Java Encoder => Solve the 2. of @yaloner

  3. There is also a project at OWASP To help you to deal withs log injections OWASP Security Logging => Solve the 1. of @yaloner

Have a look at them will solve the issue

Faultless answered 2/4, 2019 at 12:1 Comment(1)
Direct links to the projects in question: github.com/OWASP/owasp-java-encoder and github.com/javabeanz/owasp-security-loggingMandrel

© 2022 - 2025 — McMap. All rights reserved.