Skip to content

Commit

Permalink
fix: disallow identical blockchain IDs to be stored as valid targets (#…
Browse files Browse the repository at this point in the history
…121)

* fix: disallow identical blockchain IDs to be stored as valid targets

* update: CHANGELOG.md
  • Loading branch information
fdymylja authored May 21, 2020
1 parent fb562b8 commit c8a5bf6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## HEAD

- iovnscli: fix has-superuser bool flag bug
- iovnsd: fix duplicate blockchain targets ID
- remove flush domain feature

## v0.2.4
Expand Down
7 changes: 7 additions & 0 deletions x/domain/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,15 @@ func handleMsgRegisterAccount(ctx sdk.Context, k keeper.Keeper, msg *types.MsgRe
func validateBlockchainTargets(targets []types.BlockchainAddress, conf configuration.Config) error {
validBlockchainID := regexp.MustCompile(conf.ValidBlockchainID)
validBlockchainAddress := regexp.MustCompile(conf.ValidBlockchainAddress)
// create blockchain targets set to identify duplicates
sets := make(map[string]struct{}, len(targets))
// iterate over targets to check their validity
for _, target := range targets {
// check if blockchain ID was already specified
if _, ok := sets[target.ID]; ok {
return fmt.Errorf("duplicate blockchain ID: %s", target)
}
sets[target.ID] = struct{}{}
// is blockchain id valid?
if !validBlockchainID.MatchString(target.ID) {
return fmt.Errorf("%s is not a valid blockchain ID", target.ID)
Expand Down
40 changes: 40 additions & 0 deletions x/domain/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2107,3 +2107,43 @@ func Test_handlerMsgTransferDomain(t *testing.T) {

runTests(t, cases)
}

func Test_validateBlockchainTargets(t *testing.T) {
type args struct {
targets []types.BlockchainAddress
conf configuration.Config
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "duplicate blockchain target",
args: args{
targets: []types.BlockchainAddress{
{
ID: "duplicate",
Address: "does not matter",
},
{
ID: "duplicate",
Address: "does not matter",
},
},
conf: configuration.Config{
ValidBlockchainID: regexMatchAll,
ValidBlockchainAddress: regexMatchAll,
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := validateBlockchainTargets(tt.args.targets, tt.args.conf); (err != nil) != tt.wantErr {
t.Errorf("validateBlockchainTargets() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit c8a5bf6

Please sign in to comment.