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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user