|
| 1 | +const { Step } = require('../../actions'); |
| 2 | +const { exec: cexec } = require('child_process'); |
| 3 | + |
| 4 | +const path = require('path'); |
| 5 | +const config = require('../../../config'); |
| 6 | +const commitConfig = config.getCommitConfig(); |
| 7 | + |
| 8 | +// Function to extract relevant file paths from Git diff content |
| 9 | +// go to proxyconfig.json and enable the feature |
| 10 | +// gitleaks.report.json will show the secrets found and in which file they are found |
| 11 | +// Function to extract relevant file paths and their parent directories |
| 12 | + |
| 13 | +// gitleaks dir "C:/Users/ingle/Desktop/CitiHackthon/git-proxy/test/test_data/sensitive_data.js" --config="c:/Users/ingle/Desktop/CitiHackthon/git-proxy/gitleaks.toml" --report-format json --log-level debug --report-path="c:/Users/ingle/Desktop/CitiHackthon/git-proxy/gitleaks_report.json" |
| 14 | +// use the command to run gitleaks from terminal |
| 15 | +// Function to extract relevant directories from Git diff content |
| 16 | +function extractRelevantDirectories(diffContent) { |
| 17 | + const relevantDirectories = []; |
| 18 | + const relevantExtensions = ['.json', '.yaml', '.yml', '.js', '.ts', '.txt']; |
| 19 | + const lines = diffContent.split('\n'); |
| 20 | + |
| 21 | + lines.forEach((line) => { |
| 22 | + const match = line.match(/^diff --git a\/(.+?) b\/(.+?)$/); |
| 23 | + if (match) { |
| 24 | + const filePath = match[1]; |
| 25 | + const fileExtension = `.${filePath.split('.').pop()}`; |
| 26 | + |
| 27 | + if (relevantExtensions.includes(fileExtension)) { |
| 28 | + const dirPath = path.dirname(filePath); |
| 29 | + if (!relevantDirectories.includes(dirPath)) { |
| 30 | + relevantDirectories.push(dirPath); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + return relevantDirectories; |
| 37 | +} |
| 38 | + |
| 39 | +// Function to run Gitleaks with directory paths |
| 40 | +function runGitleaks(filePaths) { |
| 41 | + return new Promise((resolve, reject) => { |
| 42 | + const filesToCheck = filePaths |
| 43 | + .map((filePath) => `"${path.resolve(filePath).replace(/\\/g, '/')}"`) |
| 44 | + .join(' '); |
| 45 | + |
| 46 | + const configPath = path.resolve(__dirname, '../../../../gitleaks.toml').replace(/\\/g, '/'); |
| 47 | + const reportPath = path |
| 48 | + .resolve(__dirname, '../../../../gitleaks_report.json') |
| 49 | + .replace(/\\/g, '/'); |
| 50 | + |
| 51 | + const command = `gitleaks dir ${filesToCheck} --config="${configPath}" --report-format json --log-level error --report-path="${reportPath}"`; |
| 52 | + console.log(`Executing Gitleaks Command: ${command}`); |
| 53 | + |
| 54 | + cexec(command, (error, stdout, stderr) => { |
| 55 | + if (error) { |
| 56 | + console.error(`Error executing gitleaks: ${error.message}`); |
| 57 | + reject(new Error(`Error executing gitleaks: ${error.message}`)); |
| 58 | + } else if (stderr) { |
| 59 | + console.error(`stderr: ${stderr}`); |
| 60 | + reject(new Error(`stderr: ${stderr}`)); |
| 61 | + } else { |
| 62 | + resolve(stdout); |
| 63 | + } |
| 64 | + }); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +// Function to check for sensitive secrets in the Gitleaks output |
| 69 | +function checkForSensitiveSecrets(output) { |
| 70 | + try { |
| 71 | + const findings = JSON.parse(output); |
| 72 | + |
| 73 | + if (findings.length > 0) { |
| 74 | + findings.forEach((finding) => { |
| 75 | + console.log(`Secret found in file: ${finding.file}`); |
| 76 | + console.log(` Rule: ${finding.rule_id}`); |
| 77 | + console.log(` Secret: ${finding.secret}`); |
| 78 | + }); |
| 79 | + return true; |
| 80 | + } |
| 81 | + return false; |
| 82 | + } catch (error) { |
| 83 | + console.error('Error parsing Gitleaks output:', error); |
| 84 | + return false; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Example usage in exec function |
| 89 | +const exec = async (req, action) => { |
| 90 | + const diffStep = action.steps.find((s) => s.stepName === 'diff'); |
| 91 | + const step = new Step('checkforSecrets'); |
| 92 | + const commitinfo = commitConfig.checkForSecrets; |
| 93 | + |
| 94 | + if (!commitinfo.enabled) { |
| 95 | + action.addStep(step); |
| 96 | + return action; |
| 97 | + } |
| 98 | + |
| 99 | + if (diffStep && diffStep.content) { |
| 100 | + const dirPaths = extractRelevantDirectories(diffStep.content); |
| 101 | + |
| 102 | + if (dirPaths.length > 0) { |
| 103 | + try { |
| 104 | + const result = await runGitleaks(dirPaths); |
| 105 | + const hasSensitiveSecrets = checkForSensitiveSecrets(result); |
| 106 | + |
| 107 | + if (hasSensitiveSecrets) { |
| 108 | + step.blocked = true; |
| 109 | + step.blockedMessage = 'Sensitive secrets detected in the diff.'; |
| 110 | + console.log('Sensitive secrets detected! Push blocked.'); |
| 111 | + } else { |
| 112 | + console.log('No sensitive secrets detected.'); |
| 113 | + } |
| 114 | + action.addStep(step); |
| 115 | + } catch (err) { |
| 116 | + console.error('Error during Gitleaks execution:', err); |
| 117 | + } |
| 118 | + } else { |
| 119 | + console.log('No relevant directories found in the diff.'); |
| 120 | + } |
| 121 | + } else { |
| 122 | + console.log('No diff content available.'); |
| 123 | + } |
| 124 | + |
| 125 | + return action; |
| 126 | +}; |
| 127 | + |
| 128 | +exec.displayName = 'checkforSecrets.exec'; |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
0 commit comments