Skip to content

Content Security Policy (CSP) #1149

@ematipico

Description

@ematipico

Summary

Add a new feature that allows Astro assets, mainly scripts and styles, to support Content Security Policy.

Background & Motivation

When creating astro components/layouts with client side scripts, as documented here: docs.astro.build/en/core-concepts/astro-components, the processed+bundled scripts are rendered inline in the html of the page, during a production build.
This forces the use of a CSP with script-src: 'unsafe-inline', which is called unsafe for obvious reasons.

It would be nice to have an astro config option, or a configration attribute for script tags, to instruct astro to create a javascript file for the bundled javascript, and then reference that file via a src attribute on the rendered script tag, instead of inlining the bundled javascript.

(During devlopment npm run dev the processed javascript is actually linked via a script src already, making dev builds and production builds behave differently with some content security policies).

The same could be done for stylesheets, making it configurable whether the stylesheet is inlined in the page (requiring style-src unsafe-inline), or included via a link.

Goals

  • Support at least one solution (nonce, hash, etc.) that would support CSP.
  • Allow Astro scripts (client islands, server islands, view transitions, etc.) and Astro styles to work out of the box.
  • Update adapters to support the feature, if needed.
  • Make the feature opt-in, even when it's outside of experimental.

Non-Goals

  • Provide multiple solutions to fix the same problem
  • Support for third-party scripts dynamically imported by users e.g. document.createElement("script"), import("https://path.to/script.js");
  • Support for third-party styles dynamically imported by users.
  • Performance. Users and authors are willing to overlook possible performance drawbacks in order to increase security.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Stage 2: Accepted Proposals, No RFC

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions