Skip to content

Commit

Permalink
Check sync words count to be 24; fixes brave/brave-browser#23206
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBarabash committed Jun 3, 2022
1 parent fda3cbb commit 2b3e1ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions components/brave_sync/crypto/crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cmath>

#include "base/logging.h"
#include "base/strings/string_split.h"
#include "brave/vendor/bat-native-tweetnacl/tweetnacl.h"
#include "brave/vendor/bip39wally-core-native/include/wally_bip39.h"
#include "crypto/random.h"
Expand Down Expand Up @@ -158,6 +159,17 @@ bool PassphraseToBytes32(const std::string& passphrase,
}

bool IsPassphraseValid(const std::string& passphrase) {
// This check is dedicated for old client to reject sync code from new client
// which has time limited code (25 words)
std::vector<std::string> words = base::SplitString(
passphrase, " ", base::WhitespaceHandling::TRIM_WHITESPACE,
base::SplitResult::SPLIT_WANT_NONEMPTY);
size_t num_words = words.size();
static constexpr size_t kPureWordsCount = 24u;
if (num_words != kPureWordsCount) {
return false;
}

std::vector<uint8_t> bytes;
return PassphraseToBytes32(passphrase, &bytes);
}
Expand Down
2 changes: 2 additions & 0 deletions components/brave_sync/crypto/crypto_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ TEST(CryptoTest, Passphrase) {
EXPECT_FALSE(IsPassphraseValid(""));
EXPECT_FALSE(IsPassphraseValid(bip_passphrase + " something wrong"));
EXPECT_FALSE(IsPassphraseValid(bip_invalid_passphrase));
// It makes more sense to check against additional word from bip39 list
EXPECT_FALSE(IsPassphraseValid(bip_passphrase + " annual"));
}

} // namespace crypto
Expand Down

0 comments on commit 2b3e1ac

Please sign in to comment.