| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ed |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
const http = require('http'); |
| 22 |
const fs = require('fs'); |
| 23 |
const path = require('path'); |
| 24 |
const crypto = require('crypto'); |
| 25 |
|
| 26 |
const ROOT = __dirname; |
| 27 |
const HTMX_SRC = path.join(__dirname, '..', '..', '..', 'src', 'htmx.js'); |
| 28 |
const HX_CSP_SRC = path.join(__dirname, '..', '..', '..', 'src', 'ext', 'hx-csp.js'); |
| 29 |
const HX_ALPINE_SRC = path.join(__dirname, '..', '..', '..', 'src', 'ext', 'hx-alpine-compat.js'); |
| 30 |
|
| 31 |
function makeNonce() { |
| 32 |
return crypto.randomBytes(16).toString('base64'); |
| 33 |
} |
| 34 |
|
| 35 |
function makeCsp(mode, nonce) { |
| 36 |
const tt = `require-trusted-types-for 'script'; trusted-types htmx`; |
| 37 |
switch (mode) { |
| 38 |
case 'nonce': |
| 39 |
return `default-src 'self'; script-src 'self' 'nonce-${nonce}' 'unsafe-eval'; style-src 'self' 'nonce-${nonce}';`; |
| 40 |
case 'nonce-no-eval': |
| 41 |
case 'nonce-no-eval-no-safeeval': |
| 42 |
return `default-src 'self'; script-src 'self' 'nonce-${nonce}'; style-src 'self' 'nonce-${nonce}';`; |
| 43 |
case 'trusted-types': |
| 44 |
return `default-src 'self'; script-src 'self' 'nonce-${nonce}' 'unsafe-eval'; style-src 'self' 'nonce-${nonce}'; ${tt};`; |
| 45 |
case 'trusted-types-no-eval': |
| 46 |
return `default-src 'self'; script-src 'self' 'nonce-${nonce}'; style-src 'self' 'nonce-${nonce}'; ${tt};`; |
| 47 |
case 'trusted-types-multi': |
| 48 |
return `default-src 'self'; script-src 'self' 'nonce-${nonce}' 'unsafe-eval'; style-src 'self' 'nonce-${nonce}'; require-trusted-types-for 'script'; trusted-types htmx my-app;`; |
| 49 |
case 'trusted-types-no-htmx': |
| 50 |
ed |
| 51 |
return `default-src 'self'; script-src 'self' 'nonce-${nonce}' 'unsafe-eval'; style-src 'self' 'nonce-${nonce}'; require-trusted-types-for 'script'; trusted-types my-app;`; |
| 52 |
case 'alpine-csp': |
| 53 |
d. |
| 54 |
|
| 55 |
return `default-src 'self'; script-src 'self' 'nonce-${nonce}' https://cdn.jsdelivr.net; style-src 'self' 'nonce-${nonce}';`; |
| 56 |
case 'alpine-csp-tt': |
| 57 |
return `default-src 'self'; script-src 'self' 'nonce-${nonce}' https://cdn.jsdelivr.net; style-src 'self' 'nonce-${nonce}'; ${tt};`; |
| 58 |
default: |
| 59 |
return `default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';`; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
function stampNonce(html, nonce) { |
| 66 |
return html |
| 67 |
.replaceAll('__NONCE__', nonce) |
| 68 |
.replace(/(<[^>]+\shx-(?:get|post|put|patch|delete|boost|on)[^>]*)(>)/gi, |
| 69 |
(m, attrs, close) => (attrs.includes('hx-nonce') || attrs.includes('data-no-nonce')) ? m : `${attrs} hx-nonce="${nonce}"${close}` |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
function readFile(filePath) { |
| 74 |
try { return fs.readFileSync(filePath, 'utf8'); } catch { return null; } |
| 75 |
} |
| 76 |
|
| 77 |
function partialHeaders(partialNonce) { |
| 78 |
return { 'Content-Type': 'text/html', 'Content-Security-Policy': `script-src 'nonce-${partialNonce}';` }; |
| 79 |
} |
| 80 |
|
| 81 |
http.createServer((req, res) => { |
| 82 |
const url = new URL(req.url, 'http://localhost'); |
| 83 |
const pathname = url.pathname; |
| 84 |
|
| 85 |
ed |
| 86 |
if (pathname === '/htmx.js') { |
| 87 |
const content = readFile(HTMX_SRC); |
| 88 |
if (!content) { res.writeHead(404); res.end('htmx.js not found'); return; } |
| 89 |
res.writeHead(200, { 'Content-Type': 'application/javascript' }); |
| 90 |
res.end(content); |
| 91 |
return; |
| 92 |
} |
| 93 |
if (pathname === '/ext/hx-csp.js') { |
| 94 |
const content = readFile(HX_CSP_SRC); |
| 95 |
if (!content) { res.writeHead(404); res.end('hx-csp.js not found'); return; } |
| 96 |
res.writeHead(200, { 'Content-Type': 'application/javascript' }); |
| 97 |
res.end(content); |
| 98 |
return; |
| 99 |
} |
| 100 |
if (pathname === '/ext/hx-alpine-compat.js') { |
| 101 |
const content = readFile(HX_ALPINE_SRC); |
| 102 |
if (!content) { res.writeHead(404); res.end('hx-alpine-compat.js not found'); return; } |
| 103 |
res.writeHead(200, { 'Content-Type': 'application/javascript' }); |
| 104 |
res.end(content); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
const nonce = makeNonce(); |
| 110 |
const cookieMode = (req.headers.cookie || '').match(/csp-mode=([^;]+)/)?.[1]; |
| 111 |
const mode = url.searchParams.get('csp') || cookieMode || 'permissive'; |
| 112 |
const csp = makeCsp(mode, nonce); |
| 113 |
|
| 114 |
const pageHeaders = { |
| 115 |
'Content-Type': 'text/html', |
| 116 |
'Content-Security-Policy': csp, |
| 117 |
'Set-Cookie': `csp-mode=${mode}; Path=/`, |
| 118 |
'X-CSP-Nonce': nonce, |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
if (pathname === '/script-with-nonce') { |
| 123 |
const pn = makeNonce(); |
| 124 |
res.writeHead(200, partialHeaders(pn)); |
| 125 |
res.end(`<div hx-nonce="${pn}"> |
| 126 |
<p>Nonced script — should execute (check console).</p> |
| 127 |
<script nonce="${pn}">console.log('script-with-nonce: executed \u2713');</script> |
| 128 |
</div>`); |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
if (pathname === '/script-without-nonce') { |
| 133 |
const pn = makeNonce(); |
| 134 |
res.writeHead(200, partialHeaders(pn)); |
| 135 |
res.end(`<div hx-nonce="${pn}"> |
| 136 |
<p>Unnnonced script — should be blocked by CSP (check console for violation, NOT execution).</p> |
| 137 |
<script>console.log('script-without-nonce: executed \u2717 \u2014 CSP should have blocked this');</script> |
| 138 |
</div>`); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
if (pathname === '/partial') { |
| 143 |
const pn = makeNonce(); |
| 144 |
res.writeHead(200, partialHeaders(pn)); |
| 145 |
res.end(`<div hx-nonce="${pn}"> |
| 146 |
<p>Partial loaded at ${new Date().toLocaleTimeString()}</p> |
| 147 |
<button hx-get="/partial" hx-target="#result" hx-nonce="${pn}">Reload partial</button> |
| 148 |
</div>`); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
if (pathname === '/eval-partial') { |
| 153 |
const pn = makeNonce(); |
| 154 |
res.writeHead(200, partialHeaders(pn)); |
| 155 |
res.end(`<div hx-nonce="${pn}" hx-on:htmx:after:swap="console.log('hx-on fired from eval-partial')"> |
| 156 |
<p>hx-on: loaded \u2014 check console for eval output</p> |
| 157 |
</div>`); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
if (pathname === '/alpine' || pathname === '/alpine.html') { |
| 162 |
const template = readFile(path.join(ROOT, 'alpine.html')); |
| 163 |
if (!template) { res.writeHead(404); res.end('alpine.html not found'); return; } |
| 164 |
res.writeHead(200, pageHeaders); |
| 165 |
res.end(stampNonce(template, nonce)); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
if (pathname === '/' || pathname === '/index.html') { |
| 170 |
const template = readFile(path.join(ROOT, 'index.html')); |
| 171 |
if (!template) { res.writeHead(404); res.end('index.html not found'); return; } |
| 172 |
let html = stampNonce(template, nonce); |
| 173 |
if (mode === 'nonce-no-eval-no-safeeval') { |
| 174 |
html = html.replace('extensions:"hx-csp",safeEval:true', 'extensions:"hx-csp"'); |
| 175 |
} |
| 176 |
res.writeHead(200, pageHeaders); |
| 177 |
res.end(html); |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
res.writeHead(404); |
| 182 |
res.end('Not found'); |
| 183 |
|
| 184 |
}).listen(3002, () => { |
| 185 |
console.log('hx-csp CSP test server: http://localhost:3002\n'); |
| 186 |
console.log(' permissive http://localhost:3002/?csp=permissive'); |
| 187 |
console.log(' nonce http://localhost:3002/?csp=nonce'); |
| 188 |
console.log(' nonce-no-eval http://localhost:3002/?csp=nonce-no-eval'); |
| 189 |
console.log(' nonce-no-eval-no-safeeval http://localhost:3002/?csp=nonce-no-eval-no-safeeval'); |
| 190 |
console.log(' trusted-types http://localhost:3002/?csp=trusted-types'); |
| 191 |
console.log(' trusted-types-no-eval http://localhost:3002/?csp=trusted-types-no-eval'); |
| 192 |
console.log(' trusted-types-multi http://localhost:3002/?csp=trusted-types-multi'); |
| 193 |
console.log(' trusted-types-no-htmx http://localhost:3002/?csp=trusted-types-no-htmx'); |
| 194 |
console.log(' alpine-csp http://localhost:3002/alpine?csp=alpine-csp'); |
| 195 |
console.log(' alpine-csp-tt http://localhost:3002/alpine?csp=alpine-csp-tt'); |
| 196 |
}); |