logger = $logger; $this->needles = static::computeNeedles($sensitiveMaterial); } /** * @param array $sensitiveMaterial See the constructor. */ public function addSensitiveMaterial(array $sensitiveMaterial) { $this->needles = array_merge($this->needles, static::computeNeedles($sensitiveMaterial)); } public function log($level, $message, array $context = []): void { $message .= count($context) === 0 ? '' : ' ' . implode(', ', $context); $this->logger->log($level, static::sanitizeForLogging($message, $this->needles)); } /** * @param array $sensitiveMaterial An array of various objects typically used with the phpFinTS library that contain * some sensitive information. This array may also contain plain strings, which are themselves interpreted as * sensitive. * @return string[] An array of search-replacement "needles" that should be replaced in log messages. */ public static function computeNeedles(array $sensitiveMaterial): array { $needles = []; foreach ($sensitiveMaterial as $item) { if (is_string($item)) { $needles[] = $item; } elseif ($item instanceof Credentials) { $needles[] = $item->getBenutzerkennung(); $needles[] = $item->getPin(); } elseif ($item instanceof FinTsOptions) { $needles[] = $item->productName; } elseif ($item instanceof Account) { $needles[] = $item->getIban(); $needles[] = $item->getAccountNumber(); $needles[] = $item->getAccountOwnerName(); $needles[] = $item->getCustomerId(); } elseif ($item instanceof SEPAAccount) { $needles[] = $item->getIban(); $needles[] = $item->getAccountNumber(); } elseif ($item !== null) { throw new \InvalidArgumentException('Unsupported type of sensitive material ' . gettype($item)); } } $needles = array_filter($needles); // Filter out empty entries. $escapedNeedles = array_map(function (string $needle) { // The wire format is ISO-8859-1, so that's what will be logged and that's what to look for when replacing. return mb_convert_encoding(Serializer::escape($needle), 'ISO-8859-1', 'UTF-8'); }, $needles); return array_merge($needles, $escapedNeedles); } /** * Removes sensitive values from the given string, while preserving its overall length, so that wrappers like FinTS * messages or Bin containers, which declare the length of their contents, remain parsable. * @param string $str Some string. * @param string[] $needles The sensitive values to be replaced, usually from {@link computeNeedles()}. * @return string The same string, but with sensitive values removed. */ public static function sanitizeForLogging(string $str, array $needles): string { $replacements = array_map(function ($needle) { $len = strlen($needle); $prefix = 'PRIVATE'; return substr($prefix, 0, $len) . str_repeat('_', max(0, $len - strlen($prefix))); }, $needles); return str_replace($needles, $replacements, $str); } }