Fix && and || not short-circuiting in CSP evaluator#4823
Merged
calebporzio merged 1 commit intomainfrom Apr 30, 2026
Merged
Conversation
The BinaryExpression handler always evaluated both sides, so guards like `a && b()` and `a || b()` ran `b()` unconditionally.
&& and || not short-circuiting in CSP evaluator
|
Any ETA on when this may get merged? I have a client live project waiting on this fix :) Thanks! |
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.
The Scenario
In an app running Livewire's CSP build, a single
Flux::toast(...)call renders two toasts instead of one when the page uses a grouped toast setup.One toast appears correctly inside the group; a second appears separately, originating from the inner
<ui-toast>element. Swapping the CSP build for the regular Livewire build makes it behave correctly.The Problem
Flux's inner
<ui-toast>guards against handling thetoast-showevent when nested inside a group with a short-circuit expression:Under the regular Alpine build this works correctly: when
$el.closest('ui-toast-group')returns the group, the expression short-circuits andshowToastis never called. Under the CSP build it doesn't, because theBinaryExpressionhandler inpackages/csp/src/parser.jsevaluates both operands up front before applying the operator:By the time
left && rightis computed, the right side has already run. Any&&or||expression with a side effect on the right leaks that side effect, regardless of the left value.The Solution
Wrap the right-hand evaluation in a function and only call it when short-circuiting doesn't apply:
Four new tests in
tests/vitest/csp-evaluator.spec.jscover both the happy path (right side runs when needed) and the short-circuit path (right side does not run) for both&&and||.Fixes livewire/flux#2571