|
| 1 | +const fs = require('fs'); |
| 2 | +const csv = require('csv-parser'); |
| 3 | +const XLSX = require('xlsx'); |
| 4 | +const path = require('path'); |
| 5 | +const { exec: getDiffExec } = require('./getDiff'); |
| 6 | + |
| 7 | +// Function to check for sensitive data patterns |
| 8 | +const checkForSensitiveData = (cell) => { |
| 9 | + const sensitivePatterns = [ |
| 10 | + /\d{3}-\d{2}-\d{4}/, // Social Security Number (SSN) |
| 11 | + /\b\d{16}\b/, // Credit card numbers |
| 12 | + /\b\d{5}-\d{4}\b/, // ZIP+4 codes |
| 13 | + // Add more patterns as needed |
| 14 | + ]; |
| 15 | + return sensitivePatterns.some(pattern => { |
| 16 | + if (pattern.test(String(cell))) { |
| 17 | + console.log(`\x1b[31mDetected sensitive data: ${cell}\x1b[0m`); // Log the detected sensitive data in red |
| 18 | + return true; |
| 19 | + } |
| 20 | + return false; |
| 21 | + }); |
| 22 | +}; |
| 23 | + |
| 24 | +// Function to process CSV files |
| 25 | +const processCSV = async (filePath) => { |
| 26 | + return new Promise((resolve, reject) => { |
| 27 | + let sensitiveDataFound = false; |
| 28 | + |
| 29 | + fs.createReadStream(filePath) |
| 30 | + .pipe(csv()) |
| 31 | + .on('data', (row) => { |
| 32 | + for (const [key, value] of Object.entries(row)) { |
| 33 | + if (checkForSensitiveData(value)) { |
| 34 | + console.log(`\x1b[33mSensitive data found in CSV: ${key}: ${value}\x1b[0m`); // Log in yellow |
| 35 | + sensitiveDataFound = true; |
| 36 | + } |
| 37 | + } |
| 38 | + }) |
| 39 | + .on('end', () => { |
| 40 | + if (!sensitiveDataFound) { |
| 41 | + console.log('No sensitive data found in CSV.'); |
| 42 | + } |
| 43 | + resolve(sensitiveDataFound); // Resolve with the flag indicating if sensitive data was found |
| 44 | + }) |
| 45 | + .on('error', (err) => { |
| 46 | + console.error(`Error reading CSV file: ${err.message}`); |
| 47 | + reject(err); // Reject the promise on error |
| 48 | + }); |
| 49 | + }); |
| 50 | +}; |
| 51 | + |
| 52 | +// Function to process XLSX files |
| 53 | +const processXLSX = async (filePath) => { |
| 54 | + return new Promise((resolve, reject) => { |
| 55 | + let sensitiveDataFound = false; |
| 56 | + |
| 57 | + try { |
| 58 | + const workbook = XLSX.readFile(filePath); |
| 59 | + const sheetName = workbook.SheetNames[0]; |
| 60 | + const sheet = workbook.Sheets[sheetName]; |
| 61 | + const jsonData = XLSX.utils.sheet_to_json(sheet); |
| 62 | + |
| 63 | + jsonData.forEach((row) => { |
| 64 | + for (const [key, value] of Object.entries(row)) { |
| 65 | + if (checkForSensitiveData(value)) { |
| 66 | + console.log(`\x1b[33mSensitive data found in XLSX: ${key}: ${value}\x1b[0m`); // Log in yellow |
| 67 | + sensitiveDataFound = true; |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + if (!sensitiveDataFound) { |
| 73 | + console.log('No sensitive data found in XLSX.'); |
| 74 | + } |
| 75 | + resolve(sensitiveDataFound); // Resolve with the flag indicating if sensitive data was found |
| 76 | + } catch (error) { |
| 77 | + console.error(`Error reading XLSX file: ${error.message}`); |
| 78 | + reject(error); // Reject the promise on error |
| 79 | + } |
| 80 | + }); |
| 81 | +}; |
| 82 | + |
| 83 | +// Function to check for sensitive data in .log and .json files |
| 84 | +const checkLogJsonFiles = async (filePath) => { |
| 85 | + return new Promise((resolve, reject) => { |
| 86 | + let sensitiveDataFound = false; |
| 87 | + |
| 88 | + fs.readFile(filePath, 'utf8', (err, data) => { |
| 89 | + if (err) { |
| 90 | + console.error(`Error reading file ${filePath}: ${err.message}`); |
| 91 | + return reject(err); |
| 92 | + } |
| 93 | + |
| 94 | + if (checkForSensitiveData(data)) { |
| 95 | + console.log(`\x1b[33mSensitive data found in ${filePath}\x1b[0m`); |
| 96 | + sensitiveDataFound = true; |
| 97 | + } |
| 98 | + |
| 99 | + resolve(sensitiveDataFound); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}; |
| 103 | + |
| 104 | +// Function to parse the file based on its extension |
| 105 | +const parseFile = async (filePath) => { |
| 106 | + const ext = path.extname(filePath).toLowerCase(); |
| 107 | + |
| 108 | + switch (ext) { |
| 109 | + case '.csv': |
| 110 | + return await processCSV(filePath); |
| 111 | + case '.xlsx': |
| 112 | + return await processXLSX(filePath); |
| 113 | + case '.log': |
| 114 | + return await checkLogJsonFiles(filePath); |
| 115 | + case '.json': |
| 116 | + return await checkLogJsonFiles(filePath); |
| 117 | + default: |
| 118 | + // Skip unsupported file types without logging |
| 119 | + return false; // Indicate that no sensitive data was found for unsupported types |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +// Async exec function to handle actions |
| 124 | +const exec = async (req, action) => { |
| 125 | + // getDiffExec(req, action); // Call to getDiffExec if necessary |
| 126 | + |
| 127 | + const diffStep = action.steps.find((s) => s.stepName === 'diff'); |
| 128 | + |
| 129 | + if (diffStep && diffStep.content) { |
| 130 | + console.log('Diff content:', diffStep.content); |
| 131 | + |
| 132 | + const filePaths = diffStep.content.filePaths || []; |
| 133 | + |
| 134 | + if (filePaths.length > 0) { |
| 135 | + // Check for sensitive data in all files |
| 136 | + const sensitiveDataFound = await Promise.all(filePaths.map(parseFile)); |
| 137 | + const anySensitiveDataDetected = sensitiveDataFound.some(found => found); // Check if any file reported sensitive data |
| 138 | + |
| 139 | + if (anySensitiveDataDetected) { |
| 140 | + action.pushBlocked = true; // Block the push |
| 141 | + action.error = true; // Set error flag |
| 142 | + action.errorMessage = 'Your push has been blocked due to sensitive data detection.'; // Set error message |
| 143 | + console.log(action.errorMessage); |
| 144 | + } |
| 145 | + } else { |
| 146 | + console.log('No file paths provided in the diff step.'); |
| 147 | + } |
| 148 | + } else { |
| 149 | + console.log('No diff content available.'); |
| 150 | + } |
| 151 | + |
| 152 | + return action; // Returning action for testing purposes |
| 153 | +}; |
| 154 | + |
| 155 | +exec.displayName = 'logFileChanges.exec'; |
| 156 | +exports.exec = exec; |
0 commit comments