From 4f7b33f723c657650430472d0530678ce7a1ec13 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 10:57:19 +0800 Subject: [PATCH 1/2] fix(reporting): preserve SARIF findings on cyclic symbolic links Signed-off-by: Afloat --- CHANGELOG.md | 2 + .../reporting/sarif_reporter.py | 21 ++++++--- tests/reporting/test_sarif_symlink_loops.py | 47 +++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 tests/reporting/test_sarif_symlink_loops.py diff --git a/CHANGELOG.md b/CHANGELOG.md index f3a4fafc6..167b505ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -438,6 +438,8 @@ All notable changes to SkillEvaluator are documented in this file. ### Fixed +- SARIF reporting retains findings on cyclic symbolic links on Python 3.12, resolving + artifact locations as far as possible instead of aborting the entire report. - Direct Tier 1 validates the target, selected checks and LLM configuration before preparing scanners, so configuration errors do not trigger downloads or stdout noise. - Documentation-only CI follows imports through collected test modules and diff --git a/src/skillevaluator/reporting/sarif_reporter.py b/src/skillevaluator/reporting/sarif_reporter.py index d0d8c79b5..9b6e1d8eb 100644 --- a/src/skillevaluator/reporting/sarif_reporter.py +++ b/src/skillevaluator/reporting/sarif_reporter.py @@ -90,14 +90,23 @@ def _positive_start_line(line_number: Any) -> int | None: return None +def _resolve_for_reporting(path: Path) -> Path: + """Resolve accessible path components without failing on a cyclic link. + + Python 3.12's non-strict ``Path.resolve()`` still raises for link loops. + A report must be able to identify those links as the finding's artifact. + """ + return Path(os.path.realpath(path)) + + def _resolve_artifact_path(file_path: str, scan_root: Path | None) -> Path: """Resolve a validator file path against the scanned skill directory.""" normalized = file_path.replace("\\", "/") path = Path(normalized) if scan_root is not None and not path.is_absolute(): - return (scan_root / path).resolve() + return _resolve_for_reporting(scan_root / path) if path.is_absolute(): - return path.resolve() + return _resolve_for_reporting(path) return path @@ -147,7 +156,7 @@ def _unresolved_artifact_path(file_path: str, scan_root: Path | None) -> Path | if not path.is_absolute(): if scan_root is None: return None - path = scan_root.resolve() / path + path = _resolve_for_reporting(scan_root) / path return Path(os.path.normpath(path)) @@ -163,8 +172,8 @@ def _workspace_relative_path( target is outside it keeps the path of the link, which is what the finding is about. """ - root = workspace_root.resolve() - candidates = [path.resolve()] + root = _resolve_for_reporting(workspace_root) + candidates = [_resolve_for_reporting(path)] unresolved = _unresolved_artifact_path(file_path, scan_root) if unresolved is not None: candidates.append(unresolved) @@ -193,7 +202,7 @@ def _normalize_artifact_uri( relative = _workspace_relative_path(path, file_path, workspace_root, scan_root) if relative is not None: return _relative_reference(relative.as_posix()) - return _file_uri(path.resolve()) + return _file_uri(_resolve_for_reporting(path)) if path.is_absolute(): return _file_uri(path) return _relative_reference(path.as_posix()) diff --git a/tests/reporting/test_sarif_symlink_loops.py b/tests/reporting/test_sarif_symlink_loops.py new file mode 100644 index 000000000..8a54b1a65 --- /dev/null +++ b/tests/reporting/test_sarif_symlink_loops.py @@ -0,0 +1,47 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Findings about cyclic links must remain exportable to SARIF.""" + +import json +from pathlib import Path + +import pytest + +from skillevaluator.models import Finding, Severity, ValidationResult +from skillevaluator.reporting import SARIFReporter + + +@pytest.mark.parametrize("relative", [False, True]) +@pytest.mark.parametrize("two_links", [False, True]) +@pytest.mark.parametrize("with_workspace", [False, True]) +def test_sarif_preserves_a_finding_on_a_symlink_loop( + tmp_path: Path, relative: bool, two_links: bool, with_workspace: bool +) -> None: + skill = tmp_path / "skills" / "demo" + skill.mkdir(parents=True) + link = skill / "loop.md" + try: + link.symlink_to("other.md" if two_links else "loop.md") + if two_links: + (skill / "other.md").symlink_to("loop.md") + except OSError: + pytest.skip("symlinks are not available") + result = ValidationResult(validator_name="security") + result.add_finding( + Finding( + category="SECURITY", + severity=Severity.HIGH, + check_name="unsafe-link", + message="The resource is a cyclic symbolic link", + file_path="loop.md" if relative else str(link), + ) + ) + + output = SARIFReporter(workspace_root=tmp_path if with_workspace else None, scan_root=skill).render_all([result]) + + findings = json.loads(output)["runs"][0]["results"] + assert len(findings) == 1 + expected = "skills/demo/loop.md" if with_workspace else link.as_uri() + assert findings[0]["locations"][0]["physicalLocation"]["artifactLocation"]["uri"] == expected + assert findings[0]["message"]["text"] == "The resource is a cyclic symbolic link" From 6ef9bb60b7654f05027546ec74ce4fe28c78650b Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 11:42:42 +0800 Subject: [PATCH 2/2] fix(reporting): preserve artifact names for expanding symlink loops Signed-off-by: Afloat --- .../reporting/sarif_reporter.py | 13 +++++++--- tests/reporting/test_sarif_symlink_loops.py | 24 +++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/skillevaluator/reporting/sarif_reporter.py b/src/skillevaluator/reporting/sarif_reporter.py index 9b6e1d8eb..233cd811b 100644 --- a/src/skillevaluator/reporting/sarif_reporter.py +++ b/src/skillevaluator/reporting/sarif_reporter.py @@ -10,6 +10,7 @@ from __future__ import annotations +import errno import json import os import re @@ -91,12 +92,18 @@ def _positive_start_line(line_number: Any) -> int | None: def _resolve_for_reporting(path: Path) -> Path: - """Resolve accessible path components without failing on a cyclic link. + """Resolve accessible paths while retaining the name of a cyclic artifact. Python 3.12's non-strict ``Path.resolve()`` still raises for link loops. - A report must be able to identify those links as the finding's artifact. + Non-strict ``realpath`` can instead expand their suffix on each call, so + detect loops strictly before falling back for missing path components. """ - return Path(os.path.realpath(path)) + try: + return Path(os.path.realpath(path, strict=True)) + except OSError as exc: + if exc.errno == errno.ELOOP or getattr(exc, "winerror", None) == 1921: + return path.absolute() + return Path(os.path.realpath(path)) def _resolve_artifact_path(file_path: str, scan_root: Path | None) -> Path: diff --git a/tests/reporting/test_sarif_symlink_loops.py b/tests/reporting/test_sarif_symlink_loops.py index 8a54b1a65..6b6f5fc82 100644 --- a/tests/reporting/test_sarif_symlink_loops.py +++ b/tests/reporting/test_sarif_symlink_loops.py @@ -3,6 +3,7 @@ """Findings about cyclic links must remain exportable to SARIF.""" +import errno import json from pathlib import Path @@ -10,19 +11,22 @@ from skillevaluator.models import Finding, Severity, ValidationResult from skillevaluator.reporting import SARIFReporter +from skillevaluator.reporting.sarif_reporter import _resolve_for_reporting @pytest.mark.parametrize("relative", [False, True]) @pytest.mark.parametrize("two_links", [False, True]) @pytest.mark.parametrize("with_workspace", [False, True]) +@pytest.mark.parametrize("expanding", [False, True]) def test_sarif_preserves_a_finding_on_a_symlink_loop( - tmp_path: Path, relative: bool, two_links: bool, with_workspace: bool + tmp_path: Path, relative: bool, two_links: bool, with_workspace: bool, expanding: bool ) -> None: skill = tmp_path / "skills" / "demo" skill.mkdir(parents=True) link = skill / "loop.md" try: - link.symlink_to("other.md" if two_links else "loop.md") + target = "other.md" if two_links else "loop.md" + link.symlink_to(f"{target}/child" if expanding else target) if two_links: (skill / "other.md").symlink_to("loop.md") except OSError: @@ -45,3 +49,19 @@ def test_sarif_preserves_a_finding_on_a_symlink_loop( expected = "skills/demo/loop.md" if with_workspace else link.as_uri() assert findings[0]["locations"][0]["physicalLocation"]["artifactLocation"]["uri"] == expected assert findings[0]["message"]["text"] == "The resource is a cyclic symbolic link" + + +def test_windows_loop_error_preserves_the_named_artifact(monkeypatch, tmp_path: Path) -> None: + path = tmp_path / "loop.md" / ".." / "named.md" + error = OSError(errno.EINVAL, "The name of the file cannot be resolved by the system") + error.winerror = 1921 + + def realpath(value, *, strict=False): + if strict: + raise error + return f"{value}/child" + + monkeypatch.setattr("skillevaluator.reporting.sarif_reporter.os.path.realpath", realpath) + + assert _resolve_for_reporting(path) == path.absolute() + assert _resolve_for_reporting(_resolve_for_reporting(path)) == path.absolute()