Fix silent Keychain failures and form-encoding in auth - #26
Merged
Conversation
Three code-audit fixes in the auth layer, sharing one KeychainStore seam: - KeychainStore: introduce `KeychainStoring` protocol; make `saveCredentials`/`clearAll` throw instead of swallowing errors with `try?`. - AuthManager: `didAuthenticate` now logs a persist failure instead of pretending success; `clearKeychainIfReinstalled` sets the `hasLaunchedBefore` flag only after a confirmed wipe (a failed wipe is logged and retried next launch). The one-time-logout-on-upgrade trade-off is documented in-code (confidence low due to squashed history). - TokenRefresher: log a failed refreshed-credentials persist. - TokenClient.formEncode: use an explicit RFC 3986 unreserved CharacterSet so `+ & = ,` are escaped in a form body (`.urlQueryAllowed` left them raw). Tests (Swift Testing): MockKeychainStore stub; AuthManager persist/wipe failure paths; formEncode escaping. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
KeychainStore: introduce aKeychainStoringprotocol seam;saveCredentialsandclearAllnowthrowinstead of swallowing errors withtry?.AuthManager.didAuthenticate: logs a persistence failure (Log.error) instead of pretending success.AuthManager.clearKeychainIfReinstalled: sets thehasLaunchedBeforeflag only after a confirmed Keychain wipe — a failed wipe is logged and retried on the next launch.performLogout's clear is wrapped in a loggingdo/catch.TokenRefresher: logs a failed refreshed-credentials persist rather than discarding it.TokenClient.formEncode: uses an explicit RFC 3986 unreservedCharacterSet(hoisted to astatic let) so+ & = ,are escaped in the form body;.urlQueryAllowedleft them raw (notably+→ space).MockKeychainStorestub;AuthManagerpersist/wipe failure paths;formEncodeescaping.Why
A Keychain write failure was silently swallowed, so a user who completed login (or a background token refresh) could be dropped to onboarding with no explanation on the next cold launch — the "authenticated ⇒ persisted" invariant was violated invisibly. Separately,
clearKeychainIfReinstalledset its first-run flag even when the wipe failed (so a failed wipe was never retried), and the form encoder left structural characters unescaped in the OAuth request body.Notes
hasLaunchedBeforeflag, which is equally absent for a legitimate upgrade from a pre-flag build — so such an upgrade triggers a one-time logout. Accepted knowingly for the safer security posture (never inherit foreign credentials). Confidence that a prior build lacked the flag is low (squashed history). No credential-inheritance heuristic was added.didAuthenticatestill marks the session authenticated on a persist failure (usable now, logged loudly) — the conservative option, not an abort. Flagged in case a hard precondition is preferred.formEncodewas madeinternal(fromprivate) so it can be unit-tested via@testable import.HemeraTestssuite green (9 new tests) on iPhone 16 Pro (iOS 18.4).