eggcalc is designed with security as a priority. This page covers security features and best practices.
For any expression from an untrusted source (web requests, user input, etc.), always use evaluate_with_timeout():
from eggcalc import evaluate_with_timeout, TimeoutError
# ALWAYS use timeout with untrusted input
try:
result = evaluate_with_timeout(user_expression, timeout=5.0)
except TimeoutError:
return {"error": "Calculation timed out"}This is the single most important security practice. Without a timeout, a malicious user could submit expressions that consume excessive CPU or memory.
eggcalc uses Abstract Syntax Tree (AST) parsing instead of Python's eval(). This provides:
- No arbitrary code execution - Users cannot execute Python code
- Controlled function access - Only whitelisted functions can be called
- Safe constant evaluation - Constants are validated before use
- No system access - No access to files, network, or system resources
Built-in protections against DoS attacks (cannot be bypassed):
| Constant | Default | Description |
|---|---|---|
MAX_INPUT_LENGTH |
10,000 | Maximum input character length |
MAX_NESTING_DEPTH |
100 | Maximum parentheses nesting depth |
MAX_EXPONENT |
10,000 | Maximum exponent value |
MAX_FACTORIAL |
1,000 | Maximum factorial input |
MAX_RESULT_VALUE |
1e308 | Maximum result value |
These prevent:
- Extremely long inputs
- Deeply nested expressions
- Huge exponents or factorials
- Overflow errors
The following Python operations are blocked and will raise EvaluationError:
import os # Blocked
__import__('os') # Blocked
eval('code') # Blocked
exec('code') # Blocked
compile('code', ...) # Blocked().__class__ # Blocked
obj.__bases__ # Blocked
obj.__subclasses__() # Blockedopen('/etc/passwd') # Blocked
os.system('ls') # Blocked
subprocess.call(...) # Blocked[x for x in y] # Blocked
{x for x in y} # Blocked
{x: y for x in z} # Blockedlambda x: x # Blocked
x if y else z # Blocked
x < y # Blocked
x and y # Blocked
x[0] # Blocked| Function | Use Case | Safety |
|---|---|---|
evaluate_with_timeout() |
Untrusted input | Recommended - has timeout |
evaluate_raw() |
User input, controlled environment | Safe but no timeout |
evaluate() |
Pre-normalized, trusted input | Fastest, no normalization |
EggCalcApp.calculate() |
Webapps with caching | Safe, per-instance isolation |
evaluate_async() |
Async frameworks | Safe in thread pool |
from eggcalc import evaluate_with_timeout, EvaluationError, TimeoutError
def handle_user_input(expression: str):
"""Safely evaluate user-provided expression with timeout."""
try:
result = evaluate_with_timeout(expression, timeout=5.0)
return {"success": True, "result": str(result)}
except EvaluationError as e:
return {"success": False, "error": str(e)}
except TimeoutError:
return {"success": False, "error": "Timeout"}Only register functions during initialization:
from eggcalc import register_function
# Safe: Register during startup
def my_safe_function(x):
return x * 2
register_function("safe_double", my_safe_function)
# Dangerous: Never register from user input
# register_function(user_name, user_func) # NEVER DO THISeggcalc_config.py is imported from the working directory:
# In production, this file should:
# 1. Not be user-writable
# 2. Only contain trusted code
# 3. Be reviewed for securityIn high-security environments, disable config loading entirely:
# Option 1: Environment variable (recommended for library use)
import os
os.environ["EGGCALC_NO_CONFIG"] = "1"
from eggcalc import evaluate_raw
# Option 2: Use EggCalcApp (no config loading by default)
from eggcalc import EggCalcApp
app = EggCalcApp()
# Manually configure instead
app.register_constant("safe_const", 42)Config-loading policy: Library APIs (evaluate_raw(), evaluate_cached(), etc.) do not load eggcalc_config.py by default. Set EGGCALC_LOAD_CONFIG=1 to enable lazy config loading, or call load_user_config() explicitly. CLI loads config by default.
from eggcalc import evaluate_with_timeout
result = evaluate_with_timeout(user_input, timeout=1.0)def validate_input(expr: str) -> str:
if len(expr) > 10000:
raise ValueError("Input too long")
if "import" in expr.lower():
raise ValueError("Invalid input")
return expr.strip()from eggcalc import EggCalcApp
# Each user/tenant gets isolated instance
app = EggCalcApp()from functools import wraps
from time import time
def rate_limit(max_per_minute: int):
# Implement rate limiting
passimport logging
def safe_evaluate(expr: str):
try:
result = evaluate_with_timeout(expr)
logging.info(f"Evaluated: {expr[:50]}")
return result
except Exception as e:
logging.warning(f"Error: {e}")
raiseFor applications that process user-provided text, the eggcalc.exact module provides tools to detect Unicode-based spoofing attacks:
- Confusables detection: Identify characters from different scripts that look identical
- Invisible character detection: Find zero-width spaces, BOM, bidi controls
- Mixed script detection: Flag text with characters from multiple scripts
from eggcalc.exact.synthesis import inspect_text
def validate_user_text(text: str) -> tuple[bool, list[str]]:
"""Check text for Unicode spoofing risks before storing."""
result = inspect_text(text) # returns a TypedDict (plain dict)
warnings = []
if result["confusables"]:
warnings.append(f"Confusable characters: {len(result['confusables'])} found")
if result["invisibles"]:
warnings.append(f"Invisible characters: {len(result['invisibles'])} found")
if result["mixed_scripts"]["mixed_scripts"]:
warnings.append("Mixed Unicode scripts detected")
return len(warnings) == 0, warnings
# Usage
safe, warnings = validate_user_text("p\u0430ypal") # Cyrillic confusableSee Exact Module for comprehensive text processing documentation.
eggcalc follows these security principles:
- Principle of Least Privilege: Only necessary operations allowed
- Defense in Depth: Multiple layers of protection
- Fail Secure: Errors result in safe failures
- No eval(): AST-based parsing prevents code injection
See SECURITY.md for:
- How to report vulnerabilities
- Response timeline
- Disclosure policy