Hi everyone,
SonarCloud keeps flagging log injection issues in XWiki. While I wouldn’t consider them very serious, they’re still not nice and can indeed be an issue when you need XWiki’s log for security purposes. Basically, the main issue is that it is possible to inject content that looks like it was another log message in any user-controlled string that we log, which could be used to conceal an attack. For admins, there are easy workarounds like storing the log in a structured format like JSON or in a database instead of a plain text file. However, still, the default logging configuration of XWiki should be secure and not vulnerable to this attack (secure by default). From what I understand, the main mitigation seems to be to remove or escape newline characters in user-supplied parameters (see, e.g., this discussion and this cheat sheet by OWASP). I suggest implementing a similar mitigation. I see basically two options:
- Ensure that all user-controlled strings are logged in parameters and configure a central formatting of log parameters that escapes parameter values before inserting them. This has the advantage that many existing log calls will be covered by this, and it is hard to forget the escaping, but all log messages where user-controlled strings are logged as parameters will need to be flagged as false-positives in SonarCloud. Further, we need to be careful, as the fix won’t apply to log messages where the user-controlled string is inserted into the log message itself so we can’t simply disable the whole rule in SonarCloud. We could also possibly just apply this escaping to the whole log message, but I wonder if this won’t make exception stack traces unreadable.
- Escape all user-controlled strings before logging them. We should provide a helper method for this (like this NewlineLogScrubber by OWASP). This requires changes across the codebase but SonarCloud can assist us in finding them and there is no risk of wrongly ignoring one of them.
We can of course also combine both approaches. Any opinions?