Skip to content

Commit f41e6e9

Browse files
committed
fix(docs_build_examples): enhance security and error handling in command execution and cleanup functions
1 parent 714c69e commit f41e6e9

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

.github/scripts/docs_build_examples.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@
7272

7373
def run_cmd(cmd, check=True, capture_output=False, text=True):
7474
"""Execute a shell command with error handling."""
75+
# Security: Ensure cmd is always a list to prevent command injection
76+
if isinstance(cmd, str):
77+
raise ValueError("cmd must be a list, not a string, to prevent command injection")
78+
if not isinstance(cmd, list) or not cmd:
79+
raise ValueError("cmd must be a non-empty list")
80+
# Validate all elements are strings
81+
if not all(isinstance(arg, str) for arg in cmd):
82+
raise ValueError("All cmd arguments must be strings")
7583
try:
7684
return subprocess.run(
7785
cmd, check=check, capture_output=capture_output, text=text
@@ -260,8 +268,8 @@ def cleanup_binaries():
260268
if not os.listdir(root):
261269
try:
262270
os.rmdir(root)
263-
except Exception:
264-
pass
271+
except Exception as e:
272+
print(f"WARNING: Failed to remove empty directory {root}: {e}")
265273
print("Cleanup completed")
266274

267275

@@ -284,7 +292,8 @@ def find_examples_with_upload_binary():
284292
data = yaml.safe_load(ci_yml.read_text())
285293
if "upload-binary" in data and data["upload-binary"]:
286294
res.append(str(ino))
287-
except Exception:
295+
except Exception as e:
296+
print(f"WARNING: Failed to parse ci.yml for {ci_yml}: {e}")
288297
continue
289298
return res
290299

0 commit comments

Comments
 (0)