From 0196487d65db2918cd15164dc1a4f87aa0049858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Alfonso=20Pym=20Mej=C3=ADa?= Date: Thu, 22 Oct 2026 08:51:19 -0600 Subject: [PATCH 1/2] fix(template): skip setting status code once headers are sent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Mario Alfonso Pym Mejía --- lib/private/Template/TemplateManager.php | 13 ++++++-- tests/lib/Template/TemplateManagerTest.php | 38 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/lib/Template/TemplateManagerTest.php diff --git a/lib/private/Template/TemplateManager.php b/lib/private/Template/TemplateManager.php index 0f0af51798a1d..ed460708f2ae5 100644 --- a/lib/private/Template/TemplateManager.php +++ b/lib/private/Template/TemplateManager.php @@ -66,7 +66,7 @@ public function printErrorPage(string $error_msg, string $hint = '', int $status } $errors = [['error' => $error_msg, 'hint' => $hint]]; - http_response_code($statusCode); + $this->setStatusCode($statusCode); try { // Try rendering themed html error page $response = new TemplateResponse( @@ -111,7 +111,7 @@ public function printErrorPage(string $error_msg, string $hint = '', int $status */ public function printExceptionErrorPage(\Throwable $exception, int $statusCode = 503): never { $debug = false; - http_response_code($statusCode); + $this->setStatusCode($statusCode); try { $debug = (bool)Server::get(SystemConfig::class)->getValue('debug', false); $serverLogsDocumentation = Server::get(SystemConfig::class)->getValue('documentation_url.server_logs', ''); @@ -146,6 +146,15 @@ public function printExceptionErrorPage(\Throwable $exception, int $statusCode = die(); } + /** + * Setting the status code once output has started only triggers a warning + */ + private function setStatusCode(int $statusCode): void { + if (!headers_sent()) { + http_response_code($statusCode); + } + } + /** * @psalm-taint-escape has_quotes * @psalm-taint-escape html diff --git a/tests/lib/Template/TemplateManagerTest.php b/tests/lib/Template/TemplateManagerTest.php new file mode 100644 index 0000000000000..8e7433ee454cb --- /dev/null +++ b/tests/lib/Template/TemplateManagerTest.php @@ -0,0 +1,38 @@ +&1'; + return (string)shell_exec($command); + } + + public function testHttpResponseCodeWarnsAfterOutput(): void { + $this->assertStringContainsString('headers already sent', $this->runAfterOutput('http_response_code(500);')); + } + + public function testSetStatusCodeAfterOutput(): void { + $output = $this->runAfterOutput( + '$templateManager = (new ReflectionClass(\OC\Template\TemplateManager::class))->newInstanceWithoutConstructor();' + . ' (new ReflectionMethod($templateManager, "setStatusCode"))->invoke($templateManager, 500);' + ); + + $this->assertSame('output', $output); + } +} From 8bf834c1914e9ec1296d03d84e8668e824a857d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Alfonso=20Pym=20Mej=C3=ADa?= Date: Wed, 11 Nov 2026 12:41:48 -0600 Subject: [PATCH 2/2] fix(template): address review feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove the subprocess based test and the redundant helper comment. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Mario Alfonso Pym Mejía --- lib/private/Template/TemplateManager.php | 3 -- tests/lib/Template/TemplateManagerTest.php | 38 ---------------------- 2 files changed, 41 deletions(-) delete mode 100644 tests/lib/Template/TemplateManagerTest.php diff --git a/lib/private/Template/TemplateManager.php b/lib/private/Template/TemplateManager.php index ed460708f2ae5..c30eaa0886c3e 100644 --- a/lib/private/Template/TemplateManager.php +++ b/lib/private/Template/TemplateManager.php @@ -146,9 +146,6 @@ public function printExceptionErrorPage(\Throwable $exception, int $statusCode = die(); } - /** - * Setting the status code once output has started only triggers a warning - */ private function setStatusCode(int $statusCode): void { if (!headers_sent()) { http_response_code($statusCode); diff --git a/tests/lib/Template/TemplateManagerTest.php b/tests/lib/Template/TemplateManagerTest.php deleted file mode 100644 index 8e7433ee454cb..0000000000000 --- a/tests/lib/Template/TemplateManagerTest.php +++ /dev/null @@ -1,38 +0,0 @@ -&1'; - return (string)shell_exec($command); - } - - public function testHttpResponseCodeWarnsAfterOutput(): void { - $this->assertStringContainsString('headers already sent', $this->runAfterOutput('http_response_code(500);')); - } - - public function testSetStatusCodeAfterOutput(): void { - $output = $this->runAfterOutput( - '$templateManager = (new ReflectionClass(\OC\Template\TemplateManager::class))->newInstanceWithoutConstructor();' - . ' (new ReflectionMethod($templateManager, "setStatusCode"))->invoke($templateManager, 500);' - ); - - $this->assertSame('output', $output); - } -}