Skip to content

Commit d105bf4

Browse files
CM-23340 - Skip on collect node_modules folder for SCA npm (#119)
* added is_file_relevant for sca * added tests * emptied test data files * fixed according to windows test failures * naming refactoring * added is_file_relevant_for_sca_scan tests * removed old tests * add type hints; remove unused imports --------- Co-authored-by: Ilya Siamionau <ilya.siamionau@cycode.com>
1 parent d071284 commit d105bf4

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

cycode/cli/code_scanner.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,14 @@ def _get_package_name(detection) -> str:
730730
return f'{package_name}@{package_version}'
731731

732732

733+
def _is_file_relevant_for_sca_scan(filename: str) -> bool:
734+
if any([sca_excluded_path in filename for sca_excluded_path in SCA_EXCLUDED_PATHS]):
735+
logger.debug("file is irrelevant because it is from node_modules's inner path, %s",
736+
{'filename': filename})
737+
return False
738+
return True
739+
740+
733741
def _is_relevant_file_to_scan(scan_type: str, filename: str) -> bool:
734742
if _is_subpath_of_cycode_configuration_folder(filename):
735743
logger.debug("file is irrelevant because it is in cycode configuration directory, %s",
@@ -755,6 +763,10 @@ def _is_relevant_file_to_scan(scan_type: str, filename: str) -> bool:
755763
logger.debug("file is irrelevant because its exceeded max size limit, %s",
756764
{'filename': filename})
757765
return False
766+
767+
if scan_type == SCA_SCAN_TYPE and not _is_file_relevant_for_sca_scan(filename):
768+
return False
769+
758770
return True
759771

760772

@@ -814,8 +826,8 @@ def _does_document_exceed_max_size_limit(content: str) -> bool:
814826

815827
def _is_subpath_of_cycode_configuration_folder(filename: str) -> bool:
816828
return is_sub_path(configuration_manager.global_config_file_manager.get_config_directory_path(), filename) \
817-
or is_sub_path(configuration_manager.local_config_file_manager.get_config_directory_path(), filename) \
818-
or filename.endswith(ConfigFileManager.get_config_file_route())
829+
or is_sub_path(configuration_manager.local_config_file_manager.get_config_directory_path(), filename) \
830+
or filename.endswith(ConfigFileManager.get_config_file_route())
819831

820832

821833
def _handle_exception(context: click.Context, e: Exception):

cycode/cli/consts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
'pipfile', 'pipfile.lock', 'requirements.txt', 'setup.py'
3131
]
3232

33+
SCA_EXCLUDED_PATHS = [
34+
'node_modules'
35+
]
36+
3337
PROJECT_FILES_BY_ECOSYSTEM_MAP = {
3438
"crates": ["Cargo.lock", "Cargo.toml"],
3539
"composer": ["composer.json", "composer.lock"],

tests/cli/test_code_scanner.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import os
2+
13
import click
24
import pytest
35
from click import ClickException
46
from git import InvalidGitRepositoryError
57
from requests import Response
68

7-
from cycode.cli.code_scanner import _handle_exception # noqa
9+
from cycode.cli.code_scanner import _handle_exception, _is_file_relevant_for_sca_scan, exclude_irrelevant_files # noqa
810
from cycode.cli.exceptions import custom_exceptions
911

1012

@@ -58,3 +60,14 @@ def mock_secho(msg, *_, **__):
5860
with ctx:
5961
with pytest.raises(ClickException):
6062
_handle_exception(ctx, ValueError('test'))
63+
64+
65+
def test_is_file_relevant_for_sca_scan():
66+
path = os.path.join('some_package', 'node_modules', 'package.json')
67+
assert _is_file_relevant_for_sca_scan(path) is False
68+
path = os.path.join('some_package', 'node_modules', 'package.lock')
69+
assert _is_file_relevant_for_sca_scan(path) is False
70+
path = os.path.join('some_package', 'package.json')
71+
assert _is_file_relevant_for_sca_scan(path) is True
72+
path = os.path.join('some_package', 'package.lock')
73+
assert _is_file_relevant_for_sca_scan(path) is True

0 commit comments

Comments
 (0)