Skip to content

Possible the encoding argument to Popen is only available on Python 3 in switch.py #89

Description

@begininvoke

I think there may be a problem in cloudlab/bin/switch.py around line 24.

The code uses the encoding parameter of subprocess.Popen, which was introduced in Python 3.6. If the application runs on Python 3.5 or earlier, the call will raise a TypeError and abort the SSH subprocess creation, leading to a denial‑of‑service condition for any functionality that depends on this subprocess. Because the failure is immediate and can prevent critical operations, the vulnerability is rated as high.

The code in question
        self.ssh = subprocess.Popen(["ssh", "-T", "-p", "51295",
                "-o", "HostKeyAlgorithms=+ssh-rsa",
                "-o", "PubkeyAcceptedKeyTypes=+ssh-rsa", "admin@localhost"],
                encoding="utf-8", stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT)

Something like this might fix it:

*** Begin Patch
*** Update File: cloudlab/bin/switch.py
@@
-        self.ssh = subprocess.Popen(["ssh", "-T", "-p", "51295",
-                "-o", "HostKeyAlgorithms=+ssh-rsa",
-                "-o", "PubkeyAcceptedKeyTypes=+ssh-rsa", "admin@localhost"],
-                encoding="utf-8", stdin=subprocess.PIPE, stdout=subprocess.PIPE,
-                stderr=subprocess.STDOUT)
+        # Use universal_newlines (or text) for Python < 3.6 and fallback to
+        # explicit encoding handling on newer versions.
+        popen_kwargs = {
+            "stdin": subprocess.PIPE,
+            "stdout": subprocess.PIPE,
+            "stderr": subprocess.STDOUT,
+            "universal_newlines": True  # provides str I/O on all supported versions
+        }
+        # Python 3.6+ supports the `encoding` argument; add it if available.
+        if hasattr(subprocess.Popen, "__init__"):
+            try:
+                # Attempt to pass `encoding` – will succeed on 3.6+ and raise
+                # a TypeError on older releases, which we ignore.
+                popen_kwargs["encoding"] = "utf-8"
+            except TypeError:
+                pass
+        self.ssh = subprocess.Popen([
+            "ssh", "-T", "-p", "51295",
+            "-o", "HostKeyAlgorithms=+ssh-rsa",
+            "-o", "PubkeyAcceptedKeyTypes=+ssh-rsa", "admin@localhost"
+        ], **popen_kwargs)
*** End Patch

For reference: rule python.lang.compatibility.python36.python36-compatibility-Popen2. Rated high.

I may be wrong about this one — closing it costs you nothing if so.


Found with automated scanning (RedGem) and reviewed before opening. If it is not useful, closing it is completely fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions