Skip to content

[PM-15863] Request master password before revealing private SSH key #4481

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 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,16 @@
action: VaultItemAction.ItemType.SshKey.PrivateKeyVisibilityClicked,
) {
onSshKeyContent { content, sshKey ->
if (content.common.requiresReprompt) {
updateDialogState(
VaultItemState.DialogState.MasterPasswordDialog(
action = PasswordRepromptAction.ViewPrivateKeyClicked(
isVisible = action.isVisible,
),
),
)
return@onSshKeyContent
}
mutableStateFlow.update { currentState ->
currentState.copy(
viewState = content.copy(
Expand Down Expand Up @@ -2231,4 +2241,18 @@
override val vaultItemAction: VaultItemAction
get() = VaultItemAction.Common.RestoreVaultItemClick
}

/**
* Indicates that we should launch the
* [VaultItemAction.ItemType.SshKey.PrivateKeyVisibilityClicked] upon password validation.
*/
@Parcelize

Check warning on line 2249 in app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/item/VaultItemViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/item/VaultItemViewModel.kt#L2249

Added line #L2249 was not covered by tests
data class ViewPrivateKeyClicked(
val isVisible: Boolean,
) : PasswordRepromptAction() {
override val vaultItemAction: VaultItemAction
get() = VaultItemAction.ItemType.SshKey.PrivateKeyVisibilityClicked(
isVisible = isVisible,

Check warning on line 2255 in app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/item/VaultItemViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/item/VaultItemViewModel.kt#L2254-L2255

Added lines #L2254 - L2255 were not covered by tests
)
}

Check warning on line 2257 in app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/item/VaultItemViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/item/VaultItemViewModel.kt#L2257

Added line #L2257 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -2490,11 +2490,58 @@ class VaultItemViewModelTest : BaseViewModelTest() {

@Suppress("MaxLineLength")
@Test
fun `on PrivateKeyVisibilityClick should show password dialog when re-prompt is required`() =
fun `on PrivateKeyVisibilityClick should show private key when re-prompt is not required`() =
runTest {
val sshKeyViewState = createViewState(
common = DEFAULT_COMMON.copy(requiresReprompt = false),
type = DEFAULT_SSH_KEY_TYPE,
)
val sshKeyState = DEFAULT_STATE.copy(viewState = sshKeyViewState)
every {
mockCipherView.toViewState(
previousState = null,
isPremiumUser = true,
hasMasterPassword = true,
totpCodeItemData = null,
canDelete = true,
canAssignToCollections = true,
)
} returns sshKeyViewState
mutableVaultItemFlow.value = DataState.Loaded(data = mockCipherView)
mutableAuthCodeItemFlow.value = DataState.Loaded(data = null)
mutableCollectionsStateFlow.value = DataState.Loaded(emptyList())

assertEquals(sshKeyState, viewModel.stateFlow.value)
viewModel.trySendAction(
VaultItemAction.ItemType.SshKey.PrivateKeyVisibilityClicked(
isVisible = true,
),
)
assertEquals(
sshKeyState.copy(
viewState = sshKeyViewState.copy(
common = DEFAULT_COMMON.copy(requiresReprompt = false),
type = DEFAULT_SSH_KEY_TYPE.copy(showPrivateKey = true),
),
),
viewModel.stateFlow.value,
)
verify(exactly = 1) {
mockCipherView.toViewState(
previousState = null,
isPremiumUser = true,
hasMasterPassword = true,
totpCodeItemData = null,
canDelete = true,
canAssignToCollections = true,
)
}
}

@Suppress("MaxLineLength")
@Test
fun `on PrivateKeyVisibilityClick should show password dialog when re-prompt is required`() =
runTest {
val sshKeyState = DEFAULT_STATE.copy(viewState = SSH_KEY_VIEW_STATE)
every {
mockCipherView.toViewState(
Expand All @@ -2518,15 +2565,22 @@ class VaultItemViewModelTest : BaseViewModelTest() {
)
assertEquals(
sshKeyState.copy(
viewState = sshKeyViewState.copy(
common = DEFAULT_COMMON,
type = DEFAULT_SSH_KEY_TYPE.copy(
showPrivateKey = true,
),
dialog = VaultItemState.DialogState.MasterPasswordDialog(
PasswordRepromptAction.ViewPrivateKeyClicked(isVisible = true),
),
),
viewModel.stateFlow.value,
)
verify(exactly = 1) {
mockCipherView.toViewState(
previousState = null,
isPremiumUser = true,
hasMasterPassword = true,
totpCodeItemData = null,
canDelete = true,
canAssignToCollections = true,
)
}
}

@Test
Expand Down