aws/amazon-freertos-ble-ios-sdk

CBOR encoding crashes when saving a network SSID with UTF characters

dgranahan opened this issue · 5 comments

If you call saveNetwork with a network that has an SSID or a password that contains UTF character this results in a crash in the CBOR encoding library.

Here is the stack trace of the crash:

EXC_BREAKPOINT

amazon-freertos-ble-ios-sdk/AmazonFreeRTOS/SwiftCBOR/Extensions.swift:126:35 Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value
amazon-freertos-ble-ios-sdk/AmazonFreeRTOS/SwiftCBOR/Extensions.swift:144:29 ascii_bytes.get
merged @objc (extension in AmazonFreeRTOS):__C.NSNumber.encode() -> Swift.String
amazon-freertos-ble-ios-sdk/AmazonFreeRTOS/SwiftCBOR/Encoder.swift:35:26 getIncludedEncodings
amazon-freertos-ble-ios-sdk/AmazonFreeRTOS/SwiftCBOR/Encoder.swift:96:66 encode
merged @objc (extension in AmazonFreeRTOS):__C.NSNumber.encode() -> Swift.String
amazon-freertos-ble-ios-sdk/AmazonFreeRTOS/SwiftCBOR/CBOR.swift:16:22 encode
amazon-freertos-ble-ios-sdk/AmazonFreeRTOS/AmazonFreeRTOSManager.swift encode
amazon-freertos-ble-ios-sdk/AmazonFreeRTOS/AmazonFreeRTOSDevice.swift:292:38 saveNetwork

It looks like amazon-freertos-ble-ios-sdk uses a copy of https://github.com/hassan-shahbazi/CBORSwift as its CBOR implementation. Unfortunately it looks like this library is no longer being maintained. This CBOR implementation explicitly tries to parse strings using ASCII encoding as seen here: https://github.com/aws/amazon-freertos-ble-ios-sdk/blob/master/AmazonFreeRTOS/SwiftCBOR/Extensions.swift#L126 which is problematic.

One way to fix this is to update the SwiftCBOR implementation in this repo to handle UTF strings. But another option would be to switch to a Swift CBOR library that is actively maintained. What would you propose? Thanks.

@dgranahan

Thanks for bringing this into our attention. We will reproduce this issue and get back to you.

Thanks @ravibhagavandas. Any update on this?

It's fairly easy to reproduce if you change your network SSID to include a character like or \u2019.

@ravibhagavandas any update on this?

Hi @dgranahan ,

Thank you for reporting this. We just fix it by this PR #19 . It adds the support of utf8 to the SwiftCBOR lib and fix a bug of lenght calcutation. Currently the change was only made to the copy of the SwiftCBOR lib that host in our repo. But it should resolve the issue you have.

Thanks!

Regards,
Ming

@dgranahan I'll close this issue for now. If you have any further question please feel free to reopen it or create a new issue to us. Thank you!