Skip to content

Commit

Permalink
Rewrite command validation so it get recognize by semgrep.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdwhdw committed Nov 19, 2024
1 parent 892edae commit 5fb0882
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion host_modules/docker_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def run(self, image, command, kwargs):
client = docker.from_env()
if not DockerService.validate_image(image):
return errno.EPERM, "Image {} is not allowed.".format(image)
if not DockerService.validate_command(command):
return errno.EPERM, "Command {} is not allowed.".format(command)
container = client.containers.run(image, command, **kwargs)
return 0, "Container {} has been started.".format(container.name)
except docker.errors.ImageNotFound:
Expand Down Expand Up @@ -138,4 +140,20 @@ def validate_image(image):
"""
base_image_name = image.split(":")[0]
known_images = DockerService.get_used_images_name()
return base_image_name in known_images
return base_image_name in known_images


@staticmethod
def validate_command(command):
"""
Validate the command.
Args:
command (str): The command to run in the container.
Returns:
bool: True if the command is allowed to be use for run/create command.
"""
if command != "":
return False
return True

0 comments on commit 5fb0882

Please sign in to comment.