@@ -71,7 +71,7 @@ def get_corpus_db(url: str) -> Optional[_ResponseWrapper]:
7171 try :
7272 req = Request (url , headers = {"User-Agent" : _USER_AGENT })
7373 # SSL certificate verification is enabled by default
74- with urlopen (req , timeout = 10 ) as response :
74+ with urlopen (req , timeout = 10 ) as response : # nosec B310
7575 corpus_db = _ResponseWrapper (response )
7676 except HTTPError as http_err :
7777 print (f"HTTP error occurred: { http_err } " )
@@ -349,7 +349,7 @@ def _download(url: str, dst: str) -> int:
349349
350350 req = Request (url , headers = {"User-Agent" : _USER_AGENT })
351351 # SSL certificate verification is enabled by default
352- with urlopen (req , timeout = 10 ) as response :
352+ with urlopen (req , timeout = 10 ) as response : # nosec B310
353353 file_size = int (response .info ().get ("Content-Length" , - 1 ))
354354 with open (get_full_data_path (dst ), "wb" ) as f :
355355 pbar = None
@@ -383,7 +383,7 @@ def _check_hash(dst: str, md5: str) -> None:
383383 with open (get_full_data_path (dst ), "rb" ) as f :
384384 content = f .read ()
385385 # MD5 is insecure but sufficient here
386- file_md5 = hashlib .md5 (content ).hexdigest () # noqa: S324
386+ file_md5 = hashlib .md5 (content ).hexdigest () # noqa: S324 # nosec B324
387387
388388 if md5 != file_md5 :
389389 raise ValueError ("Hash does not match expected." )
@@ -484,7 +484,7 @@ def _safe_extract_tar(tar: tarfile.TarFile, path: str) -> None:
484484 f"Symlink { member .name } points outside extraction directory: { member .linkname } "
485485 )
486486
487- tar .extractall (path = path )
487+ tar .extractall (path = path ) # nosec B202
488488
489489
490490def _safe_extract_zip (zip_file : zipfile .ZipFile , path : str ) -> None :
@@ -539,7 +539,7 @@ def _safe_extract_zip(zip_file: zipfile.ZipFile, path: str) -> None:
539539 f"Symlink { member } points outside extraction directory: { link_target } "
540540 )
541541
542- zip_file .extractall (path = path )
542+ zip_file .extractall (path = path ) # nosec B202
543543
544544
545545def _version2int (v : str ) -> int :
@@ -853,12 +853,17 @@ def make_safe_directory_name(name: str) -> str:
853853 return safe_name
854854
855855
856- def get_hf_hub (repo_id : str , filename : str = "" ) -> str :
856+ def get_hf_hub (
857+ repo_id : str , filename : str = "" , revision : Optional [str ] = None
858+ ) -> str :
857859 """HuggingFace Hub in :mod:`pythainlp` data directory.
858860
859861 :param str repo_id: repo_id
860862 :param str filename: filename (optional, default is empty string).
861863 If empty, downloads entire snapshot.
864+ :param Optional[str] revision: a git revision id, which can be a branch
865+ name, a tag, or a commit hash (optional, default is ``None``).
866+ Pin to a full commit hash for reproducible and secure downloads.
862867 :return: path
863868 :rtype: str
864869 """
@@ -876,10 +881,15 @@ def get_hf_hub(repo_id: str, filename: str = "") -> str:
876881 root_project = safe_path_join (hf_root , name_dir )
877882 if filename :
878883 output_path = hf_hub_download (
879- repo_id = repo_id , filename = filename , local_dir = root_project
884+ repo_id = repo_id ,
885+ filename = filename ,
886+ local_dir = root_project ,
887+ revision = revision ,
880888 )
881889 else :
882890 output_path = snapshot_download (
883- repo_id = repo_id , local_dir = root_project
891+ repo_id = repo_id ,
892+ local_dir = root_project ,
893+ revision = revision ,
884894 )
885895 return str (output_path )
0 commit comments