Skip to content

Commit 2e064ae

Browse files
authored
Merge branch 'dev' into bugfix/wrong-commit-hash
2 parents 1793849 + aee194a commit 2e064ae

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

test_all.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import json
55
import io
66
from truffleHog import truffleHog
7+
from mock import patch
8+
from mock import MagicMock
79

810

911
class TestStringMethods(unittest.TestCase):
@@ -56,6 +58,13 @@ def test_return_correct_commit_hash(self):
5658
# Additionally, we cross-validate the commit comment matches the expected comment
5759
self.assertEqual(cross_valdiating_commit_w_secret_comment, filtered_results[0]['commit'].strip())
5860

61+
@patch('truffleHog.truffleHog.clone_git_repo')
62+
@patch('truffleHog.truffleHog.Repo')
63+
def test_branch(self, repo_const_mock, clone_git_repo):
64+
repo = MagicMock()
65+
repo_const_mock.return_value = repo
66+
truffleHog.find_strings("test_repo", branch="testbranch")
67+
repo.remotes.origin.fetch.assert_called_once_with("testbranch")
5968

6069
if __name__ == '__main__':
6170
unittest.main()

truffleHog/truffleHog.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ def main():
2727
parser.add_argument("--entropy", dest="do_entropy", help="Enable entropy checks")
2828
parser.add_argument("--since_commit", dest="since_commit", help="Only scan from a given commit hash")
2929
parser.add_argument("--max_depth", dest="max_depth", help="The max commit depth to go back when searching for secrets")
30+
parser.add_argument("--branch", dest="branch", help="Name of the branch to be scanned")
3031
parser.add_argument('git_url', type=str, help='URL for secret searching')
3132
parser.set_defaults(regex=False)
3233
parser.set_defaults(rules={})
3334
parser.set_defaults(max_depth=1000000)
3435
parser.set_defaults(since_commit=None)
3536
parser.set_defaults(entropy=True)
37+
parser.set_defaults(branch=None)
3638
args = parser.parse_args()
3739
rules = {}
3840
if args.rules:
@@ -48,7 +50,7 @@ def main():
4850
for regex in rules:
4951
regexes[regex] = rules[regex]
5052
do_entropy = str2bool(args.do_entropy)
51-
output = find_strings(args.git_url, args.since_commit, args.max_depth, args.output_json, args.do_regex, do_entropy, surpress_output=False)
53+
output = find_strings(args.git_url, args.since_commit, args.max_depth, args.output_json, args.do_regex, do_entropy, surpress_output=False, branch=args.branch)
5254
project_path = output["project_path"]
5355
shutil.rmtree(project_path, onerror=del_rw)
5456
if output["foundIssues"]:
@@ -240,14 +242,19 @@ def handle_results(output, output_dir, foundIssues):
240242
output["foundIssues"].append(result_path)
241243
return output
242244

243-
def find_strings(git_url, since_commit=None, max_depth=1000000, printJson=False, do_regex=False, do_entropy=True, surpress_output=True, custom_regexes={}):
245+
def find_strings(git_url, since_commit=None, max_depth=1000000, printJson=False, do_regex=False, do_entropy=True, surpress_output=True, custom_regexes={}, branch=None):
244246
output = {"foundIssues": []}
245247
project_path = clone_git_repo(git_url)
246248
repo = Repo(project_path)
247249
already_searched = set()
248250
output_dir = tempfile.mkdtemp()
249251

250-
for remote_branch in repo.remotes.origin.fetch():
252+
if branch:
253+
branches = repo.remotes.origin.fetch(branch)
254+
else:
255+
branches = repo.remotes.origin.fetch()
256+
257+
for remote_branch in branches:
251258
since_commit_reached = False
252259
branch_name = remote_branch.name
253260
prev_commit = None
@@ -281,7 +288,16 @@ def find_strings(git_url, since_commit=None, max_depth=1000000, printJson=False,
281288
output = handle_results(output, output_dir, foundIssues)
282289
output["project_path"] = project_path
283290
output["clone_uri"] = git_url
291+
output["issues_path"] = output_dir
284292
return output
285293

294+
def clean_up(output):
295+
project_path = output.get("project_path", None)
296+
if project_path and os.path.isdir(project_path):
297+
shutil.rmtree(output["project_path"])
298+
issues_path = output.get("issues_path", None)
299+
if issues_path and os.path.isdir(issues_path):
300+
shutil.rmtree(output["issues_path"])
301+
286302
if __name__ == "__main__":
287303
main()

0 commit comments

Comments
 (0)