tikhop / TPInAppReceipt

Reading and Validating In App Purchase Receipt Locally.
MIT License
631 stars 93 forks source link

Fix a crash on Mac which has no `en0` interface for Ethernet/Wi-Fi #90

Closed mshibanami closed 2 years ago

mshibanami commented 3 years ago

Hi,

I noticed that TPInAppReceipt crashes an app when verifying a receipt's hash with InAppReceipt's verifyHash() on a Mac that has no en0 for its Ethernet/Wi-Fi. This PR is for fixing it.

Problem details

Currently, TPInAppReceipt makes the app crash on some Macs because of this line (guidData is nil):

https://github.com/tikhop/TPInAppReceipt/blob/22d7d92/Sources/Validation.swift#L329

I confirmed that the crash is caused because some machines have en1 rather than en0 for their Ethernet/Wi-Fi interface. So TPInAppReceipt actually needs to check en1 in this line for them:

https://github.com/tikhop/TPInAppReceipt/blob/22d7d92/Sources/Validation.swift#L283

Otherwise, iterator becomes 0 (IO_OBJECT_NULL).

Implementation

With the change of this PR, TPInAppReceipt checks a builtin en1 and a non-builtin en0 as fallback interfaces. I got most code from here, then reformatted it to align the style with the code base: https://developer.apple.com/documentation/appstorereceipts/validating_receipts_on_the_device

Also, I changed the type of the return value of uuid() to Data? so that no potential crashes anymore.

I tested this change with built-in en0 and build-in en1 but sorry, I couldn't test it for non built-in en0 since I have no Mac like that but it should work fine.


This library helps my life a lot. Thanks so much for maintaining it!

guidedways commented 3 years ago

I'm seeing the same crash in our app

tikhop commented 3 years ago

Hi @mshibanami, I'm sorry, I totally missed your PR. I'll release new version with a fix next week. Thank you and sorry again.

mshibanami commented 3 years ago

No problems, thanks!

tikhop commented 2 years ago

Hi @mshibanami,

Thanks again for PR, I've reviewed the code and found it similar to the code I got from Apple Technical Support Team a while ago. I've pushed the implementation on master branch and going to release it in a few.

I appreciate the time you’ve taken to fix this issue and thanks again for bringing it up.

mshibanami commented 2 years ago

Oh sorry @tikhop, I didn't track this PR since I personally resolved the issue. I tried the latest version and then it worked great for me. Thanks so much! By the way, thanks to your work, I released my macOS app. I'm now porting it to iOS and this library works great on iOS too. Thanks heaps. 🙂