Browse Source

More secure command line argument handling for a URL handler.

Douglas William Thrift 11 years ago
parent
commit
d760b7cb33
1 changed files with 21 additions and 14 deletions
  1. 21 14
      ssh-handler/ssh-handler.cs

+ 21 - 14
ssh-handler/ssh-handler.cs

@@ -42,27 +42,33 @@ public class SshHandler
         {
             Regex usage = new Regex(@"^(?:/|--?)(?:h|help|usage|\?)$", RegexOptions.IgnoreCase);
             Regex putty = new Regex(@"^(?:/|--?)putty(?:[:=](?<putty_path>.*))?$", RegexOptions.IgnoreCase);
-            Uri uri = null;
+            IList<string> uriParts = null;
 
             foreach (string arg in args)
-            {
-                if (usage.IsMatch(arg))
-                    return Usage(0);
+                if (uriParts == null)
+                {
+                    if (usage.IsMatch(arg))
+                        return Usage(0);
 
-                Match match;
+                    Match match;
 
-                if ((match = putty.Match(arg)).Success)
-                {
-                    handler = Handler.Putty;
-                    Group group = match.Groups["putty_path"];
-                    if (group.Success)
-                        puttyPath = group.Value;
+                    if ((match = putty.Match(arg)).Success)
+                    {
+                        handler = Handler.Putty;
+                        Group group = match.Groups["putty_path"];
+                        if (group.Success)
+                            puttyPath = group.Value;
+                    }
+                    else
+                        uriParts = new List<string>(new string[] { arg });
                 }
                 else
-                    uri = new Uri(arg, UriKind.Absolute);
-            }
+                    uriParts.Add(arg);
+
+            if (uriParts != null)
+            {
+                Uri uri = new Uri(string.Join(" ", uriParts), UriKind.Absolute);
 
-            if (uri != null)
                 switch (handler)
                 {
                     case Handler.Unspecified:
@@ -75,6 +81,7 @@ public class SshHandler
                         Putty(uri);
                         break;
                 }
+            }
             else
                 return Usage(1);
         }