fix(page-data): harden structural delivery and Portal base URL

Reject /api/page-data/ legacy passthrough, verify live API before send,
and resolve delivery links to Portal (8081) instead of Vite (5173).
Add local repair/verify scripts for daily-register Page Data flow.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-07-13 09:35:40 +08:00
parent 249b0697cb
commit 804f13bc04
11 changed files with 712 additions and 51 deletions
+50
View File
@@ -35,6 +35,10 @@ async function save(data) {
}
</script></body></html>`;
const LEGACY_API_HTML = `<!doctype html><html><body><script>
fetch('/api/page-data/daily_todo', { method: 'POST', body: JSON.stringify({ name: 'test' }) });
</script></body></html>`;
test('evaluatePageDataHtmlContent flags missing client script and localStorage fallback', () => {
const ok = evaluatePageDataHtmlContent(SURVEY_HTML, { relativePath: 'public/diet-survey.html' });
assert.deepEqual(ok.issues, []);
@@ -44,6 +48,30 @@ test('evaluatePageDataHtmlContent flags missing client script and localStorage f
assert.ok(bad.issues.includes('forbidden_local_storage'));
});
test('evaluatePageDataHtmlContent flags forbidden /api/page-data passthrough', () => {
const bad = evaluatePageDataHtmlContent(LEGACY_API_HTML, { relativePath: 'public/daily-todo.html' });
assert.ok(bad.issues.includes('forbidden_legacy_page_data_api'));
assert.ok(bad.issues.includes('missing_page_data_client_script'));
});
test('finish guard blocks delivery when html uses legacy page-data API', () => {
const publishDir = fs.mkdtempSync(path.join(os.tmpdir(), 'page-data-guard-legacy-'));
try {
fs.mkdirSync(path.join(publishDir, 'public'), { recursive: true });
fs.writeFileSync(path.join(publishDir, 'public', 'daily-todo.html'), LEGACY_API_HTML, 'utf8');
const evaluation = evaluatePageDataFinishGuard({
publishDir,
agentText: '帮我设计一个调查问卷,要加一个后台',
messages: [{ content: [{ type: 'toolRequest', toolCall: { value: { name: 'write_file', arguments: { path: 'public/daily-todo.html' } } } }] }],
});
assert.equal(evaluation.pageDataIntent, true);
assert.ok(evaluation.htmlIssues.some((item) => item.issue === 'forbidden_legacy_page_data_api'));
assert.equal(evaluation.needsRepair, true);
} finally {
fs.rmSync(publishDir, { recursive: true, force: true });
}
});
test('inferPageDataBindAccessMode chooses password for admin html', () => {
assert.equal(
inferPageDataBindAccessMode('public/diet-survey-admin.html', SURVEY_HTML),
@@ -191,6 +219,28 @@ test('evaluatePageDataFinishGuard flags html when dataset is not registered', ()
}
});
test('collectPageDataDeliveryArtifacts uses Portal base when H5_PUBLIC_BASE_URL points at Vite', () => {
const publishDir = fs.mkdtempSync(path.join(os.tmpdir(), 'page-data-guard-portal-url-'));
const previousBase = process.env.H5_PUBLIC_BASE_URL;
const previousDelivery = process.env.MEMIND_PAGE_DATA_DELIVERY_BASE_URL;
delete process.env.MEMIND_PAGE_DATA_DELIVERY_BASE_URL;
process.env.H5_PUBLIC_BASE_URL = 'http://127.0.0.1:5173';
process.env.H5_PORT = '8081';
try {
fs.mkdirSync(path.join(publishDir, 'public'), { recursive: true });
fs.writeFileSync(path.join(publishDir, 'public', 'form.html'), SURVEY_HTML, 'utf8');
const artifacts = collectPageDataDeliveryArtifacts(publishDir);
assert.equal(artifacts.length, 1);
assert.match(artifacts[0].url, /^http:\/\/127\.0\.0\.1:8081\/MindSpace\//);
} finally {
if (previousBase == null) delete process.env.H5_PUBLIC_BASE_URL;
else process.env.H5_PUBLIC_BASE_URL = previousBase;
if (previousDelivery == null) delete process.env.MEMIND_PAGE_DATA_DELIVERY_BASE_URL;
else process.env.MEMIND_PAGE_DATA_DELIVERY_BASE_URL = previousDelivery;
fs.rmSync(publishDir, { recursive: true, force: true });
}
});
test('rewritePageDataDeliveryLinks rewrites publication route urls to MindSpace workspace urls', () => {
const publishDir = fs.mkdtempSync(path.join(os.tmpdir(), 'page-data-guard-urls-'));
const previousBase = process.env.H5_PUBLIC_BASE_URL;