diff --git a/cloud_accounts.php b/cloud_accounts.php index 23570de..3f5f57c 100644 --- a/cloud_accounts.php +++ b/cloud_accounts.php @@ -100,15 +100,22 @@ exit; } - $cloud_saved = $cloud_accounts->save($cloud_parsed->account, $cloud_user); + $cloud_cleanup_failed = false; + $cloud_saved = $cloud_accounts->save($cloud_parsed->account, $cloud_user); $cloud_raw = trim((string) get_nfilter_request_var('credential')); if ($cloud_saved > 0 && $cloud_parsed->account->credentialSource !== \Somethingwithproof\Cloud\CredentialSource::Vault) { // A credential kept in the vault would come back, unprompted, if the // account were switched back to it with the box left empty. if ($cloud_credentials->hasVaultCredential($cloud_saved)) { - $cloud_credentials->forget($cloud_saved); - $cloud_accounts->recordCredentialChange($cloud_saved, $cloud_user); + // The delete skips an account a concurrent save has moved + // back to the vault, so only a credential that is gone is + // recorded as removed. + if (!$cloud_credentials->forgetUnlessVault($cloud_saved)) { + $cloud_cleanup_failed = true; + } elseif (!$cloud_credentials->hasVaultCredential($cloud_saved)) { + $cloud_accounts->recordCredentialChange($cloud_saved, $cloud_user); + } } } elseif ($cloud_saved > 0 && $cloud_raw !== '') { try { @@ -125,7 +132,13 @@ unset($cloud_raw); - if ($cloud_saved > 0) { + if ($cloud_saved > 0 && $cloud_cleanup_failed) { + raise_message( + 'cloud_saved', + __('The account was saved, but the stored credential could not be removed.', 'cloud'), + MESSAGE_LEVEL_ERROR + ); + } elseif ($cloud_saved > 0) { raise_message('cloud_saved', __('The account was saved.', 'cloud'), MESSAGE_LEVEL_INFO); } else { raise_message('cloud_saved', __('The account could not be saved.', 'cloud'), MESSAGE_LEVEL_ERROR); diff --git a/lib/CredentialStore.php b/lib/CredentialStore.php index 97ee623..8e7c4ee 100644 --- a/lib/CredentialStore.php +++ b/lib/CredentialStore.php @@ -66,8 +66,21 @@ public function storeVaultCredential(int $accountId, Secret $secret): void { } } - public function forget(int $accountId): void { - $this->secrets->delete($accountId, SecretRepository::CREDENTIAL); + /** + * @return bool whether the delete ran; false means the credential may remain + */ + public function forget(int $accountId): bool { + return $this->secrets->delete($accountId, SecretRepository::CREDENTIAL); + } + + /** + * Drops the vault credential of an account that has moved to another + * source. An account that is on the vault again keeps it. + * + * @return bool whether the delete ran; false means the credential may remain + */ + public function forgetUnlessVault(int $accountId): bool { + return $this->secrets->deleteUnlessVault($accountId, SecretRepository::CREDENTIAL); } /** diff --git a/lib/SecretRepository.php b/lib/SecretRepository.php index db262c2..3075fcc 100644 --- a/lib/SecretRepository.php +++ b/lib/SecretRepository.php @@ -68,12 +68,29 @@ public function has(int $accountId, string $purpose): bool { ) > 0; } - public function delete(int $accountId, string $purpose): void { - db_execute_prepared( + public function delete(int $accountId, string $purpose): bool { + return db_execute_prepared( 'DELETE FROM plugin_cloud_secret WHERE account_id = ? AND purpose = ?', [$accountId, $purpose], true, $this->connection - ); + ) !== false; + } + + /** + * Deletes the row only while the account does not keep its credential in + * the vault. The check and the delete are one statement, so a concurrent + * save that switches the account to the vault cannot lose its credential + * to a cleanup that began before the switch. + */ + public function deleteUnlessVault(int $accountId, string $purpose): bool { + return db_execute_prepared( + 'DELETE s FROM plugin_cloud_secret s + INNER JOIN plugin_cloud_account a ON a.id = s.account_id + WHERE s.account_id = ? AND s.purpose = ? AND a.credential_source <> ?', + [$accountId, $purpose, CredentialSource::Vault->value], + true, + $this->connection + ) !== false; } } diff --git a/locales/LC_MESSAGES/ar-SA.mo b/locales/LC_MESSAGES/ar-SA.mo index 6a525d0..b73f10a 100644 Binary files a/locales/LC_MESSAGES/ar-SA.mo and b/locales/LC_MESSAGES/ar-SA.mo differ diff --git a/locales/LC_MESSAGES/es-419.mo b/locales/LC_MESSAGES/es-419.mo index fcf4605..3bec0ea 100644 Binary files a/locales/LC_MESSAGES/es-419.mo and b/locales/LC_MESSAGES/es-419.mo differ diff --git a/locales/LC_MESSAGES/es-ES.mo b/locales/LC_MESSAGES/es-ES.mo index e1b3838..87eb92a 100644 Binary files a/locales/LC_MESSAGES/es-ES.mo and b/locales/LC_MESSAGES/es-ES.mo differ diff --git a/locales/LC_MESSAGES/hi-IN.mo b/locales/LC_MESSAGES/hi-IN.mo index b78cc55..a57d286 100644 Binary files a/locales/LC_MESSAGES/hi-IN.mo and b/locales/LC_MESSAGES/hi-IN.mo differ diff --git a/locales/LC_MESSAGES/zh-CN.mo b/locales/LC_MESSAGES/zh-CN.mo index f4c842f..84634a3 100644 Binary files a/locales/LC_MESSAGES/zh-CN.mo and b/locales/LC_MESSAGES/zh-CN.mo differ diff --git a/locales/po/ar-SA.po b/locales/po/ar-SA.po index d19c7da..4aab468 100644 --- a/locales/po/ar-SA.po +++ b/locales/po/ar-SA.po @@ -23,6 +23,10 @@ msgstr "الاسم: يستخدم حساب آخر هذا الاسم بالفعل. msgid "The account was saved, but the credential was not stored: %s" msgstr "حُفظ الحساب، لكن لم تُخزّن بيانات الاعتماد: %s" +#: cloud_accounts.php +msgid "The account was saved, but the stored credential could not be removed." +msgstr "تم حفظ الحساب، لكن تعذر حذف بيانات الاعتماد المخزنة." + #: cloud_accounts.php msgid "The account was saved." msgstr "حُفظ الحساب." diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index a43602e..369e5b8 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -25,6 +25,10 @@ msgstr "" msgid "The account was saved, but the credential was not stored: %s" msgstr "" +#: cloud_accounts.php +msgid "The account was saved, but the stored credential could not be removed." +msgstr "" + #: cloud_accounts.php msgid "The account was saved." msgstr "" diff --git a/locales/po/es-419.po b/locales/po/es-419.po index 852d8c4..021903d 100644 --- a/locales/po/es-419.po +++ b/locales/po/es-419.po @@ -23,6 +23,10 @@ msgstr "Nombre: otra cuenta ya utiliza este nombre." msgid "The account was saved, but the credential was not stored: %s" msgstr "La cuenta se ha guardado, pero no se ha almacenado la credencial: %s" +#: cloud_accounts.php +msgid "The account was saved, but the stored credential could not be removed." +msgstr "La cuenta se guardó, pero no se pudo eliminar la credencial almacenada." + #: cloud_accounts.php msgid "The account was saved." msgstr "La cuenta se ha guardado." diff --git a/locales/po/es-ES.po b/locales/po/es-ES.po index 0063e3b..0d9e0e4 100644 --- a/locales/po/es-ES.po +++ b/locales/po/es-ES.po @@ -23,6 +23,10 @@ msgstr "Nombre: otra cuenta ya utiliza este nombre." msgid "The account was saved, but the credential was not stored: %s" msgstr "La cuenta se ha guardado, pero no se ha almacenado la credencial: %s" +#: cloud_accounts.php +msgid "The account was saved, but the stored credential could not be removed." +msgstr "La cuenta se ha guardado, pero no se ha podido eliminar la credencial almacenada." + #: cloud_accounts.php msgid "The account was saved." msgstr "La cuenta se ha guardado." diff --git a/locales/po/hi-IN.po b/locales/po/hi-IN.po index 8f87029..e831ba4 100644 --- a/locales/po/hi-IN.po +++ b/locales/po/hi-IN.po @@ -23,6 +23,10 @@ msgstr "नाम: कोई अन्य खाता पहले से इ msgid "The account was saved, but the credential was not stored: %s" msgstr "खाता सहेजा गया, लेकिन क्रेडेंशियल संग्रहीत नहीं हुआ: %s" +#: cloud_accounts.php +msgid "The account was saved, but the stored credential could not be removed." +msgstr "खाता सहेज लिया गया, लेकिन संग्रहीत क्रेडेंशियल हटाया नहीं जा सका।" + #: cloud_accounts.php msgid "The account was saved." msgstr "खाता सहेज दिया गया।" diff --git a/locales/po/zh-CN.po b/locales/po/zh-CN.po index bcba113..2e4d984 100644 --- a/locales/po/zh-CN.po +++ b/locales/po/zh-CN.po @@ -23,6 +23,10 @@ msgstr "名称:其他账户已使用此名称。" msgid "The account was saved, but the credential was not stored: %s" msgstr "账户已保存,但凭据未存储:%s" +#: cloud_accounts.php +msgid "The account was saved, but the stored credential could not be removed." +msgstr "账户已保存,但无法删除已存储的凭据。" + #: cloud_accounts.php msgid "The account was saved." msgstr "账户已保存。" diff --git a/tests/Unit/VaultTest.php b/tests/Unit/VaultTest.php index ee04205..ba399e7 100644 --- a/tests/Unit/VaultTest.php +++ b/tests/Unit/VaultTest.php @@ -369,3 +369,34 @@ function cloud_refusal(callable $read): string { ->and(VaultEntry::fromRow(['nonce' => 12] + $row))->toBeNull() ->and(VaultEntry::fromRow([]))->toBeNull(); }); + +it('reports whether the credential delete ran', function () { + $store = new CredentialStore(new SecretRepository(false, new FixedClock(1)), '/nonexistent', $this->dir); + + $GLOBALS['__cloud_db_exec'] = []; + $GLOBALS['__cloud_db_fail'] = []; + + expect($store->forget(7))->toBeTrue() + ->and($store->forgetUnlessVault(7))->toBeTrue(); + + $GLOBALS['__cloud_db_fail'] = ['DELETE FROM plugin_cloud_secret', 'DELETE s FROM plugin_cloud_secret']; + + expect($store->forget(7))->toBeFalse() + ->and($store->forgetUnlessVault(7))->toBeFalse(); + + $GLOBALS['__cloud_db_fail'] = []; +}); + +it('deletes a credential only while its account is off the vault, in one statement', function () { + $store = new CredentialStore(new SecretRepository(false, new FixedClock(1)), '/nonexistent', $this->dir); + + $GLOBALS['__cloud_db_exec'] = []; + $store->forgetUnlessVault(7); + + $statement = $GLOBALS['__cloud_db_exec'][0]; + + expect($GLOBALS['__cloud_db_exec'])->toHaveCount(1) + ->and($statement['sql'])->toContain('INNER JOIN plugin_cloud_account') + ->and($statement['sql'])->toContain('a.credential_source <> ?') + ->and($statement['params'])->toBe([7, 'credential', 'vault']); +});