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(cad,forge): stop leaking raw R2 bucket-domain URLs that trip browser ORB/CORS blocks

nirholase921cabeda12
diff --git a/api/_lib/cad/store.js b/api/_lib/cad/store.js
index 5376f7d50..c9efa975d 100644
--- a/api/_lib/cad/store.js
+++ b/api/_lib/cad/store.js
@@ -16,7 +16,7 @@
import { randomUUID } from 'node:crypto';
import { sql } from '../db.js';
import { databaseConfigured } from '../env.js';
-import { objectStorageConfigured, putObject, publicUrl } from '../r2.js';
+import { objectStorageConfigured, putObject, cdnUrl, normalizeStoredUrl } from '../r2.js';
import { recordDailyActivity, maybeAwardFirstCreation } from '../streaks.js';
export function cadStoreEnabled() {
@@ -94,7 +94,7 @@ export async function uploadArtifacts(prefix, artifacts) {
contentType: type,
metadata: { source: 'cad-forge' },
});
- urls[key] = publicUrl(objectKey);
+ urls[key] = cdnUrl(objectKey);
}),
);
return urls;
@@ -104,6 +104,15 @@ export function newDesignId() {
return randomUUID();
}
+function normalizeFiles(files) {
+ if (!files || typeof files !== 'object') return files;
+ const out = {};
+ for (const [key, url] of Object.entries(files)) {
+ out[key] = typeof url === 'string' ? (normalizeStoredUrl(url) ?? url) : url;
+ }
+ return out;
+}
+
function toDesign(row) {
if (!row) return null;
return {
@@ -115,7 +124,7 @@ function toDesign(row) {
code: row.code,
params: Array.isArray(row.params) ? row.params : [],
metrics: row.metrics,
- files: row.files,
+ files: normalizeFiles(row.files),
adjustments: Array.isArray(row.adjustments) ? row.adjustments : [],
model: row.model || null,
views: Number(row.views) || 0,
@@ -180,7 +189,7 @@ export async function getLineage(id) {
order by created_at asc
limit 40
`;
- const card = (r) => ({ id: r.id, title: r.title, prompt: r.prompt, thumb: r.thumb || null });
+ const card = (r) => ({ id: r.id, title: r.title, prompt: r.prompt, thumb: r.thumb ? (normalizeStoredUrl(r.thumb) ?? r.thumb) : null });
return {
parent: rows.filter((r) => r.rel === 'parent').map(card)[0] || null,
children: rows.filter((r) => r.rel === 'child').map(card),
@@ -220,7 +229,7 @@ export async function listDesigns({ limit = 24, q } = {}) {
title: r.title,
summary: r.summary || null,
prompt: r.prompt,
- thumb: r.thumb || null,
+ thumb: r.thumb ? (normalizeStoredUrl(r.thumb) ?? r.thumb) : null,
sizeMm: Array.isArray(r.size_mm) ? r.size_mm : null,
views: Number(r.views) || 0,
createdAt: r.created_at instanceof Date ? r.created_at.toISOString() : r.created_at,
diff --git a/api/_lib/forge-store.js b/api/_lib/forge-store.js
index 56408e563..548abd926 100644
--- a/api/_lib/forge-store.js
+++ b/api/_lib/forge-store.js
@@ -1271,8 +1271,8 @@ export async function getLineage({ rootCreationId, clientKey }) {
prompt: r.prompt,
refine_instruction: r.refine_instruction,
lineage_index: r.lineage_index,
- glb_url: r.glb_url,
- preview_image_url: r.preview_image_url,
+ glb_url: normalizeStoredUrl(r.glb_url),
+ preview_image_url: normalizeStoredUrl(r.preview_image_url),
status: r.status,
backend: r.backend,
created_at: r.created_at,
diff --git a/tests/cad-store-orb-safe.test.js b/tests/cad-store-orb-safe.test.js
new file mode 100644
index 000000000..188029412
--- /dev/null
+++ b/tests/cad-store-orb-safe.test.js
@@ -0,0 +1,104 @@
+/**
+ * cad/store.js writes and reads (work order 439, "ORB-blocking raw R2 URL").
+ *
+ * uploadArtifacts() persisted every CAD artifact URL with the raw
+ * publicUrl() bucket-domain helper instead of cdnUrl(), and getDesign(),
+ * getLineage(), and listDesigns() returned those (or any legacy) raw URLs
+ * verbatim. Chrome's Opaque Response Blocking rejects the raw *.r2.dev
+ * domain on an <img>/GLTFLoader load. Every write must use cdnUrl(), and
+ * every read must self-heal an already-stored raw URL via
+ * normalizeStoredUrl(), matching forge-store.js's established convention.
+ */
+import { describe, it, expect, vi, afterEach } from 'vitest';
+
+Object.assign(process.env, {
+ DATABASE_URL: 'postgres://test:test@localhost:5432/test',
+ S3_ENDPOINT: 'https://s3.example.com',
+ S3_BUCKET: 'test-bucket',
+ S3_PUBLIC_DOMAIN: 'https://pub-legacy.r2.dev',
+ S3_ACCESS_KEY_ID: 'test-key',
+ S3_SECRET_ACCESS_KEY: 'test-secret',
+ APP_ORIGIN: 'https://three.ws',
+});
+
+let rows = [];
+const sqlMock = vi.fn(async () => rows);
+vi.mock('../api/_lib/db.js', () => ({
+ sql: (...args) => sqlMock(...args),
+ isDbUnavailableError: () => false,
+}));
+
+vi.mock('../api/_lib/r2.js', async (importOriginal) => {
+ const actual = await importOriginal();
+ return {
+ putObject: vi.fn(async () => {}),
+ objectStorageConfigured: () => true,
+ publicUrl: actual.publicUrl,
+ keyFromPublicUrl: actual.keyFromPublicUrl,
+ normalizeStoredUrl: actual.normalizeStoredUrl,
+ cdnUrl: actual.cdnUrl,
+ };
+});
+
+vi.mock('../api/_lib/env.js', async (importOriginal) => {
+ const actual = await importOriginal();
+ return { ...actual, databaseConfigured: () => true };
+});
+
+vi.mock('../api/_lib/streaks.js', () => ({
+ recordDailyActivity: vi.fn(async () => {}),
+ maybeAwardFirstCreation: vi.fn(async () => {}),
+}));
+
+const { uploadArtifacts, getDesign, getLineage, listDesigns } = await import('../api/_lib/cad/store.js');
+const { publicUrl, cdnUrl } = await import('../api/_lib/r2.js');
+
+const LEGACY_THUMB = publicUrl('cad/d1/thumb.svg');
+const LEGACY_GLB = publicUrl('cad/d1/part.glb');
+
+afterEach(() => {
+ sqlMock.mockClear();
+});
+
+describe('cad/store.js writes and reads are ORB-safe', () => {
+ it('uploadArtifacts: returns cdnUrl()-shaped URLs, not raw publicUrl() ones', async () => {
+ const urls = await uploadArtifacts('cad/d1', {
+ glb: Buffer.from('glb-bytes').toString('base64'),
+ thumb_svg: Buffer.from('<svg/>').toString('base64'),
+ });
+ expect(urls.glb).toBe(cdnUrl('cad/d1/part.glb'));
+ expect(urls.thumb_svg).toBe(cdnUrl('cad/d1/thumb.svg'));
+ expect(urls.glb).not.toMatch(/r2\.dev/);
+ expect(urls.thumb_svg).not.toMatch(/r2\.dev/);
+ });
+
+ it('getDesign: normalizes a legacy files map stored with raw publicUrl() entries', async () => {
+ rows = [{
+ id: 'd1', parent_id: null, title: 't', summary: null, prompt: 'x', code: 'c',
+ params: [], metrics: {}, files: { glb: LEGACY_GLB, thumb_svg: LEGACY_THUMB },
+ adjustments: [], model: null, views: 0, creator_username: null,
+ created_at: new Date().toISOString(),
+ }];
+ const design = await getDesign('d1');
+ expect(design.files.glb).toBe(cdnUrl('cad/d1/part.glb'));
+ expect(design.files.thumb_svg).toBe(cdnUrl('cad/d1/thumb.svg'));
+ expect(design.files.glb).not.toMatch(/r2\.dev/);
+ });
+
+ it('getLineage: normalizes a legacy thumb value', async () => {
+ rows = [{ id: 'd2', title: 't', prompt: 'x', thumb: LEGACY_THUMB, created_at: new Date().toISOString(), rel: 'child' }];
+ const { children } = await getLineage('d1');
+ expect(children[0].thumb).toBe(cdnUrl('cad/d1/thumb.svg'));
+ expect(children[0].thumb).not.toMatch(/r2\.dev/);
+ });
+
+ it('listDesigns: normalizes a legacy thumb value', async () => {
+ rows = [{
+ id: 'd3', title: 't', summary: null, prompt: 'x', thumb: LEGACY_THUMB,
+ size_mm: null, views: 0, created_at: new Date().toISOString(),
+ }];
+ const [design] = await listDesigns({});
+ expect(design.thumb).toBe(cdnUrl('cad/d1/thumb.svg'));
+ expect(design.thumb).not.toMatch(/r2\.dev/);
+ });
+});
diff --git a/tests/forge-store-listing-orb-safe.test.js b/tests/forge-store-listing-orb-safe.test.js
index 024c67eba..16480a648 100644
--- a/tests/forge-store-listing-orb-safe.test.js
+++ b/tests/forge-store-listing-orb-safe.test.js
@@ -47,7 +47,7 @@ vi.mock('../api/_lib/r2.js', async (importOriginal) => {
const {
listCreations, listCreationsByUser, listShowcase, listRecentCreations, listRemixable, listMostRemixed,
- getPublicCreation, listRelated,
+ getPublicCreation, listRelated, getLineage,
} = await import('../api/_lib/forge-store.js');
const { publicUrl, cdnUrl } = await import('../api/_lib/r2.js');
@@ -154,6 +154,18 @@ describe('forge-store listing reads normalize legacy raw-domain URLs', () => {
expect(row.glb_url).not.toMatch(/r2\.dev/);
});
+ it('getLineage: glb_url, preview_image_url', async () => {
+ rows = [{
+ id: 'c9', parent_creation_id: null, prompt: 'x', refine_instruction: null,
+ lineage_index: 0, glb_url: LEGACY_GLB, preview_image_url: LEGACY_PREVIEW,
+ status: 'done', backend: 'trellis', created_at: new Date().toISOString(),
+ }];
+ const [row] = await getLineage({ rootCreationId: 'c9', clientKey: null });
+ expect(row.glb_url).toBe(cdnUrl('forge/client-1/legacy.glb'));
+ expect(row.preview_image_url).toBe(cdnUrl('forge/thumb/legacy.png'));
+ expect(row.glb_url).not.toMatch(/r2\.dev/);
+ });
+
it('listRelated (model-page sidebar poster read): glb_url, preview_image_url', async () => {
rows = [{
id: 'c8', prompt: 'x', glb_url: LEGACY_GLB, preview_image_url: LEGACY_PREVIEW,