Repository navigation
Conversation
…ignedness: ‘const unsigned int’ and ‘int’
…ith a details::launch_browser() helper that avoids invoking a command shell
…njection-vulnerabilities
…escapes embedded " before building cmdLine, closing the argument-injection, which validate the scheme before launching
|
Hi @lo-simon, getting rid of Discussed this with my 🤖 buddy... A few comments on the current approach:
Minor: the brace and Two simpler alternatives: A. Don't launch a browser from the library. B. Keep the launch, but check the scheme first and use each OS's URL-opening mechanism. The scheme check is what fixes the remaining risk. Once Untested sketch: bool launch_browser(const web::uri& uri)
{
if (U("https") != uri.scheme() && U("http") != uri.scheme()) return false;
#if defined(_WIN32)
return reinterpret_cast<INT_PTR>(::ShellExecuteW(nullptr, L"open", uri.to_string().c_str(), nullptr, nullptr, SW_SHOWNORMAL)) > 32;
#else
#if defined(__APPLE__)
const char* executable = "open";
#else
const char* executable = "xdg-open";
#endif
const auto u = utility::us2s(uri.to_string());
char* const argv[] = { const_cast<char*>(executable), const_cast<char*>(u.c_str()), nullptr };
pid_t pid;
if (0 != posix_spawnp(&pid, executable, nullptr, nullptr, argv, environ)) return false;
int status = 0;
return pid == waitpid(pid, &status, 0) && WIFEXITED(status) && 0 == WEXITSTATUS(status);
#endif
}I'd lean towards A if we're happy for the library to stop opening browsers by default, and B otherwise. Both are shorter than the current change, and both close the Co-authored-by: Gareth's 🤖 buddy |
|
Thank you, @garethsb. Yes, option A is definitely a good choice; let the application provide the handler to open the browser. |
This code builds a shell command string by concatenating an untrusted/dynamic value (authorization_code_uri) directly into a command, then executes it with system(). This is a shell injection vulnerability.
Problem:
• " - closes the quoted string early, allowing injection of additional shell syntax.
• &, &&, |, ;, `, $( ) - command chaining/substitution.
• On Windows, cmd.exe is particularly dangerous with &, |, ^, and %VAR% expansion.
For example, if the URI string contained:
the resulting command becomes:
The shell sees this as: open the browser to http://example.com/, then run calc.exe as a separate command. An attacker could substitute calc.exe with any arbitrary command (e.g. downloading and executing malware, deleting files, exfiltrating data).
Solution:
• Windows: use
CreateProcessWwith exePath.c_str() as lpApplicationName and the constructed cmdLine as lpCommandLine. No cmd.exe is spawned - the string is parsed only by the target executable's own argument-parsing logic, not a shell. Shell metacharacters (&, |, ;, backtick, $(), etc.) are inert here.• POSIX: use
posix_spawnpwith an explicit argv[] - the URI is passed as a single argument, never interpreted by /bin/sh.This directly addresses the original vulnerability (shell metacharacter injection via system()).