Skip to content

EspoCRM – Open Source CRM Application

Expose exceptions param

Yurii8b4f900085d2
diff --git a/application/Espo/Core/Api/ConfigDataProvider.php b/application/Espo/Core/Api/ConfigDataProvider.php
new file mode 100644
index 0000000000..490297cadd
--- /dev/null
+++ b/application/Espo/Core/Api/ConfigDataProvider.php
@@ -0,0 +1,50 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Api;
+
+use Espo\Core\Utils\Config;
+
+/**
+ * @since 10.1.0
+ */
+class ConfigDataProvider
+{
+ public function __construct(
+ private Config $config,
+ ) {}
+
+ /**
+ * Important. Should never be enabled on production.
+ */
+ public function exposeExceptions(): bool
+ {
+ return (bool) $this->config->get('apiExposeExceptions');
+ }
+}
diff --git a/application/Espo/Core/Api/ErrorOutput.php b/application/Espo/Core/Api/ErrorOutput.php
index a730586a2e..b25af2354b 100644
--- a/application/Espo/Core/Api/ErrorOutput.php
+++ b/application/Espo/Core/Api/ErrorOutput.php
@@ -38,7 +38,6 @@ use Espo\Core\Exceptions\HasLogLevel;
use Espo\Core\Exceptions\HasLogMessage;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Utils\Log;
-
use Espo\ORM\Exceptions\ValidationException;
use LogicException;
use Psr\Log\LogLevel;
@@ -88,8 +87,12 @@ class ErrorOutput
ValidationException::class => 409,
];
- public function __construct(private Log $log)
- {}
+ public const string HEADER_STATUS_REASON = 'X-Status-Reason';
+
+ public function __construct(
+ private Log $log,
+ private ConfigDataProvider $config,
+ ) {}
public function process(
Request $request,
@@ -136,8 +139,20 @@ class ErrorOutput
$response->setStatus($statusCode);
+ $statusReason = null;
+
if ($this->toPrintExceptionStatusReason($exception)) {
- $response->setHeader('X-Status-Reason', $this->stripInvalidCharactersFromHeaderValue($message));
+ $statusReason = $this->stripInvalidCharactersFromHeaderValue($message);
+ } else if ($this->config->exposeExceptions()) {
+ $strippedMessage = $this->stripInvalidCharactersFromHeaderValue($message);
+
+ if ($strippedMessage) {
+ $statusReason = 'Exposed: ' . $strippedMessage;
+ }
+ }
+
+ if ($statusReason !== null) {
+ $response->setHeader(self::HEADER_STATUS_REASON, $statusReason);
}
$this->printBody(
@@ -232,7 +247,6 @@ class ErrorOutput
private function toPrintExceptionStatusReason(Throwable $exception): bool
{
foreach ($this->printStatusReasonExceptionClassNameList as $clasName) {
-
if ($exception instanceof ($clasName)) {
return true;
}
diff --git a/application/Espo/Resources/defaults/systemConfig.php b/application/Espo/Resources/defaults/systemConfig.php
index 06b9d890b6..0890e17a6a 100644
--- a/application/Espo/Resources/defaults/systemConfig.php
+++ b/application/Espo/Resources/defaults/systemConfig.php
@@ -123,6 +123,7 @@ return [
'rabbitMq',
'redis',
'eventTransport',
+ 'apiExposeExceptions',
],
'adminItems' => [
'devMode',
diff --git a/tests/unit/Espo/Core/Api/ErrorOutputTest.php b/tests/unit/Espo/Core/Api/ErrorOutputTest.php
new file mode 100644
index 0000000000..152584e46a
--- /dev/null
+++ b/tests/unit/Espo/Core/Api/ErrorOutputTest.php
@@ -0,0 +1,95 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\unit\Espo\Core\Api;
+
+use Espo\Core\Api\ConfigDataProvider;
+use Espo\Core\Api\ErrorOutput;
+use Espo\Core\Api\Request;
+use Espo\Core\Api\Response;
+use Espo\Core\Utils\Log;
+use PHPUnit\Framework\TestCase;
+use RuntimeException;
+
+class ErrorOutputTest extends TestCase
+{
+ public function testExceptionNotExposed(): void
+ {
+ $e = new RuntimeException();
+
+ $errorOutput = $this->createErrorOutput(exposeExceptions: false);
+
+ $request = $this->createMock(Request::class);
+ $response = $this->createMock(Response::class);
+
+ $response->expects(self::never())
+ ->method('setHeader')
+ ->with(ErrorOutput::HEADER_STATUS_REASON);
+
+ $errorOutput->process($request, $response, $e);
+ }
+
+ public function testExceptionExposed(): void
+ {
+ $e = new RuntimeException("Test.");
+
+ $errorOutput = $this->createErrorOutput(exposeExceptions: true);
+
+ $request = $this->createMock(Request::class);
+ $response = $this->createMock(Response::class);
+
+ $response->expects(self::once())
+ ->method('setHeader')
+ ->with(ErrorOutput::HEADER_STATUS_REASON);
+
+ $errorOutput->process($request, $response, $e);
+ }
+
+ /**
+ * @return ConfigDataProvider
+ */
+ private function createConfig(bool $exposeExceptions): ConfigDataProvider
+ {
+ $config = $this->createMock(ConfigDataProvider::class);
+ $config
+ ->expects(self::any())
+ ->method('exposeExceptions')
+ ->willReturn($exposeExceptions);
+
+ return $config;
+ }
+
+ private function createErrorOutput(bool $exposeExceptions): ErrorOutput
+ {
+ return new ErrorOutput(
+ log: $this->createMock(Log::class),
+ config: $this->createConfig(exposeExceptions: $exposeExceptions),
+ );
+ }
+}