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

require strkey length is divisible by 8 #1815

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion build/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestAsset_ToXDR(t *testing.T) {
{
Name: "bad issuer",
Asset: CreditAsset("USD", "FUNK"),
ExpectedErr: "base32 decode failed: illegal base32 data at input byte 0",
ExpectedErr: "length has to be a multiple of 8",
},
{
Name: "bad code",
Expand Down
2 changes: 1 addition & 1 deletion build/set_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestSetOptions_Signer(t *testing.T) {
Name: "Bad",
Address: "foo",
Weight: 1,
Error: "base32 decode failed",
Error: "length has to be a multiple of 8",
},
}

Expand Down
7 changes: 7 additions & 0 deletions strkey/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,10 @@ func TestDecode(t *testing.T) {
_, err = Decode(VersionByteAccountID, "GA3D5KRYM6CB7OWOOOORR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQHES5")
assert.Error(t, err)
}

func TestDecodeDisallowPadding(t *testing.T) {
invalidKey := "GDWZCOEQRODFCH6ISYQPWY67L3ULLWS5ISXYYL5GH43W7Y"
_, err := Decode(VersionByteAccountID /* XXX */, invalidKey)

assert.Error(t, err, "Padding is disallowed")
}
9 changes: 9 additions & 0 deletions strkey/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
// strkey-encoded string is not one of the valid values.
var ErrInvalidVersionByte = errors.New("invalid version byte")

// ErrLengthIsNotDivisibleBy8 is returned when the length of a
// strkey-encoded string is not a multiple of 8.
var ErrLengthIsNotDivisibleBy8 = errors.New("length has to be a multiple of 8")

// VersionByte represents one of the possible prefix values for a StrKey base
// string--the string the when encoded using base32 yields a final StrKey.
type VersionByte byte
Expand Down Expand Up @@ -178,6 +182,11 @@ func checkValidVersionByte(version VersionByte) error {
// potentially be strkey encoded (i.e. it has both a version byte and a
// checksum, neither of which are explicitly checked by this func)
func decodeString(src string) ([]byte, error) {
// base 32 data size is (s * 5)/8 => has to be a multiple of 8
if (len(src) & 0x07) != 0 {
return nil, ErrLengthIsNotDivisibleBy8
}

raw, err := base32.StdEncoding.DecodeString(src)
if err != nil {
return nil, errors.Wrap(err, "base32 decode failed")
Expand Down
2 changes: 1 addition & 1 deletion txnbuild/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ func TestBadIssuer(t *testing.T) {
var xdrAssetCode [4]byte
copy(xdrAssetCode[:], asset.Code)
var xdrIssuer xdr.AccountId
expectedErrMsg := "base32 decode failed: illegal base32 data at input byte 16"
expectedErrMsg := "length has to be a multiple of 8"
require.EqualError(t, xdrIssuer.SetAddress(asset.Issuer), expectedErrMsg, "Issuer address should be validated")
}