Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String Reference not set to an instance of a String: Parameter name: s #382

Closed
Michael-Meredith opened this issue Feb 5, 2018 · 19 comments · Fixed by #1245
Closed

String Reference not set to an instance of a String: Parameter name: s #382

Michael-Meredith opened this issue Feb 5, 2018 · 19 comments · Fixed by #1245
Milestone

Comments

@Michael-Meredith
Copy link

Michael-Meredith commented Feb 5, 2018

I am working on a program that must upload files to various clients it has worked well except for one client.

I have stripped out the relevant code that I am using to make it easier to debug and I have included it below.

I get the error as per the subject line of this submission when I execute it, can anyone provide some clarity for me as to what am doing wrong, I am an old programmer and have learnt to accept that it is invariably my fault:

The error occurs when it tries to execute the connect

The program is written in Visual Basic using Visual Studio 3013

Imports Renci.SshNet

Public Class Form

Private Sub cmdConnect_Click(sender As Object, e As EventArgs) Handles cmdConnect.Click

    Dim strURL As String
    Dim strPort As Integer
    Dim strUser As String
    Dim strPassword As String
 
    strURL = "xxxxxxx"
    strPort = 22
    strUser = "xxx"
    strPassword = "XXXXXXXXXXXX"

    Try

        Dim kauth As New KeyboardInteractiveAuthenticationMethod(strUser)

        Dim pauth As New PasswordAuthenticationMethod(strUser, strPassword)

        Dim ConnNfo As New ConnectionInfo(strURL, strPort, strUser, pauth, kauth)

        Using Sftp As New SftpClient(ConnNfo)

            Sftp.Connect()

        End Using

        MsgBox("Connected")

    Catch ex As Exception

        MsgBox(ex.Message)

    End Try

End Sub

End Class

Regards

Juliana

@drieseng
Copy link
Member

drieseng commented Feb 5, 2018

To be honest: I have no idea what may be causing this exception. Can you share the full stack trace?

@Michael-Meredith
Copy link
Author

Hi,

Here is the stack trace

'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Users\imedsched\documents\visual studio 2013\Projects\TestSFTP\TestSFTP\bin\Debug\TestSFTP.vshost.exe'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Deployment\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualBasic\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 0x19c8 has exited with code 259 (0x103).
The thread 0x1750 has exited with code 259 (0x103).
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Users\imedsched\documents\visual studio 2013\Projects\TestSFTP\TestSFTP\bin\Debug\TestSFTP.exe'. Symbols loaded.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Remoting\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Remoting.dll'. Cannot find or open the PDB file.
'TestSFTP.vshost.exe' (CLR v4.0.30319: TestSFTP.vshost.exe): Loaded 'C:\Users\imedsched\documents\visual studio 2013\Projects\TestSFTP\TestSFTP\bin\Debug\Renci.SshNet.dll'. Module was built without symbols.
A first chance exception of type 'System.ArgumentNullException' occurred in Renci.SshNet.dll
Stack Trace
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at TestSFTP.Form1.cmdConnect_Click(Object sender, EventArgs e) in C:\Users\imedsched\documents\visual studio 2013\Projects\TestSFTP\TestSFTP\Form1.vb:line 45
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at TestSFTP.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Error Message

String reference not set to an instance of a String.
Parameter name: s
The thread 0x1238 has exited with code 259 (0x103).
The thread 0x1e74 has exited with code 259 (0x103).
The thread 0x2220 has exited with code 259 (0x103).

@kshanafelt
Copy link

kshanafelt commented Aug 10, 2018

I found this same error. I pulled down the repo, checked out the tag: "2016.1.0" and built on debug.

System.Management.Automation.MethodInvocationException: Exception calling "Connect" with "0" argument(s): "String reference not set to an instance of a String.
Parameter name: s" ---> System.ArgumentNullException: String reference not set to an instance of a String.
Parameter name: s
   at Renci.SshNet.KeyboardInteractiveAuthenticationMethod.Authenticate(Session session) in C:\src\SSH.NET\src\Renci.SshNet\KeyboardInteractiveAuthenticationMethod.cs:line 78
   at Renci.SshNet.AuthenticationMethod.Renci.SshNet.IAuthenticationMethod.Authenticate(ISession session) in C:\src\SSH.NET\src\Renci.SshNet\AuthenticationMethod.cs:line 61
   at Renci.SshNet.ClientAuthentication.TryAuthenticate(ISession session, AuthenticationState authenticationState, String[] allowedAuthenticationMethods, SshAuthenticationException& authenticationException) in C:\src\SSH.NET\src\Renci.SshNet\ClientAuthentication.cs:line 116
   at Renci.SshNet.ClientAuthentication.Authenticate(IConnectionInfoInternal connectionInfo, ISession session) in C:\src\SSH.NET\src\Renci.SshNet\ClientAuthentication.cs:line 66
   at Renci.SshNet.ConnectionInfo.Authenticate(ISession session, IServiceFactory serviceFactory) in C:\src\SSH.NET\src\Renci.SshNet\ConnectionInfo.cs:line 414
   at Renci.SshNet.Session.Connect() in C:\src\SSH.NET\src\Renci.SshNet\Session.cs:line 682
   at Renci.SshNet.BaseClient.Connect() in C:\src\SSH.NET\src\Renci.SshNet\BaseClient.cs:line 214
   at CallSite.Target(Closure , CallSite , Object )
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

This was less than helpfull since C:\src\SSH.NET\src\Renci.SshNet\KeyboardInteractiveAuthenticationMethod.cs:line 78 is the retun statement of that method.

I thought that the throw a couple lines above might be messing the the stacktrace. So I added a Console.WriteLine(exp.StackTrace); to, line 129, where _exception was geting set.

This gave a more informative stacktrace:

Found excepting System.ArgumentNullException: String reference not set to an instance of a String.
Parameter name: s
   at System.Text.Encoding.GetBytes(String s)
   at Renci.SshNet.Common.SshDataStream.Write(String s, Encoding encoding) in C:\src\SSH.NET\src\Renci.SshNet\Common\SshDataStream.cs:line 160
   at Renci.SshNet.Common.SshData.Write(String data, Encoding encoding) in C:\src\SSH.NET\src\Renci.SshNet\Common\SshData.cs:line 339
   at Renci.SshNet.Common.SshData.Write(String data) in C:\src\SSH.NET\src\Renci.SshNet\Common\SshData.cs:line 327
   at Renci.SshNet.Messages.Authentication.InformationResponseMessage.SaveData() in C:\src\SSH.NET\src\Renci.SshNet\Messages\Authentication\InformationResponseMessage.cs:line 53
   at Renci.SshNet.Common.SshData.WriteBytes(SshDataStream stream) in C:\src\SSH.NET\src\Renci.SshNet\Common\SshData.cs:line 81
   at Renci.SshNet.Messages.Message.WriteBytes(SshDataStream stream) in C:\src\SSH.NET\src\Renci.SshNet\Messages\Message.cs:line 43
   at Renci.SshNet.Messages.Message.GetPacket(Byte paddingMultiplier, Compressor compressor) in C:\src\SSH.NET\src\Renci.SshNet\Messages\Message.cs:line 84
   at Renci.SshNet.Session.SendMessage(Message message) in C:\src\SSH.NET\src\Renci.SshNet\Session.cs:line 864
   at Renci.SshNet.KeyboardInteractiveAuthenticationMethod.<>c__DisplayClass14_0.<Session_UserAuthenticationInformationRequestReceived>b__0() in C:\src\SSH.NET\src\Renci.SshNet\KeyboardInteractiveAuthenticationMethod.cs:line 126

@kshanafelt
Copy link

Looks like the problem is in writing the InformationResponseMessage to the SshDataStream.

I thought I was setting up an AuthenticationPrompt EventHandler to provide a response. I'll have to check that I am doing that correctly.

@kshanafelt
Copy link

Looks like my problem is powershell: it doesn't allow for a synchronous event handling.
https://wensveen.wordpress.com/2012/11/12/synchronous-net-event-handling-with-powershell/

@kshanafelt
Copy link

Outside of powershell's (lack of) event handling support, the most likely cause of this error is using a KeyboardInteractiveAuthenticationMethod (or a KeyboardInteractiveConnectionInfo) without registering an AuthenticationPrompt event handler.

Since if there is no AuthenticationPrompt or it comes back with an 'AuthenticationPrompt.Response' thats null, it will bubble up to SshDataStream.Write and fail to GetBytes from null.

I think the fix is to either test 'AuthenticationPrompt.Response' for null and fail early with a friendly message, or initialize 'AuthenticationPrompt.Response' to an empty string before passing it to the handler (i.e. assume no password, I guess), or both.

@darkoperator
Copy link

darkoperator commented Aug 10, 2018 via email

@VinsonY
Copy link

VinsonY commented Sep 26, 2018

The issue I had stemmed from trying to confirm the password prompt. For some reason the system was able to recognize that there was a prompt but had a null string in the prompt request. If you catch for null and send the password anyways, which I don't recommend, it may work.

@amitbarkai
Copy link

I've opened another tread, since I thought this is a version-related issue , same goes for 2020.2
is there any workaround for this issue ?
`Parameter name: s

at Renci.SshNet.KeyboardInteractiveAuthenticationMethod.Authenticate(Session session)

at Renci.SshNet.ClientAuthentication.TryAuthenticate(ISession session, AuthenticationState authenticationState, String[] allowedAuthenticationMethods, SshAuthenticationException& authenticationException)

at Renci.SshNet.ClientAuthentication.Authenticate(IConnectionInfoInternal connectionInfo, ISession session)

at Renci.SshNet.ConnectionInfo.Authenticate(ISession session, IServiceFactory serviceFactory)

at Renci.SshNet.Session.Connect()

at Renci.SshNet.BaseClient.CreateAndConnectSession()

at Renci.SshNet.BaseClient.Connect()`

@amitbarkai
Copy link

i think i've found the issue which might point to a BUG
in my case the authentication method order was :

keyboard-interactive
username password
code example :
`
// Create KeyboardInteractiveAuthenticationMethod
var keyboardAuth = new KeyboardInteractiveAuthenticationMethod(username);
keyboardAuth.AuthenticationPrompt += keyboardAuthentication_AuthenticationPrompt;

// Create ConnectionInfo with both authentication methods
var connectionInfo = new ConnectionInfo(host, port, username, keyboardAuth, passwordAuth);

`

when there is an issue / authentication method not defined
"If there is no prompt defined on the server for keyboard-interactive authentication, it could potentially lead to authentication failures, but it's not the typical cause for a NullReferenceException within the SSH.NET"

when i've change the order / removed the user interactive everything worked fine

@Rob-Hague
Copy link
Collaborator

@amitbarkai Thanks. In your keyboardAuthentication_AuthenticationPrompt, are you setting the values of Prompt.Response in the event args? Not setting those values appears to be the reason for the error as pointed out above.

As for the order of the authentication methods, the library tries them in the order you have specified. It's plausible that when you specify passwordAuth first, the password authentication succeeds and the keyboard-interactive method is not used.

If you think there is still something wrong (there could be), you would be welcome to try and demonstrate using an integration test. You can find examples in the AuthenticationTests class of the integration tests project.

@amitbarkai
Copy link

@Rob-Hague yes
this is the implementation for the prompt event
`
private void keyboardAuthentication_AuthenticationPrompt(object sender, Renci.SshNet.Common.AuthenticationPromptEventArgs e)
{
if (ConnectionInfo.AuthenticationMethod is PasswordAuthenticationMethod)
{
var passwordConnection = (PasswordAuthenticationMethod)ConnectionInfo.AuthenticationMethod;
var passwordRegex = new Regex(IOC.Get.Service().LoginPasswordPrompt, RegexOptions.Multiline | RegexOptions.IgnoreCase);

           // Write To Log

            foreach (var prompt in e.Prompts)
            {
                // Write To Log
                if (passwordRegex.IsMatch(prompt.Request))
                {
                    prompt.Response = passwordConnection.Password.GetStringFromSecureString();
                }
                else
                {
                   // Write To Log
                }
            }
        }
    }

`

this issue happens for both 2016 and 2020

since this issue happens for one of our customers , we where not able to reproduce it yet.
but it is clearly related to the machine setting / domain settings
preventing the Keyboard-interactive from working.

it sound like an edge case , but still a bug :)
when we removed the Keyboard-Interactive from the method list the ssh.connect worked without any issue.

@amitbarkai
Copy link

another important note .
this issue happens for Domain Users .
when we tried to connect via local user everything worked fine.

also what made this issue waired :) , is that when i've tried to connect via PuTTy it worked fine for both local and domain users

@Rob-Hague
Copy link
Collaborator

Thanks, I have annotated your code with what could be causing the error. prompt.Response must always be set to not-null, otherwise the error will appear (a weird design).

foreach (var prompt in e.Prompts)
{
    // Write To Log
    if (passwordRegex.IsMatch(prompt.Request))
    {
        prompt.Response = passwordConnection.Password.GetStringFromSecureString();
+      // If the value of passwordConnection.Password.GetStringFromSecureString() is null, you will get the error.
    }
    else
    {
       // Write To Log
+      // If you do not set prompt.Response here, you will get the error.
    }
}

@amitbarkai
Copy link

amitbarkai commented Nov 19, 2023 via email

@Rob-Hague
Copy link
Collaborator

The exception is actually originally thrown on a different thread and then re-thrown in KeyboardInteractiveAuthenticationMethod.Authenticate, so that's why the stack trace looks strange. I expect that if you use version 2023.0.0, the stack trace would look slightly different (because of #1182).

@amitbarkai
Copy link

amitbarkai commented Nov 19, 2023 via email

@Rob-Hague
Copy link
Collaborator

You will still get the error in 2023.0.0, but the stack trace might be different.

I can't see the image in your comment. Maybe we can continue the discussion in your issue #1237 instead of this one.

@WojciechNagorski
Copy link
Collaborator

The 2023.0.1 version has been released to Nuget: https://www.nuget.org/packages/SSH.NET/2023.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants