Security

edit

Permissions of the log files

On Unix, the log files are created with the default mode of the platform, which usually lets every local user read them. Set UnixCreateMode when the messages can contain sensitive data:

options.UnixCreateMode = UnixFileMode.UserRead | UnixFileMode.UserWrite;
  • The value is ignored on Windows, and the umask of the process still applies.
  • The mode of the directory is not changed. Create the directory yourself, with the permissions you need, when it must be private too.

Untrusted data in the messages

The text format writes the messages as they are, so a value that contains a line break produces several lines. When the value comes from an untrusted source, such as a request, it can forge entries that look exactly like real ones:

logger.LogInformation("User said: {Text}", "hello\n[2020-01-01 00:00:00.000] [CRIT] [Security] admin login succeeded");

Set EscapeControlCharacters to true to write the control characters of the messages, the scopes and the exceptions as escape sequences: \n, \r, \t, \\ for a backslash, and \uXXXX for the others. An entry then always spans a single line, including the stack trace of its exception, which is harder to read.

The JSON format is not affected: the JSON writer already escapes the control characters.