Checkmarx Java fix for Log Forging -sanitizing user input

血红的双手。 提交于 2020-05-14 07:36:31

问题


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

}

回答1:


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.




回答2:


  1. Have a look at the OWASP Injection Prevention Cheat Sheet on the section 'Log Injection'

  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



来源:https://stackoverflow.com/questions/55364577/checkmarx-java-fix-for-log-forging-sanitizing-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!