Sanitize the specified host path and make sure it does not traverse out of the rootfs directory hierarchy. Args: hostpath : a local path to sanitize strict : whether to raise an error if target path does not exist Returns: whether the path is safe
(self, hostpath: Path, strict: bool = False)
| 237 | return self._rootfs_path / vpath |
| 238 | |
| 239 | def __is_safe_host_path(self, hostpath: Path, strict: bool = False) -> bool: |
| 240 | """Sanitize the specified host path and make sure it does not traverse out |
| 241 | of the rootfs directory hierarchy. |
| 242 | |
| 243 | Args: |
| 244 | hostpath : a local path to sanitize |
| 245 | strict : whether to raise an error if target path does not exist |
| 246 | |
| 247 | Returns: whether the path is safe to use |
| 248 | """ |
| 249 | |
| 250 | # canonicalization before assertion: resolve any relative path references and |
| 251 | # symbolic links that may exist. |
| 252 | # |
| 253 | # in case strict is set to True and the path does not exist, a FileNotFoundError |
| 254 | # is raised. this error is left for the user to catch and handle |
| 255 | hostpath = hostpath.resolve(strict=strict) |
| 256 | |
| 257 | try: |
| 258 | # to prevent path-traversal issues we have to make sure hostpath ended up |
| 259 | # as a subpath of rootfs. the following method will fail if that is not |
| 260 | # the case |
| 261 | _ = hostpath.relative_to(self._rootfs_path) |
| 262 | except ValueError: |
| 263 | return False |
| 264 | |
| 265 | else: |
| 266 | return True |
| 267 | |
| 268 | def host_to_virtual_path(self, hostpath: str) -> str: |
| 269 | """Convert a host path to its corresponding virtual path relative to rootfs. |
no test coverage detected