Skip to content

fix(ios): harden registerPushToken (empty token unregisters, strict hex)#34

Open
aramslegit wants to merge 1 commit into
crisp-im:masterfrom
aramslegit:fix/push-token-and-show-robustness
Open

fix(ios): harden registerPushToken (empty token unregisters, strict hex)#34
aramslegit wants to merge 1 commit into
crisp-im:masterfrom
aramslegit:fix/push-token-and-show-robustness

Conversation

@aramslegit

Copy link
Copy Markdown
Contributor

Summary

Two small robustness fixes to iOS registerPushToken (ios/ExpoCrispSdkModule.swift). Follow-up to #33 (mentioned there as standalone fixes).

1. Empty token should unregister

guard !token.isEmpty else { return }   // before: empty input is a no-op

Today, passing an empty string early-returns, so there is no way to unregister a push token through the wrapper (e.g. when a user opts out of notifications and you want Crisp to stop routing pushes to the device). The fix treats an empty/whitespace token as "unregister" and clears the device token via CrispSDK.setDeviceToken(Data()).

2. Strict hex parsing

The previous parser silently skipped invalid bytes and accepted odd-length input:

if let byte = UInt8(token[index..<nextIndex], radix: 16) {
  data.append(byte)   // invalid byte → silently dropped
}

A malformed token would register a truncated/garbage device token with no error surfaced to the caller. The fix throws on odd-length or non-hex input (valid APNs tokens are 64 hex chars), so a bad token fails loudly instead of half-registering.

Testing

Both behaviors run in production in a shipping app (1fifty): the empty-token path is used to clear registration on notification opt-out; the strict parse guards the bridge boundary. Verified on a physical iOS device.

Backward compatibility

  • Strict hex parsing: a previously-malformed token now throws instead of silently registering a truncated one. Well-formed tokens are unaffected.
  • Empty token: previously a no-op, now clears the device token. If any caller relied on registerPushToken('') doing nothing, that changes — but a no-op on empty was almost certainly unintended (there was no other way to unregister).

Android's registerPushToken has the same if (token.isNotEmpty()) early-return, but I left it out of this PR because the Android Crisp SDK has no obvious public "clear token" call to mirror setDeviceToken(Data()); happy to address it too if you can point me at the right API.

- An empty token now clears the device token via `setDeviceToken(Data())`
  ("unregister"); previously it early-returned, so there was no way to
  unregister a push token through the wrapper (e.g. on notification opt-out).
- The hex APNs token is parsed strictly: throw on odd-length or non-hex
  input instead of silently skipping invalid bytes, which would register a
  truncated/garbage token with no error surfaced to the caller.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant