From 2cb4bce97ff28c3f725320b54f12de1caba34a1f Mon Sep 17 00:00:00 2001 From: srichs <13246896+srichs@users.noreply.github.com> Date: Sun, 8 Feb 2026 23:08:41 +0000 Subject: [PATCH] Add validation for volume inputs --- src/veracrypt/veracrypt.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/veracrypt/veracrypt.py b/src/veracrypt/veracrypt.py index 3afb5af..ec63d65 100644 --- a/src/veracrypt/veracrypt.py +++ b/src/veracrypt/veracrypt.py @@ -123,6 +123,21 @@ def _validate_keyfiles(self, keyfiles: Optional[List[str]], context: str) -> Non for keyfile in keyfiles: self._check_path(keyfile) + @staticmethod + def _validate_size(size: int) -> None: + """Validate volume size is a positive integer.""" + if not isinstance(size, int) or size <= 0: + raise ValueError("Volume size must be a positive integer.") + + @staticmethod + def _validate_volume_parent_dir(volume_path: str) -> None: + """Ensure the parent directory for a volume path exists.""" + parent_dir = os.path.dirname(volume_path) or "." + if not os.path.exists(parent_dir): + raise VeraCryptError( + f"The parent directory for {volume_path} does not exist." + ) + @staticmethod def _mask_password_in_args(args: List[str], password: str, index: int) -> None: """Safely mask a password in a command args list.""" @@ -187,6 +202,8 @@ def mount_volume( self.logger.debug("Mounting volume") self._validate_options(options, "mount_volume") self._check_path(volume_path) + if self.os_name != "Windows" and mount_point: + self._check_path(mount_point) if self.os_name == "Windows": cmd = self._mount_win(volume_path, password, mount_point, options) @@ -352,6 +369,8 @@ def create_volume( self.logger.debug("Creating volume") self._validate_options(options, "create_volume") self._validate_keyfiles(keyfiles, "create_volume") + self._validate_size(size) + self._validate_volume_parent_dir(volume_path) if self.os_name == "Windows": cmd = self._create_win(