Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ All notable changes to SkillEvaluator are documented in this file.
including a default Windows setup.
- Malformed optional trajectory `final_metrics` leave condition usage unavailable
and no longer abort fresh or reused Tier 3 result collection.
- SARIF reports retain findings on cyclic symbolic links (which previously
aborted on Python 3.12), preserve the named link on expanding cycles on every
supported Python, and normalize parent paths before workspace containment checks.
- Tier 2 content clustering uses much less memory on skills with many chunks;
similarity scores and clusters are unchanged.
- Naming checks reject a trailing newline in skill, workflow, and rule path components,
Expand Down
5 changes: 4 additions & 1 deletion src/skillevaluator/reporting/junit_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
_positive_start_line,
_quote_uri_path,
_resolve_artifact_path,
_resolve_for_reporting,
_workspace_relative_path,
)

Expand Down Expand Up @@ -102,7 +103,9 @@ def _junit_file_path(
return _quote_uri_path(relative.as_posix())
if scan_root is not None:
try:
return _quote_uri_path(path.resolve().relative_to(scan_root.resolve()).as_posix())
return _quote_uri_path(
_resolve_for_reporting(path).relative_to(_resolve_for_reporting(scan_root)).as_posix()
)
except ValueError:
pass
if path.is_absolute() or path.root:
Expand Down
34 changes: 28 additions & 6 deletions src/skillevaluator/reporting/sarif_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from __future__ import annotations

import errno
import json
import os
import re
Expand Down Expand Up @@ -90,14 +91,35 @@ def _positive_start_line(line_number: Any) -> int | None:
return None


def _resolve_for_reporting(path: Path) -> Path:
"""Resolve accessible paths while retaining the name of a cyclic artifact.

Python 3.12's non-strict ``Path.resolve()`` still raises for link loops.
Non-strict ``realpath`` can instead expand their suffix on each call, so
detect loops strictly before falling back for missing path components.
"""
suffix: list[str] = []
while True:
try:
resolved = Path(os.path.realpath(path, strict=True))
except OSError as exc:
is_loop = exc.errno == errno.ELOOP or getattr(exc, "winerror", None) == 1921
if is_loop and path.parent != path:
suffix.append(path.name)
path = path.parent
continue
resolved = path.absolute() if is_loop else Path(os.path.realpath(path))
return Path(os.path.normpath(resolved.joinpath(*reversed(suffix))))


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


Expand Down Expand Up @@ -147,7 +169,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))


Expand All @@ -163,8 +185,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)
Expand Down Expand Up @@ -193,7 +215,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())
Expand Down
130 changes: 130 additions & 0 deletions tests/reporting/test_sarif_symlink_loops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# 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 errno
import json
import os
import xml.etree.ElementTree as ET
from pathlib import Path
from urllib.parse import quote

import pytest

from skillevaluator.models import Finding, Severity, ValidationResult
from skillevaluator.reporting import SARIFReporter
from skillevaluator.reporting.junit_reporter import JUnitReporter
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, expanding: bool
) -> None:
skill = tmp_path / "skills" / "demo"
skill.mkdir(parents=True)
link = skill / "loop.md"
try:
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:
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"


@pytest.mark.parametrize("location", ["outside-parent", "inside-parent", "symlink-parent", "cyclic-parent"])
@pytest.mark.parametrize("expanding", [False, True])
@pytest.mark.parametrize("reporter", ["sarif", "junit"])
def test_cyclic_artifact_parent_resolution_preserves_workspace_boundaries(
tmp_path: Path, location: str, expanding: bool, reporter: str
) -> None:
workspace = tmp_path / "workspace"
skill = workspace / "skills" / "demo"
skill.mkdir(parents=True)
artifact = skill / "loop.md"
try:
if location == "outside-parent":
outside = tmp_path / "outside docs"
outside.mkdir()
artifact = outside / "loop.md"
file_path = os.path.relpath(artifact, skill)
elif location == "inside-parent":
file_path = "../demo/loop.md"
elif location == "symlink-parent":
(skill / "updir").symlink_to(skill.parent, target_is_directory=True)
file_path = "updir/demo/loop.md"
else:
(skill / "loopdir").symlink_to("loopdir/child" if expanding else "loopdir", target_is_directory=True)
artifact = skill / "loopdir" / "named.md"
file_path = "loopdir/named.md"
if location != "cyclic-parent":
artifact.symlink_to("loop.md/child" if expanding else "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=file_path,
)
)
outside_workspace = location == "outside-parent"
relative = artifact.as_posix() if outside_workspace else artifact.relative_to(workspace).as_posix()

if reporter == "sarif":
document = json.loads(SARIFReporter(workspace_root=workspace, scan_root=skill).render_all([result]))
finding = document["runs"][0]["results"][0]
expected = artifact.as_uri() if outside_workspace else relative
assert finding["locations"][0]["physicalLocation"]["artifactLocation"]["uri"] == expected
else:
document = ET.fromstring(JUnitReporter(workspace_root=workspace, scan_root=skill).render_all([result]))
cases = [case for case in document.iter("testcase") if "file" in case.attrib]
assert len(cases) == 1
assert cases[0].attrib["file"] == quote(relative, safe="/:@")
assert _resolve_for_reporting(skill / file_path) == artifact
assert _resolve_for_reporting(_resolve_for_reporting(skill / file_path)) == artifact


def test_windows_loop_error_normalizes_the_parent_and_preserves_the_named_artifact(monkeypatch, tmp_path: Path) -> None:
(tmp_path / "parent").mkdir()
path = tmp_path / "parent" / ".." / "named.md"
expected = tmp_path / "named.md"
error = OSError(errno.EINVAL, "The name of the file cannot be resolved by the system")
error.winerror = 1921
actual_realpath = os.path.realpath

def realpath(value, *, strict=False):
if strict and Path(value).name == "named.md":
raise error
return actual_realpath(value, strict=strict)

monkeypatch.setattr("skillevaluator.reporting.sarif_reporter.os.path.realpath", realpath)

assert _resolve_for_reporting(path) == expected
assert _resolve_for_reporting(_resolve_for_reporting(path)) == expected
Loading