Skip to content

Commit 71748c7

Browse files
committed
test: gitLeaks custom config case
1 parent c5db5a0 commit 71748c7

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

test/fixtures/gitleaks-config.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
title = "sample gitleaks config"
2+
3+
[[rules]]
4+
id = "generic-api-key"
5+
description = "Generic API Key"
6+
regex = '''(?i)(?:key|api|token|secret)[\s:=]+([a-z0-9]{32,})'''
7+
tags = ["key", "api-key"]
8+
9+
[[rules]]
10+
id = "aws-access-key-id"
11+
description = "AWS Access Key ID"
12+
regex = '''AKIA[0-9A-Z]{16}'''
13+
tags = ["aws", "key"]
14+
15+
[[rules]]
16+
id = "basic-auth"
17+
description = "Auth Credentials"
18+
regex = '''(?i)(https?://)[a-z0-9]+:[a-z0-9]+@'''
19+
tags = ["auth", "password"]
20+
21+
[[rules]]
22+
id = "jwt-token"
23+
description = "JSON Web Token"
24+
regex = '''eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.?[A-Za-z0-9._-]*'''
25+
tags = ["jwt", "token"]

test/processors/gitLeaks.test.js

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,95 @@ describe('gitleaks', () => {
162162
expect(result.steps).to.have.lengthOf(1);
163163
expect(result.steps[0].error).to.be.true;
164164
expect(stepSpy.calledWith('\nFound secret in file.txt\nWarning: potential leak')).to.be.true;
165-
});
165+
});
166+
167+
it('should handle gitleaks execution failure', async () => {
168+
stubs.getAPIs.returns({ gitleaks: { enabled: true } });
169+
170+
const gitRootCommitMock = {
171+
exitCode: 0,
172+
stdout: 'rootcommit123\n',
173+
stderr: ''
174+
};
175+
176+
const gitleaksMock = {
177+
exitCode: 1,
178+
stdout: '',
179+
stderr: 'Command failed'
180+
};
181+
182+
stubs.spawn
183+
.onFirstCall().returns({
184+
on: (event, cb) => {
185+
if (event === 'close') cb(gitRootCommitMock.exitCode);
186+
return { stdout: { on: () => {} }, stderr: { on: () => {} } };
187+
},
188+
stdout: { on: (_, cb) => cb(gitRootCommitMock.stdout) },
189+
stderr: { on: (_, cb) => cb(gitRootCommitMock.stderr) }
190+
})
191+
.onSecondCall().returns({
192+
on: (event, cb) => {
193+
if (event === 'close') cb(gitleaksMock.exitCode);
194+
return { stdout: { on: () => {} }, stderr: { on: () => {} } };
195+
},
196+
stdout: { on: (_, cb) => cb(gitleaksMock.stdout) },
197+
stderr: { on: (_, cb) => cb(gitleaksMock.stderr) }
198+
});
199+
200+
const result = await exec(req, action);
201+
202+
expect(result.error).to.be.true;
203+
expect(result.steps).to.have.lengthOf(1);
204+
expect(result.steps[0].error).to.be.true;
205+
expect(stepSpy.calledWith('failed to run gitleaks, please contact an administrator\n')).to.be.true;
206+
});
207+
208+
it('should handle custom config path', async () => {
209+
stubs.getAPIs.returns({
210+
gitleaks: {
211+
enabled: true,
212+
configPath: `../fixtures/gitleaks-config.toml`
213+
}
214+
});
215+
216+
stubs.fs.stat.resolves({ isFile: () => true });
217+
stubs.fs.access.resolves();
218+
219+
const gitRootCommitMock = {
220+
exitCode: 0,
221+
stdout: 'rootcommit123\n',
222+
stderr: ''
223+
};
224+
225+
const gitleaksMock = {
226+
exitCode: 0,
227+
stdout: '',
228+
stderr: 'No leaks found'
229+
};
230+
231+
stubs.spawn
232+
.onFirstCall().returns({
233+
on: (event, cb) => {
234+
if (event === 'close') cb(gitRootCommitMock.exitCode);
235+
return { stdout: { on: () => {} }, stderr: { on: () => {} } };
236+
},
237+
stdout: { on: (_, cb) => cb(gitRootCommitMock.stdout) },
238+
stderr: { on: (_, cb) => cb(gitRootCommitMock.stderr) }
239+
})
240+
.onSecondCall().returns({
241+
on: (event, cb) => {
242+
if (event === 'close') cb(gitleaksMock.exitCode);
243+
return { stdout: { on: () => {} }, stderr: { on: () => {} } };
244+
},
245+
stdout: { on: (_, cb) => cb(gitleaksMock.stdout) },
246+
stderr: { on: (_, cb) => cb(gitleaksMock.stderr) }
247+
});
248+
249+
const result = await exec(req, action);
250+
251+
expect(result.error).to.be.false;
252+
expect(result.steps[0].error).to.be.false;
253+
expect(stubs.spawn.secondCall.args[1]).to.include('--config=../fixtures/gitleaks-config.toml');
254+
});
166255
});
167256
});

0 commit comments

Comments
 (0)