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(forge): degrade image upload to the paste-a-URL path when R2 credentials are only whitespace

nirholase6ee18ca3c6f
diff --git a/api/forge-upload.js b/api/forge-upload.js
index c39ad38cf..0cd5a4bae 100644
--- a/api/forge-upload.js
+++ b/api/forge-upload.js
@@ -18,7 +18,7 @@
import { randomUUID } from 'node:crypto';
import { cors, json, method, readJson, wrap, rateLimited } from './_lib/http.js';
import { limits, clientIp } from './_lib/rate-limit.js';
-import { presignUpload, publicUrl } from './_lib/r2.js';
+import { presignUpload, publicUrl, objectStorageConfigured } from './_lib/r2.js';
import { hashClient } from './_lib/forge-store.js';
// Accepted reference-image types → file extension for the storage key.
@@ -30,16 +30,15 @@ const CONTENT_TYPE_EXT = Object.freeze({
const MAX_IMAGE_BYTES = 8 * 1024 * 1024; // matches forge-store's preview copy cap
-// Upload needs object storage (R2/S3). Detect from raw env so a deployment
-// without storage degrades to the "paste a URL" path rather than 500-ing.
+// Upload needs object storage (R2/S3), and the check lives in r2.js so this
+// route cannot drift from it. That module trims before testing: a credential
+// that is only a trailing newline reads truthy through a raw `process.env`
+// test, so an untrimmed copy here would call storage healthy and hand the
+// browser a presigned URL that answers 403 SignatureDoesNotMatch. The 403
+// carries no CORS header, so the page reports "Network error during upload"
+// instead of the designed "paste a URL" fallback below.
function storageConfigured() {
- return Boolean(
- process.env.S3_ENDPOINT &&
- process.env.S3_BUCKET &&
- process.env.S3_PUBLIC_DOMAIN &&
- process.env.S3_ACCESS_KEY_ID &&
- process.env.S3_SECRET_ACCESS_KEY,
- );
+ return objectStorageConfigured();
}
function clientKeyFrom(req) {
diff --git a/tests/api/forge-pipeline-endpoints.test.js b/tests/api/forge-pipeline-endpoints.test.js
index 09dd6c63b..146c6470e 100644
--- a/tests/api/forge-pipeline-endpoints.test.js
+++ b/tests/api/forge-pipeline-endpoints.test.js
@@ -113,9 +113,14 @@ vi.mock('../../api/_lib/forge-store.js', () => ({
}));
const presignMock = vi.fn(async ({ key }) => `https://r2.example/${key}?sig=abc`);
+// Storage config is read through r2.js so the route cannot drift from the
+// trimmed check; default configured, flipped per test for the 503 fallback.
+const storageState = { configured: true };
+
vi.mock('../../api/_lib/r2.js', () => ({
presignUpload: (...a) => presignMock(...a),
publicUrl: (key) => `https://cdn.example/${key}`,
+ objectStorageConfigured: () => storageState.configured,
}));
vi.mock('../../api/_lib/auth.js', () => ({
@@ -309,6 +314,27 @@ describe('forge-upload', () => {
expect(out.upload_url).toContain(out.storage_key);
});
+ // A rejected or whitespace-only R2 credential must degrade to the designed
+ // "paste a URL" 503. Before the route shared r2.js's trimmed check it would
+ // hand out a presigned URL that answers 403 with no CORS header, which the
+ // page can only report as "Network error during upload".
+ it('503s to the paste-a-URL fallback when object storage is unconfigured', async () => {
+ storageState.configured = false;
+ try {
+ const res = await call(upload, {
+ method: 'POST',
+ url: '/api/forge-upload',
+ headers: { 'x-forge-client': 'browser-1' },
+ body: { content_type: 'image/png', size_bytes: 1_000 },
+ });
+ expect(res.statusCode).toBe(503);
+ expect(parse(res).error).toBe('unconfigured');
+ expect(presignMock).not.toHaveBeenCalled();
+ } finally {
+ storageState.configured = true;
+ }
+ });
+
it('rejects a prototype key as a content type', async () => {
for (const contentType of ['constructor', '__proto__', 'tostring']) {
const res = await call(upload, {