@@ -882,6 +882,24 @@ def _value_to_string(self, value: Union[str, bytes, int, float, bool]) -> str:
882882 return str (value )
883883 return force_text (value )
884884
885+ def _value_to_string_safe (self , value : Union [str , bytes , int , float , bool ]) -> str :
886+ value_str = self ._value_to_string (value )
887+ if re .search (r"[\r\n\x00]" , value_str ):
888+ raise ValueError ("Git config values must not contain CR, LF, or NUL" )
889+ return value_str
890+
891+ @needs_values
892+ @set_dirty_and_flush_changes
893+ def set (
894+ self ,
895+ section : str ,
896+ option : str ,
897+ value : Union [str , bytes , int , float , bool , None ] = None ,
898+ ) -> None :
899+ if value is not None :
900+ value = self ._value_to_string_safe (value )
901+ return super ().set (section , option , value )
902+
885903 @needs_values
886904 @set_dirty_and_flush_changes
887905 def set_value (self , section : str , option : str , value : Union [str , bytes , int , float , bool ]) -> "GitConfigParser" :
@@ -902,9 +920,10 @@ def set_value(self, section: str, option: str, value: Union[str, bytes, int, flo
902920 :return:
903921 This instance
904922 """
923+ value_str = self ._value_to_string_safe (value )
905924 if not self .has_section (section ):
906925 self .add_section (section )
907- self .set (section , option , self . _value_to_string ( value ) )
926+ super () .set (section , option , value_str )
908927 return self
909928
910929 @needs_values
@@ -929,9 +948,10 @@ def add_value(self, section: str, option: str, value: Union[str, bytes, int, flo
929948 :return:
930949 This instance
931950 """
951+ value_str = self ._value_to_string_safe (value )
932952 if not self .has_section (section ):
933953 self .add_section (section )
934- self ._sections [section ].add (option , self . _value_to_string ( value ) )
954+ self ._sections [section ].add (option , value_str )
935955 return self
936956
937957 def rename_section (self , section : str , new_name : str ) -> "GitConfigParser" :
0 commit comments