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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(skills): run a skill on the main thread only when the host's origin served it
nirholas6399fe284195
diff --git a/src/element.js b/src/element.jsindex f63ffb08c..9f96c6014 100644--- a/src/element.js+++ b/src/element.js@@ -1958,6 +1958,9 @@ class Agent3DElement extends HTMLElement {this._skills = new SkillRegistry({trust: this.getAttribute('skill-trust') || 'owned-only',ownerAddress: manifest.id?.owner,+ // Bundles from the page itself or from the origin that served this+ // library may run on the main thread; any other origin is sandboxed.+ mainThreadOrigins: [_pageOrigin(), _scriptOrigin].filter(Boolean),});const skillList = manifest.skills || [];for (const spec of skillList) {diff --git a/src/skills/index.js b/src/skills/index.jsindex 78ea88742..10fed182c 100644--- a/src/skills/index.js+++ b/src/skills/index.js@@ -27,6 +27,17 @@ function parseFrontmatter(text) {return { meta, body: m[2] };}+// The origin of an http(s) URL (a root-relative one resolves against the page),+// or '' for anything else.+function originOf(url) {+ try {+ const u = new URL(url, typeof location !== 'undefined' ? location.href : undefined);+ return u.protocol === 'https:' || u.protocol === 'http:' ? u.origin : '';+ } catch {+ return '';+ }+}+function joinURI(base, rel) {if (/^([a-z]+:|\/)/i.test(rel)) return rel;if (!base.endsWith('/')) base += '/';@@ -34,7 +45,7 @@ function joinURI(base, rel) {}export class Skill {- constructor({ uri, manifest, instructions, tools, handlers, handlersSrc }) {+ constructor({ uri, manifest, instructions, tools, handlers, handlersSrc, mainThread = false }) {this.uri = uri;this.manifest = manifest;this.name = manifest.name;@@ -44,6 +55,9 @@ export class Skill {// trusted-main-thread: populated from dynamic import; sandbox: populated from handlersSrcthis.handlers = handlers || {};this.handlersSrc = handlersSrc || null;+ // Decided by SkillRegistry, never read straight off the manifest: a+ // manifest is authored by whoever hosts the bundle.+ this.mainThread = Boolean(mainThread);}async invoke(toolName, args, ctx) {@@ -57,8 +71,8 @@ export class Skill {...(paymentProof && { paymentProof }),};- // Owner-signed skills may opt out of the sandbox via sandboxPolicy: "trusted-main-thread"- if (this.manifest.sandboxPolicy === 'trusted-main-thread') {+ // A skill the registry cleared for the main thread (see mayRunOnMainThread).+ if (this.mainThread) {const handler = this.handlers[toolName];if (!handler) {throw new Error(`Skill "${this.name}" has no handler for tool "${toolName}"`);@@ -80,13 +94,41 @@ export class Skill {}export class SkillRegistry {- constructor({ fetchFn = fetch.bind(globalThis), trust = 'owned-only', ownerAddress } = {}) {+ constructor({+ fetchFn = fetch.bind(globalThis),+ trust = 'owned-only',+ ownerAddress,+ mainThreadOrigins,+ } = {}) {this.fetchFn = fetchFn;this.trust = trust;this.ownerAddress = ownerAddress?.toLowerCase();+ this.mainThreadOrigins = new Set(+ (mainThreadOrigins ?? [typeof location !== 'undefined' ? location.origin : ''])+ .map(originOf)+ .filter(Boolean),+ );this.skills = new Map();}+ /**+ * Whether a bundle's handlers may be imported into the page instead of the+ * worker. `sandboxPolicy: "trusted-main-thread"` is only a request: the+ * manifest is written by whoever hosts the bundle, and an on-chain agent's+ * skill URIs are written by whoever minted it, so honoring the flag on its+ * own let any such bundle run arbitrary code in the embedding page. The+ * request is granted only for bundles served from an origin the host trusts+ * (by default the page's own) and never under `any` trust.+ * @param {object} manifest+ * @param {string} uri resolved bundle URI+ */+ mayRunOnMainThread(manifest, uri) {+ if (manifest?.sandboxPolicy !== 'trusted-main-thread') return false;+ if (this.trust === 'any') return false;+ const origin = originOf(uri);+ return Boolean(origin) && this.mainThreadOrigins.has(origin);+ }+async install(spec, { bundleBase } = {}) {// spec: { uri, version? } — uri may be relative, ipfs://, or https://const uri = this._resolveBundleURI(spec.uri, bundleBase);@@ -95,7 +137,12 @@ export class SkillRegistry {const manifest = await this._fetchJSON(`${uri}manifest.json`);this._enforceTrust(manifest);- const isTrusted = manifest.sandboxPolicy === 'trusted-main-thread';+ const isTrusted = this.mayRunOnMainThread(manifest, uri);+ if (manifest.sandboxPolicy === 'trusted-main-thread' && !isTrusted) {+ log.warn(+ `[skills] "${manifest.name}" asked for the main thread from an untrusted origin; running it in the sandbox`,+ );+ }const [instructions, toolsJSON, handlersData] = await Promise.all([this._fetchText(`${uri}SKILL.md`).catch(() => ''),@@ -112,6 +159,7 @@ export class SkillRegistry {tools: toolsJSON.tools || [],handlers: isTrusted ? handlersData || {} : {},handlersSrc: isTrusted ? null : handlersData,+ mainThread: isTrusted,});// Recursively install skill dependenciesdiff --git a/tests/src/agent-skills.test.js b/tests/src/agent-skills.test.jsindex e2555f2e0..14ef648b7 100644--- a/tests/src/agent-skills.test.js+++ b/tests/src/agent-skills.test.js@@ -122,6 +122,63 @@ describe('SkillRegistry.install – trust modes', () => {});});+describe('SkillRegistry.install – main-thread opt-out', () => {+ const mainThreadManifest = () => fixtureManifest({ sandboxPolicy: 'trusted-main-thread' });++ it('sandboxes a trusted-main-thread bundle served from an untrusted origin', async () => {+ const registry = new SkillRegistry({+ fetchFn: makeFetch(fixtureRoutes(mainThreadManifest())),+ trust: 'owned-only',+ mainThreadOrigins: ['https://three.ws'],+ });+ const skill = await registry.install({ uri: 'https://cdn.jsdelivr.net/npm/[email protected]/' });+ expect(skill.mainThread).toBe(false);+ expect(skill.handlersSrc).toBe('export default {}');+ });++ it('never grants the main thread under any trust, even from a trusted origin', async () => {+ const registry = new SkillRegistry({+ fetchFn: makeFetch(fixtureRoutes(mainThreadManifest())),+ trust: 'any',+ mainThreadOrigins: ['https://three.ws'],+ });+ const skill = await registry.install({ uri: 'https://three.ws/skills/tip-jar/' });+ expect(skill.mainThread).toBe(false);+ });++ it('grants the main thread to a bundle from a trusted origin', () => {+ const registry = new SkillRegistry({+ fetchFn: makeFetch({}),+ trust: 'owned-only',+ mainThreadOrigins: ['https://three.ws'],+ });+ expect(registry.mayRunOnMainThread(mainThreadManifest(), 'https://three.ws/skills/tip-jar/')).toBe(true);+ expect(registry.mayRunOnMainThread(fixtureManifest(), 'https://three.ws/skills/tip-jar/')).toBe(false);+ expect(registry.mayRunOnMainThread(mainThreadManifest(), 'https://three.ws.evil.example/x/')).toBe(false);+ expect(registry.mayRunOnMainThread(mainThreadManifest(), 'data:text/javascript,alert(1)')).toBe(false);+ });++ it('routes a downgraded skill through the sandbox host on invoke', async () => {+ const { getHost } = await import('../../src/skills/sandbox-host.js');+ const invoke = vi.fn(async () => ({ ok: true }));+ getHost.mockReturnValue({ invoke });+ const registry = new SkillRegistry({+ fetchFn: makeFetch(fixtureRoutes(mainThreadManifest())),+ trust: 'owned-only',+ mainThreadOrigins: ['https://three.ws'],+ });+ const skill = await registry.install({ uri: 'https://attacker.example/skill/' });+ await skill.invoke('do_it', {}, {});+ expect(invoke).toHaveBeenCalledWith(+ 'https://attacker.example/skill/',+ 'do_it',+ {},+ 'export default {}',+ expect.any(Object),+ );+ });+});+describe('SkillRegistry.install – dependency loading', () => {it('recursively installs each dependency URI listed in manifest.dependencies', async () => {const depManifest = fixtureManifest({ name: 'dep-skill', dependencies: undefined });