Skip to content

EspoCRM – Open Source CRM Application

Ignore non-header authorization for forms

Yuriie4b1b0cb7caa
diff --git a/application/Espo/Core/Api/Auth.php b/application/Espo/Core/Api/Auth.php
index fea0070f46..642ea119b9 100644
--- a/application/Espo/Core/Api/Auth.php
+++ b/application/Espo/Core/Api/Auth.php
@@ -29,6 +29,7 @@
namespace Espo\Core\Api;
+use Espo\Core\Api\Route\ContentType;
use Espo\Core\Authentication\HeaderKey;
use Espo\Core\Authentication\Login\MethodResolver;
use Espo\Core\Exceptions\BadRequest;
@@ -80,7 +81,7 @@ class Auth
$hasAuthData = $username || $authenticationMethod;
- if (!$hasAuthData) {
+ if (!$hasAuthData && !$this->toIgnoreNonHeaderAuthorizationData($request)) {
$password = self::obtainTokenFromCookies($request);
if ($password) {
@@ -278,6 +279,10 @@ class Auth
return [null, null];
}
+ if ($this->toIgnoreNonHeaderAuthorizationData($request)) {
+ return [null, null];
+ }
+
if (
$request->getServerParam('PHP_AUTH_USER') &&
$request->getServerParam('PHP_AUTH_PW')
@@ -351,4 +356,16 @@ class Auth
$request->getHeader('Sec-Fetch-Mode') === 'navigate' &&
$request->getHeader('Sec-Fetch-Dest') === 'document';
}
+
+ private function toIgnoreNonHeaderAuthorizationData(Request $request): bool
+ {
+ return
+ !$this->isEntryPoint &&
+ $request->getHeader('Sec-Fetch-Mode') === 'navigate' &&
+ in_array($request->getContentType(), [
+ ContentType::MULTIPART_FORM_DATA,
+ ContentType::APPLICATION_X_WWW_FORM_URLENCODED,
+ ContentType::TEXT_PLAIN,
+ ]);
+ }
}
diff --git a/tests/unit/Espo/Core/Api/AuthTest.php b/tests/unit/Espo/Core/Api/AuthTest.php
index 3753446c7e..edac525389 100644
--- a/tests/unit/Espo/Core/Api/AuthTest.php
+++ b/tests/unit/Espo/Core/Api/AuthTest.php
@@ -33,6 +33,7 @@ use Espo\Core\Api\Auth;
use Espo\Core\Api\Method;
use Espo\Core\Api\RequestWrapper;
use Espo\Core\Api\Response;
+use Espo\Core\Api\Route\ContentType;
use Espo\Core\Authentication\Authentication;
use Espo\Core\Authentication\AuthenticationData;
use Espo\Core\Authentication\HeaderKey;
@@ -43,6 +44,8 @@ use Espo\Core\Utils\Json;
use Espo\Core\Utils\Log;
use Espo\Entities\User;
use PHPUnit\Framework\TestCase;
+use Psr\Http\Message\ServerRequestInterface;
+use RuntimeException;
use Slim\Psr7\Factory\RequestFactory;
use Slim\Psr7\Factory\StreamFactory;
@@ -146,6 +149,83 @@ class AuthTest extends TestCase
}
/**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testCookie(): void
+ {
+ $request = $this->createRequest(
+ method: Method::GET,
+ cookieParams: [
+ 'auth-token' => '001',
+ ],
+ );
+
+ $response = $this->createMock(Response::class);
+
+ $authentication = $this->createMock(Authentication::class);
+ $authentication
+ ->expects(self::once())
+ ->method('login')
+ ->with(
+ AuthenticationData::create()
+ ->withPassword('001')
+ ->withByTokenOnly(true)
+ )
+ ->willReturn(
+ Result::success($this->createMock(User::class))
+ );
+
+ $auth = new Auth(
+ log: $this->createMock(Log::class),
+ authentication: $authentication,
+ methodResolver: $this->createMock(MethodResolver::class),
+ authRequired: true,
+ );
+
+ $result = $auth->process($request, $response);
+
+ $this->assertTrue($result->isResolved());
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testIgnoreCookie(): void
+ {
+ $request = $this->createRequest(
+ method: Method::POST,
+ headers: [
+ 'Sec-Fetch-Mode' => 'navigate',
+ 'Content-Type' => ContentType::MULTIPART_FORM_DATA . '; charset=utf-8'
+ ],
+ cookieParams: [
+ 'auth-token' => '001',
+ ]
+ );
+
+ $response = $this->createMock(Response::class);
+
+ $authentication = $this->createMock(Authentication::class);
+ $authentication
+ ->expects(self::never())
+ ->method('login');
+
+ $auth = new Auth(
+ log: $this->createMock(Log::class),
+ authentication: $authentication,
+ methodResolver: $this->createMock(MethodResolver::class),
+ authRequired: true,
+ );
+
+ $result = $auth->process($request, $response);
+
+ $this->assertFalse($result->isResolved());
+ }
+
+ /**
+ * @param array<string, string> $headers
+ * @param array<string, string> $queryParams
+ * @param array<string, string> $cookieParams
* @noinspection PhpSameParameterValueInspection
*/
private function createRequest(
@@ -153,15 +233,23 @@ class AuthTest extends TestCase
array $queryParams = [],
array $headers = [],
?string $body = null,
+ array $cookieParams = [],
): RequestWrapper {
$request = (new RequestFactory())
->createRequest($method, 'http://localhost/?' . http_build_query($queryParams));
+ if (!$request instanceof ServerRequestInterface) {
+ throw new RuntimeException();
+ }
+
+
foreach ($headers as $name => $value) {
$request = $request->withHeader($name, $value);
}
+ $request = $request->withCookieParams($cookieParams);
+
if ($body) {
$request = $request->withBody(
(new StreamFactory)->createStream($body)