99 get_max_filename_length ,
1010)
1111
12- # Setup the git environment
12+ # Setup the Git environment
1313setup_git_environment ()
1414from git import Repo , GitCommandError , InvalidGitRepositoryError
1515
1616
17+ def sanitize_input (input_str , max_length = 255 ):
18+ """Sanitize and truncate inputs to avoid invalid Git operations."""
19+ sanitized = "" .join (ch for ch in input_str if ch .isalnum () or ch in ("-" , "_" , "." ))
20+ return sanitized [:max_length ]
21+
22+
1723def TestOneInput (data ):
1824 fdp = atheris .FuzzedDataProvider (data )
1925
@@ -24,12 +30,23 @@ def TestOneInput(data):
2430 try :
2531 with tempfile .TemporaryDirectory () as submodule_temp_dir :
2632 sub_repo = Repo .init (submodule_temp_dir , bare = fdp .ConsumeBool ())
27- sub_repo .index .commit (fdp .ConsumeUnicodeNoSurrogates (fdp .ConsumeIntInRange (1 , 512 )))
33+ commit_message = sanitize_input (fdp .ConsumeUnicodeNoSurrogates (fdp .ConsumeIntInRange (1 , 512 )))
34+ sub_repo .index .commit (commit_message )
2835
29- submodule_name = fdp .ConsumeUnicodeNoSurrogates (
30- fdp .ConsumeIntInRange (1 , max (1 , get_max_filename_length (repo .working_tree_dir )))
36+ submodule_name = sanitize_input (
37+ fdp .ConsumeUnicodeNoSurrogates (
38+ fdp .ConsumeIntInRange (1 , get_max_filename_length (repo .working_tree_dir ))
39+ )
3140 )
32- submodule_path = os .path .join (repo .working_tree_dir , submodule_name )
41+
42+ submodule_path = os .path .relpath (
43+ os .path .join (repo .working_tree_dir , submodule_name ),
44+ start = repo .working_tree_dir ,
45+ )
46+
47+ # Ensure submodule_path is valid
48+ if not submodule_name or submodule_name .startswith ("/" ) or ".." in submodule_name :
49+ return - 1 # Reject invalid input so they are not added to the corpus
3350
3451 submodule = repo .create_submodule (submodule_name , submodule_path , url = sub_repo .git_dir )
3552 repo .index .commit ("Added submodule" )
@@ -39,25 +56,38 @@ def TestOneInput(data):
3956 value_length = fdp .ConsumeIntInRange (1 , max (1 , fdp .remaining_bytes ()))
4057
4158 writer .set_value (
42- fdp .ConsumeUnicodeNoSurrogates (key_length ), fdp .ConsumeUnicodeNoSurrogates (value_length )
59+ sanitize_input (fdp .ConsumeUnicodeNoSurrogates (key_length )),
60+ sanitize_input (fdp .ConsumeUnicodeNoSurrogates (value_length )),
4361 )
4462 writer .release ()
4563
46- submodule .update (init = fdp .ConsumeBool (), dry_run = fdp .ConsumeBool (), force = fdp .ConsumeBool ())
64+ submodule .update (
65+ init = fdp .ConsumeBool (),
66+ dry_run = fdp .ConsumeBool (),
67+ force = fdp .ConsumeBool (),
68+ )
69+
4770 submodule_repo = submodule .module ()
4871
49- new_file_name = fdp .ConsumeUnicodeNoSurrogates (
50- fdp .ConsumeIntInRange (1 , max (1 , get_max_filename_length (submodule_repo .working_tree_dir )))
72+ new_file_name = sanitize_input (
73+ fdp .ConsumeUnicodeNoSurrogates (
74+ fdp .ConsumeIntInRange (1 , get_max_filename_length (submodule_repo .working_tree_dir ))
75+ )
5176 )
5277 new_file_path = os .path .join (submodule_repo .working_tree_dir , new_file_name )
5378 with open (new_file_path , "wb" ) as new_file :
5479 new_file .write (fdp .ConsumeBytes (fdp .ConsumeIntInRange (1 , 512 )))
80+
5581 submodule_repo .index .add ([new_file_path ])
5682 submodule_repo .index .commit ("Added new file to submodule" )
5783
5884 repo .submodule_update (recursive = fdp .ConsumeBool ())
59- submodule_repo .head .reset (commit = "HEAD~1" , working_tree = fdp .ConsumeBool (), head = fdp .ConsumeBool ())
60- # Use fdp.PickValueInList to ensure at least one of 'module' or 'configuration' is True
85+ submodule_repo .head .reset (
86+ commit = "HEAD~1" ,
87+ working_tree = fdp .ConsumeBool (),
88+ head = fdp .ConsumeBool (),
89+ )
90+
6191 module_option_value , configuration_option_value = fdp .PickValueInList (
6292 [(True , False ), (False , True ), (True , True )]
6393 )
@@ -82,12 +112,7 @@ def TestOneInput(data):
82112 ):
83113 return - 1
84114 except Exception as e :
85- if isinstance (e , ValueError ) and "embedded null byte" in str (e ):
86- return - 1
87- elif isinstance (e , OSError ) and "File name too long" in str (e ):
88- return - 1
89- else :
90- return handle_exception (e )
115+ return handle_exception (e )
91116
92117
93118def main ():
0 commit comments