@@ -4,6 +4,8 @@ const { exec: cexec } = require('child_process');
44const path = require ( 'path' ) ;
55const config = require ( '../../../config' ) ;
66const commitConfig = config . getCommitConfig ( ) ;
7+ const authorizedlist = config . getAuthorisedList ( ) ;
8+
79
810// Function to extract relevant file paths from Git diff content
911// go to proxyconfig.json and enable the feature
@@ -37,53 +39,39 @@ function extractRelevantDirectories(diffContent) {
3739}
3840
3941// Function to run Gitleaks with directory paths
40- function runGitleaks(filePaths) {
42+ function runGitleaks ( filePaths , repoRoot ) {
4143 return new Promise ( ( resolve , reject ) => {
4244 const filesToCheck = filePaths
43- .map((filePath) => `"${path.resolve(filePath).replace(/\\/g, '/')}"`)
45+ . map ( ( filePath ) => `"${ path . resolve ( repoRoot , filePath ) . replace ( / \\ / g, '/' ) } "` )
4446 . join ( ' ' ) ;
47+ console . log ( "filesToCheck:" , filesToCheck ) ;
4548
4649 const configPath = path . resolve ( __dirname , '../../../../gitleaks.toml' ) . replace ( / \\ / g, '/' ) ;
47- const reportPath = path
48- .resolve(__dirname, '../../../../gitleaks_report.json')
49- .replace(/\\/g, '/');
50+ const reportPath = repoRoot + '/gitleaks_report.json' ;
5051
51- const command = `gitleaks dir ${filesToCheck} --config="${configPath}" --report-format json --log-level error --report-path="${reportPath}"`;
52+ const command = `gitleaks dir ${ filesToCheck } --config="${ configPath } " --report-format json --log-level debug --report-path="${ reportPath } "` ;
5253 console . log ( `Executing Gitleaks Command: ${ command } ` ) ;
5354
5455 cexec ( command , ( error , stdout , stderr ) => {
5556 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}`));
57+ // If leaks are found, handle the warning gracefully
58+ console . log ( "stderrrrr:" , stderr ) ;
59+ if ( stderr . includes ( "leaks found" ) ) {
60+ console . warn ( "Leaks were found, but execution succeeded." ) ;
61+ resolve ( true ) ; // Consider this a successful run
62+ } else {
63+ console . error ( `Error executing gitleaks: ${ error . message } ` ) ;
64+ reject ( new Error ( `Error executing gitleaks: ${ error . message } ` ) ) ;
65+ }
6166 } else {
62- resolve(stdout );
67+ resolve ( false ) ;
6368 }
6469 } ) ;
6570 } ) ;
6671}
6772
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- }
73+
74+
8775
8876// Example usage in exec function
8977const exec = async ( req , action ) => {
@@ -98,13 +86,14 @@ const exec = async (req, action) => {
9886
9987 if ( diffStep && diffStep . content ) {
10088 const dirPaths = extractRelevantDirectories ( diffStep . content ) ;
101-
89+ const repoRoot = authorizedlist . find ( ( item ) => item . url === action . url ) . LocalRepoRoot ;
90+
10291 if ( dirPaths . length > 0 ) {
10392 try {
104- const result = await runGitleaks(dirPaths);
105- const hasSensitiveSecrets = checkForSensitiveSecrets(result);
93+ const res = await runGitleaks ( dirPaths , repoRoot ) ;
94+
10695
107- if (hasSensitiveSecrets ) {
96+ if ( res ) {
10897 step . blocked = true ;
10998 step . blockedMessage = 'Sensitive secrets detected in the diff.' ;
11099 console . log ( 'Sensitive secrets detected! Push blocked.' ) ;
@@ -126,7 +115,7 @@ const exec = async (req, action) => {
126115} ;
127116
128117exec . displayName = 'checkforSecrets.exec' ;
129-
118+ module . exports = { exec } ;
130119
131120
132121
0 commit comments