Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: remove sensitive logs #116

Merged
merged 7 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ [email protected]
# USE ABSOLUTE PATHS for better predictability
WEBDAV_TMP_DIR='/tmp'
WEBDAV_PUBLIC_DIR='/webdav'

# Logging path
# By default, it will log in the standard Symfony directory: var/log/prod.log (for production)
# You can use /dev/null here if you want to discard logs entirely
LOG_FILE_PATH="%kernel.logs_dir%/%kernel.environment%.log"
Comment on lines +71 to +75
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here for the env var

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ WEBDAV_TMP_DIR='/tmp'
WEBDAV_PUBLIC_DIR='/webdav'
```

g. The log file path

You can use an absolute file path here, and you can use Symfony's `%kernel.logs_dir%` and `%kernel.environment%` placeholders if needed (as in the default value). Setting it to `/dev/null` will disable logging altogether.

```
LOG_FILE_PATH="%kernel.logs_dir%/%kernel.environment%.log"
```

### Specific environment variables for IMAP and LDAP authentication methods

In case you use the `IMAP` auth type, you must specify the auth url (_the "mailbox" url_) in `IMAP_AUTH_URL`. See https://www.php.net/manual/en/function.imap-open.php for more details.
Expand Down
16 changes: 0 additions & 16 deletions config/packages/dev/easy_log_handler.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion config/packages/prod/monolog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ monolog:
buffer_size: 50 # How many messages should be saved? Prevent memory leaks
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
path: "%env(resolve:LOG_FILE_PATH)%"
level: debug
console:
type: console
Expand Down
6 changes: 5 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ services:
App\Security\LoginFormAuthenticator:
arguments:
$adminLogin: "%env(ADMIN_LOGIN)%"
$adminPassword: "%env(ADMIN_PASSWORD)%"
$adminPassword: "%env(ADMIN_PASSWORD)%"

App\Logging\Monolog\PasswordFilterProcessor:
tags:
- { name: monolog.processor }
28 changes: 28 additions & 0 deletions src/Logging/Monolog/PasswordFilterProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Logging\Monolog;

use Monolog\Processor\ProcessorInterface;

final class PasswordFilterProcessor implements ProcessorInterface
{
private const REDACTED = '****';
private const PASSWORD_KEY = 'password';
private const SENSITIVE_ARGS_FUNCTIONS = ['validateUserPass', 'ldapOpen', 'password_verify', 'imapOpen', 'ldap_bind', 'hashPassword', 'dav'];

public function __invoke(array $record): array
{
// Remove potentially sensitive data from function arguments
$shouldRedactArgs = array_key_exists('function', $record) && in_array($record['function'], self::SENSITIVE_ARGS_FUNCTIONS);

foreach ($record as $key => $item) {
if (self::PASSWORD_KEY === strtolower($key) || ('args' === $key && $shouldRedactArgs)) {
$record[$key] = self::REDACTED;
} elseif (is_array($item)) {
$record[$key] = $this($item);
}
}

return $record;
}
}
7 changes: 6 additions & 1 deletion src/Services/IMAPAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ protected function imapOpen($username, $password)
$this->utils->createPasswordlessUserWithDefaultObjects($username, $username, $username);

$em = $this->doctrine->getManager();
$em->flush();

try {
$em->flush();
} catch (\Exception $e) {
error_log('IMAP Error (flush): '.$e->getMessage());
}
Comment on lines -65 to +70
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching the exception will allow to continue (as the auth succeeded) and not log the trace, if any

}
}

Expand Down
12 changes: 8 additions & 4 deletions src/Services/LDAPAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function ldapOpen($username, $password)
try {
$ldap = ldap_connect($this->LDAPAuthUrl);
} catch (\ErrorException $e) {
error_log($e->getMessage());
error_log('LDAP Error (ldap_connect): '.ldap_error($ldap).' ('.ldap_errno($ldap).')');
}

if (!$ldap || !ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3)) {
Expand Down Expand Up @@ -124,8 +124,7 @@ protected function ldapOpen($username, $password)
$success = true;
}
} catch (\ErrorException $e) {
error_log($e->getMessage());
error_log('LDAP Error: '.ldap_error($ldap).' ('.ldap_errno($ldap).')');
error_log('LDAP Error (ldap_bind): '.ldap_error($ldap).' ('.ldap_errno($ldap).')');
}

if ($success && $this->autoCreate) {
Expand Down Expand Up @@ -161,7 +160,12 @@ protected function ldapOpen($username, $password)
$this->utils->createPasswordlessUserWithDefaultObjects($username, $displayName, $email);

$em = $this->doctrine->getManager();
$em->flush();

try {
$em->flush();
} catch (\Exception $e) {
error_log('LDAP Error (flush): '.$e->getMessage());
}
Comment on lines -164 to +168
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching the exception will allow to continue (as the auth succeeded) and not log the trace, if any

}
}

Expand Down