Skip to content

Commit ab1d77a

Browse files
authored
xdg-open filepath is escaped with quotation-marks correctly (#17682)
1 parent a42d874 commit ab1d77a

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/Avalonia.Base/Platform/Storage/FileIO/BclLauncher.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Text.RegularExpressions;
44
using System.Threading.Tasks;
55
using Avalonia.Compatibility;
6-
using Avalonia.Metadata;
76

87
namespace Avalonia.Platform.Storage.FileIO;
98

@@ -44,7 +43,8 @@ private static bool Exec(string urlOrFile)
4443
{
4544
// If no associated application/json MimeType is found xdg-open opens return error
4645
// but it tries to open it anyway using the console editor (nano, vim, other..)
47-
ShellExec($"xdg-open {urlOrFile}", waitForExit: false);
46+
var args = EscapeForShell(urlOrFile);
47+
ShellExecRaw($"xdg-open \\\"{args}\\\"", waitForExit: false);
4848
return true;
4949
}
5050
else if (OperatingSystemEx.IsWindows() || OperatingSystemEx.IsMacOS())
@@ -63,17 +63,18 @@ private static bool Exec(string urlOrFile)
6363
return false;
6464
}
6565
}
66-
67-
private static void ShellExec(string cmd, bool waitForExit = true)
66+
67+
private static string EscapeForShell(string input) => Regex
68+
.Replace(input, "(?=[`~!#&*()|;'<>])", "\\")
69+
.Replace("\"", "\\\\\\\"");
70+
71+
private static void ShellExecRaw(string cmd, bool waitForExit = true)
6872
{
69-
var escapedArgs = Regex.Replace(cmd, "(?=[`~!#&*()|;'<>])", "\\")
70-
.Replace("\"", "\\\\\\\"");
71-
7273
using (var process = Process.Start(
7374
new ProcessStartInfo
7475
{
7576
FileName = "/bin/sh",
76-
Arguments = $"-c \"{escapedArgs}\"",
77+
Arguments = $"-c \"{cmd}\"",
7778
RedirectStandardOutput = true,
7879
UseShellExecute = false,
7980
CreateNoWindow = true,

0 commit comments

Comments
 (0)