2026-07-08 21:10:47 +08:00
|
|
|
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>`;
|
|
|
|
|
|
2026-07-13 15:39:39 +08:00
|
|
|
const INLINE_ONCLICK_HTML = `<!doctype html><html><body>
|
|
|
|
|
<button id="btn" onclick="document.body.dataset.clicked='1'">点我</button>
|
|
|
|
|
</body></html>`;
|
|
|
|
|
|
|
|
|
|
test('publishedPageCsp default full html uses hybrid inline policy with self scripts', () => {
|
|
|
|
|
const csp = publishedPageCsp(PAGE_DATA_HTML);
|
2026-07-08 21:10:47 +08:00
|
|
|
assert.match(csp, /script-src 'unsafe-inline' 'self'/);
|
2026-07-13 15:39:39 +08:00
|
|
|
assert.match(csp, /script-src-attr 'unsafe-inline'/);
|
|
|
|
|
assert.doesNotMatch(csp, /'sha256-/);
|
2026-07-08 21:10:47 +08:00
|
|
|
});
|
|
|
|
|
|
2026-07-13 15:39:39 +08:00
|
|
|
test('publishedPageCsp default full html allows dynamic onclick handlers', () => {
|
|
|
|
|
const csp = publishedPageCsp(INLINE_ONCLICK_HTML);
|
2026-07-08 21:10:47 +08:00
|
|
|
assert.match(csp, /script-src 'unsafe-inline'/);
|
2026-07-13 15:39:39 +08:00
|
|
|
assert.match(csp, /script-src-attr 'unsafe-inline'/);
|
2026-07-08 21:10:47 +08:00
|
|
|
});
|
|
|
|
|
|
2026-07-13 15:39:39 +08:00
|
|
|
test('publishedPageCsp raw mode matches default hybrid policy for full html', () => {
|
|
|
|
|
const rawCsp = publishedPageCsp(PAGE_DATA_HTML, { raw: true });
|
|
|
|
|
const defaultCsp = publishedPageCsp(PAGE_DATA_HTML, { raw: false });
|
|
|
|
|
assert.equal(rawCsp, defaultCsp);
|
2026-07-08 21:10:47 +08:00
|
|
|
});
|
2026-07-12 22:21:57 +08:00
|
|
|
|
2026-07-13 15:39:39 +08:00
|
|
|
test('publishedPageCsp wechatShare mode allows self and wx bridge scripts', () => {
|
2026-07-12 22:21:57 +08:00
|
|
|
const csp = publishedPageCsp(PAGE_DATA_HTML, { wechatShare: true });
|
|
|
|
|
assert.match(csp, /script-src 'unsafe-inline' 'self' https:\/\/res\.wx\.qq\.com/);
|
2026-07-13 15:39:39 +08:00
|
|
|
assert.match(csp, /script-src-attr 'unsafe-inline'/);
|
2026-07-12 22:21:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('publishedPageCsp wechatShare mode keeps inline-only pages without self', () => {
|
|
|
|
|
const html = '<!doctype html><html><body><script>console.log(1)</script></body></html>';
|
|
|
|
|
const csp = publishedPageCsp(html, { wechatShare: true });
|
|
|
|
|
assert.match(csp, /script-src 'unsafe-inline' https:\/\/res\.wx\.qq\.com/);
|
|
|
|
|
assert.doesNotMatch(csp, /script-src 'unsafe-inline' 'self'/);
|
|
|
|
|
});
|