Support JSON.parse() with a string in the CSP evaluator - #4913
Open
m-develops wants to merge 1 commit into
Open
m-develops wants to merge 1 commit into
m-develops wants to merge 1 commit into
Conversation
Server-side helpers emit JSON.parse('…') for arrays and objects; Laravel's
@js() and Js::from() do it for every non-scalar value. The CSP evaluator
resolved JSON like any other global, so those expressions failed with
"Undefined variable: JSON" and every array reaching an Alpine attribute
through such a helper broke under the CSP build.
Parsing JSON runs no code, so the evaluator now treats JSON.parse() with a
single string argument as an intrinsic: the argument is evaluated, must be
a string, and is parsed with the native JSON.parse(). The JSON global
itself stays unreachable (JSON.stringify(), JSON['parse'], a bare JSON
all still throw), a JSON defined in scope still takes precedence, and
each evaluation returns a fresh value, as native code would.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Server-side helpers emit
JSON.parse('…')for arrays and objects; Laravel's@js()andJs::from()do it for every non-scalar value. The CSP evaluator resolvesJSONlike any other global, so every one of those expressions fails withUndefined variable: JSON, and any array that reaches an Alpine attribute through such a helper breaks under the CSP build.Why now. Livewire's
csp_safemode uses this build, and Filament is working on supporting it: a scan of Filament's templates found 57@js()arrays inside Alpine attributes, the largest single category of what breaks. Dan Harrin asked for this to be solved here rather than with a Filament-specific replacement for@js(): filamentphp/filament#7032 (reply in thread)What this does.
JSON.parse()with a single string argument becomes an intrinsic of the evaluator. Parsing JSON runs no code, so it does not weaken the sandbox. The rest of theJSONglobal stays unreachable:JSON.stringify(), a bareJSON, andJSON['parse']all still throw. AJSONdefined in scope takes precedence, the argument may come from scope as well as from a literal, and each evaluation returns a fresh value, as native code would.Unit tests for all of the above, a Cypress test with the exact output
Js::from()produces (including its"escapes), and a docs entry.