rate-limit-test.jsā¢6.07 kB
/**
* Test Rate Limiting Implementation
*/
import { RateLimiter } from '../lib/security/RateLimiter.js';
import { UniversalSandbox } from '../lib/security/UniversalSandbox.js';
async function testRateLimiter() {
console.log('š Testing Rate Limiter Implementation');
console.log('=====================================');
// Test 1: Basic rate limiting
console.log('ā Testing basic rate limiting...');
const rateLimiter = new RateLimiter({
maxRequests: 5,
windowMs: 1000,
burstLimit: 2,
burstWindowMs: 100
});
let result = await rateLimiter.isAllowed('client1', 'test', {});
console.log('ā
First request allowed:', result.allowed);
// Test burst limit
console.log('ā Testing burst limit...');
for (let i = 0; i < 3; i++) {
result = await rateLimiter.isAllowed('client1', 'test', {});
if (i < 2) {
console.log(`ā
Burst request ${i + 1} allowed:`, result.allowed);
} else {
console.log('ā
Burst limit exceeded:', !result.allowed, result.reason);
}
}
// Wait for burst window to reset
await new Promise(resolve => setTimeout(resolve, 150));
// Test rate limit
console.log('ā Testing rate limit...');
for (let i = 0; i < 6; i++) {
result = await rateLimiter.isAllowed('client1', 'test', {});
if (i < 4) { // Already used 1 request + 2 burst = 3, so 2 more allowed
console.log(`ā
Rate request ${i + 1} allowed:`, result.allowed);
} else {
console.log('ā
Rate limit exceeded:', !result.allowed, result.reason);
break;
}
await new Promise(resolve => setTimeout(resolve, 10));
}
// Test suspicious activity detection
console.log('ā Testing suspicious activity detection...');
const suspiciousClient = 'suspicious-client';
// Rapid fire requests to trigger detection
for (let i = 0; i < 25; i++) {
result = await rateLimiter.isAllowed(suspiciousClient, 'test', {});
if (!result.allowed && result.reason === 'SUSPICIOUS_ACTIVITY') {
console.log('ā
Suspicious activity detected and blocked');
break;
}
}
// Test different clients are isolated
console.log('ā Testing client isolation...');
result = await rateLimiter.isAllowed('client2', 'test', {});
console.log('ā
Different client not affected:', result.allowed);
console.log('ā Rate limiting stats:', rateLimiter.getStats());
rateLimiter.destroy();
console.log('ā
RateLimiter cleanup completed');
}
async function testUniversalSandboxWithRateLimit() {
console.log('\nš Testing Universal Sandbox with Rate Limiting');
console.log('===============================================');
const sandbox = new UniversalSandbox({
enabled: true,
mode: 'block',
maxRequests: 3,
windowMs: 1000,
burstLimit: 1
});
try {
// Test normal command execution
console.log('ā Testing normal command execution...');
let result = await sandbox.processCommand('exec', 'echo', ['test'], { clientId: 'test-client' });
console.log('ā
First command executed');
// Test rate limiting in sandbox
console.log('ā Testing rate limiting in sandbox...');
let rateLimitHit = false;
try {
// Execute commands rapidly to hit rate limit
for (let i = 0; i < 5; i++) {
await sandbox.processCommand('exec', 'echo', [`test-${i}`], { clientId: 'test-client' });
}
} catch (error) {
if (error.code === 'RATE_LIMIT_EXCEEDED') {
console.log('ā
Rate limit correctly enforced in sandbox');
rateLimitHit = true;
} else {
console.log('ā Unexpected error:', error.message);
}
}
if (!rateLimitHit) {
console.log('ā Rate limit was not enforced');
}
// Test metrics include rate limiting data
console.log('ā Testing metrics integration...');
const metrics = sandbox.getSecurityMetrics();
console.log('ā
Rate limiting metrics included:', !!metrics.rateLimiting);
console.log('ā
Rate limit violations tracked:', metrics.rateLimitViolations || 0);
} catch (error) {
console.log('ā Sandbox test failed:', error.message);
} finally {
sandbox.destroy();
console.log('ā
UniversalSandbox cleanup completed');
}
}
async function testRateLimitConfiguration() {
console.log('\nš Testing Rate Limit Configuration Options');
console.log('===========================================');
// Test custom configuration
const customLimiter = new RateLimiter({
maxRequests: 10,
windowMs: 2000,
burstLimit: 3,
burstWindowMs: 200,
enableDDoSProtection: true,
suspiciousThreshold: 15,
blockDurationMs: 1000
});
console.log('ā Testing custom configuration...');
console.log('ā
Custom configuration applied:', customLimiter.options.maxRequests === 10);
// Test DoS protection can be disabled
const nonProtectedLimiter = new RateLimiter({
enableDDoSProtection: false
});
console.log('ā Testing DoS protection disabled...');
let blocked = false;
for (let i = 0; i < 100; i++) {
const result = await nonProtectedLimiter.isAllowed('rapid-client', 'test', {});
if (!result.allowed && result.reason === 'SUSPICIOUS_ACTIVITY') {
blocked = true;
break;
}
}
console.log('ā
DoS protection correctly disabled:', !blocked);
customLimiter.destroy();
nonProtectedLimiter.destroy();
}
async function runAllRateLimitTests() {
try {
await testRateLimiter();
await testUniversalSandboxWithRateLimit();
await testRateLimitConfiguration();
console.log('\nš Rate Limiting Test Summary:');
console.log('ā
All rate limiting tests completed successfully');
console.log('š Rate limiting system is fully functional!');
return true;
} catch (error) {
console.error('ā Rate limiting tests failed:', error);
return false;
}
}
// Run tests if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
runAllRateLimitTests().then(success => {
process.exit(success ? 0 : 1);
});
}
export { runAllRateLimitTests };