Skip to content

Commit 82a321c

Browse files
committed
security: fix command injection vulnerability in container_dispatcher.py
- Add input validation for environment variables (SEMGREP-1211) - Validate Python version format (3.10-3.14) - Validate container registry format - Validate image name format - Validate semantic version format - Prevents command injection via malicious environment variables Fixes: python.lang.security.audit.dangerous-subprocess-use-tainted-env-args
1 parent 5a202d5 commit 82a321c

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

dev-tools/container/container_dispatcher.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,39 @@
22
"""Container management dispatcher for the Open Resource Broker."""
33

44
import os
5+
import re
56
import subprocess
67
import sys
78

89

10+
def validate_python_version(version):
11+
"""Validate Python version format (e.g., 3.10, 3.11, 3.12, 3.13, 3.14)."""
12+
if not re.match(r'^3\.(1[0-4]|[0-9])$', version):
13+
raise ValueError(f"Invalid Python version: {version}")
14+
return version
15+
16+
17+
def validate_registry(registry):
18+
"""Validate container registry format."""
19+
if not re.match(r'^[a-zA-Z0-9.-]+(?:\:[0-9]+)?$', registry):
20+
raise ValueError(f"Invalid registry: {registry}")
21+
return registry
22+
23+
24+
def validate_image_name(name):
25+
"""Validate container image name."""
26+
if not re.match(r'^[a-z0-9-]+$', name):
27+
raise ValueError(f"Invalid image name: {name}")
28+
return name
29+
30+
31+
def validate_version(version):
32+
"""Validate semantic version format."""
33+
if not re.match(r'^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9-]+)?$', version):
34+
raise ValueError(f"Invalid version: {version}")
35+
return version
36+
37+
938
def parse_args(args):
1039
"""Parse command line arguments."""
1140
return {
@@ -19,9 +48,9 @@ def parse_args(args):
1948

2049
def handle_version():
2150
"""Handle version command."""
22-
registry = os.getenv("CONTAINER_REGISTRY", "localhost")
23-
image = os.getenv("CONTAINER_IMAGE", "open-resource-broker")
24-
version_val = os.getenv("VERSION", "0.1.0-dev")
51+
registry = validate_registry(os.getenv("CONTAINER_REGISTRY", "localhost"))
52+
image = validate_image_name(os.getenv("CONTAINER_IMAGE", "open-resource-broker"))
53+
version_val = validate_version(os.getenv("VERSION", "0.1.0-dev"))
2554
print(f"Container Registry: {registry}")
2655
print(f"Container Image: {image}")
2756
print(f"Container Version: {version_val}")
@@ -39,10 +68,10 @@ def handle_run():
3968

4069
def handle_single():
4170
"""Handle single platform build."""
42-
python_version = os.getenv("PYTHON_VERSION", "3.12")
43-
registry = os.getenv("CONTAINER_REGISTRY", "localhost")
44-
image = os.getenv("CONTAINER_IMAGE", "open-resource-broker")
45-
version_val = os.getenv("VERSION", "0.1.0-dev")
71+
python_version = validate_python_version(os.getenv("PYTHON_VERSION", "3.12"))
72+
registry = validate_registry(os.getenv("CONTAINER_REGISTRY", "localhost"))
73+
image = validate_image_name(os.getenv("CONTAINER_IMAGE", "open-resource-broker"))
74+
version_val = validate_version(os.getenv("VERSION", "0.1.0-dev"))
4675

4776
cmd = [
4877
"docker",

0 commit comments

Comments
 (0)