EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Entry point consumes param
Yurii57b6659c05f4
diff --git a/application/Espo/Core/EntryPoint/EntryPointManager.php b/application/Espo/Core/EntryPoint/EntryPointManager.phpindex 0c07e2f1cd..71d5463567 100644--- a/application/Espo/Core/EntryPoint/EntryPointManager.php+++ b/application/Espo/Core/EntryPoint/EntryPointManager.php@@ -117,7 +117,8 @@ class EntryPointManager* className?: ?class-string<EntryPoint>,* notExposed?: bool,* noAuth?: bool,- * allowedMethods?: string[],+ * allowedMethods?: ?string[],+ * consumes?: ?string[]* } $params*/$params = $this->metadata->get("app.entryPoints.$name") ?? [];@@ -127,6 +128,7 @@ class EntryPointManagernotExposed: $params['notExposed'] ?? false,noAuth: $params['noAuth'] ?? false,allowedMethods: $params['allowedMethods'] ?? null,+ consumes: $params['consumes'] ?? null,);}}diff --git a/application/Espo/Core/EntryPoint/MetaParams.php b/application/Espo/Core/EntryPoint/MetaParams.phpindex 6dde3bf669..e4b25f9274 100644--- a/application/Espo/Core/EntryPoint/MetaParams.php+++ b/application/Espo/Core/EntryPoint/MetaParams.php@@ -37,11 +37,13 @@ readonly class MetaParams/*** @param ?class-string<EntryPoint> $className* @param ?string[] $allowedMethods+ * @param ?string[] $consumes*/public function __construct(public ?string $className = null,public bool $notExposed = false,public bool $noAuth = false,public ?array $allowedMethods = null,+ public ?array $consumes = null,) {}}diff --git a/application/Espo/Core/EntryPoint/Starter.php b/application/Espo/Core/EntryPoint/Starter.phpindex 7bbee17a31..3cfbb3e975 100644--- a/application/Espo/Core/EntryPoint/Starter.php+++ b/application/Espo/Core/EntryPoint/Starter.php@@ -78,7 +78,7 @@ class Startertry {$name = $this->getName($params, $request);- $this->checkStart($name, $params, $request->getMethod());+ $this->checkStart($name, $params, $request);} catch (BadRequest|Forbidden $e) {$this->errorOutput->processWithBodyPrinting($request, $response, $e);$this->outputEmitter->emit($response);@@ -245,8 +245,10 @@ class Starter* @throws BadRequest* @throws Forbidden*/- private function checkStart(string $name, Params $params, string $method): void+ private function checkStart(string $name, Params $params, RequestWrapper $request): void{+ $method = $request->getMethod();+$metaParams = $this->entryPointManager->getMetaParams($name);if ($metaParams->notExposed && $params->name === null) {@@ -260,5 +262,9 @@ class Starter} else if ($method !== Method::GET) {throw new BadRequest("Only GET requests allowed for the entry point.");}++ if ($metaParams->consumes !== null && !in_array($request->getContentType(), $metaParams->consumes)) {+ throw new BadRequest("Not allowed Content-Type.");+ }}}diff --git a/schema/metadata/app/entryPoints.json b/schema/metadata/app/entryPoints.jsonindex 605d8331e9..e7050198de 100644--- a/schema/metadata/app/entryPoints.json+++ b/schema/metadata/app/entryPoints.json@@ -34,6 +34,20 @@},"description": "Allowed HTTP methods.","default": ["get"]+ },+ "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 entry point. If an empty array, the Content-Type header must be empty."}}}diff --git a/tests/integration/Espo/Core/EntryPoint/EntryPointTest.php b/tests/integration/Espo/Core/EntryPoint/EntryPointTest.phpindex 5f3620e890..d1412ef88c 100644--- a/tests/integration/Espo/Core/EntryPoint/EntryPointTest.php+++ b/tests/integration/Espo/Core/EntryPoint/EntryPointTest.php@@ -257,6 +257,102 @@ class EntryPointTest extends BaseTestCase$this->assertFalse($applicationState->hasUser());}+ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ public function testConsumesFail(): void+ {+ $metadata = $this->getMetadata();+ $metadata->set('app', 'entryPoints', [+ 'oauthCallback' => [+ 'consumes' => ['application/json'],+ ],+ ]);+ $metadata->save();++ $this->reCreateApplication(reuse: true, noUser: true);++ $outputEmitter = $this->createMock(OutputEmitter::class);+ $errorOutput = $this->createMock(ErrorOutput::class);++ $request = $this->createRequest(+ method: Method::GET,+ queryParams: [+ 'entryPoint' => 'oauthCallback',+ ],+ headers: [+ 'Content-Type' => 'text/plain',+ ],+ );++ $response = $this->createMock(ResponseWrapper::class);++ $starter = $this->createStarter(+ request: $request,+ response: $response,+ outputEmitter: $outputEmitter,+ errorOutput: $errorOutput,+ );++ $errorOutput+ ->expects(self::once())+ ->method('processWithBodyPrinting');++ $starter->start();++ $applicationState = $this->getContainer()->getByClass(ApplicationState::class);++ $this->assertFalse($applicationState->hasUser());+ }++ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ public function testConsumesSuccess(): void+ {+ $metadata = $this->getMetadata();+ $metadata->set('app', 'entryPoints', [+ 'oauthCallback' => [+ 'consumes' => ['application/json'],+ ],+ ]);+ $metadata->save();++ $this->reCreateApplication(reuse: true, noUser: true);++ $outputEmitter = $this->createMock(OutputEmitter::class);+ $errorOutput = $this->createMock(ErrorOutput::class);++ $request = $this->createRequest(+ method: Method::GET,+ queryParams: [+ 'entryPoint' => 'oauthCallback',+ ],+ headers: [+ 'Content-Type' => 'application/json',+ ],+ );++ $response = $this->createMock(ResponseWrapper::class);++ $starter = $this->createStarter(+ request: $request,+ response: $response,+ outputEmitter: $outputEmitter,+ errorOutput: $errorOutput,+ );++ $errorOutput+ ->expects(self::never())+ ->method('processWithBodyPrinting');++ $starter->start();++ $applicationState = $this->getContainer()->getByClass(ApplicationState::class);++ $this->assertTrue($applicationState->hasUser());+ }+private function createStarter(RequestWrapper $request,ResponseWrapper $response,