Skip to content

Commit 34309b3

Browse files
committed
fix(analyzer): deduplicate PE4 findings per line to avoid double-reporting
A line such as `docker.DockerClient(base_url="unix:///var/run/docker.sock")` matched both the `DockerClient(` and `/var/run/docker.sock` PE4 patterns, producing two findings for the same vulnerability on the same line. Collect PE4 candidates in a dict keyed by line number, keeping only the highest-confidence match, then extend the findings list once. Adds a regression test that asserts exactly one PE4 finding on such a combined line. Addresses nit raised in review of PR NVIDIA#189. Signed-off-by: CharmingGroot <hogeun.choi@vicsnc.com> Signed-off-by: CharmingGroot <ohyes9711@gmail.com>
1 parent b19872d commit 34309b3

2 files changed

Lines changed: 33 additions & 16 deletions

File tree

src/skillspector/nodes/analyzers/static_patterns_privilege_escalation.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,24 +162,28 @@ def loc(ln: int) -> Location:
162162
matched_text=match.group(0)[:200],
163163
)
164164
)
165+
# Collect best-confidence PE4 finding per line to avoid double-counting lines
166+
# that match multiple patterns (e.g. DockerClient(base_url=".../docker.sock")).
167+
pe4_best: dict[int, AnalyzerFinding] = {}
165168
for pattern, confidence in PE4_PATTERNS:
166169
for match in re.finditer(pattern, content, re.IGNORECASE | re.MULTILINE):
167170
line_num = get_line_number(content, match.start())
168171
context = get_context(content, match.start())
169172
if _is_documentation_example(context, file_type):
170173
continue
171-
findings.append(
172-
AnalyzerFinding(
173-
rule_id="PE4",
174-
message="Docker Socket Access",
175-
severity=Severity.HIGH,
176-
location=loc(line_num),
177-
confidence=confidence,
178-
tags=tag,
179-
context=context,
180-
matched_text=match.group(0)[:200],
181-
)
174+
if line_num in pe4_best and pe4_best[line_num].confidence >= confidence:
175+
continue
176+
pe4_best[line_num] = AnalyzerFinding(
177+
rule_id="PE4",
178+
message="Docker Socket Access",
179+
severity=Severity.HIGH,
180+
location=loc(line_num),
181+
confidence=confidence,
182+
tags=tag,
183+
context=context,
184+
matched_text=match.group(0)[:200],
182185
)
186+
findings.extend(pe4_best.values())
183187
return findings
184188

185189

tests/nodes/analyzers/test_static_patterns.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,23 @@ def test_pe4_docker_sock_path_produces_finding(self):
354354
assert pe4[0].context is not None
355355
assert pe4[0].matched_text is not None
356356

357+
def test_pe4_combined_line_produces_exactly_one_finding(self):
358+
"""A line matching multiple PE4 patterns must produce exactly one PE4 finding."""
359+
state = {
360+
"components": ["skill.py"],
361+
"file_cache": {
362+
"skill.py": 'client = docker.DockerClient(base_url="unix:///var/run/docker.sock")\n',
363+
},
364+
}
365+
findings = static_runner.run_static_patterns(state, [privilege_escalation_module])
366+
pe4 = [f for f in findings if f.rule_id == "PE4"]
367+
assert len(pe4) == 1, (
368+
f"Expected 1 PE4 finding, got {len(pe4)}: {[f.matched_text for f in pe4]}"
369+
)
370+
assert (
371+
pe4[0].confidence == 0.9
372+
) # /var/run/docker.sock has higher confidence than DockerClient(
373+
357374
def test_pe4_docker_from_env_produces_finding(self):
358375
"""docker.from_env() yields PE4 (HIGH)."""
359376
state = {
@@ -406,11 +423,7 @@ def test_pe4_documentation_example_not_flagged(self):
406423
"components": ["SKILL.md"],
407424
"file_cache": {
408425
"SKILL.md": (
409-
"# Docker SDK\n\n"
410-
"For example:\n"
411-
"```python\n"
412-
"client = docker.from_env()\n"
413-
"```\n"
426+
"# Docker SDK\n\nFor example:\n```python\nclient = docker.from_env()\n```\n"
414427
),
415428
},
416429
}

0 commit comments

Comments
 (0)