Skip to content

fix shell quoting #8641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/util/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ Date: August 2012
// clang-format on
#else

#include <cstring>
#include <cerrno>
#include <cstdio>
#include <cstdlib>

#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
# include <sys/stat.h>
# include <sys/wait.h>

# include <cctype>
# include <cerrno>
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <fcntl.h>
# include <signal.h>
# include <unistd.h>

#endif

Expand Down Expand Up @@ -486,18 +487,18 @@ std::string shell_quote(const std::string &src)

// first check if quoting is needed at all

if(src.find(' ')==std::string::npos &&
src.find('"')==std::string::npos &&
src.find('*')==std::string::npos &&
src.find('$')==std::string::npos &&
src.find('\\')==std::string::npos &&
src.find('?')==std::string::npos &&
src.find('&')==std::string::npos &&
src.find('|')==std::string::npos &&
src.find('>')==std::string::npos &&
src.find('<')==std::string::npos &&
src.find('^')==std::string::npos &&
src.find('\'')==std::string::npos)
bool quotes_needed = false;

if(src.empty())
quotes_needed = true;
else
{
for(auto &ch : src)
if(!isalnum(ch) && ch != '_' && ch != '.' && ch != '/' && ch != '-')
quotes_needed = true;
}

if(!quotes_needed)
{
// seems fine -- return as is
return src;
Expand Down
18 changes: 18 additions & 0 deletions unit/util/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ Author: Michael Tautschnig

#include <fstream>

SCENARIO("shell_quote() escaping", "[core][util][run]")
{
#ifdef _WIN32
REQUIRE(shell_quote("foo.bar") == "foo.bar");
REQUIRE(shell_quote("foo&bar") == "\"foo&bar\"");
REQUIRE(shell_quote("foo(bar)") == "\"foo(bar)\"");
REQUIRE(shell_quote("foo\"bar") == "\"foo\"\"bar\"");
#else
REQUIRE(shell_quote("foo.bar") == "foo.bar");
REQUIRE(shell_quote("foo/bar") == "foo/bar");
REQUIRE(shell_quote("--foo") == "--foo");
REQUIRE(shell_quote("") == "''");
REQUIRE(shell_quote("foo\nbar") == "'foo\nbar'");
REQUIRE(shell_quote("foo(bar)") == "'foo(bar)'");
REQUIRE(shell_quote("foo'bar") == "'foo'\\'''bar'");
#endif
}

SCENARIO("run() error reporting", "[core][util][run]")
{
GIVEN("A command invoking a non-existent executable")
Expand Down
Loading