Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: let composition images upload to WebGL through a 2D canvas (#10594)

[._.]/ Adam Eivy5e5ddb5895cb
diff --git a/server/services/htmlComposition/browser.js b/server/services/htmlComposition/browser.js
index cbba96c8da..167685a5ed 100644
--- a/server/services/htmlComposition/browser.js
+++ b/server/services/htmlComposition/browser.js
@@ -264,6 +264,21 @@ async function connectComposition(assets, { signal, mediaMode, version }) {
Object.defineProperty(globalThis, key, { configurable: false, writable: false,
value: function(url) { report(String(url)); throw new Error(key + ' is disabled in compositions'); } });
}
+ // The CSP sandbox gives the page an opaque origin, so its own assets are
+ // cross-origin: a no-CORS image taints any canvas it is drawn on and
+ // WebGL then refuses the canvas upload (three.js swallows the
+ // SecurityError and renders black). Assets are served with
+ // Access-Control-Allow-Origin, so default their loads to CORS mode.
+ for (const proto of [HTMLImageElement.prototype, HTMLMediaElement.prototype]) {
+ const src = Object.getOwnPropertyDescriptor(proto, 'src');
+ Object.defineProperty(proto, 'src', { configurable: true, enumerable: src.enumerable, get: src.get,
+ set(value) { if (this.crossOrigin === null) this.crossOrigin = 'anonymous'; src.set.call(this, value); } });
+ const setAttribute = proto.setAttribute;
+ proto.setAttribute = function(name, value) {
+ if (String(name).toLowerCase() === 'src' && this.crossOrigin === null) this.crossOrigin = 'anonymous';
+ return setAttribute.call(this, name, value);
+ };
+ }
})();` });
await send('Fetch.enable', { patterns: [{ urlPattern: '*', requestStage: 'Request' }] });
const navigation = await send('Page.navigate', { url: `${ORIGIN}/index.html` });
diff --git a/server/services/musicVideo/documentRender.browser.test.js b/server/services/musicVideo/documentRender.browser.test.js
index 592c7c9aec..5fac1d061e 100644
--- a/server/services/musicVideo/documentRender.browser.test.js
+++ b/server/services/musicVideo/documentRender.browser.test.js
@@ -128,6 +128,36 @@ describe.skipIf(!canRun)('layered template with real Chrome and ffmpeg', () => {
await untouched.close();
}, 30000);
+ it('lets a composition upload its own images to WebGL through a 2D canvas', async () => {
+ const { openComposition } = await import('../htmlComposition/browser.js');
+ const directory = 'compositions/synthetic-canvas-texture';
+ await mkdir(join(PATHS.data, directory, 'media'), { recursive: true });
+ await writeFile(join(PATHS.data, directory, 'media/still.jpg'),
+ await sharp({ create: { width: 8, height: 8, channels: 3, background: '#ff0000' } }).jpeg().toBuffer());
+ await writeFile(join(PATHS.data, directory, 'index.html'), '<!doctype html><title>Canvas texture</title>');
+ const page = await openComposition(directory, { ownedBrowser: true, signal: testSignal });
+ try {
+ // The sandboxed page has an opaque origin; an image loaded without CORS
+ // would taint the canvas and texImage2D would throw a SecurityError.
+ const outcome = await page.evaluate(`new Promise(resolve => {
+ const img = new Image();
+ img.onload = () => {
+ const canvas = document.createElement('canvas');
+ canvas.width = canvas.height = 8;
+ canvas.getContext('2d').drawImage(img, 0, 0);
+ const gl = document.createElement('canvas').getContext('webgl');
+ try {
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
+ resolve('uploaded');
+ } catch (error) { resolve(error.name); }
+ };
+ img.onerror = () => resolve('image-failed');
+ img.src = 'media/still.jpg';
+ })`);
+ expect(outcome).toBe('uploaded');
+ } finally { await page.close(); }
+ }, 30000);
+
// Server-only CI does not install client dependencies; the full local install
// exercises this cross-workspace package/render contract alongside the UI proof.
const threeIt = existsSync(new URL('../../../client/node_modules/three/package.json', import.meta.url)) ? it : vitestIt.skip;