Skip to content

Commit 9db38c7

Browse files
committed
Make terminal name validation more comprehensive
1 parent 7aa2ec8 commit 9db38c7

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

Lib/_pyrepl/terminfo.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff 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

Lib/test/test_pyrepl/test_terminfo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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\x00t", 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)

0 commit comments

Comments
 (0)