Merge pull request 'fix: validate --output path to prevent arbitrary file write (#2)' (#14) from fix/issue-2 into main
Reviewed-on: https://git.example.com/jarianc/NASAImageDownloader/pulls/14
This commit is contained in:
commit
5c23a347ad
@ -203,8 +203,23 @@ def save_metadata(item: Dict, dest: Path) -> None:
|
|||||||
def main() -> None:
|
def main() -> None:
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
|
|
||||||
# Resolve output base directory and override global paths
|
# Resolve output base directory and validate it is safe
|
||||||
base_dir = Path(args.output).resolve()
|
base_dir = Path(args.output).resolve()
|
||||||
|
# Prevent writing to system-critical paths
|
||||||
|
_BLOCKED_PREFIXES = {
|
||||||
|
str(Path(p).resolve())
|
||||||
|
for p in ("/etc", "/usr", "/bin", "/sbin", "/boot", "/dev", "/proc", "/sys")
|
||||||
|
}
|
||||||
|
base_str = str(base_dir)
|
||||||
|
if base_str == "/" or any(
|
||||||
|
base_str == blocked or base_str.startswith(blocked + "/")
|
||||||
|
for blocked in _BLOCKED_PREFIXES
|
||||||
|
):
|
||||||
|
print(
|
||||||
|
f"Error: --output cannot point to system directory: {base_dir}",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
sys.exit(1)
|
||||||
global IMG_DIR, META_DIR, STATE_FILE
|
global IMG_DIR, META_DIR, STATE_FILE
|
||||||
IMG_DIR = base_dir / "images"
|
IMG_DIR = base_dir / "images"
|
||||||
META_DIR = base_dir / "metadata"
|
META_DIR = base_dir / "metadata"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user