Skip to content
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

Modify the CSP Evaluator to handle negated expressions #4441

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/csp/src/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ function generateDataStack(el) {
}

function generateEvaluator(el, expression, dataStack) {
return (receiver = () => {}, { scope = {}, params = [] } = {}) => {
return (receiver = () => { }, { scope = {}, params = [] } = {}) => {
let completeScope = mergeProxies([scope, ...dataStack])

let evaluatedExpression = expression.split('.').reduce(
let isNegated = expression.trim().charAt(0) === '!'
let expressionToEvaluate = isNegated ? expression.trim().slice(1).trim() : expression.trim()

let evaluatedExpression = expressionToEvaluate.split('.').reduce(
(currentScope, currentExpression) => {
if (currentScope[currentExpression] === undefined) {
throwExpressionError(el, expression)
Expand All @@ -39,19 +42,21 @@ function generateEvaluator(el, expression, dataStack) {
completeScope,
);

evaluatedExpression = isNegated ? !evaluatedExpression : evaluatedExpression

runIfTypeOfFunction(receiver, evaluatedExpression, completeScope, params)
}
}

function throwExpressionError(el, expression) {
console.warn(
`Alpine Error: Alpine is unable to interpret the following expression using the CSP-friendly build:
`Alpine Error: Alpine is unable to interpret the following expression using the CSP-friendly build:

"${expression}"

Read more about the Alpine's CSP-friendly build restrictions here: https://alpinejs.dev/advanced/csp

`,
el
el
)
}
25 changes: 23 additions & 2 deletions tests/cypress/integration/plugins/csp-compatibility.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test.csp('Can use components and basic expressions with CSP-compatible build',
<button @click="change">Change Foo</button>
</div>
`,
`
`
Alpine.data('test', () => ({
foo: 'bar',
change() { this.foo = 'baz' },
Expand All @@ -29,7 +29,7 @@ test.csp('Supports nested properties',
<button @click="foo.change">Change Foo</button>
</div>
`,
`
`
Alpine.data('test', () => ({
foo: {
bar: 'baz',
Expand All @@ -43,3 +43,24 @@ test.csp('Supports nested properties',
get('span').should(haveText('qux'))
}
)

test.csp('Supports simple negation',
[html`
<div x-data="test">
<span x-text=" ! foo"></span>

<button @click="toggle">Toggle Foo</button>
</div>
`,
`
Alpine.data('test', () => ({
foo: false,
toggle() { this.foo = !this.foo },
}))
`],
({ get }) => {
get('span').should(haveText('true'))
get('button').click()
get('span').should(haveText('false'))
}
)
Loading