Do I need to sanitize user input from a public facing form when passing data straight into:
error_log();
http://php.net/manual/en/function.error-log.php
I know this is a rather simplistic question but I can't find anything elsewhere.
Thanks
Do I need to sanitize user input from a public facing form when passing data straight into:
error_log();
http://php.net/manual/en/function.error-log.php
I know this is a rather simplistic question but I can't find anything elsewhere.
Thanks
The OWASP page regarding log injection attacks is helpful: https://owasp.org/www-community/attacks/Log_Injection
It is possible for someone to inject data into your logs that you wouldn't want there, such as characters that could corrupt the file (depending on what you use to process/read the file later), or just data that wasn't actually logged (forged log lines).
If someone can inject false data into your logs, they can make it harder to audit the logs and determine what has happened in the case of a compromised server.
Adding some basic sanitization could save you potential headaches, particularly down the road if you change how you process/read your logs and forget about the code you are writing today.
Like any security, it is a layer of the onion. You have to look at everything that touches those logs and determine what level of risk is acceptable and how much effort is warranted to mitigate potential issues. How does your app use the logs, how do you process/view the logs, how do those logs get used on the server (eg: fail2ban uses logs to determine who to ban from connecting to the server in various ways)?
There is a good answer here from someone that prefers not to filter/sanitize the data put into their own logs but explains how they prevent issues: https://stackoverflow.com/a/55199264/2153218
They mention that they filter out new lines to ensure there is only ever one line logged at a time, and beyond that they ensure the ways they use/view the logs are not vulnerable.
You don't need to sanitize data, if you log Request data. Then for debug, you would want to know what was send on request.
It this is after validation, then still no, as you should have valid data.
The only problem what you need to resolve, to not log important data in security context.
As the data is just passed to a file you don't have to worry about sanitising the data.
If you wanted to use that file elsewhere and display it on a website or parse it in some way you will need to sanitise the data. Basically, no :)
If you want to play it safe:
error_log(addcslashes($message, "\000..\037\177..\377\\"));
that will encode all non-printable and non-ASCII characters (i.e. any byte that wouldn't match /[\x20-\x7E]/
) and double any original backslash