WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit e224a43

Browse files
committed
wip
1 parent b23e473 commit e224a43

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { directive } from 'alpinejs/src/directives'
2+
import { handleError } from 'alpinejs/src/utils/error'
23

3-
directive('html', () => {
4-
throw new Error('Using the x-html directive is prohibited')
4+
directive('html', (el, { expression }) => {
5+
handleError(new Error('Using the x-html directive is prohibited in the CSP build'), el)
56
})

packages/csp/src/evaluator.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { generateEvaluatorFromFunction, shouldAutoEvaluateFunctions } from 'alpinejs/src/evaluator'
22
import { closestDataStack, mergeProxies } from 'alpinejs/src/scope'
3-
import { tryCatch } from 'alpinejs/src/utils/error'
3+
import { handleError, tryCatch } from 'alpinejs/src/utils/error'
44
import { generateRuntimeFunction } from './parser'
55
import { injectMagics } from 'alpinejs/src/magics'
66

@@ -13,13 +13,15 @@ export function cspEvaluator(el, expression) {
1313
}
1414

1515
if (el instanceof HTMLIFrameElement) {
16-
console.warn('Alpine CSP Error: Evaluating expressions on an iframe is prohibited')
17-
throw new Error('Alpine CSP Error: Evaluating expressions on an iframe is prohibited')
16+
handleError(new Error('Evaluating expressions on an iframe is prohibited in the CSP build'), el)
17+
18+
return;
1819
}
1920

2021
if (el instanceof HTMLScriptElement) {
21-
console.warn('Alpine CSP Error: Evaluating expressions on a script is prohibited')
22-
throw new Error('Alpine CSP Error: Evaluating expressions on a script is prohibited')
22+
handleError(new Error('Evaluating expressions on a script is prohibited in the CSP build'), el)
23+
24+
return;
2325
}
2426

2527
let evaluator = generateEvaluator(el, expression, dataStack)

packages/csp/src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ import { reactive, effect, stop, toRaw } from '@vue/reactivity'
3131
Alpine.setReactivityEngine({ reactive, effect, release: stop, raw: toRaw })
3232

3333
import 'alpinejs/src/magics/index'
34-
3534
import 'alpinejs/src/directives/index'
3635

37-
import './directives/x-html' // Disable
36+
/**
37+
* The `x-html` directive needs to be disabled here
38+
* because it is not CSP friendly. To disable it,
39+
* we'll override it with noop implementation.
40+
*/
41+
import './directives/x-html'
3842

3943
export default Alpine

packages/csp/src/parser.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
let safemap = new WeakMap()
12
let globals = new Set()
2-
Object.getOwnPropertyNames(globalThis).forEach(key => globals.add(globalThis[key]))
33

4-
let safemap = new WeakMap()
4+
Object.getOwnPropertyNames(globalThis).forEach(key => {
5+
// Prevent Safari deprecation warning...
6+
if (key === 'styleMedia') return
7+
8+
globals.add(globalThis[key])
9+
})
510

611
class Token {
712
constructor(type, value, start, end) {
@@ -839,7 +844,7 @@ class Evaluator {
839844
scope[node.left.name] = value;
840845
return value;
841846
} else if (node.left.type === 'MemberExpression') {
842-
throw new Error('Property assignments are prohibited')
847+
throw new Error('Property assignments are prohibited in the CSP build')
843848
}
844849
throw new Error('Invalid assignment target');
845850

@@ -872,7 +877,7 @@ class Evaluator {
872877
]
873878

874879
if (blacklist.includes(keyword)) {
875-
throw new Error(`Accessing "${keyword}" is prohibited`)
880+
throw new Error(`Accessing "${keyword}" is prohibited in the CSP build`)
876881
}
877882
}
878883

@@ -890,11 +895,11 @@ class Evaluator {
890895
}
891896

892897
if (prop instanceof HTMLIFrameElement || prop instanceof HTMLScriptElement) {
893-
throw new Error('Accessing iframes and scripts is prohibited')
898+
throw new Error('Accessing iframes and scripts is prohibited in the CSP build')
894899
}
895900

896901
if (globals.has(prop)) {
897-
throw new Error('Accessing global variables is prohibited')
902+
throw new Error('Accessing global variables is prohibited in the CSP build')
898903
}
899904

900905
safemap.set(prop, true)

0 commit comments

Comments
 (0)