fix(cli): spawn git init directly, no shell (MOLO-CLI-001)

molo new <name> 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 <noreply@anthropic.com>
This commit is contained in:
2026-10-25 19:57:59 +01:00
co-authored by Claude Sonnet 5
parent 16a0bb0de4
commit 3a1d55ba51
+28 -2
View File
@@ -1,5 +1,9 @@
#include "cli/scaffolder.hpp"
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdlib>
#include <filesystem>
#include <fstream>
@@ -16,6 +20,29 @@ bool write_file(const std::string& path, const std::string& content) {
out << content;
return true;
}
// Runs `git -C <dir> 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<char*>("git"), const_cast<char*>("-C"),
const_cast<char*>(dir.c_str()), const_cast<char*>("init"),
const_cast<char*>("-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;
}