Skip to content

fix(desktop/auth): use the file-based keychain so beta sign-in works (Could not securely store sign-in tokens) - #9167

Merged
eulicesl merged 2 commits into
BasedHardware:mainfrom
eulicesl:fix/desktop-keychain-file-based
Jul 6, 2026
Merged

eulicesl merged 2 commits into
BasedHardware:mainfrom
eulicesl:fix/desktop-keychain-file-based

Conversation

@eulicesl

@eulicesl eulicesl commented Jul 6, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

On the signed/notarized beta, sign-in fails with "Could not securely store sign-in tokens. Please try again." — you cannot log in (screenshot from v0.12.37).

Root cause

DesktopKeychainStore opted into the data-protection keychain (kSecUseDataProtectionKeychain: true), which on macOS requires a keychain-access-groups entitlement. This app is a non-sandboxed Developer ID app with no such entitlement (Omi-Release.entitlements: app-sandbox=false, no keychain-access-groups). So on the signed build every SecItemAdd/SecItemUpdate returned errSecMissingEntitlement (-34018) → saveTokens() threw keychainTokenStorageUnavailable → sign-in blocked. Dev/named builds use UserDefaults (usesKeychainTokenStorage = !AppBuild.isNonProduction), which is why it only failed on prod/beta and slipped through CI.

Fix

Drop kSecUseDataProtectionKeychain so the store uses the default file-based (login) keychain — a signed non-sandboxed app can read/write its own generic-password items there without any keychain entitlement, and tokens still stay out of UserDefaults. (ClientDeviceService already uses this exact pattern in the same app.) This complements #9162's UserDefaults fallback: with the keychain actually succeeding, the fallback becomes a rare safety net instead of the norm.

  • DesktopKeychainStore.swift: remove the data-protection flag from the shared query.
  • AuthTokenStorageTests.swift: source-guard against re-introducing the data-protection keychain (this regression is invisible to dev/CI since dev uses UserDefaults).
  • FailLoudConfigTests.swift: remove the now-inverted testDesktopKeychainStoreUsesDataProtectionKeychain (its correct invariant now lives in the new guard).
  • changelog.

Verification

  • Runtime: signed into a local named bundle with the keychain path forced on and the UserDefaults fallback disabled, so the outcome reflects the real keychain write — sign-in succeeded.
  • Standalone SecItemAdd probe: data-protection keychain → -34018; file-based → 0.
  • Build exit 0; AuthTokenStorageTests 5/5, FailLoudConfigTests 6/6.
  • Independent review: confirmed the file-based login keychain is an adequately secure store for a non-sandboxed Developer ID app (AES-encrypted at rest under the login key + per-item code-signature ACL — a big step up from UserDefaults, and Secure desktop local credential surfaces #9108's goal is still met), and that no migration is needed (no signed build ever wrote a data-protection item).

🤖 Generated with Claude Code

Review in cubic

…gn-in works

Root cause of "Could not securely store sign-in tokens" (beta): DesktopKeychainStore opted into
the data-protection keychain (kSecUseDataProtectionKeychain), which requires a keychain-access-
groups entitlement. This non-sandboxed Developer ID app has no such entitlement
(Omi-Release.entitlements: app-sandbox=false, no keychain-access-groups), so on the signed/
notarized build every SecItemAdd/Update failed with errSecMissingEntitlement (-34018) ->
saveTokens() threw keychainTokenStorageUnavailable -> sign-in blocked. Dev builds use UserDefaults
(usesKeychainTokenStorage = !isNonProduction), so it only ever failed on prod/beta.

Fix: use the default file-based (login) keychain — a signed non-sandboxed app can read/write its
own generic-password items without any keychain entitlement, and tokens still stay out of
UserDefaults. Complements BasedHardware#9162's UserDefaults fallback (which unblocks sign-in but stores tokens
in UserDefaults); with this the keychain actually succeeds, so the fallback isn't needed.

- DesktopKeychainStore: drop kSecUseDataProtectionKeychain from the query.
- AuthTokenStorageTests: source-guard against re-introducing the data-protection keychain
  (invisible to dev/CI since dev uses UserDefaults).
- changelog.

Verified: standalone SecItemAdd probe (data-protection -> -34018, file-based -> 0); build exit 0;
AuthTokenStorageTests 5/5; and runtime — signed into a local named bundle with keychain forced on
and the UserDefaults fallback disabled (so the outcome reflects the real keychain write).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a production/beta-only desktop sign-in failure by switching the macOS token storage Keychain implementation away from the data-protection keychain (which requires entitlements this non-sandboxed Developer ID app doesn’t have) to the default file-based login keychain, and adds a regression guard to prevent reintroducing the problematic flag.

Changes:

  • Removed kSecUseDataProtectionKeychain from DesktopKeychainStore so signed builds can persist tokens without errSecMissingEntitlement (-34018).
  • Added a source-level regression test ensuring DesktopKeychainStore does not opt into the data-protection keychain.
  • Updated fail-loud tests and added a desktop changelog entry describing the beta sign-in fix.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
desktop/macos/Desktop/Tests/FailLoudConfigTests.swift Removes the now-obsolete assertion around using the data-protection keychain and documents where the new invariant is enforced.
desktop/macos/Desktop/Tests/AuthTokenStorageTests.swift Adds a source-level regression test to prevent reintroducing kSecUseDataProtectionKeychain.
desktop/macos/Desktop/Sources/DesktopKeychainStore.swift Drops the data-protection keychain opt-in so signed builds use the login keychain and avoid entitlement failures.
desktop/macos/changelog/unreleased/20260706-fix-beta-keychain-signin.json Adds a changelog fragment for the beta sign-in/keychain storage fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1 issue found across 4 files

Confidence score: 5/5

  • In desktop/macos/Desktop/Sources/DesktopKeychainStore.swift, the current comment appears broader than the actual dev/UserDefaults fallback and could mislead future edits around LocalAgentAPISettings; merged as-is, this is mainly a maintainability/documentation risk rather than a runtime one — narrow the wording to auth-token storage scope before or shortly after merge.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="desktop/macos/Desktop/Sources/DesktopKeychainStore.swift">

<violation number="1" location="desktop/macos/Desktop/Sources/DesktopKeychainStore.swift:14">
P3: This comment overstates the scope of the dev/UserDefaults behavior: `DesktopKeychainStore` is also used by `LocalAgentAPISettings` in dev builds. Narrowing the wording to auth token storage would avoid misleading future debugging of local-agent token keychain writes.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

cubic P3: the dev/UserDefaults caveat is specific to the auth-token path
(usesKeychainTokenStorage = !isNonProduction). Note DesktopKeychainStore is also
used by LocalAgentAPIServer on all builds, whose keychain writes hit the same
entitlement failure and are fixed by the same change.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@eulicesl
eulicesl merged commit 11662e7 into BasedHardware:main Jul 6, 2026
3 checks passed
@eulicesl
eulicesl deleted the fix/desktop-keychain-file-based branch July 6, 2026 21:40
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