Files
d0b446f17a fix(security): preserve exact finding identity (#409)
* fix(security): preserve exact finding identity

Signed-off-by: Nir Paz <npaz@nvidia.com>

* test(security): isolate prose finding coverage

Signed-off-by: Nir Paz <npaz@nvidia.com>

* fix(security): bound finding evidence surfaces

Signed-off-by: Nir Paz <npaz@nvidia.com>

* Preserve finding integrity

Signed-off-by: Nir Paz <npaz@nvidia.com>

* fix(security): preserve concrete YARA rule identity

Include the YARA namespace and rule name in complete-match fingerprints so distinct same-category rules matching identical bytes cannot collapse during finding deduplication. Add regression coverage for both identity dimensions.

Signed-off-by: Mohit Gupta <mohgupta@nvidia.com>

* chore(security): retain initial YARA fingerprint schema

Keep the domain separator at v1 because the namespace and rule identity changes are part of the same unmerged fingerprint introduction.

Signed-off-by: Mohit Gupta <mohgupta@nvidia.com>

* fix(security): preserve exact occurrence spans

Carry character-precise occurrence coordinates through analyzer, projection, compaction, and meta-analysis boundaries without contaminating canonical content fingerprints. Reuse indexed source maps and UTF-8-aware AST slicing to keep high-volume scans bounded.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>

* test(security): assert yara blocking score

Verify distinct YARA rules with identical matched bytes retain both rule messages and the aggregate 67/HIGH/DO_NOT_INSTALL verdict after compaction.

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>

* fix(security): preserve projected occurrences and context

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>

* fix(security): handle unavailable AST columns

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>

* fix(security): deduplicate projected slice overlaps

Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>

---------

Signed-off-by: Nir Paz <npaz@nvidia.com>
Signed-off-by: Mohit Gupta <mohgupta@nvidia.com>
Signed-off-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-authored-by: Mohit Gupta <mohgupta@nvidia.com>
Co-authored-by: Narendran Raghavan <nraghavan@nvidia.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-09-15 21:00:23 -07:00

83 lines
2.7 KiB
Python

# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""Contracts for finding identity."""
from skillspector.models import Finding
from skillspector.state import merge_findings_by_id
def test_finding_has_unique_instance_id_without_changing_rule_id() -> None:
"""Each logical finding has an opaque identity while rule IDs stay compatible."""
first = Finding(rule_id="P1", message="first")
second = Finding(rule_id="P1", message="second")
assert first.finding_id.startswith("finding-")
assert second.finding_id.startswith("finding-")
assert first.finding_id != second.finding_id
assert first.to_dict()["id"] == "P1"
assert first.to_dict()["finding_id"] == first.finding_id
def test_finding_reducer_replaces_same_id_without_duplicating_payload() -> None:
"""An enriched finding replaces its canonical instance in reducer order."""
original = Finding(rule_id="P1", message="raw", finding_id="finding-a")
enriched = Finding(
rule_id="P1",
message="confirmed",
finding_id="finding-a",
explanation="confirmed by meta-analysis",
)
merged = merge_findings_by_id([original], [enriched])
assert len(merged) == 1
assert merged[0].finding_id == "finding-a"
assert merged[0].message == "confirmed"
def test_finding_serializes_structured_evidence() -> None:
finding = Finding(
rule_id="SC9",
message="concealed executable",
evidence={"outer_path": "archive.docx", "nested_path": "payload.sh"},
)
assert finding.to_dict()["evidence"] == {
"outer_path": "archive.docx",
"nested_path": "payload.sh",
}
def test_optional_columns_preserve_legacy_shape_and_serialize_exact_locations() -> None:
legacy = Finding(rule_id="P1", message="legacy", file="SKILL.md", start_line=2)
precise = Finding(
rule_id="P1",
message="precise",
file="SKILL.md",
start_line=2,
end_line=2,
start_column=4,
end_column=12,
)
legacy_data = legacy.to_dict()
assert legacy_data["location"] == {
"file": "SKILL.md",
"start_line": 2,
"end_line": None,
}
assert "start_column" not in legacy_data["occurrences"][0]
assert "end_column" not in legacy_data["occurrences"][0]
precise_data = precise.to_dict()
assert precise_data["location"] == {
"file": "SKILL.md",
"start_line": 2,
"end_line": 2,
"start_column": 4,
"end_column": 12,
}
assert precise_data["occurrences"][0]["start_column"] == 4
assert precise_data["occurrences"][0]["end_column"] == 12