Skip to content

fix: invalid cookie data in NSE - WPB-24202#4734

Open
samwyndham wants to merge 16 commits into
developfrom
chore/log-cookie-key-creation-WPB-24202
Open

fix: invalid cookie data in NSE - WPB-24202#4734
samwyndham wants to merge 16 commits into
developfrom
chore/log-cookie-key-creation-WPB-24202

Conversation

@samwyndham
Copy link
Copy Markdown
Contributor

@samwyndham samwyndham commented May 19, 2026

BugWPB-24202 [iOS] Push Issue - Message only appears when opening the app

Issue

Some users are experiencing an issue with notifications not being delivered. Looking through their logs we see repeated issues with invalidCookieData.

[notifications] - [ERROR] Failed to fetch cookies: invalidCookieData(reason: "Error Domain=NSCocoaErrorDomain Code=4864 "*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: incomprehensible archive..."

In our code base this means that we have fetched the cookie data from the keychain but we fail to convert this to cookie data using the NSKeyedUnarchiver. The data should be in the bplist format but it is not (see my comment in the ticket for more info).

From testing this is the exact behavior we would see if we tried to decrypt the cookie with an incorrect cookie key. However strangely this issue only affects the NSE not the main app which both use the same cookie key stored in shared user defaults. The cookie key is created lazily if user defaults doesn't already have one set.

Searching online I found cases where UserDefaults would return nil for an already set value and the best guess at the cause was related to permissions - UserDefaults being accessed before the device was first unlocked after a restart.

In the logs for a user I looked at this happens over and over again. Additionally of note we also have this line for each time a notification is received with the exact same process id:

[INFO] loading new notification service[process_id:319][process_name:Wire Notification Service Extension]

So it is possible that these repeated failures are all happening in the same process.

A hypothesis

This PR attempts to do a possible fix of the following hypothesis that I have not managed to reproduce.

  1. Device is restarted
  2. A notification is received before the device pin has been entered.
  3. The cookies key is requested to instantiate CookiesStorage.
  4. Although there is already a cookie key in UserDefaults (a new one is created on first run after original install), the cookie key is unavailable because the device has not yet been unlocked - therefore a new incorrect key is returned.
  5. As the process is for some reason kept alive each new notification also fails as we continue using the incorrect cookie key.

The fix here is to fail early if we don't have an existing cookie key.

Testing

Ensure that push notifications are still working.


Checklist

  • Title contains a reference JIRA issue number like [WPB-XXX].
  • Description is filled and free of optional paragraphs.
  • Adds/updates automated tests.

@samwyndham samwyndham requested review from johnxnguyen and netbe and removed request for netbe May 19, 2026 12:59
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 19, 2026

Test Results

2 173 tests   2 146 ✅  2m 11s ⏱️
  349 suites     27 💤
    2 files        0 ❌

Results for commit 83d8f54.

♻️ This comment has been updated with latest results.

Summary: workflow run #26153092219
Allure report (download zip): html-report-30140-chore_log-cookie-key-creation-WPB-24202

Comment on lines +109 to +112
WireLogger.notifications.critical(
"no cookie encryption key, not loading service",
attributes: .safePublic
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: maybe it's worth stating in the log that this may be expected if the phone restarted and hasn't been unlocked yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to warn level as it may not exactly be an error but might surprise users.

@samwyndham samwyndham requested a review from johnxnguyen May 20, 2026 09:15
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants