(code: string)
| 40 | * @returns Detection result with list of found functions and their locations |
| 41 | */ |
| 42 | export function detectSensitiveFunctions(code: string): SensitiveDataDetection { |
| 43 | const detectedFunctions: string[] = []; |
| 44 | const lineNumbers: Record<string, number[]> = {}; |
| 45 | |
| 46 | if (!code || typeof code !== 'string') { |
| 47 | return { |
| 48 | hasSensitiveData: false, |
| 49 | detectedFunctions: [], |
| 50 | lineNumbers: {} |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | // Split code into lines for line number tracking |
| 55 | const lines = code.split('\n'); |
| 56 | |
| 57 | // Check each sensitive function |
| 58 | for (const funcName of SENSITIVE_FUNCTIONS) { |
| 59 | // Create regex to match function calls |
| 60 | // Matches: funcName( with optional whitespace |
| 61 | const regex = new RegExp(`\\b${funcName}\\s*\\(`, 'g'); |
| 62 | |
| 63 | // Track line numbers where this function appears |
| 64 | const foundLines: number[] = []; |
| 65 | |
| 66 | lines.forEach((line, index) => { |
| 67 | if (regex.test(line)) { |
| 68 | foundLines.push(index + 1); // Line numbers start at 1 |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | if (foundLines.length > 0) { |
| 73 | detectedFunctions.push(funcName); |
| 74 | lineNumbers[funcName] = foundLines; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return { |
| 79 | hasSensitiveData: detectedFunctions.length > 0, |
| 80 | detectedFunctions, |
| 81 | lineNumbers |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get human-readable description of what data type a function may contain |
no test coverage detected