When code crosses security boundaries (subprocess/PowerShell execution, network URLs, filesystem writes, or auth/TLS), treat user/environment input as untrusted and enforce safety rules:
Example (PowerShell injection-safe pattern):
# Avoid building -Command strings with unsafe quoting.
# Prefer passing the URL as a separate argument, not interpolated into a quoted executable string.
return subprocess.Popen(
['powershell.exe', '-NoProfile', '-Command', 'Start-Process', url]
).wait()
Example (safe kubeconfig write):
if os.path.islink(existing_path):
raise CLIError('Refusing to write to symlink target.')
parent = os.path.dirname(existing_path) or '.'
tmp_fd, tmp_path = tempfile.mkstemp(dir=parent)
try:
with os.fdopen(tmp_fd, 'w') as f:
yaml.safe_dump(data, f)
os.chmod(tmp_path, 0o600)
os.replace(tmp_path, existing_path)
finally:
# handle cleanup if needed
pass
Apply this standard in code reviews by asking: (1) What untrusted data is reaching a command string, URL, or file path? (2) Is it validated/sanitized/escaped correctly? (3) Are secrets redacted in logs/output? (4) Are there protections against path/symlink and unintended binary execution? (5) Are auth/TLS and URL restrictions enforced safely?