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

miio-extract-tokens raises a TypeError when running against extracted SQLite database #467

Closed
domibarton opened this issue Jan 23, 2019 · 5 comments
Labels

Comments

@domibarton
Copy link
Contributor

When running miio-extract-tokens against an extracted SQLite database from an iOS backup, the script raises a TypeError:

(homeassistant) root@rpi:/opt/homeassistant# miio-extract-tokens /tmp/1672094281_mihome.sqlite
INFO:miio.extract_tokens:Reading database from /tmp/1672094281_mihome.sqlite
INFO:miio.extract_tokens:Reading tokens from Apple DB
Traceback (most recent call last):
  File "/opt/homeassistant/homeassistant/bin/miio-extract-tokens", line 11, in <module>
    sys.exit(main())
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 155, in main
    devices = list(reader.read_tokens(backup))
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 113, in read_tokens
    yield from self.read_apple()
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 74, in read_apple
    token = BackupDatabaseReader.decrypt_ztoken(dev['ZTOKEN'])
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 50, in decrypt_ztoken
    if len(ztoken) <= 32:
TypeError: object of type 'NoneType' has no len()

Please note this is a follow up of the PR #460, as I want to leave issue-related discussions in the issue section.

I'll post more as soon as I've done some additional testing.

@rytilahti rytilahti added the bug label Jan 23, 2019
@domibarton
Copy link
Contributor Author

@rytilahti I think I found the bug:

(homeassistant) root@rpi:/opt/homeassistant# miio-extract-tokens --dump-raw /tmp/1672094281_mihome.sqlite
…
INFO:miio.extract_tokens:{'ZADMINFLAG': 1,
 'ZBSSID': '',
 'ZDESC': 'Tap to connect',
 'ZDID': 'fimi.camera.c1_lanapcommondid',
 'ZEXTFWVERSION': None,
 'ZEXTISPWDENCRYPT': None,
 'ZEXTISSETPINCODE': None,
 'ZEXTKEYID': None,
 'ZEXTMCUVERSION': None,
 'ZEXTNEEDVERIFYCODE': None,
 'ZEXTP2PID': None,
 'ZEXTTOKEN': None,
 'ZISFROMSERVER': 0,
 'ZISONLINE': 1,
 'ZLATITUDE': 0.0,
 'ZLOCALIP': None,
 'ZLONGITUDE': 0.0,
 'ZMAC': '',
 'ZMODEL': 'fimi.camera.c1',
 'ZNAME': 'Mi Action Camera 4K(Hotspot direct connection)',
 'ZPARENT_ID': '',
 'ZPARENT_MODEL': '',
 'ZPERMITLEVEL': 0,
 'ZPID': 0,
 'ZRESETFLAG': 0,
 'ZRSSI': 0,
 'ZSHAREFLAG': 0,
 'ZSHOWMODE': 1,
 'ZSSID': '',
 'ZTOKEN': None,
 'ZTOKENENCRYPT': 1,
 'Z_ENT': 1,
 'Z_OPT': 1,
 'Z_PK': 7636}
Traceback (most recent call last):
  File "/opt/homeassistant/homeassistant/bin/miio-extract-tokens", line 11, in <module>
    sys.exit(main())
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 155, in main
    devices = list(reader.read_tokens(backup))
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 113, in read_tokens
    yield from self.read_apple()
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 74, in read_apple
    token = BackupDatabaseReader.decrypt_ztoken(dev['ZTOKEN'])
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 50, in decrypt_ztoken
    if len(ztoken) <= 32:
TypeError: object of type 'NoneType' has no len()

The action cam has only bluetooth connection, thus no token ('ZTOKEN': None,). I can have a look at the code and send a PR if you like.

@rytilahti
Copy link
Owner

Great! 👍 A PR is very welcome, I think the best way is simply to check for Noneness inside decrypt_token, that way those devices will still be listed in the output.

domibarton added a commit to confirm/python-miio that referenced this issue Jan 24, 2019
The `miio.extra_tokens.BackupDatabaseReader.decrypt_ztoken` method will
raise a `TypeError` when a device in the SQLite database file has no
token defined. This occurs for local (bluetooth) devices, such as the
Mi Action Cam.

This change will fix the `miio-extra-tokens` utility, so that it will
work again, even if there are devices without tokens.

See also issue rytilahti#467 for more informations.
@domibarton
Copy link
Contributor Author

Great! 👍 A PR is very welcome, I think the best way is simply to check for Noneness inside decrypt_token, that way those devices will still be listed in the output.

Done, that should work. I simply check for Noneness as well and typecast it to a str before returning (in favour of None).

@domibarton
Copy link
Contributor Author

FYI I manually tested it and it works again…

rytilahti pushed a commit that referenced this issue Jan 24, 2019
The `miio.extra_tokens.BackupDatabaseReader.decrypt_ztoken` method will
raise a `TypeError` when a device in the SQLite database file has no
token defined. This occurs for local (bluetooth) devices, such as the
Mi Action Cam.

This change will fix the `miio-extra-tokens` utility, so that it will
work again, even if there are devices without tokens.

See also issue #467 for more informations.
@rytilahti
Copy link
Owner

Thanks, this can be closed then!

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

No branches or pull requests

2 participants