Skip to content
Merged
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
46 changes: 28 additions & 18 deletions apps/user_ldap/lib/LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
use OCA\User_LDAP\DataCollector\LdapDataCollector;
use OCA\User_LDAP\Exceptions\ConstraintViolationException;
use OCP\IConfig;
use OCP\ILogger;
use OCP\Profiler\IProfiler;
use OCP\Server;
use Psr\Log\LoggerInterface;

class LDAP implements ILDAPWrapper {
protected array $curArgs = [];
protected LoggerInterface $logger;
protected IConfig $config;

private ?LdapDataCollector $dataCollector = null;

Expand All @@ -32,6 +34,7 @@ public function __construct(
}

$this->logger = Server::get(LoggerInterface::class);
$this->config = Server::get(IConfig::class);
}

/**
Expand Down Expand Up @@ -291,6 +294,21 @@ protected function invokeLDAPMethod(string $func, ...$arguments) {
return null;
}

/**
* Turn resources into string, and removes potentially problematic cookie string to avoid breaking logfiles
*/
private function sanitizeFunctionParameters(array $args): array {
return array_map(function ($item) {
if ($this->isResource($item)) {
return '(resource)';
}
if (isset($item[0]['value']['cookie']) && $item[0]['value']['cookie'] !== '') {
$item[0]['value']['cookie'] = '*opaque cookie*';
}
return $item;
}, $args);
}

private function preFunctionCall(string $functionName, array $args): void {
$this->curArgs = $args;
if (strcasecmp($functionName, 'ldap_bind') === 0 || strcasecmp($functionName, 'ldap_exop_passwd') === 0) {
Expand All @@ -301,32 +319,24 @@ private function preFunctionCall(string $functionName, array $args): void {
$args[2] = IConfig::SENSITIVE_VALUE;
}

$this->logger->debug('Calling LDAP function {func} with parameters {args}', [
'app' => 'user_ldap',
'func' => $functionName,
'args' => json_encode($args),
]);
if ($this->config->getSystemValue('loglevel') === ILogger::DEBUG) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed if we then call logger->debug()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sanitizeFunctionParameters felt a bit expensive to run for nothing, so I wanted to only use the computing time if the log is actually gonna happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually the comment says just that on the line below: /* Only running this if debug loglevel is on, to avoid processing parameters on production */

/* Only running this if debug loglevel is on, to avoid processing parameters on production */
$this->logger->debug('Calling LDAP function {func} with parameters {args}', [
'app' => 'user_ldap',
'func' => $functionName,
'args' => $this->sanitizeFunctionParameters($args),
]);
}

if ($this->dataCollector !== null) {
$args = array_map(function ($item) {
if ($this->isResource($item)) {
return '(resource)';
}
if (isset($item[0]['value']['cookie']) && $item[0]['value']['cookie'] !== '') {
$item[0]['value']['cookie'] = '*opaque cookie*';
}
return $item;
}, $this->curArgs);

$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$this->dataCollector->startLdapRequest($functionName, $args, $backtrace);
$this->dataCollector->startLdapRequest($functionName, $this->sanitizeFunctionParameters($args), $backtrace);
}

if ($this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
$args = array_map(fn ($item) => (!$this->isResource($item) ? $item : '(resource)'), $this->curArgs);
file_put_contents(
$this->logFile,
$functionName . '::' . json_encode($args) . "\n",
$functionName . '::' . json_encode($this->sanitizeFunctionParameters($args)) . "\n",
FILE_APPEND
);
}
Expand Down
Loading