1. 8.5.x core/lib/Drupal/Core/Access/CsrfRequestHeaderAccessCheck.php CsrfRequestHeaderAccessCheck
  2. 8.2.x core/lib/Drupal/Core/Access/CsrfRequestHeaderAccessCheck.php CsrfRequestHeaderAccessCheck
  3. 8.3.x core/lib/Drupal/Core/Access/CsrfRequestHeaderAccessCheck.php CsrfRequestHeaderAccessCheck
  4. 8.4.x core/lib/Drupal/Core/Access/CsrfRequestHeaderAccessCheck.php CsrfRequestHeaderAccessCheck
  5. 8.6.x core/lib/Drupal/Core/Access/CsrfRequestHeaderAccessCheck.php CsrfRequestHeaderAccessCheck

Access protection against CSRF attacks.

Hierarchy

Expanded class hierarchy of CsrfRequestHeaderAccessCheck

1 file declares its use of CsrfRequestHeaderAccessCheck
CsrfTokenController.php in core/modules/system/src/Controller/CsrfTokenController.php
1 string reference to 'CsrfRequestHeaderAccessCheck'
core.services.yml in core/core.services.yml
core/core.services.yml

File

core/lib/Drupal/Core/Access/CsrfRequestHeaderAccessCheck.php, line 13

Namespace

Drupal\Core\Access
View source
class CsrfRequestHeaderAccessCheck implements AccessCheckInterface {

  /**
   * A string key that will used to designate the token used by this class.
   */
  const TOKEN_KEY = 'X-CSRF-Token request header';

  /**
   * The session configuration.
   *
   * @var \Drupal\Core\Session\SessionConfigurationInterface
   */
  protected $sessionConfiguration;

  /**
   * The token generator.
   *
   * @var \Drupal\Core\Access\CsrfTokenGenerator
   */
  protected $csrfToken;

  /**
   * Constructs a new rest CSRF access check.
   *
   * @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration
   *   The session configuration.
   * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token
   *   The token generator.
   */
  public function __construct(SessionConfigurationInterface $session_configuration, CsrfTokenGenerator $csrf_token) {
    $this->sessionConfiguration = $session_configuration;
    $this->csrfToken = $csrf_token;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(Route $route) {
    $requirements = $route
      ->getRequirements();

    // Check for current requirement _csrf_request_header_token and deprecated
    // REST requirement.
    $applicable_requirements = [
      '_csrf_request_header_token',
      // @todo Remove _access_rest_csrf in Drupal 9.0.0.
      '_access_rest_csrf',
    ];
    $requirement_keys = array_keys($requirements);
    if (array_intersect($applicable_requirements, $requirement_keys)) {
      if (isset($requirements['_method'])) {

        // There could be more than one method requirement separated with '|'.
        $methods = explode('|', $requirements['_method']);

        // CSRF protection only applies to write operations, so we can filter
        // out any routes that require reading methods only.
        $write_methods = array_diff($methods, [
          'GET',
          'HEAD',
          'OPTIONS',
          'TRACE',
        ]);
        if (empty($write_methods)) {
          return FALSE;
        }
      }

      // No method requirement given, so we run this access check to be on the
      // safe side.
      return TRUE;
    }
  }

  /**
   * Checks access.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(Request $request, AccountInterface $account) {
    $method = $request
      ->getMethod();

    // This check only applies if
    // 1. this is a write operation
    // 2. the user was successfully authenticated and
    // 3. the request comes with a session cookie.
    if (!in_array($method, [
      'GET',
      'HEAD',
      'OPTIONS',
      'TRACE',
    ]) && $account
      ->isAuthenticated() && $this->sessionConfiguration
      ->hasSession($request)) {
      if (!$request->headers
        ->has('X-CSRF-Token')) {
        return AccessResult::forbidden()
          ->setReason('X-CSRF-Token request header is missing')
          ->setCacheMaxAge(0);
      }
      $csrf_token = $request->headers
        ->get('X-CSRF-Token');

      // @todo Remove validate call using 'rest' in 8.3.
      //   Kept here for sessions active during update.
      if (!$this->csrfToken
        ->validate($csrf_token, self::TOKEN_KEY) && !$this->csrfToken
        ->validate($csrf_token, 'rest')) {
        return AccessResult::forbidden()
          ->setReason('X-CSRF-Token request header is invalid')
          ->setCacheMaxAge(0);
      }
    }

    // Let other access checkers decide if the request is legit.
    return AccessResult::allowed()
      ->setCacheMaxAge(0);
  }

}

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description Overrides
CsrfRequestHeaderAccessCheck::$csrfToken protected property The token generator.
CsrfRequestHeaderAccessCheck::$sessionConfiguration protected property The session configuration.
CsrfRequestHeaderAccessCheck::access public function Checks access.
CsrfRequestHeaderAccessCheck::applies public function Declares whether the access check applies to a specific route or not. Overrides AccessCheckInterface::applies
CsrfRequestHeaderAccessCheck::TOKEN_KEY constant A string key that will used to designate the token used by this class.
CsrfRequestHeaderAccessCheck::__construct public function Constructs a new rest CSRF access check.