diff --git a/core/lib/Drupal/Core/Access/NoCookieAuthCheck.php b/core/lib/Drupal/Core/Access/NoCookieAuthCheck.php new file mode 100644 index 0000000..23474b3 --- /dev/null +++ b/core/lib/Drupal/Core/Access/NoCookieAuthCheck.php @@ -0,0 +1,83 @@ +sessionConfiguration = $session_configuration; + } + + /** + * {@inheritdoc} + */ + public function applies(Route $route) { + return $this->appliesToRoute($route); + } + + /** + * Checks whether an incoming request has cookie auth credentials. + * + * This check is only applied to routes where cookie auth is *not* allowed. + */ + public function access(Request $request) { + if ($this->hasCredentials($request)) { + return AccessResult::forbidden()->setCacheable(FALSE); + } + else { + return AccessResult::allowed()->setCacheable(FALSE); + } + } + + /** + * Returns whether cookie auth is enabled on a route. + * + * @param \Symfony\Component\Routing\Route $route + * The route to be checked. + * + * @return bool + * TRUE if cookie auth is enabled, FALSE otherwise. + */ + protected function appliesToRoute(Route $route) { + return $route->hasOption('_auth') && !in_array('cookie', $route->getOption('_auth')); + } + + /** + * Returns whether cookie auth credentials are on the request. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The request to be checked. + * + * @return bool + * TRUE if cookie auth credentials are on the request, FALSE otherwise. + */ + protected function hasCredentials(Request $request) { + return $request->hasSession() && $this->sessionConfiguration->hasSession($request); + } + +} diff --git a/core/modules/basic_auth/src/Tests/Authentication/BasicAuthTest.php b/core/modules/basic_auth/src/Tests/Authentication/BasicAuthTest.php index a38b696..227f11a 100644 --- a/core/modules/basic_auth/src/Tests/Authentication/BasicAuthTest.php +++ b/core/modules/basic_auth/src/Tests/Authentication/BasicAuthTest.php @@ -50,6 +50,11 @@ public function testBasicAuth() { $this->drupalGet('admin'); $this->assertResponse('403', 'No authentication prompt for routes not explicitly defining authentication providers.'); + $account = $this->drupalCreateUser(array('access administration pages')); + + $this->basicAuthGet(Url::fromRoute('system.admin'), $account->getUsername(), $account->pass_raw); + $this->assertNoLink('Log out', 0, 'User is not logged in'); + $this->assertResponse('403', 'No basic authentication for routes not explicitly defining authentication providers.'); $this->curlClose(); } diff --git a/core/modules/rest/src/Tests/AuthTest.php b/core/modules/rest/src/Tests/AuthTest.php index 37c830f..706f357 100644 --- a/core/modules/rest/src/Tests/AuthTest.php +++ b/core/modules/rest/src/Tests/AuthTest.php @@ -53,9 +53,10 @@ public function testRead() { $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); - // Read the resource with session cookie authentication. + // Try to read the resource with session cookie authentication, which is + // not enabled and should not work. $this->httpRequest($entity->urlInfo(), 'GET', NULL, $this->defaultMimeType); - $this->assertResponse('200', 'HTTP response code is 200 for successfully authenticated requests.'); + $this->assertResponse('403', 'HTTP response code is 403 when the request was authenticated by the wrong authentication provider.'); // Ensure that cURL settings/headers aren't carried over to next request. unset($this->curlHandle); diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index 2ef1a5c..dca89ea 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -20,6 +20,11 @@ services: arguments: ['@session_configuration'] tags: - { name: authentication_provider, priority: 0 } + access_check.no_cookie_auth: + class: Drupal\Core\Access\NoCookieAuthCheck + arguments: ['@session_configuration'] + tags: + - { name: access_check, needs_incoming_request: TRUE } cache_context.user: class: Drupal\user\Cache\UserCacheContext arguments: ['@current_user']