Skip to main content

Content Security Policy

Run bootstrap-ng under a strict Content Security Policy (CSP), with nonces for Angular's styles and no 'unsafe-eval'.

What bootstrap-ng needs

bootstrap-ng doesn't use eval(), inline scripts or Bootstrap's JavaScript, so it adds nothing to script-src. Its needs are about styles and the files it loads:

  • A few components, such as bs-icon, data tables and the clear button of clearable form controls, have component styles. Angular adds them in <style> elements, which need a nonce or 'unsafe-inline' in style-src.
  • Components set style properties from script, for example to position tooltips, popovers and menus with Floating UI, or to size progress bars. A CSP doesn't block styles set this way.
  • With server-side rendering, the same styles are written as style attributes in the HTML, which style-src-attr controls.
  • Bootstrap's CSS from jsDelivr and icons that bs-icon loads from jsDelivr need their origin allowed.

Example policy

A policy for an app that loads Bootstrap's CSS and icons from jsDelivr. Replace RANDOM with a new random value for each response:

Content-Security-Policy:
  default-src 'self';
  script-src 'self' 'nonce-RANDOM';
  style-src 'self' 'nonce-RANDOM' https://cdn.jsdelivr.net;
  style-src-attr 'unsafe-inline';
  connect-src 'self' https://cdn.jsdelivr.net;
  img-src 'self' data:;
  object-src 'none';
  base-uri 'self'

Nonces for component styles

Tell Angular the nonce so that it adds it to the <style> elements it creates. Have your server put the same nonce in the ngCspNonce attribute of the root element and in the policy header of each response:

<body>
  <app-root ngCspNonce="RANDOM"></app-root>
</body>

Or provide it with the CSP_NONCE token when you know it while the app starts:

import { ApplicationConfig, CSP_NONCE } from '@angular/core';

export const appConfig: ApplicationConfig = {
  providers: [{ provide: CSP_NONCE, useValue: globalThis.myRandomNonceValue }],
};

Without a nonce, add 'unsafe-inline' to style-src instead. Component styles blocked by the policy leave those components without their extra styles, and the browser reports a CSP violation in the console.

Inline scripts

Angular's own inline scripts, such as the event replay script of hydrated pages, are not bootstrap-ng's. Allow them with the nonce, or let the Angular CLI hash them with autoCsp in angular.json:

{
  "projects": {
    "my-app": {
      "architect": {
        "build": {
          "options": {
            "security": { "autoCsp": true }
          }
        }
      }
    }
  }
}

Server-side rendering

Styles that components bind, such as the width of a stacked progress bar, the fill of a range or the color of a password strength meter, are written as style attributes in server-rendered HTML. A policy without 'unsafe-inline' blocks these attributes until the values change in the browser. Allow them with style-src-attr 'unsafe-inline', which still blocks <style> elements without a nonce. See Server-side rendering for the rest of the setup.

External files

  • Loading Bootstrap's CSS from jsDelivr needs https://cdn.jsdelivr.net in style-src. Bundle it with your app instead to keep style-src 'self'.
  • bs-icon fetches SVG files, from jsDelivr by default, which needs their origin in connect-src. Self-host the icons or bundle them to keep connect-src 'self'.

Trusted Types

Angular supports Trusted Types. bs-icon parses SVG markup by setting innerHTML on an inert <template> and cleans it before use, so a policy with require-trusted-types-for 'script' needs a default Trusted Types policy to show icons.