Skip to content

Commit

Permalink
add aliases.IsValid for name checks
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptix committed Mar 16, 2021
1 parent 6fe202c commit 9d60d09
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
42 changes: 42 additions & 0 deletions aliases/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: MIT

package aliases

import (
"fmt"
)

// IsValid decides weather an alias is okay for use or not.
// The room spec defines it as _labels valid under RFC 1035_ ( https://ssb-ngi-pointer.github.io/rooms2/#alias-string )
// but that can be mostly any string since DNS is a 8bit binary protocol,
// as long as it's shorter then 63 charachters.
//
// Right now it's pretty basic set of charachters (a-z, A-Z, 0-9).
// In theory we could be more liberal but there is a bunch of stuff to figure out,
// like homograph attacks (https://en.wikipedia.org/wiki/IDN_homograph_attack),
// if we would decide to allow full utf8 unicode.
func IsValid(alias string) bool {
if len(alias) > 63 {
return false
}

var valid = true
for _, char := range alias {
if char >= '0' && char <= '9' { // is an ASCII number
continue
}

if char >= 'a' && char <= 'z' { // is an ASCII char between a and z
continue
}

if char >= 'A' && char <= 'Z' { // is an ASCII upper-case char between a and z
continue
}

fmt.Println("found", char)
valid = false
break
}
return valid
}
31 changes: 31 additions & 0 deletions aliases/names_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT

package aliases

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsValid(t *testing.T) {
a := assert.New(t)

cases := []struct {
alias string
valid bool
}{
{"basic", true},
{"no spaces", false},
{"no.dots", false},
{"#*!(! nope", false},

// too long
{"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", false},
}

for i, tc := range cases {
yes := IsValid(tc.alias)
a.Equal(tc.valid, yes, "wrong for %d: %s", i, tc.alias)
}
}
4 changes: 3 additions & 1 deletion muxrpc/handlers/alias/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func (h Handler) Register(ctx context.Context, req *muxrpc.Request) (interface{}
}

// check alias is valid
// if !aliases.IsValid(confirmation.Alias) { ... }
if !aliases.IsValid(confirmation.Alias) {
return nil, fmt.Errorf("registerAlias: invalid alias")
}

// get the user from the muxrpc connection
userID, err := network.GetFeedRefFromAddr(req.RemoteAddr())
Expand Down

0 comments on commit 9d60d09

Please sign in to comment.