|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import json |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | +from typer.testing import CliRunner |
| 9 | + |
| 10 | +from skillspector.cli import app |
| 11 | +from skillspector.nodes.analyzers.static_patterns_tool_misuse import has_bounded_parse_exhaustion |
| 12 | + |
| 13 | +_COMMENT_PADDING = "\n" + "#" * 6000 + "\n" |
| 14 | +_APOSTROPHE_PADDING = "\n" + "y = 'b'\n" * 800 |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize( |
| 18 | + "source", |
| 19 | + [ |
| 20 | + 'out="$(date)"', |
| 21 | + 'v="a (b)"', |
| 22 | + 'v="|$a|"', |
| 23 | + 'printf "%s" "$(f "$x")"', |
| 24 | + '[ "$(f)" = true ]', |
| 25 | + 'cat <<< "$(f)"', |
| 26 | + 'v="a \\" (b)"', |
| 27 | + 'v="a \\" |$a|"', |
| 28 | + ], |
| 29 | +) |
| 30 | +def test_complete_shell_quotes_do_not_consume_following_padding(source: str) -> None: |
| 31 | + assert not has_bounded_parse_exhaustion(source + "\n" + "#" * 6000 + "\n", lambda: None) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize( |
| 35 | + "source", ['x="$(unterminated', 'x="unclosed', '"$(printf "%s" "$x")" -rf /'] |
| 36 | +) |
| 37 | +def test_unresolved_shell_words_still_exhaust(source: str) -> None: |
| 38 | + assert has_bounded_parse_exhaustion(source + "\n" + "#" * 6000 + "\n", lambda: None) |
| 39 | + |
| 40 | + |
| 41 | +def test_assignment_ownership_retains_nested_command_budget() -> None: |
| 42 | + nested_word = "r" * 4097 |
| 43 | + source = f'out="$({nested_word} -rf /)"' |
| 44 | + assert has_bounded_parse_exhaustion(source, lambda: None) |
| 45 | + |
| 46 | + |
| 47 | +def test_failed_word_parse_does_not_claim_following_command() -> None: |
| 48 | + source = 'Test-Path "$($_.FullName)\\cli-path"; $($CMD) -rf /' |
| 49 | + assert has_bounded_parse_exhaustion(source, lambda: None) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize( |
| 53 | + "source", ['Run "$(resolve_tool) -rf /"', '# see "notes\n$(resolve_tool) -rf /\n# "'] |
| 54 | +) |
| 55 | +def test_quoted_runtime_commands_keep_fail_closed_coverage(source: str) -> None: |
| 56 | + from skillspector.inspection_ledger import LedgerOutcome, LedgerReason |
| 57 | + from skillspector.nodes.analyzers import static_patterns_tool_misuse, static_runner |
| 58 | + |
| 59 | + assert has_bounded_parse_exhaustion(source, lambda: None) |
| 60 | + path = "script.sh" if source.startswith("#") else "SKILL.md" |
| 61 | + result = static_runner.run_static_patterns_with_ledger( |
| 62 | + {"components": [path], "local_file_cache": {path: source}, "file_cache": {path: source}}, |
| 63 | + [static_patterns_tool_misuse], |
| 64 | + ) |
| 65 | + assert any( |
| 66 | + e["outcome"] == LedgerOutcome.PARTIAL |
| 67 | + and e["reason_code"] == LedgerReason.STATIC_PARSE_LIMIT |
| 68 | + for e in result["inspection_ledger"] |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize("file_type", ["shell", "markdown", "python"]) |
| 73 | +def test_non_shell_unclosed_assignment_remains_conservative(file_type: str) -> None: |
| 74 | + source = 'name="value' + "\n" + "text " * 1200 |
| 75 | + assert has_bounded_parse_exhaustion(source, lambda: None, file_type=file_type) |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.parametrize( |
| 79 | + ("source", "file_type"), |
| 80 | + [ |
| 81 | + ('# use "$(cmd)" here', "shell"), |
| 82 | + ('```bash\nout="$(date)"\n```', "markdown"), |
| 83 | + ('x="$(printf \'%s\' "$X")"', "shell"), |
| 84 | + ("model_count=\"$(printf '%s\\n' \"$j\" | jq -r 'length')\"", "shell"), |
| 85 | + ], |
| 86 | + ids=["issue-comment-form", "markdown-fence", "printf-value", "printf-pipeline-value"], |
| 87 | +) |
| 88 | +def test_benign_quoted_values_do_not_consume_comment_padding(source: str, file_type: str) -> None: |
| 89 | + assert not has_bounded_parse_exhaustion( |
| 90 | + source + _COMMENT_PADDING, lambda: None, file_type=file_type |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize( |
| 95 | + ("source", "file_type"), |
| 96 | + [ |
| 97 | + ('# Keep `X="${X}"` so X\'s value wins.', "python"), |
| 98 | + ("# Keep `X='${X}'`, so X's value wins.", "python"), |
| 99 | + ('Use `X="${X}"`. That\'s the self-reference.', "shell"), |
| 100 | + ], |
| 101 | + ids=["double-quoted", "single-quoted", "prose"], |
| 102 | +) |
| 103 | +def test_backtick_enclosed_assignment_stops_at_its_closing_backtick( |
| 104 | + source: str, file_type: str |
| 105 | +) -> None: |
| 106 | + assert not has_bounded_parse_exhaustion( |
| 107 | + source + _APOSTROPHE_PADDING, lambda: None, file_type=file_type |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.parametrize( |
| 112 | + ("source", "file_type"), |
| 113 | + [ |
| 114 | + ('ls # see "notes\n"$(resolve_tool)" -rf /\n# "', "shell"), |
| 115 | + ('Set name="value in prose.\n\n```bash\n"$(resolve_tool)" -rf /\n```\n"\n', "markdown"), |
| 116 | + ('x = f(name="value)\n"$(resolve_tool)" -rf /\n"\n', "python"), |
| 117 | + ('Set name="value\n$T -rf /\n"\n', "markdown"), |
| 118 | + ('name="a "$(resolve_tool)" -rf /"', "shell"), |
| 119 | + ], |
| 120 | + ids=[ |
| 121 | + "trailing-comment", |
| 122 | + "prose-assignment-before-fence", |
| 123 | + "host-language-assignment", |
| 124 | + "prose-assignment-parameter", |
| 125 | + "same-line-runtime-opener", |
| 126 | + ], |
| 127 | +) |
| 128 | +def test_misaligned_quote_claims_keep_runtime_commands_partial(source: str, file_type: str) -> None: |
| 129 | + assert has_bounded_parse_exhaustion(source, lambda: None, file_type=file_type) |
| 130 | + |
| 131 | + |
| 132 | +def test_issue_628_bundle_reports_complete_coverage(tmp_path: Path) -> None: |
| 133 | + (tmp_path / "SKILL.md").write_text( |
| 134 | + "---\nname: repro\ndescription: Minimal repro.\n---\nRun `scripts/run.sh`.\n", |
| 135 | + encoding="utf-8", |
| 136 | + ) |
| 137 | + padding = "".join(f"# padding line {index} " + "." * 50 + "\n" for index in range(90)) |
| 138 | + (tmp_path / "scripts").mkdir() |
| 139 | + (tmp_path / "scripts" / "run.sh").write_text( |
| 140 | + '#!/bin/bash\nout="$(date)"\necho "$out"\n' + padding, encoding="utf-8" |
| 141 | + ) |
| 142 | + |
| 143 | + result = CliRunner().invoke(app, ["scan", str(tmp_path), "--format", "json", "--no-llm"]) |
| 144 | + |
| 145 | + # Exit 1 is the documented high-risk verdict, not a scan execution failure. |
| 146 | + assert result.exit_code in {0, 1}, result.output |
| 147 | + report = json.loads(result.output) |
| 148 | + assert report["analysis_completeness"]["status"] == "complete" |
| 149 | + assert report["analysis_completeness"]["ledger_exceptions"] == [] |
| 150 | + assert not any(issue["id"] == "AE1" for issue in report["issues"]) |
0 commit comments