11import contextlib
2+ import os
23from collections import namedtuple
34from csv import reader
45
@@ -13,17 +14,28 @@ def csv(file_path: str, delimiter=',', encoding="utf-8") -> list:
1314 :param file_path: The path to the CSV file.
1415 :param delimiter: The delimiter used in the CSV file.
1516 """
17+ file_path = __validate_path (file_path )
1618 with open (file_path , 'r' , newline = '' , encoding = encoding ) as csvfile :
1719 csvreader = reader (csvfile , delimiter = delimiter )
1820
1921 # Create a namedtuple type, casting the header values to int or float if possible
20- Row = namedtuple ('Row' , list (next (csvreader )))
22+ Row = namedtuple ('Row' , list (next (csvreader , [] )))
2123
2224 # Process the data, casting values to int or float if possible
2325 data = [Row (* [__try_cast (value ) for value in row ]) for row in csvreader ]
2426
2527 return data
2628
29+ def __validate_path (file_path : str ):
30+ """Validate a path string to prevent path traversal attacks"""
31+ if not os .path .isabs (file_path ):
32+ raise ValueError ("The file_path must be an absolute path." )
33+
34+ if not os .path .exists (file_path ):
35+ raise FileNotFoundError ("The specified file does not exist." )
36+
37+ return file_path
38+
2739def __try_cast (value ):
2840 """Try to cast value to primary data types from python (int, float, bool)"""
2941 for cast in (int , float ):
0 commit comments