Skip to content
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

Encode the minimum and maximum passphrase lengths just once. #124

Merged
merged 1 commit into from
Mar 27, 2019
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
20 changes: 16 additions & 4 deletions src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ module Cardano.Wallet.Api.Types
, AddressPoolGap
, Passphrase(..)

-- * Limits
, passphraseMinLength
, passphraseMaxLength

-- * Polymorphic Types
, ApiT (..)
, ApiMnemonicT (..)
Expand Down Expand Up @@ -176,13 +180,21 @@ instance ToJSON WalletPostData where

instance FromJSON (ApiT (Passphrase "encryption")) where
parseJSON = parseJSON >=> \case
t | T.length t < 10 ->
fail "passphrase is too short: expected at least 10 chars"
t | T.length t > 255 ->
fail "passphrase is too long: expect at most 255 chars"
t | T.length t < passphraseMinLength ->
fail $ "passphrase is too short: expected at least "
<> show passphraseMinLength <> " chars"
t | T.length t > passphraseMaxLength ->
fail $ "passphrase is too long: expected at most "
<> show passphraseMaxLength <> " chars"
t ->
return $ ApiT $ Passphrase $ BA.convert $ T.encodeUtf8 t

passphraseMinLength :: Int
passphraseMinLength = 10

passphraseMaxLength :: Int
passphraseMaxLength = 255
Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps these named constants should be located in the AddressDerivation module?

Copy link
Member

Choose a reason for hiding this comment

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

Not really because, that's really just a user-facing thing here. From the derivation perspective, we don't care about it and the passphrase could as well be empty. Actually, I've discussed this with Charles Morgan and, in the short-long run, we do want to enforce some better strength validation at the API boundary. Yet, this doesn't have to "leak" through the rest IMO.


instance ToJSON (ApiT (Passphrase "encryption")) where
toJSON (ApiT (Passphrase bytes)) = toJSON $ T.decodeUtf8 $ BA.convert bytes

Expand Down
12 changes: 9 additions & 3 deletions test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import Cardano.Wallet.Api.Types
, WalletPassphraseInfo (..)
, WalletPostData (..)
, WalletState (..)
, passphraseMaxLength
, passphraseMinLength
)
import Cardano.Wallet.Primitive.Mnemonic
( CheckSumBits
Expand Down Expand Up @@ -229,12 +231,16 @@ instance Arbitrary WalletName where

instance Arbitrary (Passphrase "encryption") where
arbitrary = do
n <- choose (10, 255)
n <- choose (passphraseMinLength, passphraseMaxLength)
bytes <- T.encodeUtf8 . T.pack <$> replicateM n arbitraryPrintableChar
return $ Passphrase $ BA.convert bytes
shrink (Passphrase bytes)
| BA.length bytes <= 10 = []
| otherwise = [Passphrase $ BA.convert $ B8.take 10 $ BA.convert bytes]
| BA.length bytes <= passphraseMinLength = []
| otherwise =
[ Passphrase
$ BA.convert
$ B8.take passphraseMinLength
$ BA.convert bytes ]

instance Arbitrary WalletPassphraseInfo where
arbitrary = genericArbitrary
Expand Down