CSP - Alpine preprocessor? #3793
Replies: 2 comments 9 replies
-
It's definitely possible. CSP is somewhat already "solved", it's more that no one is really putting any time into it. The people that seem to want it don't care about making it real. |
Beta Was this translation helpful? Give feedback.
-
I actually implemented this, and it's not so easy to get a full working solution without sacrificing a lot of things. My implementation consists of:
I tested this with many examples from the official documentation and they all worked. I just patched a little part in the original code, so nothing broke. It even detects wrong syntax inside the directives during the building process. For the compiled ouptut, I chose a module format so I can then bundle the result into a project. The result is something like this: /*autogenerated*/
export const precompiledExpressions = {
"{}": async function anonymous(__self, $scope) {
__self.result = {};
__self.finished = true;
return __self.result;
},
"{ count: 5 }": async function anonymous(__self, $scope) {
__self.result = { count: 5 };
__self.finished = true;
return __self.result;
},
"$scope.open = Math.random() > 0.5": async function anonymous(
__self,
$scope,
) {
__self.result = $scope.open = Math.random() > 0.5;
__self.finished = true;
return __self.result;
},
"$scope.$store.darkMode.value && 'bg-black'": async function anonymous(
__self,
$scope,
) {
__self.result = $scope.$store.darkMode.value && "bg-black";
__self.finished = true;
return __self.result;
},
}; Note that when bundling a file using esbuild or similar tools, you can't use the <div x-data="{ open: false }">
Toggle if the random number is higher than 0.5
<button @click="$scope.open = Math.random() > 0.5">Toggle</button>
<div x-show="$scope.open" x-transition>Hello 👋</div>
</div> In normal Alpinejs the Why is this approach not good enough?The problem is that this only works for static files, and they must be raw HTML. Alpine is used in the PETAL (Phoenix, Elixir, Tailwind, Alpine, LiveView) stack, since most of the work is done by Phoenix, you only need to sprinkle JS when needed. But you wouldn't be able to parse Alpine directives from Phoenix files, since they look like this: <.button type="submit" {["x-bind:disabled": "some_js_code"]}>
The syntax is too different from raw HTML, and it's not easy to parse the text. If we were to support formats other than raw HTML there would be too much work. On the other hand it doesn't support dynamically outputted (e.g. server rendered) data such as: <div x-data="{ someData: <%= 3 + 6 %> }"> The best thing we could do is to keep expanding |
Beta Was this translation helpful? Give feedback.
-
Hi
Was almost sold on Alpine and saw the documentation on CSP here
https://alpinejs.dev/advanced/csp
and also the discussion #237.
Just a thought:
Is it possible to have preprocessor similar to Tailwind that looks at Alpine directives in files and pulls those out into a Javascript output file?
All the expressions will be known at compile time and the JS file above would have a map of strings to functions
Then evaluating these expressions would just be just an internal lookup for the corresponding function
I know this'll be an extra build step, but would this not solve CSP?
Thanks
Ajai
Beta Was this translation helpful? Give feedback.
All reactions