-
Notifications
You must be signed in to change notification settings - Fork 259
Add JSON option to @routes
for improved CSP compatibility
#831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Conversation
Is there functionally any difference between this and using a nonce? Like are there any CSP directives that would disallow a nonce but allow this, and if so is there any reason to prefer that approach? I'm definitely open to this if it provides concrete advantages over Ziggy's current functionality. |
Thanks for the quick response to the PR! Here's the CSP header I use across several projects:
In this case, there's no nonce in use—but the JSON-based approach still works seamlessly. Avoiding the nonce has a few practical advantages:
This also ties into a broader benefit: improving CSP compatibility of Laravel out of the box. For example, if you enable a strict CSP like the one above on a fresh Laravel Starter Kit install, the only two things that break are the progress bar and Ziggy. With this PR, it would probably be a one-line change in the starter kit to get the Ziggy part compliant with strict CSP. |
I’ve moved the config loading logic into I can confirm that Ziggy now works with a strict CSP ( Thanks for considering this PR! |
I’ve made another small update: the This addresses a subtle issue in Vue 3 when using Example:<script setup>
const globalRoute = route('login'); // uses window.route
</script>
<template>
<p>{{ route('login') }}</p> <!-- uses app.config.globalProperties.route -->
</template> By ensuring @bakerkretzmar let me know what you think! |
@flexponsive I think this is looking good. I made a few tweaks:
|
asJson
option to @routes
for improved CSP compatibility@routes
for improved CSP compatibility
This pull request introduces a new
asJson
boolean option to the@routes
Blade directive, improving compatibility with strict Content Security Policies (CSP).When
asJson
is set totrue
, the directive outputs the Ziggy configuration as a JSON object inside a<script>
tag withtype="application/json"
. This type is treated by browsers as inert data (not executable code), which allows the directive to work even under strictscript-src
policies—without requiring anonce
.The Ziggy JavaScript (specifically
useRoute()
androute()
) has been updated to detect when the globalZiggy
object is missing and, in that case, fallback to reading the configuration from a script tag with the IDZiggy_routes
.This approach simplifies CSP configuration for Ziggy users, since it removes the need to configure or pass a nonce when CSP is enforced.
Usage
Testing
Manual testing:
Tested on macOS in Chrome, Safari, and Firefox with CSP enabled.
Automated tests:
useRoute()
works with JSON-based configuration.