Yubico / python-fido2

Provides library functionality for FIDO 2.0, including communication with a device over USB.
BSD 2-Clause "Simplified" License
432 stars 109 forks source link

macOS: add early return to __del__ #95

Closed claui closed 4 years ago

claui commented 4 years ago

I ran into this situation on my Mac when __init__ failed before obtaining a handle. In that case, the newly-constructed object would have no device_handle attribute.

When the time comes to destroy the object, __del__ assumes that self.device_handle exists. That causes the following unhelpful user-facing error:

$ ykman fido info
Exception ignored in: <function MacOsHidDevice.__del__ at 0x10702b4c0>
Traceback (most recent call last):
  File "/usr/local/Cellar/ykman/3.1.1_1/libexec/lib/python3.8/site-packages/fido2/_pyu2f/macos.py", line 469, in __del__
    self.device_handle,
AttributeError: 'MacOsHidDevice' object has no attribute 'device_handle'
[…]
Error: Failed connecting to YubiKey 5 [OTP+FIDO+CCID]. Make sure the application have the required permissions.

One possible failure mode is the bug that #94 attempts to fix.
There are still other legitimate reasons for __init__ to fail and return early.

This PR adds a check to __del__ and an early return.

dainnilsson commented 4 years ago

Thanks!