(code: string)
| 89 | * @returns Detection result with list of found functions and their locations |
| 90 | */ |
| 91 | export function detectSensitiveFunctions(code: string): SensitiveDataDetection { |
| 92 | const detectedFunctions: string[] = []; |
| 93 | const lineNumbers: Record<string, number[]> = {}; |
| 94 | |
| 95 | if (!code || typeof code !== 'string') { |
| 96 | return { |
| 97 | hasSensitiveData: false, |
| 98 | detectedFunctions: [], |
| 99 | lineNumbers: {} |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | // Split code into lines for line number tracking |
| 104 | const lines = code.split('\n'); |
| 105 | |
| 106 | // Check each sensitive function |
| 107 | for (const funcName of SENSITIVE_FUNCTIONS) { |
| 108 | // Create regex to match function calls |
| 109 | // Matches: funcName( with optional whitespace |
| 110 | const regex = new RegExp(`\\b${funcName}\\s*\\(`, 'g'); |
| 111 | |
| 112 | // Track line numbers where this function appears |
| 113 | const foundLines: number[] = []; |
| 114 | |
| 115 | lines.forEach((line, index) => { |
| 116 | if (regex.test(line)) { |
| 117 | foundLines.push(index + 1); // Line numbers start at 1 |
| 118 | } |
| 119 | }); |
| 120 | |
| 121 | if (foundLines.length > 0) { |
| 122 | detectedFunctions.push(funcName); |
| 123 | lineNumbers[funcName] = foundLines; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return { |
| 128 | hasSensitiveData: detectedFunctions.length > 0, |
| 129 | detectedFunctions, |
| 130 | lineNumbers |
| 131 | }; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get human-readable description of what data type a function may contain |
no test coverage detected