@@ -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"\n echo { _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"\n echo { _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