File tree Expand file tree Collapse file tree 2 files changed +29
-6
lines changed Expand file tree Collapse file tree 2 files changed +29
-6
lines changed Original file line number Diff line number Diff line change @@ -112,18 +112,28 @@ def _get_terminfo_dirs() -> list[Path]:
112112 return [Path (d ) for d in dirs if Path (d ).is_dir ()]
113113
114114
115- def _read_terminfo_file (terminal_name : str ) -> bytes :
116- """Find and read terminfo file for given terminal name.
117-
118- Terminfo files are stored in directories using the first character
119- of the terminal name as a subdirectory.
120- """
115+ def _validate_terminal_name_or_raise (terminal_name : str ) -> None :
121116 if not isinstance (terminal_name , str ):
122117 raise TypeError ("`terminal_name` must be a string" )
123118
124119 if not terminal_name :
125120 raise ValueError ("`terminal_name` cannot be empty" )
126121
122+ if "\x00 " in terminal_name :
123+ raise ValueError ("NUL character found in `terminal_name`" )
124+
125+ t = Path (terminal_name )
126+ if len (t .parts ) > 1 :
127+ raise ValueError ("`terminal_name` cannot contain path separators" )
128+
129+
130+ def _read_terminfo_file (terminal_name : str ) -> bytes :
131+ """Find and read terminfo file for given terminal name.
132+
133+ Terminfo files are stored in directories using the first character
134+ of the terminal name as a subdirectory.
135+ """
136+ _validate_terminal_name_or_raise (terminal_name )
127137 first_char = terminal_name [0 ].lower ()
128138 filename = terminal_name
129139
Original file line number Diff line number Diff line change @@ -652,3 +652,16 @@ def test_terminfo_fallback(self):
652652 self .assertIsNotNone (
653653 bel , "PyREPL should provide basic capabilities after fallback"
654654 )
655+
656+ def test_invalid_terminal_names (self ):
657+ cases = [
658+ (42 , TypeError ),
659+ ("" , ValueError ),
660+ ("w\x00 t" , ValueError ),
661+ (f"..{ os .sep } name" , ValueError ),
662+ ]
663+
664+ for term , exc in cases :
665+ with self .subTest (term = term ):
666+ with self .assertRaises (exc ):
667+ terminfo ._validate_terminal_name_or_raise (term )
You can’t perform that action at this time.
0 commit comments