Yubico / Yubico.NET.SDK

A YubiKey SDK for .NET developers
Apache License 2.0
98 stars 48 forks source link

[BUG] KeyNotFoundException in CmDevice causes application to quit when plugging / unplugging Yubikey #144

Open canton7 opened 3 weeks ago

canton7 commented 3 weeks ago

Is there an existing issue for this?

Current Behavior

If you plug / unplug a Yubikey a lot (and YubiKeyDeviceListener has been instantiated), it's possible to occasionally hit a KeyNotFoundException in CmDevice.

This exception is thrown on a background thread, meaning that it crashes the user's application, and there is no way for the user to prevent this.

The stack trace is:

Yubico.Core.dll!Yubico.PlatformInterop.CmDevice.GetProperty<string>(Yubico.PlatformInterop.CmDeviceProperty property) Line 112
Yubico.Core.dll!Yubico.PlatformInterop.CmDevice.CmDevice(string devicePath) Line 41
Yubico.Core.dll!Yubico.Core.Devices.Hid.WindowsHidDeviceListener.OnEventReceived(nint hNotify, nint context, Yubico.PlatformInterop.NativeMethods.CM_NOTIFY_ACTION action, nint eventDataPtr, int eventDataSize) Line 97

The line in question is this one, called with CmDeviceProperty.PdoName from here.

image

Expected Behavior

The expected behaviour is that Yubikey does not crash the user's application in an unrecoverable way.

Steps To Reproduce

  1. In a Windows project, subscribe to YubiKeyDeviceListener.Instance.Arrived. I don't think the subscription is actually necessary (just accessing YubiKeyDeviceListener.Instance might be enough), since the YuibKeyDeviceListener ctor calls HidDeviceListener.Create(), which is what instantiates WindowsHidDeviceListener, which is what has the unsafe event handler.
  2. Plug and unplug the Yubikey a lot (might take a number of minutes). Vary the amount of time that the Yubikey is plugged in for, but no need to have it plugged in for more than 2s. Having Yubico Authenticator open at the same time might help reproduce, but I have seen it with nothing else open
  3. Watch your application crash with the above exception

I have seen this pop up two or three times in normal usage.

Version

1.9.0

Version

5.4.3

Anything else?

No response

DennisDyallo commented 3 weeks ago

Adding this to our internal issue tracker. Thanks.