You are here

class Ie9CspSubscriber in Content-Security-Policy 8

Alter CSP policy for IE9 Compatibility.

Hierarchy

  • class \Drupal\csp\EventSubscriber\Ie9CspSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of Ie9CspSubscriber

1 file declares its use of Ie9CspSubscriber
Ie9CspSubscriberTest.php in tests/src/Unit/EventSubscriber/Ie9CspSubscriberTest.php
1 string reference to 'Ie9CspSubscriber'
csp.services.yml in ./csp.services.yml
csp.services.yml
1 service uses Ie9CspSubscriber
csp.ie9_csp_subscriber in ./csp.services.yml
Drupal\csp\EventSubscriber\Ie9CspSubscriber

File

src/EventSubscriber/Ie9CspSubscriber.php, line 15

Namespace

Drupal\csp\EventSubscriber
View source
class Ie9CspSubscriber implements EventSubscriberInterface {

  /**
   * The Module Handler Service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  private $moduleHandler;

  /**
   * The Config Factory Service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  private $configFactory;

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[CspEvents::POLICY_ALTER] = [
      'onCspPolicyAlter',
    ];
    return $events;
  }

  /**
   * Ie9CspSubscriber constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The Config Factory service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The Module Handler service.
   */
  public function __construct(ConfigFactoryInterface $configFactory, ModuleHandlerInterface $moduleHandler) {
    $this->configFactory = $configFactory;
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * Alter CSP policy for compatibility with IE9 if needed.
   *
   * Prior to Drupal 8.7, in order to support IE9, CssCollectionRenderer
   * outputs more than 31 stylesheets as inline @import statements.
   * Since checking the actual number of stylesheets included on the page is
   * more difficult, just check the optimization settings, as in
   * HtmlResponseAttachmentsProcessor::processAssetLibraries()
   *
   * @param \Drupal\csp\Event\PolicyAlterEvent $alterEvent
   *   The Policy Alter event.
   *
   * @see https://www.drupal.org/node/2993171
   * @see CssCollectionRenderer::render()
   * @see HtmlResponseAttachmentsProcessor::processAssetLibraries()
   */
  public function onCspPolicyAlter(PolicyAlterEvent $alterEvent) {
    if ((version_compare(\Drupal::VERSION, '8.7', '<') || $this->moduleHandler
      ->moduleExists('ie9')) && (defined('MAINTENANCE_MODE') || !$this->configFactory
      ->get('system.performance')
      ->get('css.preprocess'))) {
      $policy = $alterEvent
        ->getPolicy();

      // Prevent style-src-attr from falling back to style-src and having
      // 'unsafe-inline' enabled.
      $policy
        ->fallbackAwareAppendIfEnabled('style-src-attr', []);
      $policy
        ->fallbackAwareAppendIfEnabled('style-src', [
        Csp::POLICY_UNSAFE_INLINE,
      ]);
      $policy
        ->fallbackAwareAppendIfEnabled('style-src-elem', [
        Csp::POLICY_UNSAFE_INLINE,
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Ie9CspSubscriber::$configFactory private property The Config Factory Service.
Ie9CspSubscriber::$moduleHandler private property The Module Handler Service.
Ie9CspSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
Ie9CspSubscriber::onCspPolicyAlter public function Alter CSP policy for compatibility with IE9 if needed.
Ie9CspSubscriber::__construct public function Ie9CspSubscriber constructor.