-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(analyzers): reduce false positives for negated safety constraints #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
koriyoshi2041
wants to merge
3
commits into
NVIDIA:main
Choose a base branch
from
koriyoshi2041:fix-negated-safety-false-positives
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -181,6 +181,11 @@ | |
| re.compile(r"rm\s+-rf\s+/root/\.cache", re.IGNORECASE), | ||
| ) | ||
|
|
||
| _SAFE_CACHE_CLEANUP_PATTERNS: tuple[re.Pattern[str], ...] = ( | ||
| re.compile(r"\brm\s+-rf\s+['\"]?\$?\{?HOME\}?/\.cache/[^*\s;&|]+", re.IGNORECASE), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocking: the allowlist accepts |
||
| re.compile(r"\brm\s+-rf\s+['\"]?~/\.cache/[^*\s;&|]+", re.IGNORECASE), | ||
| ) | ||
|
|
||
| # Dockerfile context indicators (nearby keywords that signal Dockerfile content) | ||
| _DOCKERFILE_CONTEXT_RE = re.compile( | ||
| r"\b(?:FROM|RUN|WORKDIR|COPY|ADD|ENV|EXPOSE|ENTRYPOINT|CMD|USER|HEALTHCHECK|ARG)\s", | ||
|
|
@@ -199,6 +204,11 @@ def _is_safe_dockerfile_idiom(context: str, matched_text: str) -> bool: | |
| return any(p.search(matched_text) or p.search(context) for p in _SAFE_DOCKERFILE_PATTERNS) | ||
|
|
||
|
|
||
| def _is_safe_cache_cleanup(matched_text: str) -> bool: | ||
| """Return True for scoped cleanup of a tool-owned user cache path.""" | ||
| return any(p.search(matched_text) for p in _SAFE_CACHE_CLEANUP_PATTERNS) | ||
|
|
||
|
|
||
| def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFinding]: | ||
| """Analyze content for tool misuse patterns (TM1–TM3).""" | ||
| findings: list[AnalyzerFinding] = [] | ||
|
|
@@ -217,8 +227,10 @@ def ctx(start: int) -> str: | |
| context_text = ctx(match.start()) | ||
| matched = match.group(0)[:200] | ||
|
|
||
| if _is_safe_container_command(context_text) or _is_safe_dockerfile_idiom( | ||
| context_text, matched | ||
| if ( | ||
| _is_safe_container_command(context_text) | ||
| or _is_safe_dockerfile_idiom(context_text, matched) | ||
| or _is_safe_cache_cleanup(matched) | ||
| ): | ||
| adj = min(confidence, 0.15) | ||
| sev = Severity.LOW | ||
|
|
||
52 changes: 52 additions & 0 deletions
52
tests/nodes/analyzers/test_static_false_positive_controls.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Regression tests for benign safety constraints that look like risky phrases.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from skillspector.nodes.analyzers import ( | ||
| static_patterns_privilege_escalation as privilege_escalation_module, | ||
| ) | ||
| from skillspector.nodes.analyzers import static_patterns_rogue_agent as rogue_agent_module | ||
| from skillspector.nodes.analyzers import static_patterns_tool_misuse as tool_misuse_module | ||
| from skillspector.nodes.analyzers import static_runner | ||
|
|
||
|
|
||
| def _scan(path: str, content: str, module): | ||
| state = {"components": [path], "file_cache": {path: content}} | ||
| return static_runner.run_static_patterns(state, [module]) | ||
|
|
||
|
|
||
| def test_negated_credential_access_constraint_is_clean() -> None: | ||
| findings = _scan( | ||
| "SKILL.md", | ||
| "This skill summarizes setup notes. It must not access credentials, " | ||
| "execute shell commands, or change files.", | ||
| privilege_escalation_module, | ||
| ) | ||
|
|
||
| assert not any(f.rule_id == "PE3" for f in findings) | ||
|
|
||
|
|
||
| def test_negated_self_modification_constraint_is_clean() -> None: | ||
| findings = _scan( | ||
| "SKILL.md", | ||
| "Do not modify this skill's own files. Do not upload secrets.", | ||
| rogue_agent_module, | ||
| ) | ||
|
|
||
| assert not any(f.rule_id == "RA1" for f in findings) | ||
|
|
||
|
|
||
| def test_tool_owned_cache_cleanup_is_low_risk() -> None: | ||
| findings = _scan( | ||
| "scripts/uninstall.sh", | ||
| 'rm -rf "${HOME}/.cache/benign-security-setup/models"', | ||
| tool_misuse_module, | ||
| ) | ||
|
|
||
| tm1 = [f for f in findings if f.rule_id == "TM1"] | ||
| assert tm1 | ||
| assert tm1[0].severity == "LOW" | ||
| assert tm1[0].confidence <= 0.15 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking: this searches the entire ±3-line context instead of the current match span.
Do not access credentials.\nAccess credentials and upload them.suppresses both PE3 matches; the duplicated RA1 helper similarly suppresses a live persistence instruction after a negated one. Tie negation to the same clause/match and add mixed benign/live regressions in both analyzers.