SharpNetSH icon indicating copy to clipboard operation
SharpNetSH copied to clipboard

Strings are not escaped

Open MadbHatter opened this issue 8 years ago • 1 comments

Currently, using Method("Local Area Network") will result in ... method argument=Local Area Network. Doing so will result in an error because how the parsing of the cmd works.

In your test code, you manually escaped your input strings via "\"", which seems a bit counter-intuitive.

A little check if the string contains a space and is not escaped would go a long way. Is there a particular reason why strings that contain spaces are not automatically escaped?

Also this command (WLAN/SetActionTests.cs => VerifyProfileParameterOutput) would not execute correctly (interface sl is not escaped) and from what I get all your test commands are supposed to work (with correct parameters and names).

Assert.AreEqual(harness.Value, "netsh wlan set profileparameter name=profile_name interface=interface sl SSIDname=ssid1 autoSwitch=no ConnectionMode=auto");

PS: Sorry for my poor English and double sorry If I misused the issues function somehow (I'm somewhat new to github).

MadbHatter avatar Jul 15 '17 12:07 MadbHatter

The same issue exists for the username parameter in Ignite.SharpNetSH.NetSH.CMD.Http.Add.UrlAcl(...)

Changing the /Utilities/ActionProxy.cs ActionProxy.ProcessParameters accordingly should fix those issues and retain backward compatibility:

      private string ProcessParameters(MethodBase method, IMethodCallMessage methodCall)
      {
         var results = new List<string>();
         var i = 0;
         foreach (var value in methodCall.InArgs)
         {
            var parameter = method.GetParameters().FirstOrDefault(x => x.Name == methodCall.GetInArgName(i));
            var parameterName = parameter.GetParameterName();
            i++;

            if (value == null) continue;

            var parameterValue = string.Empty;
            if (value is bool)
               // We have to process booleans differently based upon the configured boolean type (i.e. Yes/No, Enable/Disable, True/False outputs)
               parameterValue = parameter.GetBooleanType().GetBooleanValue((bool)value);
            else if (value is Guid)
               // Guids have to contain braces
               parameterValue = ((Guid)value).ToString("B");
            else if (value.GetType().IsEnum)
               // Enums might be configured with a custom description to change how to output their text
               parameterValue = value.GetDescription();
            else
               // Otherwise it's a stringable (i.e. ToString()) property
               parameterValue = value.ToString();

            // Values containing spaces have to be added with quotation marks (as long as they are not already present)
            if (parameterValue.Contains(" ") && !parameterValue.StartsWith("\"") && !parameterValue.EndsWith("\""))
               results.Add(parameterName + "=\"" + parameterValue + "\"");
            else
               results.Add(parameterName + "=" + parameterValue);
         }
         if (results.Count == 0) return method.GetMethodName();
         return method.GetMethodName() + " " + results.Aggregate((x, y) => string.IsNullOrWhiteSpace(x) ? y : x + " " + y);
      }

...sorry for just copying the code instead of creating a PR.

hypetsch avatar Jan 23 '19 10:01 hypetsch