|
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | +import { imports } from '@shgysk8zer0/importmap'; |
| 3 | + |
| 4 | +const pkg = JSON.parse(await readFile(process.cwd() + '/package.json', { encoding: 'utf8' })); |
| 5 | + |
| 6 | +export const importmap = JSON.stringify({ |
| 7 | + imports: { |
| 8 | + ...imports, |
| 9 | + [pkg.name]: pkg.exports['.'].import, |
| 10 | + [`${pkg.name}/`]: './', |
| 11 | + } |
| 12 | +}); |
| 13 | + |
| 14 | +const sri = async (input) => await Promise.resolve(input) |
| 15 | + .then(json => new TextEncoder().encode(json)) |
| 16 | + .then(bytes => crypto.subtle.digest('SHA-384', bytes)) |
| 17 | + .then(hash => 'sha384-' + new Uint8Array(hash).toBase64()); |
| 18 | + |
| 19 | +export const integrity = await sri(importmap); |
| 20 | + |
| 21 | +const DEFAULT_SRC = ['\'self\'']; |
| 22 | +const SCRIPT_SRC = ['\'self\'', 'https://unpkg.com/@shgysk8zer0/', 'https://unpkg.com/@kernvalley/', 'https://unpkg.com/@aegisjsproject/', `'${integrity}'`]; |
| 23 | +const STYLE_SRC = ['\'self\'', 'https://unpkg.com/@agisjsproject/', 'blob:']; |
| 24 | +const IMAGE_SRC = ['\'self\'', 'https://i.imgur.com/', 'https://secure.gravatar.com/avatar/', 'blob:', 'data:']; |
| 25 | +const MEDIA_SRC = ['\'self\'', 'blob:']; |
| 26 | +const CONNECT_SRC = ['\'self\'']; |
| 27 | +const FONT_SRC = ['\'self\'']; |
| 28 | +const FRAME_SRC = ['\'self\'', 'https://www.youtube-nocookie.com']; |
| 29 | +const TRUSTED_TYPES = ['aegis-sanitizer#html']; |
| 30 | + |
| 31 | +export const addDefaultSrc = (...srcs) => DEFAULT_SRC.push(...srcs); |
| 32 | +export const addScriptSrc = (...srcs) => SCRIPT_SRC.push(...srcs); |
| 33 | +export const addStyleSrc = (...srcs) => STYLE_SRC.push(...srcs); |
| 34 | +export const addImageSrc = (...srcs) => IMAGE_SRC.push(...srcs); |
| 35 | +export const addMediaSrc = (...srcs) => MEDIA_SRC.push(...srcs); |
| 36 | +export const addConnectSrc = (...srcs) => CONNECT_SRC.push(...srcs); |
| 37 | +export const addFontSrc = (...srcs) => FONT_SRC.push(...srcs); |
| 38 | +export const addFrameSrc = (...srcs) => FRAME_SRC.push(...srcs); |
| 39 | +export const addTrustedType = (...policies) => TRUSTED_TYPES.push(...policies); |
| 40 | + |
| 41 | +export const getCSP = () => [ |
| 42 | + 'default-src ' + DEFAULT_SRC.join(' '), |
| 43 | + 'script-src ' + SCRIPT_SRC.join(' '), |
| 44 | + 'style-src ' + STYLE_SRC.join(' '), |
| 45 | + 'image-src ' + IMAGE_SRC.join(' '), |
| 46 | + 'media-src ' + MEDIA_SRC.join(' '), |
| 47 | + 'font-src ' + FONT_SRC.join(' '), |
| 48 | + 'frame-src ' + FRAME_SRC.join(' '), |
| 49 | + 'connect-src ' + CONNECT_SRC.join(' '), |
| 50 | + 'tusted-types ' + TRUSTED_TYPES.join(' '), |
| 51 | + 'require-trusted-types-for \'script\'', |
| 52 | +].join('; '); |
0 commit comments