Skip to content

Conversation

@ematipico
Copy link
Member

@ematipico ematipico commented May 15, 2025

Changes

This PR adds support for additional directives inside the <meta> tag. As for now, Astro is only responsible for customising the script-src and style-src directives, however other directives aren't provided.

A user can always provide additional directives via the Response header; however, to avoid the use of multiple values (Response headers and meta data), and incurr in situations where the browser picks a different directive, Astro now allows user to add additional directives, which will be eventually rendered in the meta tag.

There's a very tiny level of validation, where if directives start with script-src or style-src, we throw an error.

We will provide additional configurations in case users need to customise script-src and style-src directives.

To note that I updated the RFC to use the term "directive". Initially I chose the term "policy". Directive is the right term.

https://github.com/withastro/roadmap/blob/feat/rfc-csp/proposals/0055-csp.md#configuration-apis

Note

The API is different from the RFC, I changed it now to make it strictly typed. I will update the RFC once we merge this PR

Testing

Added new tests

Docs

@changeset-bot
Copy link

changeset-bot bot commented May 15, 2025

⚠️ No Changeset found

Latest commit: a191009

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) docs pr labels May 15, 2025
@ematipico ematipico changed the title wip feat(csp): allow additional directives May 15, 2025
@ematipico ematipico marked this pull request as ready for review May 15, 2025 14:14
@ascorbic
Copy link
Contributor

Can you describe the API, where it differs from the RFC?

Copy link
Member

@florian-lefebvre florian-lefebvre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM, I'm curious to see the implementation of insertDirective 👀

@ematipico
Copy link
Member Author

ematipico commented May 16, 2025

Yeah, sorry, here's the API change

Before:

{
	csp: {
		directives: ["img-src 'self' 'https://cdn.example.com'"]
	}
}

After:

{
	csp: {
		directives: [{
			type: "img-src" // strongly typed,
			content: "'sef' 'https://cdn.example.com'"
		}]
	}
}

Since Astro needs to manage script-src, and style-src, with this new API change users won't be able to tinker with those directives. Also, you get some auto complete too :)

@ascorbic
Copy link
Contributor

Ah, got it. It's a shame that stuff like this doesn't work:

type NotScriptSrc = Exclude<string, `script-src ${string}`>

@ascorbic
Copy link
Contributor

Oh, actually you could do this if you want to keep the old API:

type CspDirective = `${AllowedDirectives} ${string}`

@ematipico
Copy link
Member Author

RFC updated: withastro/roadmap@cc6d3bf (#1168)

@ematipico ematipico merged commit cf75892 into feat/csp May 16, 2025
15 checks passed
@ematipico ematipico deleted the feat/csp-custom-policies branch May 16, 2025 08:58
ematipico added a commit that referenced this pull request May 22, 2025
Co-authored-by: ascorbic <[email protected]>
Co-authored-by: florian-lefebvre <[email protected]>
ascorbic added a commit that referenced this pull request Jun 4, 2025
* chore: build hashes of scripts (#13590)

* chore: build hashes of scripts

* chore: fix changes

* chore: fix changes

* chore: fix changes

* feat(csp): create hashes of tracked scripts and hashes (#13675)

Co-authored-by: florian-lefebvre <[email protected]>

* feat(csp): fix CSP header, inject astro island script/style (#13687)

* feat(csp): track client scripts and CSS (#13725)

Co-authored-by: ascorbic <[email protected]>

* feat(csp): support view transitions (#13738)

Co-authored-by: florian-lefebvre <[email protected]>
Co-authored-by: ascorbic <[email protected]>
fix CSP header, inject astro island script/style (#13687)

* feat(csp): server islands (#13775)

Co-authored-by: florian-lefebvre <[email protected]>

* feat(csp): customise algorithm (#13803)

Co-authored-by: Florian Lefebvre <[email protected]>

* chore: build hashes of scripts (#13590) (#13805)

Co-authored-by: Florian Lefebvre <[email protected]>

* feat(csp): allow additional directives (#13810)

Co-authored-by: ascorbic <[email protected]>
Co-authored-by: florian-lefebvre <[email protected]>

* feat(csp): resources for script and styles directives (#13812)

Co-authored-by: ascorbic <[email protected]>

* feat(csp): runtime APIs (#13824)

Co-authored-by: Matt Kane <[email protected]>

* feat(csp): add script-dynamic keyword support (#13834)

* update lockfile

* chore: docs and changeset (#13870)

* chore: add changeset

* grammar

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <[email protected]>

* Update JSDoc with examples to match docs

* Sarah's changeset edits

* Apply suggestions from code review

Thanks, @ArmandPhilippot

Co-authored-by: Armand Philippot <[email protected]>

* Fix indentation

* Update .changeset/crazy-doors-buy.md

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <[email protected]>

---------

Co-authored-by: Sarah Rainsberger <[email protected]>
Co-authored-by: Matt Kane <[email protected]>
Co-authored-by: Armand Philippot <[email protected]>

* Update lockfile

* dedupe deps

* Lock

* Lock

* fix: server islands in mdx

---------

Co-authored-by: florian-lefebvre <[email protected]>
Co-authored-by: ascorbic <[email protected]>
Co-authored-by: Florian Lefebvre <[email protected]>
Co-authored-by: Matt Kane <[email protected]>
Co-authored-by: Sarah Rainsberger <[email protected]>
Co-authored-by: Armand Philippot <[email protected]>
openscript pushed a commit to openscript/astro that referenced this pull request Sep 12, 2025
* chore: build hashes of scripts (withastro#13590)

* chore: build hashes of scripts

* chore: fix changes

* chore: fix changes

* chore: fix changes

* feat(csp): create hashes of tracked scripts and hashes (withastro#13675)

Co-authored-by: florian-lefebvre <[email protected]>

* feat(csp): fix CSP header, inject astro island script/style (withastro#13687)

* feat(csp): track client scripts and CSS (withastro#13725)

Co-authored-by: ascorbic <[email protected]>

* feat(csp): support view transitions (withastro#13738)

Co-authored-by: florian-lefebvre <[email protected]>
Co-authored-by: ascorbic <[email protected]>
fix CSP header, inject astro island script/style (withastro#13687)

* feat(csp): server islands (withastro#13775)

Co-authored-by: florian-lefebvre <[email protected]>

* feat(csp): customise algorithm (withastro#13803)

Co-authored-by: Florian Lefebvre <[email protected]>

* chore: build hashes of scripts (withastro#13590) (withastro#13805)

Co-authored-by: Florian Lefebvre <[email protected]>

* feat(csp): allow additional directives (withastro#13810)

Co-authored-by: ascorbic <[email protected]>
Co-authored-by: florian-lefebvre <[email protected]>

* feat(csp): resources for script and styles directives (withastro#13812)

Co-authored-by: ascorbic <[email protected]>

* feat(csp): runtime APIs (withastro#13824)

Co-authored-by: Matt Kane <[email protected]>

* feat(csp): add script-dynamic keyword support (withastro#13834)

* update lockfile

* chore: docs and changeset (withastro#13870)

* chore: add changeset

* grammar

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <[email protected]>

* Update JSDoc with examples to match docs

* Sarah's changeset edits

* Apply suggestions from code review

Thanks, @ArmandPhilippot

Co-authored-by: Armand Philippot <[email protected]>

* Fix indentation

* Update .changeset/crazy-doors-buy.md

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <[email protected]>

---------

Co-authored-by: Sarah Rainsberger <[email protected]>
Co-authored-by: Matt Kane <[email protected]>
Co-authored-by: Armand Philippot <[email protected]>

* Update lockfile

* dedupe deps

* Lock

* Lock

* fix: server islands in mdx

---------

Co-authored-by: florian-lefebvre <[email protected]>
Co-authored-by: ascorbic <[email protected]>
Co-authored-by: Florian Lefebvre <[email protected]>
Co-authored-by: Matt Kane <[email protected]>
Co-authored-by: Sarah Rainsberger <[email protected]>
Co-authored-by: Armand Philippot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs pr pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants