Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

refactor: move sandbox code to lib instead of edge function

Brad Barbinccb66b11db86
diff --git a/apps/erp/app/components/Configurator/ConfigurationEditor/ConfigurationEditor.tsx b/apps/erp/app/components/Configurator/ConfigurationEditor/ConfigurationEditor.tsx
index b115510d13..242e812058 100644
--- a/apps/erp/app/components/Configurator/ConfigurationEditor/ConfigurationEditor.tsx
+++ b/apps/erp/app/components/Configurator/ConfigurationEditor/ConfigurationEditor.tsx
@@ -230,6 +230,13 @@ export default function Configurator({
}, [fetcher.data]);
const runCode = useCallback(() => {
+ if (isUnsafeCode(code)) {
+ setOutput(
+ "Error: Unsupported code detected. The code you're trying to run contains disallowed patterns."
+ );
+ return;
+ }
+
try {
// Create parameters object from the panel
const parametersObj = parameters.reduce((acc, v) => {
@@ -421,3 +428,18 @@ export default function Configurator({
</>
);
}
+
+function isUnsafeCode(code: string) {
+ // Check for disallowed code patterns
+ const disallowedPatterns = [
+ /\b(for|while|do)\b/, // loops
+ /\bfetch\b/, // fetch calls
+ /setTimeout|setInterval/, // timeouts
+ /\bimport\b/, // dynamic imports
+ /new Promise/, // promise construction
+ /\beval\b/, // eval
+ /Function\(/, // Function constructor
+ ];
+
+ return disallowedPatterns.some((pattern) => pattern.test(code));
+}
diff --git a/packages/database/supabase/functions/sandbox/index.ts b/packages/database/supabase/functions/sandbox/index.ts
deleted file mode 100644
index fd0a1fd470..0000000000
--- a/packages/database/supabase/functions/sandbox/index.ts
+++ /dev/null
@@ -1,78 +0,0 @@
-import { serve } from "https://deno.land/[email protected]/http/server.ts";
-import { transpile } from "https://deno.land/x/[email protected]/mod.ts";
-import { z } from "https://deno.land/x/[email protected]/mod.ts";
-import { corsHeaders } from "../lib/headers.ts";
-
-const payloadValidator = z.object({
- code: z.string(),
- parameters: z.record(z.unknown()),
-});
-
-async function ts(code: string) {
- try {
- // Transpile TypeScript to JavaScript
- const jsCode = await transpile(
- `export function configure(params: Params) {${code}}`
- );
-
- return await import(`data:application/typescript;base64,${btoa(jsCode)}`);
- } catch (err) {
- console.error("Transpilation error:", err);
- throw new Error(`Failed to transpile TypeScript: ${err.message}`);
- }
-}
-
-serve(async (req: Request) => {
- if (req.method === "OPTIONS") {
- return new Response("ok", { headers: corsHeaders });
- }
-
- try {
- const payload = await req.json();
- const { code, parameters } = payloadValidator.parse(payload);
-
- console.log({
- function: "sandbox",
- code,
- parameters,
- });
-
- let result;
- // Check for disallowed code patterns
- const disallowedPatterns = [
- /\b(for|while|do)\b/, // loops
- /\bfetch\b/, // fetch calls
- /setTimeout|setInterval/, // timeouts
- /\bimport\b/, // dynamic imports
- /new Promise/, // promise construction
- /\beval\b/, // eval
- /Function\(/, // Function constructor
- ];
-
- if (disallowedPatterns.some((pattern) => pattern.test(code))) {
- return new Response(JSON.stringify({ result: null }), {
- headers: { ...corsHeaders, "Content-Type": "application/json" },
- status: 200,
- });
- }
-
- try {
- const mod = await ts(code);
- result = await mod.configure(parameters);
- } catch (err) {
- console.error("Execution error:", err);
- throw new Error(`Failed to execute code: ${err.message}`);
- }
-
- return new Response(JSON.stringify({ result }), {
- headers: { ...corsHeaders, "Content-Type": "application/json" },
- status: 200,
- });
- } catch (err) {
- console.error("Error:", err);
- return new Response(JSON.stringify({ result: null }), {
- headers: { ...corsHeaders, "Content-Type": "application/json" },
- status: 500,
- });
- }
-});