From 5f829bdaeb0f045c906ac0f77824a0beea5b5bcc Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 18 May 2020 16:22:26 +0200 Subject: [PATCH 1/3] [HttpKernel] Fix error logger when stderr is redirected to /dev/null (FPM) --- .../Component/HttpKernel/Log/Logger.php | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Log/Logger.php b/src/Symfony/Component/HttpKernel/Log/Logger.php index e05d8c32ec79..bbdd101d7dc2 100644 --- a/src/Symfony/Component/HttpKernel/Log/Logger.php +++ b/src/Symfony/Component/HttpKernel/Log/Logger.php @@ -37,10 +37,10 @@ class Logger extends AbstractLogger private $formatter; private $handle; - public function __construct($minLevel = null, $output = 'php://stderr', callable $formatter = null) + public function __construct($minLevel = null, $output = null, callable $formatter = null) { if (null === $minLevel) { - $minLevel = 'php://stdout' === $output || 'php://stderr' === $output ? LogLevel::CRITICAL : LogLevel::WARNING; + $minLevel = null === $output || 'php://stdout' === $output || 'php://stderr' === $output ? LogLevel::ERROR : LogLevel::WARNING; if (isset($_ENV['SHELL_VERBOSITY']) || isset($_SERVER['SHELL_VERBOSITY'])) { switch ((int) (isset($_ENV['SHELL_VERBOSITY']) ? $_ENV['SHELL_VERBOSITY'] : $_SERVER['SHELL_VERBOSITY'])) { @@ -58,7 +58,7 @@ public function __construct($minLevel = null, $output = 'php://stderr', callable $this->minLevelIndex = self::$levels[$minLevel]; $this->formatter = $formatter ?: [$this, 'format']; - if (false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) { + if ($output && false === $this->handle = \is_resource($output) ? $output : @fopen($output, 'a')) { throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output)); } } @@ -77,7 +77,11 @@ public function log($level, $message, array $context = []) } $formatter = $this->formatter; - @fwrite($this->handle, $formatter($level, $message, $context)); + if ($this->handle) { + @fwrite($this->handle, $formatter($level, $message, $context)); + } else { + error_log($formatter($level, $message, $context, false)); + } } /** @@ -86,7 +90,7 @@ public function log($level, $message, array $context = []) * * @return string */ - private function format($level, $message, array $context) + private function format($level, $message, array $context, $prefixDate = true) { if (false !== strpos($message, '{')) { $replacements = []; @@ -105,6 +109,11 @@ private function format($level, $message, array $context) $message = strtr($message, $replacements); } - return sprintf('%s [%s] %s', date(\DateTime::RFC3339), $level, $message).PHP_EOL; + $log = sprintf('[%s] %s', $level, $message).PHP_EOL; + if ($prefixDate) { + $log = date(\DateTime::RFC3339).' '.$log; + } + + return $log; } } From b447433b67d8546eadea3c8dbd40aaf0d1f004bf Mon Sep 17 00:00:00 2001 From: rfaivre Date: Mon, 18 May 2020 20:54:16 +0200 Subject: [PATCH 2/3] [Security] Unserialize $parentData, if needed, to avoid errors --- .../Security/Core/Authentication/Token/AnonymousToken.php | 1 + .../Core/Authentication/Token/PreAuthenticatedToken.php | 2 +- .../Security/Core/Authentication/Token/RememberMeToken.php | 1 + .../Security/Core/Authentication/Token/SwitchUserToken.php | 1 + .../Core/Authentication/Token/UsernamePasswordToken.php | 1 + .../Security/Core/Exception/AccountStatusException.php | 1 + .../Core/Exception/CustomUserMessageAuthenticationException.php | 1 + .../Security/Core/Exception/UsernameNotFoundException.php | 1 + .../Security/Guard/Token/PostAuthenticationGuardToken.php | 1 + 9 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Security/Core/Authentication/Token/AnonymousToken.php b/src/Symfony/Component/Security/Core/Authentication/Token/AnonymousToken.php index 8c658060ad4a..db94766d3f16 100644 --- a/src/Symfony/Component/Security/Core/Authentication/Token/AnonymousToken.php +++ b/src/Symfony/Component/Security/Core/Authentication/Token/AnonymousToken.php @@ -68,6 +68,7 @@ public function __serialize(): array public function __unserialize(array $data): void { [$this->secret, $parentData] = $data; + $parentData = \is_array($parentData) ? $parentData : unserialize($parentData); parent::__unserialize($parentData); } } diff --git a/src/Symfony/Component/Security/Core/Authentication/Token/PreAuthenticatedToken.php b/src/Symfony/Component/Security/Core/Authentication/Token/PreAuthenticatedToken.php index eb20f7fe6bbd..80ac0fff38df 100644 --- a/src/Symfony/Component/Security/Core/Authentication/Token/PreAuthenticatedToken.php +++ b/src/Symfony/Component/Security/Core/Authentication/Token/PreAuthenticatedToken.php @@ -26,7 +26,6 @@ class PreAuthenticatedToken extends AbstractToken /** * @param string|\Stringable|UserInterface $user * @param mixed $credentials - * @param string $providerKey * @param string[] $roles */ public function __construct($user, $credentials, string $providerKey, array $roles = []) @@ -88,6 +87,7 @@ public function __serialize(): array public function __unserialize(array $data): void { [$this->credentials, $this->providerKey, $parentData] = $data; + $parentData = \is_array($parentData) ? $parentData : unserialize($parentData); parent::__unserialize($parentData); } } diff --git a/src/Symfony/Component/Security/Core/Authentication/Token/RememberMeToken.php b/src/Symfony/Component/Security/Core/Authentication/Token/RememberMeToken.php index 403e3ae8803d..13d3314534a2 100644 --- a/src/Symfony/Component/Security/Core/Authentication/Token/RememberMeToken.php +++ b/src/Symfony/Component/Security/Core/Authentication/Token/RememberMeToken.php @@ -101,6 +101,7 @@ public function __serialize(): array public function __unserialize(array $data): void { [$this->secret, $this->providerKey, $parentData] = $data; + $parentData = \is_array($parentData) ? $parentData : unserialize($parentData); parent::__unserialize($parentData); } } diff --git a/src/Symfony/Component/Security/Core/Authentication/Token/SwitchUserToken.php b/src/Symfony/Component/Security/Core/Authentication/Token/SwitchUserToken.php index 4177cee658f6..4390d68a6e5c 100644 --- a/src/Symfony/Component/Security/Core/Authentication/Token/SwitchUserToken.php +++ b/src/Symfony/Component/Security/Core/Authentication/Token/SwitchUserToken.php @@ -54,6 +54,7 @@ public function __serialize(): array public function __unserialize(array $data): void { [$this->originalToken, $parentData] = $data; + $parentData = \is_array($parentData) ? $parentData : unserialize($parentData); parent::__unserialize($parentData); } } diff --git a/src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php b/src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php index bf35c98d5580..fb1c2b33bb1e 100644 --- a/src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php +++ b/src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php @@ -99,6 +99,7 @@ public function __serialize(): array public function __unserialize(array $data): void { [$this->credentials, $this->providerKey, $parentData] = $data; + $parentData = \is_array($parentData) ? $parentData : unserialize($parentData); parent::__unserialize($parentData); } } diff --git a/src/Symfony/Component/Security/Core/Exception/AccountStatusException.php b/src/Symfony/Component/Security/Core/Exception/AccountStatusException.php index f3fa661c31f4..1b4e818a1157 100644 --- a/src/Symfony/Component/Security/Core/Exception/AccountStatusException.php +++ b/src/Symfony/Component/Security/Core/Exception/AccountStatusException.php @@ -53,6 +53,7 @@ public function __serialize(): array public function __unserialize(array $data): void { [$this->user, $parentData] = $data; + $parentData = \is_array($parentData) ? $parentData : unserialize($parentData); parent::__unserialize($parentData); } } diff --git a/src/Symfony/Component/Security/Core/Exception/CustomUserMessageAuthenticationException.php b/src/Symfony/Component/Security/Core/Exception/CustomUserMessageAuthenticationException.php index 203e8ba133da..879012c65f61 100644 --- a/src/Symfony/Component/Security/Core/Exception/CustomUserMessageAuthenticationException.php +++ b/src/Symfony/Component/Security/Core/Exception/CustomUserMessageAuthenticationException.php @@ -69,6 +69,7 @@ public function __serialize(): array public function __unserialize(array $data): void { [$parentData, $this->messageKey, $this->messageData] = $data; + $parentData = \is_array($parentData) ? $parentData : unserialize($parentData); parent::__unserialize($parentData); } } diff --git a/src/Symfony/Component/Security/Core/Exception/UsernameNotFoundException.php b/src/Symfony/Component/Security/Core/Exception/UsernameNotFoundException.php index 31dd486eec12..10c78b2056ae 100644 --- a/src/Symfony/Component/Security/Core/Exception/UsernameNotFoundException.php +++ b/src/Symfony/Component/Security/Core/Exception/UsernameNotFoundException.php @@ -71,6 +71,7 @@ public function __serialize(): array public function __unserialize(array $data): void { [$this->username, $parentData] = $data; + $parentData = \is_array($parentData) ? $parentData : unserialize($parentData); parent::__unserialize($parentData); } } diff --git a/src/Symfony/Component/Security/Guard/Token/PostAuthenticationGuardToken.php b/src/Symfony/Component/Security/Guard/Token/PostAuthenticationGuardToken.php index 1c58199c5b0f..511f455531ec 100644 --- a/src/Symfony/Component/Security/Guard/Token/PostAuthenticationGuardToken.php +++ b/src/Symfony/Component/Security/Guard/Token/PostAuthenticationGuardToken.php @@ -83,6 +83,7 @@ public function __serialize(): array public function __unserialize(array $data): void { [$this->providerKey, $parentData] = $data; + $parentData = \is_array($parentData) ? $parentData : unserialize($parentData); parent::__unserialize($parentData); } } From 9d5bb11ec92950f1caf60925f6b8be007f060750 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 19 May 2020 10:04:03 +0200 Subject: [PATCH 3/3] [PhpUnitBridge] fix bad detection of unsilenced deprecations --- src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php | 4 ++-- .../Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php index 7cccd8eb83a4..e9c8d19d8f0c 100644 --- a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php +++ b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php @@ -140,7 +140,7 @@ public static function register($mode = 0) $Test = $UtilPrefix.'Test'; - if (0 !== error_reporting()) { + if (error_reporting() & $type) { $group = 'unsilenced'; } elseif (0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') @@ -292,7 +292,7 @@ public static function collectDeprecations($outputFile) return \call_user_func(DeprecationErrorHandler::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context); } - $deprecations[] = array(error_reporting(), $msg, $file); + $deprecations[] = array(error_reporting() & $type, $msg, $file); return null; }); diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php index ee9378d140f7..b6d05cb0f102 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php @@ -352,7 +352,7 @@ public function handleError($type, $msg, $file, $line, $context = array()) if (\is_array($parsedMsg)) { $msg = $parsedMsg['deprecation']; } - if (error_reporting()) { + if (error_reporting() & $type) { $msg = 'Unsilenced deprecation: '.$msg; } $this->gatheredDeprecations[] = $msg;