|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import path from 'path'; |
| 3 | +import { checkRateLimitHeuristic } from '../../src/scanners/rateLimiting'; |
| 4 | +import type { DependencyInfo } from '../../src/scanners/dependencies'; |
| 5 | + |
| 6 | +const detectedTech = { |
| 7 | + hasFrontend: false, |
| 8 | + hasBackend: true, |
| 9 | + isNextJs: false, |
| 10 | + hasAuth: false, |
| 11 | + hasMiddleware: false, |
| 12 | + hasHttpClient: false, |
| 13 | + hasCors: false, |
| 14 | + hasFileUpload: false, |
| 15 | +}; |
| 16 | + |
| 17 | +describe('rateLimiting scanner', () => { |
| 18 | + it('issues advisory when routes exist but no rate-limit package is in dependencies', () => { |
| 19 | + const missingFile = path.join(__dirname, '../../test-data/rate-limit-missing.js'); |
| 20 | + const dependencies: DependencyInfo[] = [ |
| 21 | + { |
| 22 | + name: 'express', |
| 23 | + version: '4.18.0', |
| 24 | + packageManager: 'npm', |
| 25 | + sourceFile: 'package.json', |
| 26 | + }, |
| 27 | + ]; |
| 28 | + |
| 29 | + const findings = checkRateLimitHeuristic(dependencies, [missingFile], detectedTech); |
| 30 | + |
| 31 | + expect(findings.length).toBe(1); |
| 32 | + expect(findings[0].type).toBe('Project-Level Rate Limit Advisory'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns no advisory when express-rate-limit is in dependencies', () => { |
| 36 | + const presentFile = path.join(__dirname, '../../test-data/rate-limit-present.js'); |
| 37 | + const dependencies: DependencyInfo[] = [ |
| 38 | + { |
| 39 | + name: 'express', |
| 40 | + version: '4.18.0', |
| 41 | + packageManager: 'npm', |
| 42 | + sourceFile: 'package.json', |
| 43 | + }, |
| 44 | + { |
| 45 | + name: 'express-rate-limit', |
| 46 | + version: '6.7.0', |
| 47 | + packageManager: 'npm', |
| 48 | + sourceFile: 'package.json', |
| 49 | + }, |
| 50 | + ]; |
| 51 | + |
| 52 | + const findings = checkRateLimitHeuristic(dependencies, [presentFile], detectedTech); |
| 53 | + |
| 54 | + expect(findings).toHaveLength(0); |
| 55 | + }); |
| 56 | +}); |
0 commit comments