I am getting this sonar issue(Security - Potential CRLF Injection for logs) while logging request body parameter in code.

public ResponseEntity<SomeDto> someMethod(@RequestBody User user) {
 log.info("user received as --> {}", user);
}

How to resolve this issue? Thank you.

1

There are 1 best solutions below

0
On

That is really annoying warning produced by sonarqube. The problem is sonarqube expects from you to write dumb code like:

public ResponseEntity<SomeDto> someMethod(@RequestBody User user) {
    log.info(
            "user received as --> {}", 
            String.valueOf(user)
                    .replace("\r", "\\r")
                    .replace("\n", "\\n")
    );
}

which is obviously unacceptable.

If your security team do believe CR/LF characters in logs is a real security problem the only correct approach is following: