diff --git a/frontend/src/csp.test.ts b/frontend/src/csp.test.ts index baac15c..e05c989 100644 --- a/frontend/src/csp.test.ts +++ b/frontend/src/csp.test.ts @@ -1,12 +1,10 @@ -import { readFileSync } from 'node:fs'; -import { resolve } from 'node:path'; import { describe, expect, it } from 'vitest'; +import html from '../index.html?raw'; +import themeScript from '../public/theme-init.js?raw'; // The host sends Content-Security-Policy "script-src 'self'": an inline script in the admin page is blocked there, so the // page must have none (the theme is set by /theme-init.js, a file of its own). describe('the admin page and the Content-Security-Policy', () => { - const html = readFileSync(resolve(__dirname, '../index.html'), 'utf-8'); - it('has no inline script', () => { const inline = [...html.matchAll(/]*)>([\s\S]*?)<\/script>/gi)].filter(([, attributes]) => !/\bsrc\s*=/.test(attributes)); @@ -16,9 +14,8 @@ describe('the admin page and the Content-Security-Policy', () => { it('sets the theme from a file of its own, before the app', () => { expect(html.indexOf('src="/theme-init.js"')).toBeGreaterThan(-1); expect(html.indexOf('src="/theme-init.js"')).toBeLessThan(html.indexOf('src="/src/main.tsx"')); - const script = readFileSync(resolve(__dirname, '../public/theme-init.js'), 'utf-8'); - expect(script).toContain("localStorage.getItem('cms-theme')"); - expect(script).toContain('classList.add'); + expect(themeScript).toContain("localStorage.getItem('cms-theme')"); + expect(themeScript).toContain('classList.add'); }); it('has no inline event handlers or javascript: links either', () => {