Debian Codegraph @

test/manual/hx-csp/alpine.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 <title>hx-csp + Alpine CSP test</title>
6 <meta name="htmx-config" content='extensions:"hx-csp,hx-alpine-compat",safeEval:true'>
7 <!-- Alpine CSP build — no unsafe-eval required -->
8 <script defer nonce="__NONCE__" src="https://cdn.jsdelivr.net/npm/@alpinejs/[email protected]/dist/cdn.min.js"></script>
9 <script nonce="__NONCE__" src="/htmx.js"></script>
10 <script nonce="__NONCE__" src="/ext/hx-csp.js"></script>
11 <script nonce="__NONCE__" src="/ext/hx-alpine-compat.js"></script>
12 <style nonce="__NONCE__">
13 body { font-family: sans-serif; max-width: 700px; margin: 2rem auto; }
14 .modes { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: 1.5rem; }
15 .modes a { padding: .3rem .7rem; border: 1px solid #999; border-radius: 4px; text-decoration: none; color: #333; }
16 .modes a.active { background: #333; color: #fff; }
17 section { border: 1px solid #ddd; border-radius: 6px; padding: 1rem; margin-bottom: 1rem; }
18 h2 { margin: 0 0 .5rem; font-size: 1rem; }
19 .result { min-height: 2rem; padding: .5rem; background: #f5f5f5; border-radius: 4px; }
20 .csp-info { font-size: .8rem; color: #666; margin-bottom: 1rem; }
21 </style>
22 </head>
23 <body>
24
25 <h1>hx-csp + Alpine CSP build test</h1>
26
27 <div class="modes">
28 <a href="/alpine?csp=alpine-csp">alpine-csp</a>
29 <a href="/alpine?csp=alpine-csp-tt">alpine-csp-tt</a>
30 <a href="/">back to main tests</a>
31 </div>
32
33 <p class="csp-info">
34 Current mode: <strong id="mode-label"></strong><br>
35 Alpine CSP build — no <code>unsafe-eval</code> required. Check console for violations.
36 </p>
37
38 <!-- 1. Alpine reactivity — basic expressions without unsafe-eval -->
39 <section>
40 <h2>1. Alpine reactivity (no unsafe-eval)</h2>
41 <p>Basic Alpine expressions using the CSP build. Should work without unsafe-eval.</p>
42 <div x-data="{ count: 0 }">
43 <button x-on:click="count++">Increment</button>
44 <button x-on:click="count = 0">Reset</button>
45 <span x-text="count"></span>
46 <span x-show="count > 5"> — count is greater than 5!</span>
47 </div>
48 </section>
49
50 <!-- 2. htmx + Alpine together -->
51 <section>
52 <h2>2. htmx swap + Alpine reactive content</h2>
53 <p>htmx loads a partial, Alpine reacts to the new content.</p>
54 <button hx-get="/partial" hx-target="#alpine-result">Load partial</button>
55 <div id="alpine-result" class="result">— not loaded —</div>
56 </section>
57
58 <!-- 3. Alpine x-data with htmx hx-vals -->
59 <section>
60 <h2>3. Alpine state read by htmx hx-vals js:</h2>
61 <p>htmx reads Alpine component state via hx-vals js:. Requires safeEval:true.</p>
62 <div x-data="{ userId: 42 }">
63 <button hx-post="/partial"
64 hx-vals="js:{userId: document.querySelector('[x-data]')._x_dataStack?.[0]?.userId}"
65 hx-target="#vals-result">POST with Alpine state</button>
66 </div>
67 <div id="vals-result" class="result">— not loaded —</div>
68 </section>
69
70 <!-- 4. Alpine CSP limitation — globals not supported -->
71 <section>
72 <h2>4. Alpine CSP limitation — globals not supported</h2>
73 <p>The Alpine CSP build blocks global access entirely — <code>console</code>, <code>window</code>
74 etc. throw <em>"Undefined variable"</em> Alpine errors (check console). Extract logic to
75 <code>Alpine.data()</code> in a nonced <code>&lt;script&gt;</code> tag instead.</p>
76 <div x-data="{}">
77 <button x-on:click="console.log('clicked')">console.log (throws Alpine error)</button>
78 <button x-on:click="window.alert('hi')">window.alert (throws Alpine error)</button>
79 </div>
80 </section>
81
82 <!-- 5. Alpine CSP self-policed sinks -->
83 <section>
84 <h2>5. Alpine CSP self-policed sinks</h2>
85 <p>The Alpine CSP build actively blocks its own dangerous sinks before they reach the DOM —
86 no TT violation needed. These throw Alpine's own errors, not browser TT errors.
87 This means <code>trusted-types htmx</code> alone is sufficient alongside the Alpine CSP build.</p>
88
89 <p><strong>x-html</strong> — Alpine CSP throws <em>"Using the x-html directive is prohibited"</em></p>
90 <div x-data="{ msg: '<b>bold</b>' }">
91 <span x-html="msg"></span>
92 </div>
93
94 <p><strong>x-init + insertAdjacentHTML</strong> — Alpine CSP throws <em>"Accessing insertAdjacentHTML is prohibited"</em></p>
95 <div x-data x-init="$el.insertAdjacentHTML('beforeend', '<span>injected</span>')">
96 (insertAdjacentHTML target)
97 </div>
98
99 <p><strong>x-if / x-for</strong> — Alpine CSP handles template cloning internally without hitting TT sinks</p>
100 <div x-data="{ show: true, items: ['a', 'b', 'c'] }">
101 <template x-if="show"><p>x-if rendered &#x2713;</p></template>
102 <ul><template x-for="item in items"><li x-text="item"></li></template></ul>
103 </div>
104
105 <p><strong>x-bind:innerHTML</strong> — Alpine CSP blocks this too</p>
106 <div x-data="{ html: '<em>bound</em>' }">
107 <div x-bind:innerHTML="html"></div>
108 </div>
109 </section>
110
111 <script nonce="__NONCE__">
112 const params = new URLSearchParams(location.search);
113 const mode = params.get('csp') || document.cookie.match(/csp-mode=([^;]+)/)?.[1] || 'alpine-csp';
114 document.getElementById('mode-label').textContent = mode;
115 document.querySelectorAll('.modes a').forEach(a => {
116 if (a.href.includes('csp=' + mode)) a.classList.add('active');
117 });
118
119 document.addEventListener('htmx:security:strip', e => {
120 console.warn('htmx:security:strip', e.target, e.detail);
121 });
122 </script>
123
124 </body>
125 </html>