/** * 安全上下文管理器 * * 提供更细粒度的安全控制,管理沙箱内的执行上下文, * 包括变量作用域、API 权限、资源限制等。 * * @author md-editor * @version 1.0.0 */ import { SandboxConfig, SandboxResult } from './ProxySandbox'; /** * 权限配置接口 */ export interface PermissionConfig { /** 是否允许网络请求 */ network: boolean; /** 是否允许文件操作 */ fileSystem: boolean; /** 是否允许摄像头/麦克风访问 */ media: boolean; /** 是否允许地理位置访问 */ geolocation: boolean; /** 是否允许通知 */ notifications: boolean; } /** * 资源限制接口 */ export interface ResourceLimits { /** 最大执行时间 */ maxExecutionTime: number; /** 最大内存使用 */ maxMemoryUsage: number; /** 最大调用栈深度 */ maxCallStackDepth: number; /** 最大循环次数 */ maxLoopIterations: number; } /** * 监控配置接口 */ export interface MonitoringConfig { /** 是否启用性能监控 */ enablePerformanceMonitoring: boolean; /** 是否启用错误追踪 */ enableErrorTracking: boolean; /** 是否启用资源使用监控 */ enableResourceMonitoring: boolean; } /** * 安全上下文配置接口 */ export interface SecurityContextConfig extends SandboxConfig { /** API 权限配置 */ permissions?: Partial; /** 资源限制 */ limits?: Partial; /** 监控配置 */ monitoring?: Partial; } /** * 执行上下文信息 */ export interface ExecutionContext { /** 上下文 ID */ id: string; /** 创建时间 */ createdAt: number; /** 变量作用域 */ scope: Record; /** 权限设置 */ permissions: PermissionConfig; /** 资源使用情况 */ resourceUsage: { memoryUsage: number; executionTime: number; callStackDepth: number; loopIterations: number; }; } /** * 安全上下文管理器类 */ export declare class SecurityContextManager { private contexts; private config; private globalMonitors; constructor(config?: SecurityContextConfig); /** * 合并默认配置 */ private mergeDefaultConfig; /** * 初始化全局监控器 */ private initializeGlobalMonitors; /** * 创建性能监控器 */ private createPerformanceMonitor; /** * 创建错误追踪器 */ private createErrorTracker; /** * 创建资源监控器 */ private createResourceMonitor; /** * 创建新的执行上下文 */ createContext(contextId?: string): string; /** * 生成上下文 ID */ private generateContextId; /** * 获取执行上下文 */ getContext(contextId: string): ExecutionContext | undefined; /** * 删除执行上下文 */ deleteContext(contextId: string): boolean; /** * 在指定上下文中执行代码 */ executeInContext(contextId: string, code: string, additionalScope?: Record): Promise; /** * 包装代码以添加监控 */ private wrapCodeWithMonitoring; /** * 更新上下文的资源使用情况 */ private updateContextResourceUsage; /** * 检查资源限制 */ private checkResourceLimits; /** * 设置上下文变量 */ setContextVariable(contextId: string, name: string, value: any): boolean; /** * 获取上下文变量 */ getContextVariable(contextId: string, name: string): any; /** * 清理所有上下文 */ clearAllContexts(): void; /** * 获取所有上下文的统计信息 */ getStatistics(): { totalContexts: number; totalMemoryUsage: number; totalExecutionTime: number; averageExecutionTime: number; oldestContext: ExecutionContext; }; /** * 获取管理器配置 */ getConfig(): { permissions: PermissionConfig; limits: ResourceLimits; monitoring: MonitoringConfig; } & Required; /** * 销毁管理器 */ destroy(): void; } /** * 创建安全上下文管理器的工厂函数 */ export declare function createSecurityContextManager(config?: SecurityContextConfig): SecurityContextManager; /** * 快速在受控环境中执行代码的工具函数 */ export declare function runInSecureContext(code: string, config?: SecurityContextConfig, contextScope?: Record): Promise;