sec(installer): escape server path in pgrep pattern to prevent injection/regex issues

main
David Sarno 2025-08-24 09:46:54 -07:00
parent 175d5ae150
commit 742d168b51
1 changed files with 22 additions and 1 deletions

View File

@ -368,10 +368,11 @@ namespace MCPForUnity.Editor.Helpers
if (string.IsNullOrEmpty(serverSrcPath)) return; if (string.IsNullOrEmpty(serverSrcPath)) return;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return;
string safePath = EscapeForPgrep(serverSrcPath);
var psi = new System.Diagnostics.ProcessStartInfo var psi = new System.Diagnostics.ProcessStartInfo
{ {
FileName = "/usr/bin/pgrep", FileName = "/usr/bin/pgrep",
Arguments = $"-f \"uv .*--directory {serverSrcPath}\"", Arguments = $"-f \"uv .*--directory {safePath}\"",
UseShellExecute = false, UseShellExecute = false,
RedirectStandardOutput = true, RedirectStandardOutput = true,
RedirectStandardError = true, RedirectStandardError = true,
@ -406,6 +407,26 @@ namespace MCPForUnity.Editor.Helpers
catch { return null; } catch { return null; }
} }
// Escape regex metacharacters so the path is treated literally by pgrep -f
private static string EscapeForPgrep(string path)
{
if (string.IsNullOrEmpty(path)) return path;
// Escape backslash first, then regex metacharacters
string s = path.Replace("\\", "\\\\");
char[] meta = new[] {'.','+','*','?','^','$','(',')','[',']','{','}','|'};
var sb = new StringBuilder(s.Length * 2);
foreach (char c in s)
{
if (Array.IndexOf(meta, c) >= 0)
{
sb.Append('\\');
}
sb.Append(c);
}
// Also escape double quotes which we wrap the pattern with
return sb.ToString().Replace("\"", "\\\"");
}
private static int CompareSemverSafe(string a, string b) private static int CompareSemverSafe(string a, string b)
{ {
try try