Skip to content

Commit e8850a8

Browse files
Merge pull request #844 from NVIDIA/yashraj/fix-release-scan-regressions-20261015
fix: avoid checksum false positives and preserve archive error origins
2 parents 8ccf920 + a602ef7 commit e8850a8

5 files changed

Lines changed: 239 additions & 25 deletions

File tree

‎src/skillspector/nested_artifacts.py‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ class NestedInspectionResult:
128128
# Expected extensions and format mismatches are not content recognition.
129129
recognized_zip_paths: set[str] = field(default_factory=set)
130130
ledger_events: list[InspectionLedgerEvent] = field(default_factory=list)
131+
# Keep the actual event objects: disk and virtual paths can share both a
132+
# spelling and a work ID, so neither identifies which input produced it.
133+
disk_container_events: list[InspectionLedgerEvent] = field(default_factory=list)
134+
_active_disk_path: str | None = None
131135
uncompressed_bytes: int = 0
132136
# Exceptions can target a top-level container before a virtual artifact row
133137
# exists. Preserve their canonical disposition so build_context can apply
@@ -621,27 +625,26 @@ def _exception(
621625
limit_seconds: float | None = None,
622626
) -> None:
623627
_mark_inventory_exception(result, path=path, reason=reason)
624-
result.ledger_events.append(
625-
ledger_event(
626-
outcome=(
627-
LedgerOutcome.PARTIAL
628-
if reason in _PARTIAL_INVENTORY_REASONS
629-
else LedgerOutcome.SKIPPED
630-
),
631-
record_type=LedgerRecordType.SYSTEM,
632-
phase="nested_artifact_inspection",
633-
path=path,
634-
reason=reason,
635-
observed_bytes=observed_bytes,
636-
limit_bytes=limit_bytes,
637-
observed_artifacts=observed_artifacts,
638-
limit_artifacts=limit_artifacts,
639-
observed_depth=observed_depth,
640-
limit_depth=limit_depth,
641-
observed_seconds=observed_seconds,
642-
limit_seconds=limit_seconds,
643-
)
628+
event = ledger_event(
629+
outcome=(
630+
LedgerOutcome.PARTIAL if reason in _PARTIAL_INVENTORY_REASONS else LedgerOutcome.SKIPPED
631+
),
632+
record_type=LedgerRecordType.SYSTEM,
633+
phase="nested_artifact_inspection",
634+
path=path,
635+
reason=reason,
636+
observed_bytes=observed_bytes,
637+
limit_bytes=limit_bytes,
638+
observed_artifacts=observed_artifacts,
639+
limit_artifacts=limit_artifacts,
640+
observed_depth=observed_depth,
641+
limit_depth=limit_depth,
642+
observed_seconds=observed_seconds,
643+
limit_seconds=limit_seconds,
644644
)
645+
result.ledger_events.append(event)
646+
if path == result._active_disk_path:
647+
result.disk_container_events.append(event)
645648

646649

647650
def _time_exception(
@@ -1233,6 +1236,7 @@ def inspect_nested_artifacts(
12331236
last_checked_at=started_at,
12341237
)
12351238
for path in dict.fromkeys(components):
1239+
result._active_disk_path = path
12361240
if budget.halted:
12371241
break
12381242
if budget.expired():
@@ -1336,6 +1340,7 @@ def inspect_nested_artifacts(
13361340
result=result,
13371341
)
13381342

1343+
result._active_disk_path = None
13391344
result.components = list(dict.fromkeys(result.components))
13401345
result.uncompressed_bytes = budget.uncompressed_bytes
13411346
_apply_inventory_overrides(result)

‎src/skillspector/nodes/analyzers/static_patterns_supply_chain.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,20 +330,40 @@ def _decoded_literal_xor_calls(content: str) -> list[tuple[int, str]]:
330330
re.IGNORECASE,
331331
)
332332
_SC3_SHELL_DECODE_COMMAND = re.compile(_SC3_SHELL_DECODER, re.IGNORECASE)
333+
_SC3_DECODED_DATA_SINK = re.compile(
334+
r"(?:[ \t]{1,16}-[A-Za-z0-9-]{1,16}){0,4}[ \t]{0,16}"
335+
r"\|(?!\|)&?[ \t]{0,16}sha256sum(?=$|[\s;&|)`])"
336+
)
333337

334338

335339
def _decoded_shell_literal_payloads(content: str) -> list[tuple[int, str]]:
336340
"""Decode literal payloads fed to a shell decoder.
337341
338342
Only an inline echo/printf argument or a here-string is decoded, as base64,
339343
base32 or (for xxd) hex; an ``openssl enc`` cipher is not applied.
344+
Direct checksum consumers are exempt only in a simple shell context without
345+
alias, function or dynamic-command syntax. Other flows remain conservative
346+
because a saved payload can be executed later.
340347
Variables, files and downloads stay opaque. Invalid UTF-8 becomes U+FFFD,
341348
so a stray byte cannot hide the command. Returns ``(offset, text)`` pairs
342349
in source order.
343350
"""
351+
checksum_exemption_allowed: bool | None = None
344352
decoded: list[tuple[int, str]] = []
345353
for pattern in (_SC3_PIPED_LITERAL, _SC3_HERE_STRING_LITERAL):
346354
for match in pattern.finditer(content):
355+
if _SC3_DECODED_DATA_SINK.match(content, match.end()) is not None:
356+
if checksum_exemption_allowed is None:
357+
# Normalize once: escaped or quoted words can still configure aliases.
358+
tokens = _shell_tokens(content)
359+
checksum_exemption_allowed = tokens is not None and not any(
360+
token in {"alias", "function", "eval", "source", "."}
361+
or token.startswith("sha256sum=")
362+
or any(character.isspace() or character in "()" for character in token)
363+
for token in tokens
364+
)
365+
if checksum_exemption_allowed:
366+
continue
347367
literal = match.group("literal")
348368
decoder = match.group("decoder").lower()
349369
try:

‎src/skillspector/nodes/build_context.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3366,14 +3366,11 @@ def mark_excluded_nested_metadata(
33663366
" Distinct descendant members may still be analyzed."
33673367
)
33683368
prework_events.append(event)
3369+
disk_container_event_ids = {id(event) for event in nested.disk_container_events}
33693370
for event in nested.ledger_events:
33703371
path = event["path"]
33713372
if path in reserved_paths:
3372-
# These reasons are emitted only for top-level disk inputs.
3373-
if path in nested.outer_metadata and event.get("reason_code") in {
3374-
LedgerReason.ARCHIVE_FORMAT_MISMATCH,
3375-
LedgerReason.ARCHIVE_MALFORMED,
3376-
}:
3373+
if id(event) in disk_container_event_ids:
33773374
continue
33783375
event["path"] = member_containers.get(path, path.rsplit("!/", 1)[0])
33793376
event["work_id"] = inspection_work_id(

‎tests/nodes/test_nested_artifacts.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,43 @@ def test_collision_preserves_disk_container_errors(
15451545
assert result["execution_successful"] is False
15461546

15471547

1548+
@pytest.mark.parametrize("unsafe_source", ["disk", "member", "both"])
1549+
def test_collision_preserves_unsafe_member_event_source(tmp_path: Path, unsafe_source: str) -> None:
1550+
from skillspector.graph import graph
1551+
1552+
(tmp_path / "SKILL.md").write_text("---\nname: disk-error\n---\nA helper.\n")
1553+
(tmp_path / "bundle.zip!").mkdir()
1554+
path = "bundle.zip!/inner.zip"
1555+
disk_members = {"disk.txt": b"Disk note.\n"}
1556+
archive_members = {"archive.txt": b"Archive note.\n"}
1557+
if unsafe_source in {"disk", "both"}:
1558+
disk_members["../unsafe.txt"] = b"A rejected disk entry.\n"
1559+
if unsafe_source in {"member", "both"}:
1560+
archive_members["../unsafe.txt"] = b"A rejected archive entry.\n"
1561+
disk_bytes = _zip_bytes(disk_members)
1562+
(tmp_path / path).write_bytes(disk_bytes)
1563+
_write_archive(tmp_path / "bundle.zip", {"inner.zip": _zip_bytes(archive_members)})
1564+
1565+
result = graph.invoke({"skill_path": str(tmp_path), "use_llm": False})
1566+
1567+
events = [
1568+
event
1569+
for event in result["inspection_ledger"]
1570+
if event.get("reason_code") == LedgerReason.ARCHIVE_UNSAFE_MEMBER_PATH
1571+
]
1572+
expected_paths = (
1573+
{path, "bundle.zip"}
1574+
if unsafe_source == "both"
1575+
else {path if unsafe_source == "disk" else "bundle.zip"}
1576+
)
1577+
assert len(events) == len(expected_paths)
1578+
assert {event["path"] for event in events} == expected_paths
1579+
for event in events:
1580+
assert ("withheld archive member" in event["message"]) is (event["path"] == "bundle.zip")
1581+
assert result["raw_file_cache"][path] == disk_bytes
1582+
assert result["execution_successful"] is False
1583+
1584+
15481585
@pytest.mark.parametrize("disk_is_archive", [False, True])
15491586
def test_collision_keeps_distinct_archive_descendants(
15501587
tmp_path: Path, disk_is_archive: bool

‎tests/unit/test_patterns_new.py‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,6 +2741,92 @@ def test_shell_decode_without_execution_is_not_sc3(self, command: str) -> None:
27412741

27422742
assert not [f for f in findings if f.rule_id == "SC3"]
27432743

2744+
@pytest.mark.parametrize(
2745+
"command",
2746+
[
2747+
pytest.param(f"echo {_SHELL_FETCH_B64} | base64 -d | sha256sum", id="base64"),
2748+
pytest.param(f"echo {_SHELL_FETCH_B32} | base32 -d | sha256sum", id="base32"),
2749+
pytest.param(f"echo {_SHELL_FETCH_HEX} | xxd -r -p | sha256sum", id="hex"),
2750+
pytest.param(
2751+
f"echo {_SHELL_FETCH_B64} | openssl base64 -d -A | sha256sum", id="openssl"
2752+
),
2753+
pytest.param(f"base64 -d <<< '{_SHELL_FETCH_B64}' | sha256sum", id="here_string"),
2754+
],
2755+
)
2756+
def test_checksum_only_literal_is_not_sc2_or_sc3(self, command: str) -> None:
2757+
findings = sc_mod.analyze(command + "\n", "setup.sh", "shell")
2758+
2759+
assert not [f for f in findings if f.rule_id in {"SC2", "SC3"}]
2760+
2761+
@pytest.mark.parametrize(
2762+
"override",
2763+
[
2764+
"sha256sum() { sh; }",
2765+
"sha256sum ( ) { sh; }",
2766+
"sha256sum" + " " * 17 + "() { sh; }",
2767+
"function sha256sum { sh; }",
2768+
"function sha256sum() { sh; }",
2769+
"function" + " " * 30 + "sha256sum { sh; }",
2770+
"'sha256sum'() { sh; }",
2771+
"alias sha256sum=sh",
2772+
"alias" + " " * 24 + "sha256sum=sh",
2773+
"alias 'sha256sum=sh'",
2774+
'alias "sha256sum=sh"',
2775+
"alias 'sha256sum'=sh",
2776+
'alias "sha256sum"=sh',
2777+
"alias -- sha256sum=sh",
2778+
"alias x=y sha256sum=sh",
2779+
r"alias sha256su\m=sh",
2780+
r"a\lias sha256su\m=sh",
2781+
"a'li'as sha256su'm'=sh",
2782+
"alias x=y sha'256'sum=sh",
2783+
],
2784+
)
2785+
def test_shadowed_checksum_keeps_decoded_sc2(self, override: str) -> None:
2786+
content = override + f"\necho {_SHELL_FETCH_B64} | base64 -d | sha256sum\n"
2787+
2788+
findings = sc_mod.analyze(content, "setup.sh", "shell")
2789+
2790+
assert any(f.rule_id == "SC2" and f.matched_text == _SHELL_FETCH for f in findings)
2791+
2792+
@pytest.mark.parametrize(
2793+
"setup",
2794+
[
2795+
"eval setup",
2796+
"source aliases.sh",
2797+
". aliases.sh",
2798+
"helper() { :; }",
2799+
"printf 'unclosed",
2800+
],
2801+
)
2802+
def test_uncertain_command_identity_keeps_decoded_sc2(self, setup: str) -> None:
2803+
content = setup + f"\necho {_SHELL_FETCH_B64} | base64 -d | sha256sum\n"
2804+
2805+
findings = sc_mod.analyze(content, "setup.sh", "shell")
2806+
2807+
assert any(f.rule_id == "SC2" and f.matched_text == _SHELL_FETCH for f in findings)
2808+
2809+
@pytest.mark.parametrize("separator", ["\n", "; ", " && "])
2810+
@pytest.mark.parametrize("data_first", [True, False])
2811+
def test_only_executed_literal_produces_sc2(self, separator: str, data_first: bool) -> None:
2812+
data = f"echo {_SHELL_FETCH_B64} | base64 -d | sha256sum"
2813+
executed = "echo ZWNobyBoZWxsbw== | base64 -d | sh"
2814+
commands = [data, executed] if data_first else [executed, data]
2815+
2816+
findings = sc_mod.analyze(separator.join(commands), "setup.sh", "shell")
2817+
2818+
assert any(f.rule_id == "SC3" for f in findings)
2819+
assert not any(f.rule_id == "SC2" for f in findings)
2820+
2821+
@pytest.mark.parametrize("wrapper", ['eval "$(%s)"', "eval `%s`", "bash <(%s)"])
2822+
def test_execution_substitution_keeps_conservative_decoded_sc2(self, wrapper: str) -> None:
2823+
body = f"echo {_SHELL_FETCH_B64} | base64 -d | sha256sum; echo ZWNobyBoZWxsbwo= | base64 -d"
2824+
2825+
findings = sc_mod.analyze(wrapper % body, "setup.sh", "shell")
2826+
2827+
assert any(f.rule_id == "SC3" for f in findings)
2828+
assert any(f.rule_id == "SC2" and f.matched_text == _SHELL_FETCH for f in findings)
2829+
27442830
@pytest.mark.parametrize(
27452831
"command",
27462832
[
@@ -2755,6 +2841,55 @@ def test_shell_decode_without_execution_is_not_sc3(self, command: str) -> None:
27552841
pytest.param(f"echo {_SHELL_FETCH_HEX} | xxd -r -p | sh", id="echo_hex"),
27562842
pytest.param(f"sh <(xxd -r -p <<< {_SHELL_FETCH_HEX})", id="here_string_hex"),
27572843
pytest.param(f"echo -n {_SHELL_FETCH_B64} | openssl base64 -d -A | sh", id="openssl"),
2844+
pytest.param(f'eval "$(echo {_SHELL_FETCH_B64} | base64 -d)"', id="eval"),
2845+
pytest.param(f"eval `echo {_SHELL_FETCH_B64} | base64 -d`", id="eval_backticks"),
2846+
pytest.param(f'bash -c "$(echo {_SHELL_FETCH_B64} | base64 -d)"', id="bash_c"),
2847+
pytest.param(f"source <(base64 -d <<< '{_SHELL_FETCH_B64}')", id="source"),
2848+
pytest.param(f"bash < <(echo {_SHELL_FETCH_B64} | base64 -d)", id="redirected_process"),
2849+
pytest.param(
2850+
f'bash <<< "$(echo {_SHELL_FETCH_B64} | base64 -d)"', id="here_string_sub"
2851+
),
2852+
pytest.param(
2853+
f'eval "$(echo ZWNobyBoZWxsbwo= | base64 -d; echo {_SHELL_FETCH_B64} | base64 -d)"',
2854+
id="second_decoder_in_substitution",
2855+
),
2856+
pytest.param(
2857+
f"echo ZWNobyBoZWxsbw== | base64 -d | echo {_SHELL_FETCH_B64} | base64 -d | sh",
2858+
id="overlapping_decoded_pipeline",
2859+
),
2860+
pytest.param(
2861+
f'eval "$(echo {_SHELL_FETCH_B64} | base64 -d | cat)"',
2862+
id="substitution_with_filter",
2863+
),
2864+
pytest.param(
2865+
f'eval "$(echo {_SHELL_FETCH_B64} | base64 -d > >(cat))"',
2866+
id="substitution_with_redirected_filter",
2867+
),
2868+
pytest.param(
2869+
f'eval "$(echo {_SHELL_FETCH_B64} | base64 -d > payload.sh; cat payload.sh)"',
2870+
id="substitution_with_file_round_trip",
2871+
),
2872+
pytest.param(
2873+
f"echo {_SHELL_FETCH_B64} | base64 -d > payload.sh; sh payload.sh",
2874+
id="saved_then_executed",
2875+
),
2876+
pytest.param(
2877+
f"echo {_SHELL_FETCH_B64} | base64 -d | tee payload.sh | sha256sum; sh payload.sh",
2878+
id="saved_before_checksum_then_executed",
2879+
),
2880+
pytest.param(
2881+
f"echo {_SHELL_FETCH_B64} | base64 -d > payload.sh | sha256sum; sh payload.sh",
2882+
id="redirected_before_checksum_then_executed",
2883+
),
2884+
pytest.param(
2885+
f"echo {_SHELL_FETCH_B64} | base64 -d | sha256sum-wrapper",
2886+
id="unknown_checksum_named_wrapper",
2887+
),
2888+
pytest.param(
2889+
"eval \"$(echo ZWNobyBoZWxsbwo= | base64 -d; $(printf ''); "
2890+
f'echo {_SHELL_FETCH_B64} | base64 -d)"',
2891+
id="second_decoder_after_nested_substitution",
2892+
),
27582893
],
27592894
)
27602895
def test_sc2_shell_literal_decoded_command(self, command: str) -> None:
@@ -2765,6 +2900,16 @@ def test_sc2_shell_literal_decoded_command(self, command: str) -> None:
27652900
assert decoded[0].severity == Severity.HIGH
27662901
assert decoded[0].location.start_line == 2
27672902

2903+
@pytest.mark.parametrize("wrapper", ['eval "$(%s)"', "bash <(%s)"])
2904+
def test_maximum_literal_retains_sc2_in_execution_substitution(self, wrapper: str) -> None:
2905+
script = (_SHELL_FETCH + "\n#").ljust(12_288)
2906+
payload = base64.b64encode(script.encode()).decode()
2907+
assert len(payload) == sc_mod._MAX_SHELL_LITERAL_CHARS
2908+
2909+
findings = sc_mod.analyze(wrapper % f"echo {payload} | base64 -d", "setup.sh", "shell")
2910+
2911+
assert any(f.rule_id == "SC2" and f.matched_text.startswith(_SHELL_FETCH) for f in findings)
2912+
27682913
def test_sc3_benign_decoded_payload_is_not_sc2(self) -> None:
27692914
payload = base64.b64encode(b"echo hello").decode()
27702915
findings = sc_mod.analyze(f"echo {payload} | base64 -d | sh\n", "setup.sh", "shell")
@@ -2840,6 +2985,12 @@ def test_shell_decode_mentions_in_skill_markdown_are_not_sc3(self, body: str) ->
28402985
{"SC2"},
28412986
id="newline_ends_stage",
28422987
),
2988+
pytest.param(
2989+
r"a\lias sha256su\m=sh; "
2990+
f"echo {_SHELL_FETCH_B64} | base64 -d | sha256sum",
2991+
{"SC2"},
2992+
id="fragmented_alias",
2993+
),
28432994
],
28442995
)
28452996
def test_shell_decode_in_hook_config(self, command: str, expected: set[str]) -> None:
@@ -2910,6 +3061,10 @@ def test_decoded_shell_literal_payloads_skips_unusable_literals(self, content: s
29103061
"content",
29113062
[
29123063
pytest.param("echo QUFB | base64 -d " * 12_000, id="decoders_without_sink"),
3064+
pytest.param(
3065+
f"echo {_SHELL_FETCH_B64} | base64 -d | sha256sum\n" * 2_000,
3066+
id="repeated_checksum_pipelines",
3067+
),
29133068
pytest.param("eval $(echo " * 20_000 + "base64 -d", id="unclosed_substitutions"),
29143069
pytest.param("eval $(" + "a " * 128_000 + "base64 -d", id="long_substitution"),
29153070
pytest.param("echo " + "A" * 256_000 + " | base64 -d | sh", id="long_literal"),

0 commit comments

Comments
 (0)