Files
memind/mindspace-published-page-csp.test.mjs

26 lines
1.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { publishedPageCsp } from './mindspace-published-page-csp.mjs';
const PAGE_DATA_HTML = `<!doctype html><html><head></head><body>
<script src="/assets/page-data-client.js"></script>
<script>var client = MindSpacePageData.createClient({ apiBase: '/api' });</script>
</body></html>`;
test('publishedPageCsp raw mode allows same-origin external scripts for page-data-client.js', () => {
const csp = publishedPageCsp(PAGE_DATA_HTML, { raw: true });
assert.match(csp, /script-src 'unsafe-inline' 'self'/);
});
test('publishedPageCsp raw mode keeps inline-only pages restricted', () => {
const html = '<!doctype html><html><body><script>console.log(1)</script></body></html>';
const csp = publishedPageCsp(html, { raw: true });
assert.match(csp, /script-src 'unsafe-inline'/);
assert.doesNotMatch(csp, /script-src 'unsafe-inline' 'self'/);
});
test('publishedPageCsp non-raw full html allows self when external scripts are present', () => {
const csp = publishedPageCsp(PAGE_DATA_HTML, { raw: false });
assert.match(csp, /script-src 'self'/);
});