Skip to content

Commit cea46be

Browse files
fix(general): escape default ignored directories (#6738)
* Escape `ignored_directories` * Implement windows compatability in `re_dir` * refactor `EXCLUDED_PATHS` * Add test for `re_dir` * Add test: `TestBaseRunner::tests_re_dir_test_pattern` * typo --------- Co-authored-by: pazbec <paz8097@gmail.com>
1 parent b479004 commit cea46be

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

checkov/common/runners/base_runner.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@ def strtobool(val: str) -> int:
4444
raise ValueError("invalid boolean value %r for environment variable CKV_IGNORE_HIDDEN_DIRECTORIES" % (val,))
4545

4646

47-
IGNORED_DIRECTORIES_ENV = os.getenv("CKV_IGNORED_DIRECTORIES", "node_modules,.terraform,.serverless")
47+
def re_dir(path: str) -> str:
48+
"""Compile a regex pattern that matches paths containing the given directory at any level."""
49+
sep = re.escape(os.sep) # windows compatibility
50+
return rf"(^|.*{sep}){re.escape(path)}($|{sep}.*)"
51+
52+
53+
IGNORED_DIRECTORIES_ENV = os.getenv(
54+
"CKV_IGNORED_DIRECTORIES",
55+
",".join(re_dir(p) for p in ["node_modules", ".terraform", ".serverless"])
56+
)
4857
IGNORE_HIDDEN_DIRECTORY_ENV = strtobool(os.getenv("CKV_IGNORE_HIDDEN_DIRECTORIES", "True"))
4958

5059
ignored_directories = IGNORED_DIRECTORIES_ENV.split(",")

checkov/secrets/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
import re
55
from collections.abc import Iterable
66

7-
from checkov.common.runners.base_runner import ignored_directories, safe_remove
7+
from checkov.common.runners.base_runner import ignored_directories, safe_remove, re_dir
88
from checkov.common.util.consts import DEFAULT_EXTERNAL_MODULES_DIR
99

10-
EXCLUDED_PATHS = [*ignored_directories, DEFAULT_EXTERNAL_MODULES_DIR, ".idea", ".git", "venv"]
10+
EXCLUDED_PATHS = [
11+
*ignored_directories,
12+
re_dir(DEFAULT_EXTERNAL_MODULES_DIR),
13+
re_dir(".idea"),
14+
re_dir(".git"),
15+
re_dir("venv"),
16+
]
1117

1218

1319
def filter_excluded_paths(

tests/common/runners/test_base_runner.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
11
import os
2+
import re
23
import unittest
34
from typing import Optional, List
45

56
from checkov.common.output.report import Report
6-
from checkov.common.runners.base_runner import filter_ignored_paths, BaseRunner
7+
from checkov.common.runners.base_runner import filter_ignored_paths, BaseRunner, re_dir
78
from checkov.runner_filter import RunnerFilter
89

910

1011
class TestBaseRunner(unittest.TestCase):
1112

13+
def test_re_dir(self):
14+
sep = '\\' if os.name == 'nt' else '/'
15+
# add regex prefix and suffix to the (unmodified) directory name
16+
self.assertEqual(re_dir('dir'), fr'(^|.*{sep})dir($|{sep}.*)')
17+
# escape the directory name (but leave the os separator unaltered)
18+
self.assertEqual(re_dir('.dir1/.dir2'), fr'(^|.*{sep})\.dir1/\.dir2($|{sep}.*)')
19+
20+
def tests_re_dir_pattern(self):
21+
dir_name_to_ignore = ".hidden"
22+
dir_name_to_ignore_re = re.compile(re_dir(dir_name_to_ignore))
23+
paths_to_ignore = [
24+
".hidden",
25+
"/.hidden",
26+
"/path/.hidden",
27+
"path/.hidden",
28+
".hidden/path",
29+
"path/.hidden/path",
30+
"path/.hidden/path/",
31+
]
32+
paths_to_keep = [
33+
".hidden1",
34+
"not.hidden",
35+
"nothidden",
36+
"not.hidden/path",
37+
"path/not.hidden",
38+
"also/nothidden",
39+
"hidden/not",
40+
"also/hidden/not",
41+
]
42+
self.assertTrue(all(dir_name_to_ignore_re.match(p) for p in paths_to_ignore))
43+
self.assertFalse(any(dir_name_to_ignore_re.match(p) for p in paths_to_keep))
44+
1245
def test_filter_ignored_directories_regex_legacy(self):
1346
d_names = ['bin', 'integration_tests', 'tests', 'docs', '.github', 'checkov', 'venv', '.git', 'kubernetes', '.idea']
1447
expected = ['bin', 'docs', 'checkov', 'venv', 'kubernetes']

0 commit comments

Comments
 (0)