You are here

private static function CspSubscriber::fallbackAwareAppendIfEnabled in Raven: Sentry Integration 8.2

Same name and namespace in other branches
  1. 3.x src/EventSubscriber/CspSubscriber.php \Drupal\raven\EventSubscriber\CspSubscriber::fallbackAwareAppendIfEnabled()

Append to a directive if it or a fallback directive is enabled.

If the specified directive is not enabled but one of its fallback directives is, it will be initialized with the same value as the fallback before appending the new value.

If none of the specified directive's fallbacks are enabled, the directive will not be enabled.

Parameters

\Drupal\csp\Csp $policy: The CSP directive to alter.

string $directive: The directive name.

array|string $value: The directive value.

1 call to CspSubscriber::fallbackAwareAppendIfEnabled()
CspSubscriber::onCspPolicyAlter in src/EventSubscriber/CspSubscriber.php
Alter CSP policy to allow Sentry to send JS errors.

File

src/EventSubscriber/CspSubscriber.php, line 93

Class

CspSubscriber
Subscriber for CSP events.

Namespace

Drupal\raven\EventSubscriber

Code

private static function fallbackAwareAppendIfEnabled(Csp $policy, $directive, $value) {
  if ($policy
    ->hasDirective($directive)) {
    $policy
      ->appendDirective($directive, $value);
    return;
  }

  // Duplicate the closest fallback directive with a value.
  foreach (Csp::getDirectiveFallbackList($directive) as $fallback) {
    if ($policy
      ->hasDirective($fallback)) {
      $policy
        ->setDirective($directive, $policy
        ->getDirective($fallback));
      $policy
        ->appendDirective($directive, $value);
      return;
    }
  }
}