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
chore: sync working tree
nirholas6fd7a1234423
diff --git a/api/_lib/forge-store.js b/api/_lib/forge-store.jsindex e5767a910..340765905 100644--- a/api/_lib/forge-store.js+++ b/api/_lib/forge-store.js@@ -251,6 +251,17 @@ async function scoreAndCompress(buf, { computeQuality, compress, cleanup = falsereturn { buf: outBuf, quality, compression, cleaned };}+// An upstream Content-Type ends up as the object's stored type, which the CDN+// then serves from the three.ws origin. Only ever store a media type: a+// provider (or a redirect chain that ends somewhere unexpected) answering with+// `text/html` or `image/svg+xml` must not get to decide that.+const STORABLE_TYPE_RE = /^(?:image\/(?:png|jpeg|webp|gif|avif)|model\/[\w.+-]+|video\/(?:mp4|webm)|audio\/[\w.+-]+|application\/(?:json|octet-stream))$/;++function mediaTypeOr(header, fallback) {+ const type = String(header || '').split(';')[0].trim().toLowerCase();+ return STORABLE_TYPE_RE.test(type) ? type : fallback;+}+async function copyToBucket({ sourceUrl, key, fallbackContentType, maxBytes, computeQuality = false, compress = null, cleanup = false, forceContentType = null }) {let lastErr;for (let attempt = 1; attempt <= COPY_MAX_ATTEMPTS; attempt++) {@@ -275,7 +286,7 @@ async function copyToBucket({ sourceUrl, key, fallbackContentType, maxBytes, com// won't render a cross-origin <img> whose declared type isn't an image// type. Callers that know the asset kind (e.g. the preview image, whose// extension is already decided by imageExtFor) should force it.- const contentType = forceContentType || resp.headers.get('content-type') || fallbackContentType;+ const contentType = forceContentType || mediaTypeOr(resp.headers.get('content-type'), fallbackContentType);let quality = null;let compression = null;let cleaned = null;diff --git a/api/cdn-object.js b/api/cdn-object.jsindex e40593265..da8df3f0c 100644--- a/api/cdn-object.js+++ b/api/cdn-object.js@@ -9,7 +9,13 @@// `s-maxage`, so each object is fetched from R2 roughly once per region per day.//// Exposure parity: the bucket is already fully readable through the public-// r2.dev domain, so serving the same namespace here grants nothing new.+// r2.dev domain, so serving the same namespace here grants read access to+// nothing new. What it DOES change is the origin the bytes arrive on: r2.dev is+// a foreign origin, three.ws is where the session cookie and the wallet live.+// So every response here is pinned to "data, never a document": the type comes+// from the server-chosen extension rather than the stored header, anything not+// safe to render inline is sent as an attachment, and `sandbox` puts whatever+// does render into an opaque origin. See `isInlineSafe` below.import { GetObjectCommand } from '@aws-sdk/client-s3';import { cors, error, wrap } from './_lib/http.js';@@ -39,12 +45,43 @@ const CONTENT_TYPES = {webm: 'video/webm',};-function contentTypeFor(key, stored) {- // R2 objects uploaded without an explicit type default to octet-stream —- // prefer the extension in that case so browsers render instead of download.- if (stored && stored !== 'application/octet-stream') return stored;+// Types a browser may render inline from the app origin. SVG is deliberately+// absent: an SVG document executes script, and one served inline from three.ws+// would run with three.ws's cookies. It still renders through <img>/<image>,+// which ignore content-disposition and never run script.+const INLINE_SAFE = new Set([+ 'image/png',+ 'image/jpeg',+ 'image/webp',+ 'image/gif',+ 'image/avif',+ 'model/gltf-binary',+ 'model/gltf+json',+ 'model/vnd.usdz+zip',+ 'audio/mpeg',+ 'audio/wav',+ 'audio/ogg',+ 'video/mp4',+ 'video/webm',+ 'application/json',+ 'application/octet-stream',+]);++export function isInlineSafe(type) {+ return INLINE_SAFE.has(String(type).split(';')[0].trim().toLowerCase());+}++export function contentTypeFor(key, stored) {const ext = key.split('.').pop()?.toLowerCase();- return CONTENT_TYPES[ext] || stored || 'application/octet-stream';+ const byExt = CONTENT_TYPES[ext];+ // The extension wins over the stored header. Every write path picks the+ // extension server-side from an allowlisted type, whereas a stored+ // Content-Type can be copied verbatim from an upstream provider's response+ // (copyToBucket in _lib/forge-store.js). Preferring the header, as this used+ // to, let a remote `text/html` decide what three.ws serves.+ if (byExt) return byExt;+ if (stored && isInlineSafe(stored)) return stored;+ return 'application/octet-stream';}function cacheControlFor(key) {@@ -82,8 +119,16 @@ export default wrap(async (req, res) => {}),);+ const type = contentTypeFor(key, obj.ContentType);res.statusCode = range && obj.ContentRange ? 206 : 200;- res.setHeader('content-type', contentTypeFor(key, obj.ContentType));+ res.setHeader('content-type', type);+ // A bucket object is data. `sandbox` drops anything that does reach a+ // top-level navigation into an opaque origin, so it can neither script+ // against three.ws nor read its storage; CORP stays permissive because the+ // CORS policy above is `*` and embeds depend on it.+ res.setHeader('content-security-policy', "default-src 'none'; sandbox");+ res.setHeader('cross-origin-resource-policy', 'cross-origin');+ if (!isInlineSafe(type)) res.setHeader('content-disposition', 'attachment');res.setHeader('cache-control', cacheControlFor(key));res.setHeader('accept-ranges', 'bytes');if (obj.ETag) res.setHeader('etag', obj.ETag);diff --git a/api/glb.js b/api/glb.jsindex 7e00e1821..b019ef6ef 100644--- a/api/glb.js+++ b/api/glb.js@@ -57,6 +57,12 @@ export default wrap(async function handler(req, res) {res.statusCode = 200;res.setHeader('content-type', 'model/gltf-binary');+ // The bytes come from an arbitrary remote URL but leave on the three.ws+ // origin. Pin them as data: an opaque-origin sandbox, plus the declared model+ // type and the nosniff set by the wrapper, so nothing here can ever be+ // interpreted as a document with three.ws's cookies.+ res.setHeader('content-security-policy', "default-src 'none'; sandbox");+ res.setHeader('cross-origin-resource-policy', 'cross-origin');res.setHeader('content-length', String(bytes.length));// Generated GLBs are content-addressed by their unique object key, so a// given src never changes bytes: cache hard at every layer.diff --git a/tests/cdn-object-content-type.test.js b/tests/cdn-object-content-type.test.jsnew file mode 100644index 000000000..20b830213--- /dev/null+++ b/tests/cdn-object-content-type.test.js@@ -0,0 +1,52 @@+// /cdn/<key> serves bucket objects from the three.ws origin, where the session+// cookie lives. These cover the rule that keeps that safe: the served type comes+// from the server-chosen extension, never from a stored header an upstream+// provider may have written, and anything that could render as a scripting+// document is not inline-safe.+import { describe, it, expect } from 'vitest';+import { contentTypeFor, isInlineSafe } from '../api/cdn-object.js';++describe('contentTypeFor', () => {+ it('derives the type from the extension', () => {+ expect(contentTypeFor('u/abc/model.glb')).toBe('model/gltf-binary');+ expect(contentTypeFor('thumb/abc.png')).toBe('image/png');+ expect(contentTypeFor('forge/abc.webp')).toBe('image/webp');+ });++ it('ignores a stored type that contradicts the extension', () => {+ expect(contentTypeFor('u/abc/model.glb', 'text/html')).toBe('model/gltf-binary');+ expect(contentTypeFor('thumb/abc.png', 'image/svg+xml')).toBe('image/png');+ });++ it('still upgrades objects stored as octet-stream', () => {+ expect(contentTypeFor('u/abc/model.glb', 'application/octet-stream')).toBe('model/gltf-binary');+ });++ it('falls back to octet-stream for an unknown extension with an unsafe stored type', () => {+ expect(contentTypeFor('legacy/blob.weird', 'text/html; charset=utf-8')).toBe('application/octet-stream');+ expect(contentTypeFor('legacy/blob', 'application/xhtml+xml')).toBe('application/octet-stream');+ });++ it('keeps a safe stored type when the extension says nothing', () => {+ expect(contentTypeFor('legacy/blob', 'image/png')).toBe('image/png');+ });+});++describe('isInlineSafe', () => {+ it('accepts the media types the product actually renders', () => {+ for (const type of ['image/png', 'model/gltf-binary', 'video/mp4', 'audio/mpeg', 'application/json']) {+ expect(isInlineSafe(type)).toBe(true);+ }+ });++ it('rejects anything that can execute script in a document context', () => {+ for (const type of ['image/svg+xml', 'text/html', 'application/xhtml+xml', 'text/xml']) {+ expect(isInlineSafe(type)).toBe(false);+ }+ });++ it('ignores parameters when matching', () => {+ expect(isInlineSafe('image/png; charset=binary')).toBe(true);+ expect(isInlineSafe('text/html; charset=utf-8')).toBe(false);+ });+});