The open-source agent harness - the runtime layer that turns an LLM into a working agent.
feat: validate sandbox environment resource limits and enhance form validation (#976)
Sujai Kumar Guptae25945dd9299
diff --git a/.changeset/sandbox-environment-resource-limits.md b/.changeset/sandbox-environment-resource-limits.mdnew file mode 100644index 000000000..64810882d--- /dev/null+++ b/.changeset/sandbox-environment-resource-limits.md@@ -0,0 +1,6 @@+---+'@truefoundry/trueforge': patch+'@truefoundry/trueforge-ui': patch+---++Add configurable sandbox resource caps and limit environment variable and secret names in the UI.diff --git a/.github/fern/openapi/openapi.json b/.github/fern/openapi/openapi.jsonindex 4753eb36a..0b6d0e1b9 100644--- a/.github/fern/openapi/openapi.json+++ b/.github/fern/openapi/openapi.json@@ -3985,6 +3985,8 @@},"environment_variables": {"additionalProperties": {+ "maxLength": 4096,+ "minLength": 1,"type": "string"},"type": "object"@@ -4017,6 +4019,8 @@},"environment_variables": {"additionalProperties": {+ "maxLength": 4096,+ "minLength": 1,"type": "string"},"type": "object"@@ -4044,6 +4048,7 @@"properties": {"domain_allow_list": {"description": "Comma-separated allowed domains.",+ "maxLength": 1024,"minLength": 1,"type": "string"},@@ -4056,6 +4061,7 @@"items": {"$ref": "#/components/schemas/SandboxEnvironmentSecret"},+ "maxItems": 1024,"type": "array"}},@@ -4095,7 +4101,9 @@"properties": {"env": {"description": "Environment variable name injected into the sandbox.",+ "maxLength": 4096,"minLength": 1,+ "pattern": "^[A-Za-z_][A-Za-z0-9_]*$","type": "string"},"hosts": {@@ -8529,7 +8537,7 @@}}},- "description": "Invalid request body, or missing secret value."+ "description": "Invalid request body, resource allocation, or missing secret value."},"401": {"content": {diff --git a/docs/openapi.json b/docs/openapi.jsonindex 4753eb36a..0b6d0e1b9 100644--- a/docs/openapi.json+++ b/docs/openapi.json@@ -3985,6 +3985,8 @@},"environment_variables": {"additionalProperties": {+ "maxLength": 4096,+ "minLength": 1,"type": "string"},"type": "object"@@ -4017,6 +4019,8 @@},"environment_variables": {"additionalProperties": {+ "maxLength": 4096,+ "minLength": 1,"type": "string"},"type": "object"@@ -4044,6 +4048,7 @@"properties": {"domain_allow_list": {"description": "Comma-separated allowed domains.",+ "maxLength": 1024,"minLength": 1,"type": "string"},@@ -4056,6 +4061,7 @@"items": {"$ref": "#/components/schemas/SandboxEnvironmentSecret"},+ "maxItems": 1024,"type": "array"}},@@ -4095,7 +4101,9 @@"properties": {"env": {"description": "Environment variable name injected into the sandbox.",+ "maxLength": 4096,"minLength": 1,+ "pattern": "^[A-Za-z_][A-Za-z0-9_]*$","type": "string"},"hosts": {@@ -8529,7 +8537,7 @@}}},- "description": "Invalid request body, or missing secret value."+ "description": "Invalid request body, resource allocation, or missing secret value."},"401": {"content": {diff --git a/packages/trueforge/.env.example b/packages/trueforge/.env.exampleindex bae9825e3..c679fd054 100644--- a/packages/trueforge/.env.example+++ b/packages/trueforge/.env.example@@ -190,6 +190,11 @@ TRUEFORGE_API_KEY=placeholder-value-please-generate-your-own## Max bytes for a single file download out of the sandbox. Default 20 MB (same as gateway).# SANDBOX_FILE_MAX_BYTES_FOR_DOWNLOAD=20971520+## Maximum resources accepted for one sandbox environment.+# SANDBOX_ENVIRONMENT_CPU_MAX=32+# SANDBOX_ENVIRONMENT_MEMORY_GIB_MAX=64+# SANDBOX_ENVIRONMENT_DISK_GIB_MAX=256+## Max bytes for an HTTP request body. Default 30 MB.## Reverse proxies must allow at least this (e.g. nginx client_max_body_size).# MAX_REQUEST_BODY_BYTES=31457280diff --git a/packages/trueforge/src/apis/sandboxEnvironments.ts b/packages/trueforge/src/apis/sandboxEnvironments.tsindex 785add495..26021c118 100644--- a/packages/trueforge/src/apis/sandboxEnvironments.ts+++ b/packages/trueforge/src/apis/sandboxEnvironments.ts@@ -120,13 +120,13 @@ export function createSandboxEnvironmentsRouter<TTransaction>(const putHandler: RouteHandler<typeof putSandboxEnvironmentRoute> = async c => {const body = c.req.valid('json');const requestContext = resolveRequestContext(c);+ const { manifest } = body;const provider = await resolveSandboxProviderRecord(deps.resolveSandboxProviderStore(c), requestContext.tenant_id);if (provider === undefined) {return c.json({ error: { message: 'No sandbox provider configured' } }, 422);}const created_by_subject = createdBySubjectFromRequestContext(requestContext);- const { manifest } = body;try {const existing = await store.getEnvironment({diff --git a/packages/trueforge/src/config.ts b/packages/trueforge/src/config.tsindex c5dd617d0..472d7a7ca 100644--- a/packages/trueforge/src/config.ts+++ b/packages/trueforge/src/config.ts@@ -737,6 +737,12 @@ export interface SharedServerConfiguration {* Env: `SANDBOX_FILE_MAX_BYTES_FOR_DOWNLOAD`. Default 20 MB (same as gateway).*/SANDBOX_FILE_MAX_BYTES_FOR_DOWNLOAD: number;+ /** Max CPU cores for a sandbox environment. Env: `SANDBOX_ENVIRONMENT_CPU_MAX`. Default 32. */+ SANDBOX_ENVIRONMENT_CPU_MAX: number;+ /** Max memory GiB for a sandbox environment. Env: `SANDBOX_ENVIRONMENT_MEMORY_GIB_MAX`. Default 64. */+ SANDBOX_ENVIRONMENT_MEMORY_GIB_MAX: number;+ /** Max disk GiB for a sandbox environment. Env: `SANDBOX_ENVIRONMENT_DISK_GIB_MAX`. Default 256. */+ SANDBOX_ENVIRONMENT_DISK_GIB_MAX: number;/*** Max bytes for an HTTP request body. Env: `MAX_REQUEST_BODY_BYTES`. Default 30 MB.*/@@ -1125,6 +1131,21 @@ const shared: SharedServerConfiguration = {raw: getEnv('SANDBOX_FILE_MAX_BYTES_FOR_DOWNLOAD'),defaultValue: 20_971_520,}),+ SANDBOX_ENVIRONMENT_CPU_MAX: parsePositiveInt({+ envKey: 'SANDBOX_ENVIRONMENT_CPU_MAX',+ raw: getEnv('SANDBOX_ENVIRONMENT_CPU_MAX'),+ defaultValue: 32,+ }),+ SANDBOX_ENVIRONMENT_MEMORY_GIB_MAX: parsePositiveInt({+ envKey: 'SANDBOX_ENVIRONMENT_MEMORY_GIB_MAX',+ raw: getEnv('SANDBOX_ENVIRONMENT_MEMORY_GIB_MAX'),+ defaultValue: 64,+ }),+ SANDBOX_ENVIRONMENT_DISK_GIB_MAX: parsePositiveInt({+ envKey: 'SANDBOX_ENVIRONMENT_DISK_GIB_MAX',+ raw: getEnv('SANDBOX_ENVIRONMENT_DISK_GIB_MAX'),+ defaultValue: 256,+ }),MAX_REQUEST_BODY_BYTES: parsePositiveInt({envKey: 'MAX_REQUEST_BODY_BYTES',raw: getEnv('MAX_REQUEST_BODY_BYTES'),diff --git a/packages/trueforge/src/routes/sandboxEnvironmentRoutes.ts b/packages/trueforge/src/routes/sandboxEnvironmentRoutes.tsindex 53bfd00a6..29e0c1d69 100644--- a/packages/trueforge/src/routes/sandboxEnvironmentRoutes.ts+++ b/packages/trueforge/src/routes/sandboxEnvironmentRoutes.ts@@ -111,7 +111,7 @@ export const putSandboxEnvironmentRoute = createRoute({},400: {content: { 'application/json': { schema: RequestErrorResponseSchema } },- description: 'Invalid request body, or missing secret value.',+ description: 'Invalid request body, resource allocation, or missing secret value.',},401: {content: { 'application/json': { schema: RequestErrorResponseSchema } },diff --git a/packages/trueforge/src/schemas/sandboxEnvironment.ts b/packages/trueforge/src/schemas/sandboxEnvironment.tsindex 333cfad4e..74f8118f2 100644--- a/packages/trueforge/src/schemas/sandboxEnvironment.ts+++ b/packages/trueforge/src/schemas/sandboxEnvironment.ts@@ -7,9 +7,16 @@*/import { z } from '@hono/zod-openapi';import { CreatedBySubjectSchema, TokenPaginationSchema } from '@truefoundry/trueforge-core/agent-session';+import configuration from '../config';import { NameSchema } from './common';+/** Same max as NameSchema (shared resource name). */+export const SANDBOX_ENVIRONMENT_NAME_MAX_LENGTH = 64;export const SANDBOX_ENVIRONMENT_DESCRIPTION_MAX_LENGTH = 1024;+export const SANDBOX_ENVIRONMENT_VARIABLE_NAME_MAX_LENGTH = 4096;+export const SANDBOX_ENVIRONMENT_VARIABLE_VALUE_MAX_LENGTH = 4096;+export const SANDBOX_ENVIRONMENT_DOMAIN_ALLOW_LIST_MAX_LENGTH = 1024;+export const SANDBOX_ENVIRONMENT_SECRETS_MAX_LENGTH = 1024;/** Reserved system environment name (tenant default; not creatable via public CRUD). */export const DEFAULT_SANDBOX_ENVIRONMENT_NAME = 'default';@@ -48,16 +55,30 @@ export const SandboxEnvironmentImageSchema = zexport const SandboxEnvironmentResourcesSchema = z.object({- cpu: z.number().positive().default(1).describe('CPU allocation in cores.'),- memory: z.number().positive().default(1).describe('Memory allocation in GiB.'),- disk: z.number().positive().default(3).describe('Disk allocation in GiB.'),+ cpu: z.number().positive().default(DEFAULT_SANDBOX_ENVIRONMENT_RESOURCES.cpu).describe('CPU allocation in cores.'),+ memory: z+ .number()+ .positive()+ .default(DEFAULT_SANDBOX_ENVIRONMENT_RESOURCES.memory)+ .describe('Memory allocation in GiB.'),+ disk: z.number().positive().default(DEFAULT_SANDBOX_ENVIRONMENT_RESOURCES.disk).describe('Disk allocation in GiB.'),}).strict().openapi('SandboxEnvironmentResources');+// Env/secret names: letter or `_` first, then letters/digits/`_` only.+// Rejects leading digits, hyphens, dots, spaces, and other punctuation.+const SandboxEnvironmentVariableNameSchema = z+ .string()+ .min(1)+ .max(SANDBOX_ENVIRONMENT_VARIABLE_NAME_MAX_LENGTH)+ .regex(/^[A-Za-z_][A-Za-z0-9_]*$/, 'must be a valid environment variable name');++const SandboxEnvironmentVariableValueSchema = z.string().min(1).max(SANDBOX_ENVIRONMENT_VARIABLE_VALUE_MAX_LENGTH);+export const SandboxEnvironmentSecretSchema = z.object({- env: z.string().min(1).describe('Environment variable name injected into the sandbox.'),+ env: SandboxEnvironmentVariableNameSchema.describe('Environment variable name injected into the sandbox.'),value: z.string().min(1).describe('Secret value; GET responses use a redacted stand-in.'),hosts: z.array(z.string().min(1)).describe('Hosts this secret may be sent to.'),})@@ -70,8 +91,17 @@ export const SandboxEnvironmentNetworkingSchema = z.boolean().optional().describe('Block all outbound network access. When true, domain_allow_list and secrets are not used.'),- domain_allow_list: z.string().min(1).optional().describe('Comma-separated allowed domains.'),- secrets: z.array(SandboxEnvironmentSecretSchema).optional().describe('Network-scoped secrets.'),+ domain_allow_list: z+ .string()+ .min(1)+ .max(SANDBOX_ENVIRONMENT_DOMAIN_ALLOW_LIST_MAX_LENGTH)+ .optional()+ .describe('Comma-separated allowed domains.'),+ secrets: z+ .array(SandboxEnvironmentSecretSchema)+ .max(SANDBOX_ENVIRONMENT_SECRETS_MAX_LENGTH)+ .optional()+ .describe('Network-scoped secrets.'),}).strict().superRefine((value, ctx) => {@@ -105,7 +135,9 @@ const SandboxEnvironmentManifestFieldsSchema = zdescription: SandboxEnvironmentDescriptionSchema.optional(),image: SandboxEnvironmentImageSchema.optional(),resources: SandboxEnvironmentResourcesSchema.default(DEFAULT_SANDBOX_ENVIRONMENT_RESOURCES),- environment_variables: z.record(z.string().min(1), z.string()).optional(),+ environment_variables: z+ .record(SandboxEnvironmentVariableNameSchema, SandboxEnvironmentVariableValueSchema)+ .optional(),networking: SandboxEnvironmentNetworkingSchema.optional(),}).strict();@@ -156,6 +188,20 @@ export const UpdateSandboxEnvironmentRequestSchema = zmanifest: SandboxEnvironmentManifestRequestSchema,}).strict()+ .superRefine((body, ctx) => {+ const { cpu, memory, disk } = body.manifest.resources;+ if (+ cpu > configuration.SANDBOX_ENVIRONMENT_CPU_MAX ||+ memory > configuration.SANDBOX_ENVIRONMENT_MEMORY_GIB_MAX ||+ disk > configuration.SANDBOX_ENVIRONMENT_DISK_GIB_MAX+ ) {+ ctx.addIssue({+ code: 'custom',+ path: ['manifest', 'resources'],+ message: 'Sandbox environment resources exceed configured limits',+ });+ }+ }).openapi('UpdateSandboxEnvironmentRequest');const IsoTimestamp = z.iso.datetime().openapi({ type: 'string', format: 'date-time' });