|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Tests for SARIF security-severity on results and rule descriptors.""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from skillspector.models import Finding |
| 23 | +from skillspector.nodes.report import _build_sarif, _severity_to_security_severity |
| 24 | +from skillspector.sarif_models import validate_sarif_report |
| 25 | +from skillspector.suppression import SuppressedFinding |
| 26 | + |
| 27 | + |
| 28 | +def _make_finding(rule_id: str = "P5", severity: str = "CRITICAL", **kwargs) -> Finding: |
| 29 | + defaults = { |
| 30 | + "message": "Malicious instruction", |
| 31 | + "confidence": 0.9, |
| 32 | + "file": "SKILL.md", |
| 33 | + "start_line": 1, |
| 34 | + "end_line": 1, |
| 35 | + "remediation": "Remove the instruction", |
| 36 | + "tags": ["malicious"], |
| 37 | + "context": "context", |
| 38 | + "matched_text": "match", |
| 39 | + "category": "prompt_injection", |
| 40 | + "pattern": "P5", |
| 41 | + "finding": "snippet", |
| 42 | + "explanation": "explain", |
| 43 | + "code_snippet": "code", |
| 44 | + "intent": None, |
| 45 | + } |
| 46 | + defaults.update(kwargs) |
| 47 | + return Finding(rule_id=rule_id, severity=severity, **defaults) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + ("severity", "expected"), |
| 52 | + [ |
| 53 | + ("CRITICAL", "9.5"), |
| 54 | + ("HIGH", "8.0"), |
| 55 | + ("MEDIUM", "5.5"), |
| 56 | + ("LOW", "3.0"), |
| 57 | + ("critical", "9.5"), |
| 58 | + ("UNKNOWN", None), |
| 59 | + ("", None), |
| 60 | + ], |
| 61 | +) |
| 62 | +def test_severity_to_security_severity_mapping(severity: str, expected: str | None) -> None: |
| 63 | + """Each known severity maps inside its GitHub code scanning band.""" |
| 64 | + assert _severity_to_security_severity(severity) == expected |
| 65 | + |
| 66 | + |
| 67 | +def test_result_carries_security_severity() -> None: |
| 68 | + """A CRITICAL result keeps level error and gains security-severity 9.5.""" |
| 69 | + sarif = _build_sarif([_make_finding(severity="CRITICAL")]) |
| 70 | + result = sarif["runs"][0]["results"][0] |
| 71 | + assert result["level"] == "error" |
| 72 | + assert result["properties"]["security-severity"] == "9.5" |
| 73 | + assert result["properties"]["severity"] == "CRITICAL" |
| 74 | + |
| 75 | + |
| 76 | +def test_high_and_critical_distinguishable() -> None: |
| 77 | + """HIGH and CRITICAL share level error but carry different security-severity.""" |
| 78 | + sarif = _build_sarif( |
| 79 | + [ |
| 80 | + _make_finding(rule_id="P5", severity="CRITICAL"), |
| 81 | + _make_finding(rule_id="E2", severity="HIGH"), |
| 82 | + ] |
| 83 | + ) |
| 84 | + by_rule = {r["ruleId"]: r for r in sarif["runs"][0]["results"]} |
| 85 | + assert by_rule["P5"]["level"] == by_rule["E2"]["level"] == "error" |
| 86 | + assert by_rule["P5"]["properties"]["security-severity"] == "9.5" |
| 87 | + assert by_rule["E2"]["properties"]["security-severity"] == "8.0" |
| 88 | + |
| 89 | + |
| 90 | +def test_unknown_severity_omits_security_severity() -> None: |
| 91 | + """Unknown severities emit no security-severity rather than a wrong score.""" |
| 92 | + sarif = _build_sarif([_make_finding(severity="UNKNOWN")]) |
| 93 | + result = sarif["runs"][0]["results"][0] |
| 94 | + assert "security-severity" not in result["properties"] |
| 95 | + |
| 96 | + |
| 97 | +def test_rule_descriptor_carries_security_severity_for_single_severity_rule() -> None: |
| 98 | + """A rule with one severity across all findings is annotated.""" |
| 99 | + sarif = _build_sarif( |
| 100 | + [ |
| 101 | + _make_finding(rule_id="P5", severity="HIGH"), |
| 102 | + _make_finding(rule_id="P5", severity="HIGH", file="other.py"), |
| 103 | + ] |
| 104 | + ) |
| 105 | + rules = {r["id"]: r for r in sarif["runs"][0]["tool"]["driver"]["rules"]} |
| 106 | + assert rules["P5"]["properties"]["security-severity"] == "8.0" |
| 107 | + |
| 108 | + |
| 109 | +def test_rule_descriptor_omits_security_severity_for_mixed_severity_rule() -> None: |
| 110 | + """A rule with mixed severities stays unannotated instead of mislabeling.""" |
| 111 | + sarif = _build_sarif( |
| 112 | + [ |
| 113 | + _make_finding(rule_id="P5", severity="CRITICAL"), |
| 114 | + _make_finding(rule_id="P5", severity="LOW"), |
| 115 | + ] |
| 116 | + ) |
| 117 | + rules = {r["id"]: r for r in sarif["runs"][0]["tool"]["driver"]["rules"]} |
| 118 | + assert "properties" not in rules["P5"] |
| 119 | + |
| 120 | + |
| 121 | +def test_suppressed_finding_carries_security_severity() -> None: |
| 122 | + """Baseline-suppressed findings keep the annotation for audit consumers.""" |
| 123 | + finding = _make_finding(severity="MEDIUM") |
| 124 | + sarif = _build_sarif([], suppressed=[SuppressedFinding(finding=finding, reason="baseline")]) |
| 125 | + result = sarif["runs"][0]["results"][0] |
| 126 | + assert result["suppressions"][0]["kind"] == "external" |
| 127 | + assert result["properties"]["security-severity"] == "5.5" |
| 128 | + |
| 129 | + |
| 130 | +def test_security_severity_report_validates() -> None: |
| 131 | + """Annotated output still satisfies the SARIF 2.1.0 model.""" |
| 132 | + sarif = _build_sarif( |
| 133 | + [ |
| 134 | + _make_finding(rule_id="P5", severity="CRITICAL"), |
| 135 | + _make_finding(rule_id="E2", severity="HIGH"), |
| 136 | + _make_finding(rule_id="M1", severity="MEDIUM"), |
| 137 | + _make_finding(rule_id="L1", severity="LOW"), |
| 138 | + ] |
| 139 | + ) |
| 140 | + validate_sarif_report(sarif) |
| 141 | + assert sarif["runs"][0]["results"][0]["properties"]["security-severity"] == "9.5" |
0 commit comments