Debian Codegraph @

test/manual/hx-csp/index.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 <title>hx-csp CSP test</title>
6 <meta name="htmx-config" content='extensions:"hx-csp",safeEval:true'>
7 <script nonce="__NONCE__" src="/htmx.js"></script>
8 <script nonce="__NONCE__" src="/ext/hx-csp.js"></script>
9 <style nonce="__NONCE__">
10 body { font-family: sans-serif; max-width: 700px; margin: 2rem auto; }
11 .modes { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: 1.5rem; }
12 .modes a { padding: .3rem .7rem; border: 1px solid #999; border-radius: 4px; text-decoration: none; color: #333; }
13 .modes a.active { background: #333; color: #fff; }
14 section { border: 1px solid #ddd; border-radius: 6px; padding: 1rem; margin-bottom: 1rem; }
15 h2 { margin: 0 0 .5rem; font-size: 1rem; }
16 #result, #eval-result, #unnonced-result { min-height: 2rem; padding: .5rem; background: #f5f5f5; border-radius: 4px; }
17 .csp-info { font-size: .8rem; color: #666; margin-bottom: 1rem; }
18 </style>
19 </head>
20 <body>
21
22 <h1>hx-csp + Trusted Types CSP test</h1>
23
24 <div class="modes">
25 <a href="/?csp=permissive">permissive</a>
26 <a href="/?csp=nonce">nonce</a>
27 <a href="/?csp=nonce-no-eval">nonce-no-eval</a>
28 <a href="/?csp=nonce-no-eval-no-safeeval">nonce-no-eval-no-safeeval &#x26A0;</a>
29 <a href="/?csp=trusted-types">trusted-types</a>
30 <a href="/?csp=trusted-types-no-eval">trusted-types-no-eval</a>
31 <a href="/?csp=trusted-types-multi">trusted-types-multi</a>
32 <a href="/?csp=trusted-types-no-htmx">trusted-types-no-htmx &#x26A0;</a>
33 </div>
34
35 <p class="csp-info">
36 Current mode: <strong id="mode-label"></strong><br>
37 Check the browser console and Network tab for CSP violations.
38 </p>
39
40 <!-- 1. Basic AJAX swap -->
41 <section>
42 <h2>1. Basic hx-get swap</h2>
43 <p>Tests that nonced elements can make requests and swap content.</p>
44 <button hx-get="/partial" hx-target="#result">Load partial</button>
45 <div id="result">— not loaded —</div>
46 </section>
47
48 <!-- 2. JS eval paths -->
49 <section>
50 <h2>2. JS eval paths (all require safeEval under no-eval CSP)</h2>
51 <p>Each button exercises a different htmx JS eval entry point. Check console for output.</p>
52
53 <p><strong>hx-on:click</strong> — inline event handler</p>
54 <button hx-on:click="console.log('hx-on:click fired')" hx-nonce="__NONCE__">hx-on:click</button>
55
56 <p><strong>hx-trigger filter</strong> — click[expression] guard</p>
57 <button hx-get="/partial" hx-target="#eval-result"
58 hx-trigger="click[console.log('filter ran') || true]">hx-trigger filter</button>
59
60 <p><strong>hx-vals js:</strong> — JS object expression for request body</p>
61 <button hx-post="/partial" hx-target="#eval-result"
62 hx-vals="js:{ts: Date.now(), rand: Math.random()}">hx-vals js:</button>
63
64 <p><strong>hx-headers js:</strong> — JS object expression for request headers</p>
65 <button hx-get="/partial" hx-target="#eval-result"
66 hx-headers="js:{'X-Timestamp': String(Date.now())}">hx-headers js:</button>
67
68 <p><strong>hx-confirm js:</strong> — async confirm via JS expression</p>
69 <button hx-get="/partial" hx-target="#eval-result"
70 hx-confirm="js:confirm('hx-confirm js: — proceed?')">hx-confirm js:</button>
71
72 <p><strong>hx-get js:</strong> — JS expression executed instead of HTTP request (side-effect only, no swap)</p>
73 <button hx-get="js:console.log('hx-get js: executed') || undefined"
74 hx-target="#eval-result">hx-get js:</button>
75
76 <div id="eval-result">— eval results —</div>
77 </section>
78
79 <!-- 3. Script tag execution -->
80 <section>
81 <h2>3. Script tag execution in swapped responses</h2>
82 <p>Tests whether scripts in partial responses execute based on their nonce.</p>
83
84 <p><strong>With nonce</strong> — script carries the response nonce, rewritten to page nonce by hx-csp. Should execute under all modes.</p>
85 <button hx-get="/script-with-nonce" hx-target="#script-result">Swap in nonced script</button>
86
87 <p><strong>Without nonce</strong> — script has no nonce. Should execute in permissive, blocked by CSP in all nonce modes.</p>
88 <button hx-get="/script-without-nonce" hx-target="#script-result">Swap in unnnonced script</button>
89
90 <div id="script-result">— script results —</div>
91 </section>
92
93 <!-- 4. Blocked elements (should never fire a request) -->
94 <section>
95 <h2>4. Blocked elements (should never fire a request)</h2>
96 <p>Both buttons should be stripped by hx-csp and fire htmx:security:strip.</p>
97 <button hx-get="/partial" hx-target="#unnonced-result" hx-nonce="INVALID">
98 Wrong nonce (mismatch)
99 </button>
100 <button data-no-nonce hx-get="/partial" hx-target="#unnonced-result">
101 No nonce (missing)
102 </button>
103 <div id="unnonced-result">— should stay empty —</div>
104 </section>
105
106 <script nonce="__NONCE__">
107 // Mark active mode link
108 const params = new URLSearchParams(location.search);
109 const mode = params.get('csp') || document.cookie.match(/csp-mode=([^;]+)/)?.[1] || 'permissive';
110 document.getElementById('mode-label').textContent = mode;
111 document.querySelectorAll('.modes a').forEach(a => {
112 if (a.href.includes('csp=' + mode)) a.classList.add('active');
113 });
114
115 // Log security events
116 document.addEventListener('htmx:security:strip', e => {
117 console.warn('htmx:security:strip fired on', e.target, e.detail);
118 });
119 document.addEventListener('htmx:security:violation', e => {
120 console.error('htmx:security:violation fired', e.detail);
121 });
122 </script>
123
124 </body>
125 </html>