Skip to content

Commit 55e948c

Browse files
committed
fix: include custom rules in all-language scans
1 parent b5eafcf commit 55e948c

2 files changed

Lines changed: 59 additions & 9 deletions

File tree

socket_basics/core/connector/opengrep/__init__.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ def scan(self) -> Dict[str, Any]:
6161
logger.info('No scan targets to analyze (scoped scan matched no existing files); skipping OpenGrep')
6262
return {}
6363

64+
# Locate bundled rules directory for fallback and all-language expansion.
65+
module_dir = Path(__file__).resolve().parents[3]
66+
bundled_rules_dir = module_dir / 'rules'
67+
rules_dir = self.config.get('opengrep_rules_dir') or (str(bundled_rules_dir) if bundled_rules_dir.exists() else None)
68+
if not rules_dir:
69+
logger.error('No rules directory found')
70+
return {}
71+
72+
if not rule_files and self.config.get('all_languages_enabled', False):
73+
try:
74+
rule_files = [
75+
p.name
76+
for p in Path(rules_dir).glob('*.yml')
77+
if p.name != 'tests.yml'
78+
]
79+
logger.info("Expanded all-languages scan to rule files: %s", rule_files)
80+
except Exception:
81+
logger.debug('Failed expanding all-languages into rule files', exc_info=True)
82+
rule_files = []
83+
6484
# Check if custom rules mode is enabled
6585
custom_rules_path = self.config.get_custom_rules_path()
6686
custom_rule_files: Dict[str, Path] = {}
@@ -80,14 +100,6 @@ def scan(self) -> Dict[str, Any]:
80100
logger.error(f"Failed to build custom rule files: {e}", exc_info=True)
81101
custom_rule_files = {}
82102

83-
# Locate bundled rules directory for fallback
84-
module_dir = Path(__file__).resolve().parents[3]
85-
bundled_rules_dir = module_dir / 'rules'
86-
rules_dir = self.config.get('opengrep_rules_dir') or (str(bundled_rules_dir) if bundled_rules_dir.exists() else None)
87-
if not rules_dir:
88-
logger.error('No rules directory found')
89-
return {}
90-
91103
# Read filtered rule definitions if available
92104
try:
93105
filtered = self.config.build_filtered_opengrep_rules() or {}
@@ -788,4 +800,3 @@ def generate_notifications(self, components: List[Dict[str, Any]]) -> Dict[str,
788800
notifications_by_notifier['webhook'] = webhook.format_notifications(groups)
789801

790802
return notifications_by_notifier
791-

tests/test_opengrep_custom_rules.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,45 @@ def test_scan_uses_custom_rule_file_when_available(tmp_path, monkeypatch):
7676
assert str(bundled_rules_dir / "javascript_typescript.yml") not in cmd_str
7777

7878

79+
def test_all_languages_custom_rules_without_individual_language_flags(tmp_path, monkeypatch):
80+
workspace = tmp_path / "workspace"
81+
workspace.mkdir(parents=True, exist_ok=True)
82+
83+
custom_rules_file = workspace / ".socket" / "rules" / "org-rules.yml"
84+
_write_custom_rules_file(custom_rules_file, ["org.no-eval"])
85+
86+
bundled_rules_dir = tmp_path / "bundled-rules"
87+
bundled_js_file = bundled_rules_dir / "javascript_typescript.yml"
88+
bundled_python_file = bundled_rules_dir / "python.yml"
89+
_write_rule_file(bundled_js_file, ["js-default-rule"])
90+
_write_rule_file(bundled_python_file, ["py-default-rule"])
91+
92+
config = Config(
93+
{
94+
"workspace": str(workspace),
95+
"output_dir": str(workspace),
96+
"all_languages_enabled": True,
97+
"use_custom_sast_rules": True,
98+
"custom_sast_rule_path": ".socket/rules",
99+
"opengrep_rules_dir": str(bundled_rules_dir),
100+
"all_rules_enabled": False,
101+
"verbose": False,
102+
}
103+
)
104+
scanner = OpenGrepScanner(config)
105+
scanner._convert_to_socket_facts = lambda _: {"components": []}
106+
scanner.generate_notifications = lambda _: {}
107+
108+
captured_cmd: list[str] = []
109+
_mock_subprocess_run(monkeypatch, captured_cmd)
110+
scanner.scan()
111+
112+
cmd_str = " ".join(captured_cmd)
113+
assert "socket_custom_rules_" in cmd_str
114+
assert str(bundled_js_file) not in cmd_str
115+
assert str(bundled_python_file) in cmd_str
116+
117+
79118
def test_scan_falls_back_to_bundled_file_when_custom_missing(tmp_path, monkeypatch):
80119
workspace = tmp_path / "workspace"
81120
workspace.mkdir(parents=True, exist_ok=True)

0 commit comments

Comments
 (0)