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.

Integrate pinned guard fetch for enhanced SSRF protection across multiple endpoints

nirholas90ac6483e58c
diff --git a/api/_lib/material-studio-store.js b/api/_lib/material-studio-store.js
index dfffd9b5a..6a95adf7c 100644
--- a/api/_lib/material-studio-store.js
+++ b/api/_lib/material-studio-store.js
@@ -36,6 +36,7 @@
import { randomUUID } from 'node:crypto';
import { putObject, publicUrl } from './r2.js';
import { assertPublicHttpsUrl } from './ssrf.js';
+import { fetchSafePublicUrlPinned, MaxBytesExceededError } from './ssrf-guard.js';
import { watsonxConfig, watsonxChatComplete } from './watsonx.js';
import { materialVariants, materialPreset, MATERIAL_PRESET_NAMES } from '@three-ws/viewer-presets';
import { seedLineage, appendVersion, branchFrom, buildLineageChain } from '../../mcp-server/src/tools/_lineage.js';
@@ -79,8 +80,22 @@ function stripJsonFence(text) {
async function fetchGlbBytes(url) {
let resp;
try {
- resp = await fetch(url, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) });
+ // Pinned guard fetch: the URL is validated upstream, but a validated
+ // host could still redirect to an internal address, and validation
+ // alone leaves a DNS-rebinding window. The streaming cap replaces the
+ // old post-hoc length check.
+ resp = await fetchSafePublicUrlPinned(
+ url,
+ { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) },
+ { maxBytes: MAX_SOURCE_GLB_BYTES },
+ );
} catch (err) {
+ if (err instanceof MaxBytesExceededError) {
+ throw new MaterialStudioError(`source GLB exceeds the ${MAX_SOURCE_GLB_BYTES}-byte limit`, {
+ status: 413,
+ code: 'source_too_large',
+ });
+ }
throw new MaterialStudioError(`could not fetch glb_url: ${err.message}`, {
status: 502,
code: 'fetch_failed',
@@ -94,12 +109,6 @@ async function fetchGlbBytes(url) {
}
const buf = Buffer.from(await resp.arrayBuffer());
if (!buf.length) throw new MaterialStudioError('glb_url is empty', { status: 502, code: 'empty_source' });
- if (buf.length > MAX_SOURCE_GLB_BYTES) {
- throw new MaterialStudioError(`source GLB is ${buf.length} bytes; max is ${MAX_SOURCE_GLB_BYTES}`, {
- status: 413,
- code: 'source_too_large',
- });
- }
if (!isGlbMagic(buf)) {
throw new MaterialStudioError('glb_url is not a binary glTF (.glb) — its bytes lack the "glTF" magic header', {
status: 415,
diff --git a/api/_lib/pipeline-stage.js b/api/_lib/pipeline-stage.js
index f430f58b5..30b408050 100644
--- a/api/_lib/pipeline-stage.js
+++ b/api/_lib/pipeline-stage.js
@@ -19,6 +19,7 @@
import { createRegenProvider } from '../_providers/gcp.js';
import { validatePublicUrl, resolvePublicHost, SsrfError } from './ssrf.js';
+import { fetchSafePublicUrlPinned, MaxBytesExceededError } from './ssrf-guard.js';
import { putObject, publicUrl } from './r2.js';
// Poll cadence + budget. The x402 payment authorization carries
@@ -132,11 +133,19 @@ function magicMatches(kind, bytes) {
export async function sniffRemoteAsset(url, kind = 'glb') {
let resp;
try {
- resp = await fetch(url, {
- redirect: 'follow',
- headers: { range: 'bytes=0-63', accept: '*/*' },
- signal: AbortSignal.timeout(15_000),
- });
+ // Pinned guard fetch: the URL was validated upstream, but a plain
+ // redirect-following fetch would let a validated host 302 us to an
+ // internal address. Every hop is re-validated and the socket is pinned
+ // to the checked IP. The byte cap only bounds hosts that ignore the
+ // Range header and stream the whole asset.
+ resp = await fetchSafePublicUrlPinned(
+ url,
+ {
+ headers: { range: 'bytes=0-63', accept: '*/*' },
+ signal: AbortSignal.timeout(15_000),
+ },
+ { maxBytes: MAX_OUTPUT_BYTES },
+ );
} catch (err) {
throw new StageError(`could not fetch ${kind}: ${err.message}`, {
status: 502,
@@ -252,8 +261,22 @@ export async function persistStageOutput({ resultUrl, key, contentType, kind = '
}
let resp;
try {
- resp = await fetch(resultUrl, { signal: AbortSignal.timeout(OUTPUT_FETCH_TIMEOUT_MS) });
+ // Worker-returned URL: lower trust than our own storage. Pinned guard
+ // fetch so a compromised worker reply cannot redirect the retrieve into
+ // an internal address; the streaming cap replaces the old post-hoc
+ // length check so an over-limit body never buffers in full.
+ resp = await fetchSafePublicUrlPinned(
+ resultUrl,
+ { signal: AbortSignal.timeout(OUTPUT_FETCH_TIMEOUT_MS) },
+ { maxBytes: MAX_OUTPUT_BYTES },
+ );
} catch (err) {
+ if (err instanceof MaxBytesExceededError) {
+ throw new StageError(`worker output exceeds the ${MAX_OUTPUT_BYTES}-byte limit`, {
+ status: 502,
+ code: 'output_too_large',
+ });
+ }
throw new StageError(`could not retrieve worker output: ${err.message}`, {
status: 502,
code: 'output_fetch_failed',
@@ -269,12 +292,6 @@ export async function persistStageOutput({ resultUrl, key, contentType, kind = '
if (!buf.length) {
throw new StageError('worker output was empty', { status: 502, code: 'empty_output' });
}
- if (buf.length > MAX_OUTPUT_BYTES) {
- throw new StageError(`worker output is ${buf.length} bytes; max is ${MAX_OUTPUT_BYTES}`, {
- status: 502,
- code: 'output_too_large',
- });
- }
if (!magicMatches(kind, new Uint8Array(buf.subarray(0, 64)))) {
throw new StageError(
`worker output is not a valid ${kind === 'image' ? 'image' : 'GLB'}`,
diff --git a/api/_okx3d/identity.js b/api/_okx3d/identity.js
index 55802f15a..369aa7f76 100644
--- a/api/_okx3d/identity.js
+++ b/api/_okx3d/identity.js
@@ -25,7 +25,7 @@
import { createHash } from 'node:crypto';
import { putObject, getObjectBuffer, publicUrl } from '../_lib/r2.js';
import { encodeJobToken, decodeJobToken } from '../_lib/forge-job-token.js';
-import { assertSafePublicUrl } from '../_lib/ssrf-guard.js';
+import { assertSafePublicUrl, fetchSafePublicUrlPinned } from '../_lib/ssrf-guard.js';
import { llmComplete } from '../_lib/llm.js';
import { PRESETS } from '../../src/pose-presets.js';
@@ -392,11 +392,18 @@ export async function validateReferenceImage(url) {
await assertSafePublicUrl(url, { allowHttp: true });
let res;
try {
- res = await fetch(url, {
- method: 'GET',
- headers: { range: 'bytes=0-0' },
- signal: AbortSignal.timeout(15_000),
- });
+ // Pinned guard fetch: validation alone leaves a DNS-rebinding window and
+ // a validated host could redirect to an internal address. The byte cap
+ // bounds hosts that ignore the Range header and stream the whole image.
+ res = await fetchSafePublicUrlPinned(
+ url,
+ {
+ method: 'GET',
+ headers: { range: 'bytes=0-0' },
+ signal: AbortSignal.timeout(15_000),
+ },
+ { allowHttp: true, maxBytes: 16 * 1024 * 1024 },
+ );
} catch (err) {
throw pipelineError(
'reference_image_unreachable',
diff --git a/api/bnb/vault-upload.js b/api/bnb/vault-upload.js
index 1c7998096..452b93071 100644
--- a/api/bnb/vault-upload.js
+++ b/api/bnb/vault-upload.js
@@ -49,6 +49,7 @@ import { cors, json, method, wrap, error, readJson, rateLimited } from '../_lib/
import { limits, clientIp } from '../_lib/rate-limit.js';
import { env } from '../_lib/env.js';
import { assertPublicHttpsUrl } from '../_lib/ssrf.js';
+import { fetchSafePublicUrlPinned, MaxBytesExceededError } from '../_lib/ssrf-guard.js';
import { assertBscAddress, BNB_CHAINS } from '../_lib/bnb/chains.js';
import { encryptGlb, VAULT_CRYPTO_PARAMS } from '../_lib/bnb/vault-crypto.js';
import { ensureBucket, createObject, GreenfieldWriteError, MAX_VAULT_OBJECT_BYTES } from '../_lib/bnb/greenfield-write.js';
@@ -103,8 +104,21 @@ async function fetchGlbBytes(rawUrl) {
}
let resp;
try {
- resp = await fetch(safeUrl, { signal: AbortSignal.timeout(30_000) });
+ // Pinned guard fetch: validation alone leaves a validate-then-connect
+ // window, and a validated host could redirect to an internal address.
+ // The streaming cap keeps an over-limit body from ever buffering.
+ resp = await fetchSafePublicUrlPinned(
+ safeUrl,
+ { signal: AbortSignal.timeout(30_000) },
+ { maxBytes: MAX_INPUT_GLB_BYTES },
+ );
} catch (err) {
+ if (err instanceof MaxBytesExceededError) {
+ throw new VaultUploadError(`GLB exceeds the ${MAX_INPUT_GLB_BYTES}-byte limit`, { status: 413, code: 'glb_too_large' });
+ }
+ if (err?.code === 'ssrf_blocked') {
+ throw new VaultUploadError(`glbUrl rejected: ${err.message}`, { status: 400, code: 'invalid_url' });
+ }
throw new VaultUploadError(`could not fetch glbUrl: ${err.message}`, { status: 502, code: 'fetch_failed' });
}
if (!resp.ok) {