This repository has been archived by the owner on Jan 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
50 lines (44 loc) · 1.44 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { defineMiddleware } from 'astro:middleware';
import { parse, walk, walkSync, renderSync, ELEMENT_NODE } from 'ultrahtml';
export const onRequest = defineMiddleware(async (ctx, next) => {
const response = await next();
const html = await response.text();
const ast = parse(html);
const styleSrcs = [];
await walk(ast, async (node) => {
const inlineStyle = node?.attributes?.style;
if (inlineStyle) {
const encodedStyle = new TextEncoder().encode(inlineStyle);
const digest = await crypto.subtle.digest('SHA-256', encodedStyle);
styleSrcs.push('sha256-' + toBase64(digest));
}
});
const headerValue = `style-src-attr 'unsafe-hashes' ${styleSrcs
.map((src) => `'${src}'`)
.join(' ')}`;
walkSync(ast, async (node) => {
if (node.type === ELEMENT_NODE && node.name === 'head') {
node.children.push({
type: ELEMENT_NODE,
name: 'meta',
attributes: {
'http-equiv': 'Content-Security-Policy',
content: headerValue,
},
children: [],
parent: node,
loc: undefined as any,
isSelfClosingTag: true,
});
}
});
return new Response(renderSync(ast));
});
function toBase64(arrayBuffer: ArrayBuffer) {
const byteArray = new Uint8Array(arrayBuffer);
let byteString = '';
for (let i = 0, { length } = byteArray; i < length; i++) {
byteString += String.fromCharCode(byteArray[i]);
}
return btoa(byteString);
}