From f7fb2ac0430c9beee5814b801b3dac82eea77e1a Mon Sep 17 00:00:00 2001 From: JohnDoe6345789 Date: Fri, 26 Dec 2025 01:20:44 +0000 Subject: [PATCH] code: lua,script,sandboxed (2 files) --- .../core/entities/lua-script-operations.ts | 99 ++++--------------- .../src/lib/lua/sandboxed-lua-engine.ts | 3 + 2 files changed, 22 insertions(+), 80 deletions(-) diff --git a/dbal/ts/src/core/entities/lua-script-operations.ts b/dbal/ts/src/core/entities/lua-script-operations.ts index 3cb098d83..5ce518620 100644 --- a/dbal/ts/src/core/entities/lua-script-operations.ts +++ b/dbal/ts/src/core/entities/lua-script-operations.ts @@ -7,107 +7,46 @@ import type { DBALAdapter } from '../../adapters/adapter' import type { LuaScript, ListOptions, ListResult } from '../types' -import { DBALError } from '../errors' -import { validateLuaScriptCreate, validateLuaScriptUpdate, validateId } from '../validation' +import { createLuaScript } from './lua-script/create-lua-script' +import { deleteLuaScript } from './lua-script/delete-lua-script' +import { getLuaScript } from './lua-script/get-lua-script' +import { listLuaScripts } from './lua-script/list-lua-scripts' +import { updateLuaScript } from './lua-script/update-lua-script' + +export interface LuaScriptOperations { + create: (data: Omit) => Promise + read: (id: string) => Promise + update: (id: string, data: Partial) => Promise + delete: (id: string) => Promise + list: (options?: ListOptions) => Promise> +} /** * Create Lua script operations object for the DBAL client */ -export const createLuaScriptOperations = (adapter: DBALAdapter) => ({ +export const createLuaScriptOperations = (adapter: DBALAdapter): LuaScriptOperations => ({ /** * Create a new Lua script */ - create: async (data: Omit): Promise => { - const validationErrors = validateLuaScriptCreate(data) - if (validationErrors.length > 0) { - throw DBALError.validationError( - 'Invalid Lua script data', - validationErrors.map(error => ({ field: 'luaScript', error })) - ) - } - - try { - return adapter.create('LuaScript', data) as Promise - } catch (error) { - if (error instanceof DBALError && error.code === 409) { - throw DBALError.conflict(`Lua script with name '${data.name}' already exists`) - } - throw error - } - }, + create: (data) => createLuaScript(adapter, data), /** * Read a Lua script by ID */ - read: async (id: string): Promise => { - const validationErrors = validateId(id) - if (validationErrors.length > 0) { - throw DBALError.validationError( - 'Invalid Lua script ID', - validationErrors.map(error => ({ field: 'id', error })) - ) - } - - const result = await adapter.read('LuaScript', id) as LuaScript | null - if (!result) { - throw DBALError.notFound(`Lua script not found: ${id}`) - } - return result - }, + read: (id) => getLuaScript(adapter, id), /** * Update an existing Lua script */ - update: async (id: string, data: Partial): Promise => { - const idErrors = validateId(id) - if (idErrors.length > 0) { - throw DBALError.validationError( - 'Invalid Lua script ID', - idErrors.map(error => ({ field: 'id', error })) - ) - } - - const validationErrors = validateLuaScriptUpdate(data) - if (validationErrors.length > 0) { - throw DBALError.validationError( - 'Invalid Lua script update data', - validationErrors.map(error => ({ field: 'luaScript', error })) - ) - } - - try { - return adapter.update('LuaScript', id, data) as Promise - } catch (error) { - if (error instanceof DBALError && error.code === 409) { - throw DBALError.conflict('Lua script name already exists') - } - throw error - } - }, + update: (id, data) => updateLuaScript(adapter, id, data), /** * Delete a Lua script by ID */ - delete: async (id: string): Promise => { - const validationErrors = validateId(id) - if (validationErrors.length > 0) { - throw DBALError.validationError( - 'Invalid Lua script ID', - validationErrors.map(error => ({ field: 'id', error })) - ) - } - - const result = await adapter.delete('LuaScript', id) - if (!result) { - throw DBALError.notFound(`Lua script not found: ${id}`) - } - return result - }, + delete: (id) => deleteLuaScript(adapter, id), /** * List Lua scripts with filtering and pagination */ - list: async (options?: ListOptions): Promise> => { - return adapter.list('LuaScript', options) as Promise> - }, + list: (options) => listLuaScripts(adapter, options), }) diff --git a/frontends/nextjs/src/lib/lua/sandboxed-lua-engine.ts b/frontends/nextjs/src/lib/lua/sandboxed-lua-engine.ts index f01b54847..5858bb45a 100644 --- a/frontends/nextjs/src/lib/lua/sandboxed-lua-engine.ts +++ b/frontends/nextjs/src/lib/lua/sandboxed-lua-engine.ts @@ -5,6 +5,7 @@ import { setupSandboxedEnvironment } from './functions/sandbox/setup-sandboxed-e import { executeWithTimeout } from './functions/sandbox/execute-with-timeout' import { getLuaMemoryUsageBytes } from './functions/sandbox/get-lua-memory-usage-bytes' import { enforceMaxMemory } from './functions/sandbox/enforce-max-memory' +import { setAllowedGlobals } from './functions/sandbox/set-allowed-globals' import { setExecutionTimeout } from './functions/sandbox/set-execution-timeout' import { destroy } from './functions/sandbox/destroy' @@ -15,11 +16,13 @@ export class SandboxedLuaEngine { engine: LuaEngine | null = null executionTimeout = 5000 maxMemory = 10 * 1024 * 1024 + allowedGlobals: string[] | undefined constructor(timeout: number = 5000) { this.executionTimeout = timeout } + setAllowedGlobals = setAllowedGlobals executeWithSandbox = executeWithSandbox disableDangerousFunctions = disableDangerousFunctions setupSandboxedEnvironment = setupSandboxedEnvironment