Files
proffesor-for-testing__agen…/security/safe-math-evaluator.ts
Profa 5a2883a4a0 chore: Initial release v1.0.0
Agentic Quality Engineering Fleet - AI-driven quality management platform

Features:
- 16 specialized QE agents for comprehensive testing
- AI-powered test generation with 95% coverage targets
- O(log n) sublinear algorithms for massive scale
- Parallel test execution with intelligent orchestration
- Real-time coverage analysis and gap detection
- Smart quality gates with ML-driven decisions
- Security scanning (SAST, DAST, dependency analysis)
- Performance testing with load orchestration
- Flaky test detection with automatic stabilization
- API contract validation with breaking change detection
- Test data generation (10,000+ records/sec)
- Production intelligence (convert incidents to tests)
- Chaos engineering for resilience testing
- MCP integration with Claude Code
- Multi-framework support (Jest, Mocha, Cypress, Playwright)

Architecture:
- TypeScript implementation
- MCP (Model Context Protocol) integration
- Claude Code agent orchestration
- Event-driven agent coordination
- Distributed fleet management
- Cross-session memory persistence

Documentation:
- Complete API documentation (TypeDoc)
- User guides (Getting Started, MCP Integration, Test Generation, etc.)
- Architecture deep-dive
- CLI reference
- Contributing guidelines
- MIT License

Tech Stack:
- TypeScript 5.3+
- Node.js 18+
- @anthropic-ai/sdk ^0.64.0
- @modelcontextprotocol/sdk ^1.18.2
- Jest for testing
- Winston for logging
- SQLite for persistence

Project Structure:
- src/ - Source code (agents, core, MCP, CLI)
- tests/ - Comprehensive test suite
- docs/ - Complete documentation
- .claude/ - Agent definitions and commands
- config/ - Configuration files

🤖 Generated with Claude Code
https://claude.com/claude-code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-30 19:59:56 +00:00

130 lines
4.3 KiB
TypeScript

/**
* Safe Math Evaluator for TypeScript
* Provides a secure method for evaluating mathematical expressions
* without using eval() or similar dangerous functions
*/
export class SafeMathEvaluator {
private readonly allowedOperators = new Set(['+', '-', '*', '/', '%', '**', '(', ')']);
private readonly allowedMathFunctions = new Set([
'abs', 'ceil', 'floor', 'round', 'max', 'min', 'sqrt', 'pow',
'sin', 'cos', 'tan', 'log', 'PI', 'E'
]);
/**
* Safely evaluate a mathematical expression
* @param expression - The mathematical expression to evaluate
* @returns The result of the evaluation
*/
public safeEvaluate(expression: string): number {
if (typeof expression !== 'string') {
throw new Error('Expression must be a string');
}
// Clean the expression
const cleaned = expression.replace(/\s+/g, '').trim();
// Validate the expression contains only safe characters
if (!this.isValidExpression(cleaned)) {
throw new Error('Invalid mathematical expression');
}
try {
// Use Function constructor with strict mode (safer than eval)
const result = Function('"use strict"; return (' + cleaned + ')')();
if (typeof result !== 'number' || !isFinite(result)) {
throw new Error('Expression did not evaluate to a valid number');
}
return result;
} catch (error) {
throw new Error(`Mathematical expression evaluation failed: ${error.message}`);
}
}
/**
* Validate that an expression contains only safe mathematical operations
* @param expression - The expression to validate
* @returns True if the expression is safe, false otherwise
*/
private isValidExpression(expression: string): boolean {
// Check for dangerous patterns
const dangerousPatterns = [
/[a-zA-Z_$][a-zA-Z0-9_$]*(?!\s*[.(])/, // Variables (but allow Math.function)
/;/, // Statement separators
/=/, // Assignment operators
/\[|\]/, // Array access
/\{|\}/, // Objects
/require|import|export|function|class/, // Keywords
/process|global|window|document/, // Dangerous globals
/eval|Function|setTimeout|setInterval/, // Dynamic execution
];
// Check against dangerous patterns
for (const pattern of dangerousPatterns) {
if (pattern.test(expression)) {
return false;
}
}
// Allow only numbers, operators, Math functions, and parentheses
const allowedPattern = /^[\d+\-*/.%()^E\s]*(?:Math\.(?:abs|ceil|floor|round|max|min|sqrt|pow|sin|cos|tan|log|PI|E)[\d+\-*/.%()^E\s]*)*$/;
return allowedPattern.test(expression);
}
/**
* Create a Math context for safe evaluation
* @returns A safe Math object with allowed functions
*/
private createSafeMathContext(): any {
const safeMath: any = {};
for (const func of this.allowedMathFunctions) {
if (func in Math) {
safeMath[func] = (Math as any)[func];
}
}
return { Math: safeMath };
}
/**
* Evaluate expression with safe Math context
* @param expression - The expression to evaluate
* @returns The result
*/
public evaluateWithSafeContext(expression: string): number {
if (!this.isValidExpression(expression)) {
throw new Error('Invalid mathematical expression');
}
const context = this.createSafeMathContext();
try {
const fn = new Function('Math', '"use strict"; return (' + expression + ');');
const result = fn(context.Math);
if (typeof result !== 'number' || !isFinite(result)) {
throw new Error('Expression must evaluate to a finite number');
}
return result;
} catch (error) {
throw new Error(`Safe evaluation failed: ${error.message}`);
}
}
}
// Create and export singleton instance
export const safeMathEvaluator = new SafeMathEvaluator();
/**
* Convenience function for safe mathematical evaluation
* @param expression - Mathematical expression to evaluate
* @returns Result of the evaluation
*/
export function safeEvaluateMath(expression: string): number {
return safeMathEvaluator.safeEvaluate(expression);
}