EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Route consumes
Yurii6fd57e5c0b50
diff --git a/application/Espo/Core/Api/Route.php b/application/Espo/Core/Api/Route.phpindex fd966726ee..84f9632d3b 100644--- a/application/Espo/Core/Api/Route.php+++ b/application/Espo/Core/Api/Route.php@@ -35,6 +35,7 @@ class Route/*** @param array<string, string> $params+ * @param ?string[] $consumes* @param ?class-string<Action> $actionClassName*/public function __construct(@@ -43,6 +44,7 @@ class Routeprivate string $adjustedRoute,private array $params,private bool $noAuth,+ private ?array $consumes,private ?string $actionClassName,) {$this->method = strtoupper($method);@@ -89,4 +91,15 @@ class Route{return $this->noAuth;}++ /**+ * Allowed media types.+ *+ * @return ?string[]+ * @since 10.1.0+ */+ public function getConsumes(): ?array+ {+ return $this->consumes;+ }}diff --git a/application/Espo/Core/Api/RouteProcessor.php b/application/Espo/Core/Api/RouteProcessor.phpindex 622e3aca7d..40f30a5100 100644--- a/application/Espo/Core/Api/RouteProcessor.php+++ b/application/Espo/Core/Api/RouteProcessor.php@@ -36,6 +36,7 @@ use Espo\Core\Utils\Config;use Espo\Core\Utils\Log;use Espo\Core\ApplicationUser;+use Psr\Http\Message\ResponseInterface;use Psr\Http\Message\ResponseInterface as Psr7Response;use Psr\Http\Message\ServerRequestInterface as Psr7Request;@@ -103,6 +104,8 @@ class RouteProcessorResponseWrapper $response,): Psr7Response {+ $this->assertContentTypeAllowed($processData, $request);+$authRequired = !$processData->getRoute()->noAuth();$apiAuth = $this->authBuilderFactory@@ -283,4 +286,16 @@ class RouteProcessor$dispatcher->addMiddleware($middleware);}}++ /**+ * @throws BadRequest+ */+ private function assertContentTypeAllowed(ProcessData $processData, RequestWrapper $request): void+ {+ $consumes = $processData->getRoute()->getConsumes();++ if ($consumes !== null && !in_array($request->getContentType(), $consumes)) {+ throw new BadRequest("Not allowed Content-Type.");+ }+ }}diff --git a/application/Espo/Core/Portal/Utils/Route.php b/application/Espo/Core/Portal/Utils/Route.phpindex 0cff63316a..f5c3cf976f 100644--- a/application/Espo/Core/Portal/Utils/Route.php+++ b/application/Espo/Core/Portal/Utils/Route.php@@ -55,6 +55,7 @@ class Route extends BaseRouteadjustedRoute: $path,params: $route->getParams(),noAuth: $route->noAuth(),+ consumes: $route->getConsumes(),actionClassName: $route->getActionClassName(),);diff --git a/application/Espo/Core/Utils/Route.php b/application/Espo/Core/Utils/Route.phpindex 42020367b6..015c757529 100644--- a/application/Espo/Core/Utils/Route.php+++ b/application/Espo/Core/Utils/Route.php@@ -43,6 +43,7 @@ use Espo\Core\Utils\Resource\PathProvider;* method: string,* noAuth?: bool,* params?: array<string, mixed>,+ * consumes?: ?string[],* actionClassName: ?class-string<Action>* }*/@@ -83,6 +84,7 @@ class RouteadjustedRoute: $item['adjustedRoute'],params: $item['params'] ?? [],noAuth: $item['noAuth'] ?? false,+ consumes: $item['consumes'] ?? null,actionClassName: $item['actionClassName'] ?? null,);},diff --git a/application/Espo/Resources/routes.json b/application/Espo/Resources/routes.jsonindex 8df19ffde7..2a6e4cd0fe 100644--- a/application/Espo/Resources/routes.json+++ b/application/Espo/Resources/routes.json@@ -328,6 +328,9 @@{"route": "/Attachment/chunk/:id","method": "post",+ "consumes": [+ "multipart/form-data"+ ],"actionClassName": "Espo\\Tools\\Attachment\\Api\\PostChunk"},{diff --git a/schema/routes.json b/schema/routes.jsonindex b1d69c78d5..c3b3794946 100644--- a/schema/routes.json+++ b/schema/routes.json@@ -32,6 +32,20 @@"type": "string","description": "An action class. Should implement the interface Espo\\Core\\Api\\Action."},+ "consumes": {+ "type": "array",+ "items": {+ "type": "string",+ "description": "Media type.",+ "examples": [+ "application/json",+ "multipart/form-data",+ "application/x-www-form-urlencoded",+ "text/plain"+ ]+ },+ "description": "If defined, restricts content types allowed for the endpoint. If an empty array, the Content-Type header must be empty. As of v10.1."+ },"params": {"type": "object","description": "Parameters. If starts with a colon, then will be parsed from the URI. Example: `:id`.",diff --git a/tests/unit/Espo/Core/Api/AuthTest.php b/tests/unit/Espo/Core/Api/AuthTest.phpindex edac525389..8c658b4747 100644--- a/tests/unit/Espo/Core/Api/AuthTest.php+++ b/tests/unit/Espo/Core/Api/AuthTest.php@@ -226,9 +226,8 @@ class AuthTest extends TestCase* @param array<string, string> $headers* @param array<string, string> $queryParams* @param array<string, string> $cookieParams- * @noinspection PhpSameParameterValueInspection*/- private function createRequest(+ public static function createRequest(string $method,array $queryParams = [],array $headers = [],diff --git a/tests/unit/Espo/Core/Api/RouteProcessorTest.php b/tests/unit/Espo/Core/Api/RouteProcessorTest.phpnew file mode 100644index 0000000000..4ea869cb55--- /dev/null+++ b/tests/unit/Espo/Core/Api/RouteProcessorTest.php@@ -0,0 +1,109 @@+<?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\AuthBuilderFactory;+use Espo\Core\Api\ControllerActionProcessor;+use Espo\Core\Api\ErrorOutput;+use Espo\Core\Api\Method;+use Espo\Core\Api\MiddlewareProvider;+use Espo\Core\Api\ProcessData;+use Espo\Core\Api\Route;+use Espo\Core\Api\Route\ContentType;+use Espo\Core\Api\RouteProcessor;+use Espo\Core\ApplicationUser;+use Espo\Core\Authentication\AuthenticationFactory;+use Espo\Core\Exceptions\BadRequest;+use Espo\Core\InjectableFactory;+use Espo\Core\Utils\Config;+use Espo\Core\Utils\Log;+use PHPUnit\Framework\TestCase;+use Slim\Psr7\Factory\ResponseFactory;++class RouteProcessorTest extends TestCase+{+ public function testConsumesFail(): void+ {+ $request = AuthTest::createRequest(+ method: Method::POST,+ headers: [+ 'Content-Type' => ContentType::TEXT_PLAIN,+ ],+ );++ $route = new Route(+ method: Method::POST,+ route: '/test',+ adjustedRoute: '/test',+ params: [],+ noAuth: false,+ consumes: [+ ContentType::APPLICATION_JSON,+ ],+ actionClassName: null,+ );++ $errorOutput = $this->createMock(ErrorOutput::class);++ $processor = new RouteProcessor(+ authenticationFactory: $this->createMock(AuthenticationFactory::class),+ authBuilderFactory: $this->createMock(AuthBuilderFactory::class),+ errorOutput: $errorOutput,+ config: $this->createMock(Config::class),+ log: $this->createMock(Log::class),+ applicationUser: $this->createMock(ApplicationUser::class),+ actionProcessor: $this->createMock(ControllerActionProcessor::class),+ middlewareProvider: $this->createMock(MiddlewareProvider::class),+ injectableFactory: $this->createMock(InjectableFactory::class),+ );++ $response = (new ResponseFactory())->createResponse();++ $errorOutput->expects(self::once())+ ->method('process')+ ->with(+ self::anything(),+ self::anything(),+ $this->callback(function ($e) {+ return $e instanceof BadRequest;+ })+ );++ $processor->process(+ processData: new ProcessData(+ route: $route,+ basePath: '',+ routeParams: [],+ ),+ request: $request->toPsr7(),+ response: $response,+ );+ }+}diff --git a/tests/unit/Espo/Core/Utils/RouteTest.php b/tests/unit/Espo/Core/Utils/RouteTest.phpindex 3a255c83ca..cfd574b20c 100644--- a/tests/unit/Espo/Core/Utils/RouteTest.php+++ b/tests/unit/Espo/Core/Utils/RouteTest.php@@ -189,12 +189,13 @@ class RouteTest extends TestCase$expectedItemList = array_map(function (array $item) {return new RouteItem(- $item['method'],- $item['route'],- $item['adjustedRoute'],- $item['params'] ?? [],- $item['noAuth'] ?? false,- null+ method: $item['method'],+ route: $item['route'],+ adjustedRoute: $item['adjustedRoute'],+ params: $item['params'] ?? [],+ noAuth: $item['noAuth'] ?? false,+ consumes: null,+ actionClassName: null,);},$expected@@ -292,12 +293,13 @@ class RouteTest extends TestCase$expectedItemList = array_map(function (array $item) {return new RouteItem(- $item['method'],- $item['route'],- $item['adjustedRoute'],- $item['params'] ?? [],- $item['noAuth'] ?? false,- null+ method: $item['method'],+ route: $item['route'],+ adjustedRoute: $item['adjustedRoute'],+ params: $item['params'] ?? [],+ noAuth: $item['noAuth'] ?? false,+ consumes: null,+ actionClassName: null,);},$expected@@ -395,12 +397,13 @@ class RouteTest extends TestCase$expectedItemList = array_map(function (array $item) {return new RouteItem(- $item['method'],- $item['route'],- $item['adjustedRoute'],- $item['params'] ?? [],- $item['noAuth'] ?? false,- false+ method: $item['method'],+ route: $item['route'],+ adjustedRoute: $item['adjustedRoute'],+ params: $item['params'] ?? [],+ noAuth: $item['noAuth'] ?? false,+ consumes: null,+ actionClassName: null,);},$expected