From 3a1d55ba51f7d8892bc79906b1108de5607f1b7b Mon Sep 17 00:00:00 2001 From: retoor Date: Sun, 25 Oct 2026 19:57:59 +0100 Subject: [PATCH] fix(cli): spawn git init directly, no shell (MOLO-CLI-001) molo new passed the project name through system("git -C \"" + dir + "\" init -q"), a real shell; a name containing a double quote broke out of the quoting and ran arbitrary shell commands (CWE-78). Replace the system() call with fork+execvp passing argv as an array, so there is no shell to parse the name. grep confirms this was the only system() call in scaffolder.cpp. Co-Authored-By: Claude Sonnet 5 --- src/cli/cli/scaffolder.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/cli/cli/scaffolder.cpp b/src/cli/cli/scaffolder.cpp index 7528251..68d0965 100644 --- a/src/cli/cli/scaffolder.cpp +++ b/src/cli/cli/scaffolder.cpp @@ -1,5 +1,9 @@ #include "cli/scaffolder.hpp" +#include +#include +#include + #include #include #include @@ -16,6 +20,29 @@ bool write_file(const std::string& path, const std::string& content) { out << content; return true; } + +// Runs `git -C init -q`, discarding stderr. Spawns the process directly +// (fork+execvp) with argv passed as an array, so there is no shell to parse +// `dir` and no quoting for an attacker-controlled name to break out of. +bool run_git_init(const std::string& dir) { + pid_t pid = fork(); + if (pid < 0) return false; + if (pid == 0) { + int devnull = open("/dev/null", O_WRONLY); + if (devnull >= 0) { + dup2(devnull, STDERR_FILENO); + close(devnull); + } + char* argv[] = {const_cast("git"), const_cast("-C"), + const_cast(dir.c_str()), const_cast("init"), + const_cast("-q"), nullptr}; + execvp("git", argv); + _exit(127); // execvp only returns on failure. + } + int status = 0; + if (waitpid(pid, &status, 0) < 0) return false; + return WIFEXITED(status) && WEXITSTATUS(status) == 0; +} } // namespace ScaffoldResult Scaffolder::scaffold(const std::string& dir, const ScaffoldOptions& opts) const { @@ -90,8 +117,7 @@ ScaffoldResult Scaffolder::scaffold(const std::string& dir, const ScaffoldOption "msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n")) { return out; } - std::string cmd = "git -C \"" + dir + "\" init -q 2>/dev/null"; - if (system(cmd.c_str()) != 0) { + if (!run_git_init(dir)) { out.error = "git init failed in " + dir; return out; }