8000 PM-11697: Fix potential crash when creating a new account by matt-livefront · Pull Request #901 · bitwarden/ios · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

PM-11697: Fix potential crash when creating a new account #901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 6, 2024

Conversation

matt-livefront
Copy link
Collaborator
@matt-livefront matt-livefront commented Sep 5, 2024

🎟️ Tracking

PM-11697

📔 Objective

When creating a new account on a device that has at least one existing account, it's possible for the app to crash when checking the strength of the new account's password.

The root cause is that DefaultClientService maintains a cache of the SDK client as a dictionary, mapping a user ID to their SDK. It's possible for this dictionary to be modified from multiple threads. Making the service an actor fixes the concurrency issue.

I also noticed another issue here. When adding a new account, a user ID doesn't exist for the account yet, so we have some logic that returns a new SDK for these calls, and this SDK doesn't end up being cached. If there's an existing account on the device when adding a new account, that existing account is still considered the active user, so we end up using the existing account's SDK for some operations for the new account. To fix this, I added a isPreAuth property that can be used when getting a SDK client so we always return a new SDK instead of potentially using an SDK for an existing account.

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

Copy link
codecov bot commented Sep 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.80%. Comparing base (aa6ba21) to head (090780b).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #901      +/-   ##
==========================================
+ Coverage   88.77%   88.80%   +0.02%     
==========================================
  Files         617      617              
  Lines       30898    30932      +34     
==========================================
+ Hits        27430    27469      +39     
+ Misses       3468     3463       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor
github-actions bot commented Sep 5, 2024

Logo
Checkmarx One – Scan Summary & Details9447f95f-f94f-4350-bacc-38111ffb6ecd

No New Or Fixed Issues Found

Comment on lines 230 to 238
let userId = try await stateService.getAccountIdOrActiveId(userId: userId)

// If the user has a client, return it.
guard let client = userClientArray[userId] else {
// If not, create one, map it to the user, then return it.
let newClient = createAndMapClient(for: userId)
return newClient
}
return client
Copy link
Member

Choose a reason for hiding this comment

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

🤔 Shouldn't we maintain the catch StateServiceError.noAccounts, StateServiceError.noActiveAccount logic in case this happens? Perhaps because of passing wrongly the isPreAuth flag.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My thinking on removing it was that it would help us catch any potential uses where isPreAuth flag isn't set, but should be. Since if there's an active account and isPreAuth isn't set, it will just silently use the account's client.

But it would be safer to keep this, so I'll add it back and maybe see about logging an error.

Comment on lines 592 to 595
func passwordStrength(email: String, password: String) async throws -> UInt8 {
try await clientService.auth().passwordStrength(password: password, email: email, additionalInputs: [])
try await clientService.auth(isPreAuth: true)
.passwordStrength(password: password, email: email, additionalInputs: [])
}
Copy link
Member
8000

Choose a reason for hiding this comment

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

🤔 Is there a potential drawback if someone in the future uses this passwordStrength method while the vault is unlocked? I mean someone could just see the protocol definition and miss this implementation detail. Maybe just a comment on the protocol definition would be enough, if needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe! I could expose it as a parameter here? I was thinking that passwordStrength probably isn't specific to the user, so maybe it wouldn't matter, but that could change.

@fedemkr
Copy link
Member
fedemkr commented Sep 5, 2024

Really nice catch, great job!!! 🎉 🎉

@matt-livefront matt-livefront merged commit 7d1437f into main Sep 6, 2024
8 checks passed
@matt-livefront matt-livefront deleted the matt/PM-11697-client-service-crash branch September 6, 2024 21:57
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
0