Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(server): match cron paths the way the dispatcher normalizes them and keep a handler's own CSP

nirholas8ca11edd3a65
diff --git a/server/cron-edge-auth.mjs b/server/cron-edge-auth.mjs
index 9c950b93a..e1a03703b 100644
--- a/server/cron-edge-auth.mjs
+++ b/server/cron-edge-auth.mjs
@@ -159,9 +159,23 @@ export async function cronEdgeVerdict(req, { env = process.env, keys } = {}) {
return { allow: false, via: 'none', status: 401 };
}
-/** Does this path route into api/cron/? */
+/**
+ * Does this path route into api/cron/? Judged on the same view of the path the
+ * API dispatcher uses (server/route-resolve.mjs apiSegments): empty segments
+ * dropped and each segment percent-decoded. Matching the raw string let
+ * `/api//cron/x` and `/api/%63ron/x` skip this gate while still dispatching to
+ * api/cron/x.js.
+ */
export function isCronPath(pathname) {
- return typeof pathname === 'string' && pathname.startsWith(CRON_PREFIX) && pathname.length > CRON_PREFIX.length;
+ if (typeof pathname !== 'string') return false;
+ let segments;
+ try {
+ segments = pathname.split('/').filter(Boolean).map(decodeURIComponent);
+ } catch {
+ // Undecodable: the dispatcher refuses it too, so treat it as gated.
+ return pathname.toLowerCase().includes('cron');
+ }
+ return segments.length > 2 && segments[0] === 'api' && segments[1] === 'cron';
}
/**
diff --git a/server/csp-hashes.mjs b/server/csp-hashes.mjs
index 2eda2b099..bf12bdcbc 100644
--- a/server/csp-hashes.mjs
+++ b/server/csp-hashes.mjs
@@ -224,17 +224,25 @@ function bodyText(chunk) {
* `javascript:` href) ran with nothing behind the escaping to stop it.
*
* Only a body delivered whole through `res.end()` before headers are sent is
- * rewritten; a streamed response keeps the policy it was given.
+ * rewritten; a streamed response keeps the policy it was given. So does a
+ * handler that sets a policy of its own (api/artifact.js deliberately answers
+ * with the policy its host sandbox expects): only the inherited route-table
+ * policy, the one in place when this is installed, is ever rewritten.
*
* @param {import('node:http').ServerResponse} res
* @returns {import('node:http').ServerResponse}
*/
export function hardenOnEnd(res) {
+ const inherited = res.getHeader('content-security-policy');
const end = res.end;
res.end = function hardenedEnd(chunk, ...rest) {
if (!res.headersSent && chunk != null && typeof chunk !== 'function') {
const csp = res.getHeader('content-security-policy');
- if (typeof csp === 'string' && ACTIVE_DOCUMENT.test(String(res.getHeader('content-type') || ''))) {
+ if (
+ typeof csp === 'string' &&
+ csp === inherited &&
+ ACTIVE_DOCUMENT.test(String(res.getHeader('content-type') || ''))
+ ) {
const html = bodyText(chunk);
if (html !== null) res.setHeader('content-security-policy', hardenInlineScripts(csp, html));
}
diff --git a/tests/csp-hashes.test.js b/tests/csp-hashes.test.js
index 20ea4da3b..58f11067b 100644
--- a/tests/csp-hashes.test.js
+++ b/tests/csp-hashes.test.js
@@ -273,6 +273,14 @@ describe('hardenOnEnd', () => {
expect(res.getHeader('content-security-policy')).toBe(POLICY);
});
+ it('keeps a policy the handler set for itself', () => {
+ const res = hardenOnEnd(fakeRes({ 'content-security-policy': POLICY, 'content-type': 'text/html' }));
+ const own = "default-src 'none'; script-src 'unsafe-inline'";
+ res.setHeader('content-security-policy', own);
+ res.end('<script>a()</script>');
+ expect(res.getHeader('content-security-policy')).toBe(own);
+ });
+
it('passes a bodiless end straight through', () => {
const res = hardenOnEnd(fakeRes({ 'content-security-policy': POLICY, 'content-type': 'text/html' }));
res.end();
diff --git a/tests/server-cron-edge-auth.test.js b/tests/server-cron-edge-auth.test.js
index eba7ff160..310618df3 100644
--- a/tests/server-cron-edge-auth.test.js
+++ b/tests/server-cron-edge-auth.test.js
@@ -91,6 +91,17 @@ describe('cron edge gate: path scoping', () => {
expect(isCronPath('/api/crontab')).toBe(false);
expect(isCronPath('/')).toBe(false);
});
+
+ it('sees through the path spellings the API dispatcher normalizes', () => {
+ // route-resolve drops empty segments and percent-decodes each one, so
+ // these all dispatch to api/cron/x.js and must be gated like it.
+ expect(isCronPath('/api//cron/economy-tick')).toBe(true);
+ expect(isCronPath('//api/cron/economy-tick')).toBe(true);
+ expect(isCronPath('/api/%63ron/economy-tick')).toBe(true);
+ expect(isCronPath('/api/cron//economy-tick')).toBe(true);
+ expect(isCronPath('/api/cron/%E0%A4%A')).toBe(true);
+ expect(isCronPath('/api/%63rontab')).toBe(false);
+ });
});
describe('cron edge gate: the secret path', () => {