Skip to content

Commit bb4eb01

Browse files
Merge pull request #53357 from nextcloud/backport/53250/stable31
[stable31] fix(user_ldap): Harmonize parameter obfuscation and serialization accross logging methods
2 parents 8729a42 + 0e6195e commit bb4eb01

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

apps/user_ldap/lib/LDAP.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
use OCA\User_LDAP\DataCollector\LdapDataCollector;
1212
use OCA\User_LDAP\Exceptions\ConstraintViolationException;
1313
use OCP\IConfig;
14+
use OCP\ILogger;
1415
use OCP\Profiler\IProfiler;
1516
use OCP\Server;
1617
use Psr\Log\LoggerInterface;
1718

1819
class LDAP implements ILDAPWrapper {
1920
protected array $curArgs = [];
2021
protected LoggerInterface $logger;
22+
protected IConfig $config;
2123

2224
private ?LdapDataCollector $dataCollector = null;
2325

@@ -32,6 +34,7 @@ public function __construct(
3234
}
3335

3436
$this->logger = Server::get(LoggerInterface::class);
37+
$this->config = Server::get(IConfig::class);
3538
}
3639

3740
/**
@@ -291,6 +294,21 @@ protected function invokeLDAPMethod(string $func, ...$arguments) {
291294
return null;
292295
}
293296

297+
/**
298+
* Turn resources into string, and removes potentially problematic cookie string to avoid breaking logfiles
299+
*/
300+
private function sanitizeFunctionParameters(array $args): array {
301+
return array_map(function ($item) {
302+
if ($this->isResource($item)) {
303+
return '(resource)';
304+
}
305+
if (isset($item[0]['value']['cookie']) && $item[0]['value']['cookie'] !== '') {
306+
$item[0]['value']['cookie'] = '*opaque cookie*';
307+
}
308+
return $item;
309+
}, $args);
310+
}
311+
294312
private function preFunctionCall(string $functionName, array $args): void {
295313
$this->curArgs = $args;
296314
if (strcasecmp($functionName, 'ldap_bind') === 0 || strcasecmp($functionName, 'ldap_exop_passwd') === 0) {
@@ -301,32 +319,24 @@ private function preFunctionCall(string $functionName, array $args): void {
301319
$args[2] = IConfig::SENSITIVE_VALUE;
302320
}
303321

304-
$this->logger->debug('Calling LDAP function {func} with parameters {args}', [
305-
'app' => 'user_ldap',
306-
'func' => $functionName,
307-
'args' => json_encode($args),
308-
]);
322+
if ($this->config->getSystemValue('loglevel') === ILogger::DEBUG) {
323+
/* Only running this if debug loglevel is on, to avoid processing parameters on production */
324+
$this->logger->debug('Calling LDAP function {func} with parameters {args}', [
325+
'app' => 'user_ldap',
326+
'func' => $functionName,
327+
'args' => $this->sanitizeFunctionParameters($args),
328+
]);
329+
}
309330

310331
if ($this->dataCollector !== null) {
311-
$args = array_map(function ($item) {
312-
if ($this->isResource($item)) {
313-
return '(resource)';
314-
}
315-
if (isset($item[0]['value']['cookie']) && $item[0]['value']['cookie'] !== '') {
316-
$item[0]['value']['cookie'] = '*opaque cookie*';
317-
}
318-
return $item;
319-
}, $this->curArgs);
320-
321332
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
322-
$this->dataCollector->startLdapRequest($functionName, $args, $backtrace);
333+
$this->dataCollector->startLdapRequest($functionName, $this->sanitizeFunctionParameters($args), $backtrace);
323334
}
324335

325336
if ($this->logFile !== '' && is_writable(dirname($this->logFile)) && (!file_exists($this->logFile) || is_writable($this->logFile))) {
326-
$args = array_map(fn ($item) => (!$this->isResource($item) ? $item : '(resource)'), $this->curArgs);
327337
file_put_contents(
328338
$this->logFile,
329-
$functionName . '::' . json_encode($args) . "\n",
339+
$functionName . '::' . json_encode($this->sanitizeFunctionParameters($args)) . "\n",
330340
FILE_APPEND
331341
);
332342
}

0 commit comments

Comments
 (0)