From 641b8b0b82edd54efbc150083b445c400c9d96eb Mon Sep 17 00:00:00 2001 From: johndoe6345789 Date: Tue, 23 Dec 2025 23:10:24 +0000 Subject: [PATCH] Generated by Spark: Improve security, sandbox as much stuff as possible and scan and filter malicious code - probably warn user if some virus code has been inputted. --- PRD.md | 26 ++ SECURITY_GUIDE.md | 267 +++++++++++ src/components/CodeEditor.tsx | 157 +++++-- src/components/JsonEditor.tsx | 204 +++++--- src/components/LuaEditor.tsx | 103 ++++- src/components/SecurityWarningDialog.tsx | 235 ++++++++++ src/lib/sandboxed-lua-engine.ts | 181 ++++++++ src/lib/security-scanner.ts | 565 +++++++++++++++++++++++ src/lib/workflow-engine.ts | 26 +- 9 files changed, 1652 insertions(+), 112 deletions(-) create mode 100644 SECURITY_GUIDE.md create mode 100644 src/components/SecurityWarningDialog.tsx create mode 100644 src/lib/sandboxed-lua-engine.ts create mode 100644 src/lib/security-scanner.ts diff --git a/PRD.md b/PRD.md index bbafcb38c..6c47d4fae 100644 --- a/PRD.md +++ b/PRD.md @@ -234,6 +234,24 @@ Elevate MetaBuilder to support multi-tenant architecture with a Super God level - Includes code examples where relevant - Provides best practices and tips +### 8. Security Scanning & Sandboxing +**Functionality:** Comprehensive code security analysis with sandboxed execution for Lua scripts +**Purpose:** Protect against malicious code, XSS attacks, SQL injection, and other vulnerabilities +**Trigger:** Automatic scan on save/execute, manual scan via Security Scan button +**Progression:** User writes code → Clicks save/execute → System scans for security issues → If critical/high severity detected → Show security warning dialog → Display all issues with details → User reviews and either fixes code or force-proceeds (non-critical only) → System logs security events +**Success Criteria:** +- All JavaScript code scanned for: eval(), innerHTML, XSS patterns, prototype pollution +- All Lua code scanned for: os/io module usage, file loading, infinite loops, global manipulation +- All JSON scanned for: __proto__ injection, script tags, malformed data +- Critical severity blocks execution/saving completely +- High severity requires user acknowledgment to proceed +- Medium/Low severity shows warnings but allows operation +- Each issue shows: type, severity, message, line number, code pattern, recommendation +- Lua scripts execute in sandbox with: disabled os/io/debug modules, 5s timeout, restricted globals +- Security scan button available in: Lua Editor, Code Editor, JSON Editor +- Security dialog shows color-coded severity levels with icons +- Sandboxed Lua engine blocks file system, OS commands, and package loading + ## Edge Case Handling - **Multiple supergod attempts** - Database constraint ensures only one supergod role exists; attempting to create second fails - **Power transfer to self** - UI prevents selecting current supergod user as transfer target @@ -257,10 +275,18 @@ Elevate MetaBuilder to support multi-tenant architecture with a Super God level - **Empty dropdown options** - Validation prevents saving dropdowns with zero options - **Duplicate class selection** - System prevents selecting same class twice - **Import/export conflicts** - Monaco editor validates JSON before import, shows detailed errors +- **Malicious code injection** - Security scanner blocks critical threats, warns on suspicious patterns +- **XSS attacks via innerHTML** - Scanner detects and prevents dangerous HTML injection patterns +- **Lua sandbox escape attempts** - Sandboxed engine disables os/io modules and dangerous functions +- **Infinite loops in Lua** - Execution timeout (5s) prevents resource exhaustion +- **SQL injection in strings** - Pattern matching detects and warns about SQL injection attempts +- **Prototype pollution** - Scanner detects __proto__ manipulation in JavaScript and JSON ## Design Direction The Level 5 interface should feel like a command center with regal, powerful aesthetics distinct from the purple god-tier panel. Use amber/gold accents to signify supreme authority. The multi-tenant view uses card-based layouts with organizational emphasis. Power transfer UI employs serious warning states with amber colors to communicate irreversibility. The interface balances grandeur with usability—never sacrificing clarity for visual flair. Color hierarchy: amber for supergod actions, purple for god-level previews, standard accent colors for tenant management. +**Security UX:** Security warnings use shield icons and color-coded severity badges. Critical issues display prominent red warnings with block actions. The security scan dialog provides educational content explaining each issue with recommendations. Warnings are never dismissive—they empower users to write better, safer code. + ## Color Selection **Primary Color:** `oklch(0.55 0.18 290)` - Purple/magenta representing creativity and visual design (Levels 1-4) diff --git a/SECURITY_GUIDE.md b/SECURITY_GUIDE.md new file mode 100644 index 000000000..34340829f --- /dev/null +++ b/SECURITY_GUIDE.md @@ -0,0 +1,267 @@ +# MetaBuilder Security Guide + +## Overview + +MetaBuilder now includes comprehensive security features to protect against malicious code injection, XSS attacks, SQL injection, and other vulnerabilities. This document outlines the security measures implemented and best practices for users. + +## Security Features + +### 1. Code Scanning + +All user-generated code (JavaScript, Lua, JSON, HTML) is automatically scanned for security vulnerabilities before execution or saving. + +#### Scan Levels + +- **Safe**: No security issues detected +- **Low**: Minor warnings that don't pose significant risk +- **Medium**: Potential security concerns that should be reviewed +- **High**: Serious security issues that require immediate attention +- **Critical**: Severe vulnerabilities that block execution/saving + +### 2. Sandboxed Lua Execution + +Lua scripts are executed in a sandboxed environment with the following restrictions: + +#### Disabled Functions & Modules + +- **os module**: All operating system functions disabled + - `os.execute`, `os.exit`, `os.remove`, `os.rename`, `os.tmpname` + +- **io module**: All file I/O operations disabled + - `io.popen`, `io.tmpfile`, `io.open`, `io.input`, `io.output`, `io.lines` + +- **File loading**: Dynamic file loading disabled + - `loadfile`, `dofile` + +- **Package loading**: Dynamic library loading disabled + - `package.loadlib`, `package.searchpath`, C library path cleared + +#### Limited Functions + +- **debug module**: Advanced debugging features limited + - `debug.getfenv`, `debug.setfenv` disabled + +- **Environment manipulation**: Global environment modifications restricted + - Direct `_G` manipulation monitored + +#### Safe Functions Available + +The sandbox provides access to these safe functions: +- Basic: `assert`, `error`, `ipairs`, `next`, `pairs`, `pcall`, `select`, `tonumber`, `tostring`, `type`, `unpack`, `xpcall` +- Libraries: `string`, `table`, `math`, `bit32` +- Logging: `print`, `log` +- Context: `context.data`, `context.user`, `context.kv` + +### 3. Execution Timeout + +All Lua scripts have a maximum execution time of 5 seconds (configurable) to prevent infinite loops and resource exhaustion. + +### 4. Pattern Detection + +The security scanner detects the following malicious patterns: + +#### JavaScript Threats + +- **Code Execution** + - `eval()` + - Dynamic `Function()` constructor + - `setTimeout/setInterval` with string arguments + +- **XSS Vulnerabilities** + - `innerHTML` assignments + - `dangerouslySetInnerHTML` + - `