The Script programming language is designed with security as a first-class concern. This document outlines the security features, best practices, and considerations when working with Script.
Script includes comprehensive protection against DoS attacks during compilation:
- Timeout Protection: All compilation phases have configurable timeouts
- Memory Limits: System memory usage is monitored and bounded
- Iteration Limits: Recursive operations have maximum iteration counts
- Recursion Depth: Stack overflow protection through depth tracking
- Specialization Limits: Generic instantiation explosion prevention
use script_lang::compilation::resource_limits::ResourceLimits;
// Production environment (secure defaults)
let limits = ResourceLimits::production();
// Development environment (more permissive)
let limits = ResourceLimits::development();
// Custom limits
let limits = ResourceLimits::custom()
.max_iterations(10_000)
.phase_timeout(Duration::from_secs(30))
.max_memory_bytes(512 * 1024 * 1024) // 512MB
.build()?;- All array accesses include runtime bounds checking
- Negative index detection
- Automatic length validation
- Panic-on-violation with clear error messages
let arr = [1, 2, 3];
let val = arr[10]; // Runtime panic: "Array index 10 out of bounds (length: 3)"
- Automatic null pointer detection in field access
- Runtime validation of object references
- Memory corruption prevention
- Compile-time field existence checking
- Type-safe field offset calculation
- Prevention of invalid memory access
- Specialization explosion prevention
- Type variable limit enforcement
- Constraint system protection
- Proper Arc reference counting in async operations
- FFI pointer lifetime tracking
- Race condition elimination
- Bounded async state allocation
- Automatic cleanup of async resources
- Overflow protection in state machines
// Use production resource limits in production
let context = CompilationContext::with_resource_limits(
ResourceLimits::production()
);
// Enable all security features
let bounds_checker = BoundsChecker::new(BoundsCheckMode::Always);// More permissive limits for development
let context = CompilationContext::for_development();When processing external Script code, always use restrictive resource limits:
let limits = ResourceLimits::custom()
.max_iterations(1_000) // Low iteration limit
.phase_timeout(Duration::from_secs(10)) // Short timeout
.max_memory_bytes(50 * 1024 * 1024) // 50MB limit
.build()?;
let mut context = CompilationContext::with_resource_limits(limits);- Validate user-defined struct/enum definitions
- Limit nesting depth for complex types
- Prevent recursive type definitions
// Safe array access with bounds checking
fn safe_access(arr: [i32], index: i32) -> Option<i32> {
if index >= 0 && index < arr.length {
Some(arr[index])
} else {
None
}
}
// Use Result types for potentially failing operations
fn divide(a: i32, b: i32) -> Result<i32, String> {
if b == 0 {
Err("Division by zero")
} else {
Ok(a / b)
}
}
Attack Vector: Malicious code that consumes excessive compilation resources
Mitigation: Comprehensive resource monitoring and limits
- Timeout enforcement at all compilation phases
- Memory usage tracking and limits
- Iteration count restrictions
- Specialization explosion prevention
Example Protection:
// This will be caught and prevented
fn recursive_type_bomb() {
// Attempting to create exponential type specializations
// Compiler will abort with security violation
}Attack Vector: Buffer overflows through array access or pointer manipulation
Mitigation: Runtime bounds checking and memory safety
- All array accesses validated at runtime
- Negative index detection
- Null pointer protection
- Automatic memory management
Example Protection:
let arr = [1, 2, 3];
let val = arr[-1]; // Caught: negative index
let val2 = arr[100]; // Caught: index out of bounds
Attack Vector: Exploiting type system weaknesses to access invalid memory
Mitigation: Strong type safety and validation
- Compile-time type checking
- Runtime type validation
- Field access validation
- Generic type constraints
Attack Vector: Code that causes infinite compilation or excessive resource usage
Mitigation: Comprehensive DoS protection
- Phase timeouts prevent infinite compilation
- Memory limits prevent memory exhaustion
- Iteration limits prevent infinite loops in type checking
- Recursion depth limits prevent stack overflow
The Script compiler includes comprehensive security tests:
# Run all security tests
cargo test security
# Run resource limit tests specifically
cargo test resource_limits_test
# Run DoS protection tests
cargo test test_dos_attack_simulationFor additional security validation, consider fuzzing the compiler:
# Install cargo-fuzz
cargo install cargo-fuzz
# Run parser fuzzing
cargo fuzz run parser_fuzz
# Run type inference fuzzing
cargo fuzz run inference_fuzzRegular security audits should include:
- Resource limit effectiveness testing
- Memory safety verification
- Attack vector analysis
- Dependency security scanning
If you discover a security vulnerability in the Script language:
- Do not create a public issue
- Email security reports to: [security@script-lang.org]
- Include detailed reproduction steps
- Allow time for investigation and patching
Security updates will be:
- Released immediately for critical vulnerabilities
- Clearly marked in release notes
- Documented with mitigation strategies
- Backwards compatible when possible
Script follows these security guidelines:
- OWASP Secure Coding Practices
- SANS Top 25 Software Errors prevention
- CWE (Common Weakness Enumeration) mitigation
The Script compiler is designed to support:
- SOC 2 compliance requirements
- Security auditing and logging
- Reproducible builds
- Supply chain security
let limits = ResourceLimits::custom()
.max_iterations(1_000) // Very restrictive
.phase_timeout(Duration::from_secs(5)) // Short timeout
.total_timeout(Duration::from_secs(15)) // Total limit
.max_memory_bytes(10 * 1024 * 1024) // 10MB only
.max_recursion_depth(50) // Shallow recursion
.max_specializations(100) // Limited generics
.build()?;
let context = CompilationContext::with_resource_limits(limits);let limits = ResourceLimits::production(); // Secure defaults
let context = CompilationContext::with_resource_limits(limits);let limits = ResourceLimits::development(); // More permissive
let context = CompilationContext::for_development();let stats = resource_monitor.get_stats();
println!("Compilation time: {:?}", stats.compilation_time);
println!("Memory usage: {} bytes", stats.memory_usage);
println!("Type variables: {}", stats.type_variable_count);// Security violations are automatically logged
match context.compile_file(&path) {
Err(Error::SecurityViolation(msg)) => {
log::warn!("Security violation detected: {}", msg);
// Alert security team
}
Ok(module) => { /* Success */ }
Err(other) => { /* Handle other errors */ }
}The Script language provides comprehensive security features designed to protect against common attack vectors while maintaining usability and performance. By following the guidelines in this document and leveraging the built-in security features, developers can build secure applications with confidence.
For the latest security updates and announcements, monitor the Script language security advisories and keep your installation up to date.