Skip to content

armgit / ECC

Public Imported from GitHub
0

The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.

fix(scripts): confine plan-canvas artifact assets and escape 404 paths

WRG742395bca70a
diff --git a/scripts/lib/plan-canvas/server.js b/scripts/lib/plan-canvas/server.js
index 11b44062a..961dd559c 100644
--- a/scripts/lib/plan-canvas/server.js
+++ b/scripts/lib/plan-canvas/server.js
@@ -15,7 +15,7 @@ const http = require('http');
const path = require('path');
const { buildAllowedHostnames, isAllowedHostHeader, isAllowedOrigin } = require('../loopback-guard');
-const { renderMarkdown } = require('./markdown');
+const { escapeHtml, renderMarkdown } = require('./markdown');
const { artifactSdkJs } = require('./sdk');
const {
canvasCss,
@@ -101,9 +101,21 @@ function sendJson(res, statusCode, payload) {
res.end(body);
}
+// Artifact pages render inside a sandboxed iframe (no allow-same-origin) and
+// legitimately run CDN scripts (Mermaid) plus inline loaders, so the default
+// restrictive CSP cannot apply. A sandbox-only CSP mirrors the iframe
+// attribute instead: scripts keep working, but the document gets an opaque
+// origin, which neuters direct-navigation abuse of the loopback API (no CORS
+// reads, JSON POSTs are preflight-blocked) without changing in-iframe
+// behavior. A hostile <meta> CSP in a raw HTML artifact can only narrow this
+// further, never loosen it.
+const ARTIFACT_CSP = 'sandbox allow-scripts allow-forms allow-popups';
+
function sendHtml(res, statusCode, html, { csp = true } = {}) {
const headers = { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' };
- if (csp) {
+ if (csp === 'artifact') {
+ headers['content-security-policy'] = ARTIFACT_CSP;
+ } else if (csp) {
headers['content-security-policy'] =
"default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; frame-src 'self'";
}
@@ -493,7 +505,7 @@ function createPlanCanvasServer({
try {
content = fs.readFileSync(session.file, 'utf8');
} catch {
- return sendHtml(res, 404, `<h1>Artifact missing</h1><p>${session.file} no longer exists.</p>`, { csp: false });
+ return sendHtml(res, 404, `<h1>Artifact missing</h1><p>${escapeHtml(session.file)} no longer exists.</p>`);
}
const ext = path.extname(session.file).toLowerCase();
if (ext === '.md' || ext === '.markdown') {
@@ -501,29 +513,41 @@ function createPlanCanvasServer({
title: path.basename(session.file),
sdkSrc: '/sdk.js'
});
- return sendHtml(res, 200, html, { csp: false });
+ return sendHtml(res, 200, html, { csp: 'artifact' });
}
const sdkTag = '<script src="/sdk.js"></script>';
const injected = content.includes('</body>')
? content.replace('</body>', `${sdkTag}\n</body>`)
: `${content}\n${sdkTag}`;
- return sendHtml(res, 200, injected, { csp: false });
+ return sendHtml(res, 200, injected, { csp: 'artifact' });
}
// Sibling assets resolve relative to the artifact's directory and must
- // stay confined to it.
+ // stay confined to it. The prefix check alone is insufficient: a symlink
+ // inside the directory can point outside it, so the check is repeated
+ // against the real paths and fails closed when they cannot be resolved.
const baseDir = path.dirname(session.file);
const resolved = path.resolve(baseDir, assetPath);
if (resolved !== baseDir && !resolved.startsWith(baseDir + path.sep)) {
return sendJson(res, 403, { error: 'asset path escapes artifact directory' });
}
+ let realTarget;
+ try {
+ const realBase = fs.realpathSync(baseDir);
+ realTarget = fs.realpathSync(resolved);
+ if (realTarget !== realBase && !realTarget.startsWith(realBase + path.sep)) {
+ return sendJson(res, 403, { error: 'asset path escapes artifact directory' });
+ }
+ } catch {
+ return sendJson(res, 404, { error: 'asset not found' });
+ }
let data;
try {
- data = fs.readFileSync(resolved);
+ data = fs.readFileSync(realTarget);
} catch {
return sendJson(res, 404, { error: 'asset not found' });
}
- const type = CONTENT_TYPES[path.extname(resolved).toLowerCase()] || 'application/octet-stream';
+ const type = CONTENT_TYPES[path.extname(realTarget).toLowerCase()] || 'application/octet-stream';
res.writeHead(200, { 'content-type': type, 'cache-control': 'no-store' });
return res.end(data);
}
diff --git a/tests/scripts/plan-canvas.test.js b/tests/scripts/plan-canvas.test.js
index 5d1e8745f..152d9c35b 100644
--- a/tests/scripts/plan-canvas.test.js
+++ b/tests/scripts/plan-canvas.test.js
@@ -182,7 +182,7 @@ async function main() {
assert.ok(res.body.includes('<h1 id="plan-demo">'));
assert.ok(res.body.includes('<table>'));
assert.ok(res.body.includes('<script src="/sdk.js">'));
- assert.strictEqual(res.headers['content-security-policy'], undefined);
+ assert.strictEqual(res.headers['content-security-policy'], 'sandbox allow-scripts allow-forms allow-popups');
// No diagram in this plan → no Mermaid loader shipped.
assert.ok(!res.body.includes('mermaid.run'));
})) passed++; else failed++;
@@ -214,6 +214,36 @@ async function main() {
assert.strictEqual(escape.statusCode, 403);
})) passed++; else failed++;
+ if (await test('artifact responses carry a sandbox CSP (direct-navigation hardening)', async () => {
+ const md = await request(port, 'GET', `/artifact/${key}/`);
+ assert.strictEqual(md.statusCode, 200);
+ assert.strictEqual(md.headers['content-security-policy'], 'sandbox allow-scripts allow-forms allow-popups');
+ const html = await request(port, 'GET', `/artifact/${htmlKey}/`);
+ assert.strictEqual(html.statusCode, 200);
+ assert.strictEqual(html.headers['content-security-policy'], 'sandbox allow-scripts allow-forms allow-popups');
+ })) passed++; else failed++;
+
+ if (await test('missing-artifact 404 escapes the file path', async () => {
+ const evilFile = path.join(tmp, 'evil<img src=x onerror=alert(1)>.plan.md');
+ fs.writeFileSync(evilFile, '# Evil\n');
+ const opened = jsonBody(await request(port, 'POST', '/api/sessions', { body: { file: evilFile } }));
+ fs.rmSync(evilFile);
+ const res = await request(port, 'GET', `/artifact/${opened.key}/`);
+ assert.strictEqual(res.statusCode, 404);
+ assert.ok(!res.body.includes('<img src=x'), 'raw filename must not appear in the 404 page');
+ assert.ok(res.body.includes('&lt;img'), 'filename must be HTML-escaped in the 404 page');
+ })) passed++; else failed++;
+
+ if (await test('symlinked sibling assets escaping the artifact dir are blocked', async () => {
+ fs.symlinkSync(path.join(outsideDir, 'secret.txt'), path.join(tmp, 'evil-link.txt'));
+ const blocked = await request(port, 'GET', `/artifact/${key}/evil-link.txt`);
+ assert.strictEqual(blocked.statusCode, 403);
+ fs.symlinkSync(path.join(tmp, 'style.css'), path.join(tmp, 'ok-link.css'));
+ const allowed = await request(port, 'GET', `/artifact/${key}/ok-link.css`);
+ assert.strictEqual(allowed.statusCode, 200);
+ assert.ok(allowed.body.includes('color: red'));
+ })) passed++; else failed++;
+
if (await test('static chrome assets are served', async () => {
for (const asset of ['/canvas.css', '/client.js', '/sdk.js']) {
const res = await request(port, 'GET', asset);