Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

security: validate CLI paths/args in dev tools (SonarCloud S2083/S8707/S8705/S7044)

Dylan M. Taylor68d5118a9d2f
diff --git a/docs/showcase/run.py b/docs/showcase/run.py
index 8ea6c9fc9..6b680e9f2 100644
--- a/docs/showcase/run.py
+++ b/docs/showcase/run.py
@@ -1,6 +1,16 @@
#!/usr/bin/env python3
"""lcb.py FILE.jsonl — run control requests from a JSON-lines file ({"method":..., "params":...} or {"sleep": s})."""
import json, socket, sys, time
+from pathlib import Path
+
+
+def _jsonl_path(arg):
+ """Resolve the CLI file argument; require an existing regular file."""
+ path = Path(arg).resolve()
+ if not path.is_file():
+ sys.exit(f"not a readable file: {arg}")
+ return path
+
def call(method, params):
s = socket.create_connection(("127.0.0.1", 7980), timeout=180)
s.sendall((json.dumps({"id": 1, "method": method, "params": params}) + "\n").encode())
@@ -10,7 +20,7 @@ def call(method, params):
if not c: break
buf += c
return json.loads(buf)
-for line in open(sys.argv[1]):
+for line in open(_jsonl_path(sys.argv[1])):
line = line.strip()
if not line or line.startswith("#"): continue
r = json.loads(line)
diff --git a/tools/spectral_camera_table.py b/tools/spectral_camera_table.py
index b05ac2802..0e6fef7df 100644
--- a/tools/spectral_camera_table.py
+++ b/tools/spectral_camera_table.py
@@ -19,6 +19,23 @@ import sys
import colour
import numpy as np
from scipy.optimize import minimize
+from pathlib import Path
+
+
+def _existing_dir(arg):
+ """Resolve a directory CLI argument; require it to exist."""
+ path = Path(arg).resolve()
+ if not path.is_dir():
+ sys.exit(f"not a directory: {arg}")
+ return str(path)
+
+
+def _out_path(arg):
+ """Resolve an output path CLI argument; require its parent to exist."""
+ path = Path(arg).resolve()
+ if not path.parent.is_dir():
+ sys.exit(f"output directory does not exist: {arg}")
+ return str(path)
WL = np.arange(380, 781, 5)
D50 = colour.xy_to_XYZ(colour.CCS_ILLUMINANTS["CIE 1931 2 Degree Standard Observer"]["D50"])
@@ -119,4 +136,4 @@ def main(root, out):
if __name__ == "__main__":
if len(sys.argv) != 3:
sys.exit("usage: spectral_camera_table.py <rawtoaces-data checkout> <output .rs>")
- main(sys.argv[1], sys.argv[2])
+ main(_existing_dir(sys.argv[1]), _out_path(sys.argv[2]))