CVE-2023-39523: Improper Neutralization of Special Elements used in a Command ('Command Injection') (CWE-77)
value: ScanCode.io is a server to script and automate software composition analysis with ScanPipe pipelines. Prior to version 32.5.1, the software has a possible command injection vulnerability in the docker fetch process as it allows to append malicious commands in the `docker_reference` parameter.

In the function `scanpipe/pipes/fetch.py:fetch_docker_image` the parameter `docker_reference` is user controllable. The `docker_reference` variable is then passed to the vulnerable function `get_docker_image_platform`.  However, the `get_docker_image_plaform` function constructs a shell command with the passed `docker_reference`. The `pipes.run_command` then executes the shell command without any prior sanitization, making the function vulnerable to command injections. A malicious user who is able to create or add inputs to a project can inject commands. Although the command injections are blind and the user will not receive direct feedback without logs, it is still possible to cause damage to the server/container. The vulnerability appears for example if a malicious user adds a semicolon after the input of `docker://;`, it would allow appending malicious commands.

Version 32.5.1 contains a patch for this issue. The `docker_reference` input should be sanitized to avoid command injections and, as a workaround, one may avoid creating commands with user controlled input directly.
CWE description: The product constructs all or part of a command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended command when it is sent to a downstream component.
CVSS v3 base score: 8.8
Published: 2023-08-07T21:15Z
Repository: https://github.com/nexB/scancode.io
Language: Python
Files changed: scanpipe/pipes/__init__.py, scanpipe/pipes/fetch.py
Commit date: 2023-08-07 16:38:33 +0200
Commit message: Merge pull request from GHSA-2ggp-cmvm-f62f

- Regex that validates docker_reference variable
- Securing against possible command injection
Diff: diff --git a/scanpipe/pipes/__init__.py b/scanpipe/pipes/__init__.py
index be85f56..f20fadc 100644
--- a/scanpipe/pipes/__init__.py
+++ b/scanpipe/pipes/__init__.py
@@ -276,11 +276,11 @@ def run_command(cmd, log_output=False):
         exitcode, output = subprocess.getstatusoutput(cmd)
         return exitcode, output
 
     process = subprocess.Popen(
         cmd,
-        shell=True,
+        shell=False,
         stdout=subprocess.PIPE,
         stderr=subprocess.STDOUT,
         universal_newlines=True,
     )
 
diff --git a/scanpipe/pipes/fetch.py b/scanpipe/pipes/fetch.py
index d17791b..d795deb 100644
--- a/scanpipe/pipes/fetch.py
+++ b/scanpipe/pipes/fetch.py
@@ -22,10 +22,11 @@
 
 import cgi
 import json
 import logging
 import os
+import re
 import tempfile
 from collections import namedtuple
 from pathlib import Path
 from urllib.parse import urlparse
 
@@ -188,10 +189,15 @@ def fetch_docker_image(docker_reference, to=None):
     docker:// reference URL. Return a `download` object.
 
     Docker references are documented here:
     https://github.com/containers/skopeo/blob/0faf16017/docs/skopeo.1.md#image-names
     """
+
+    whitelist = r"^docker://[a-zA-Z0-9_.:/@-]+$"
+    if not re.match(whitelist, docker_reference):
+        raise ValueError("Invalid Docker reference.")
+
     name = python_safe_name(docker_reference.replace("docker://", ""))
     filename = f"{name}.tar"
     download_directory = to or tempfile.mkdtemp()
     output_file = Path(download_directory, filename)
     target = f"docker-archive:{output_file}"

Vulnerable code:         shell=True,
Fixed code:         shell=False,
import re

    whitelist = r"^docker://[a-zA-Z0-9_.:/@-]+$"
    if not re.match(whitelist, docker_reference):
        raise ValueError("Invalid Docker reference.")