Files
memind/mindspace-published-page-csp.test.mjs
T
john ae9090948a fix(mindspace): relax published page CSP so agent HTML interactions work
Switch default MindSpace HTML delivery to a hybrid inline CSP so onclick handlers and addEventListener pages both run under real HTTP headers, and add Playwright interaction verify beyond HTTP 200 checks.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-13 15:39:39 +08:00

45 lines
2.0 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>`;
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);
assert.match(csp, /script-src 'unsafe-inline' 'self'/);
assert.match(csp, /script-src-attr 'unsafe-inline'/);
assert.doesNotMatch(csp, /'sha256-/);
});
test('publishedPageCsp default full html allows dynamic onclick handlers', () => {
const csp = publishedPageCsp(INLINE_ONCLICK_HTML);
assert.match(csp, /script-src 'unsafe-inline'/);
assert.match(csp, /script-src-attr 'unsafe-inline'/);
});
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);
});
test('publishedPageCsp wechatShare mode allows self and wx bridge scripts', () => {
const csp = publishedPageCsp(PAGE_DATA_HTML, { wechatShare: true });
assert.match(csp, /script-src 'unsafe-inline' 'self' https:\/\/res\.wx\.qq\.com/);
assert.match(csp, /script-src-attr 'unsafe-inline'/);
});
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'/);
});