-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters