3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 10:42:52 +01:00

restrict ASCII mode to printable characters only

This commit is contained in:
Shivaram Lingamneni 2019-12-22 09:57:49 -05:00
parent 781bb6b051
commit 4391b1ba5a
2 changed files with 42 additions and 16 deletions

View File

@ -9,7 +9,6 @@ import (
"fmt"
"regexp"
"strings"
"unicode"
"github.com/oragono/confusables"
"golang.org/x/text/cases"
@ -262,15 +261,18 @@ func CanonicalizeMaskWildcard(userhost string) (expanded string, err error) {
}
func foldASCII(str string) (result string, err error) {
if !IsPureASCII(str) {
if !IsPrintableASCII(str) {
return "", errInvalidCharacter
}
return strings.ToLower(str), nil
}
func IsPureASCII(str string) bool {
func IsPrintableASCII(str string) bool {
for i := 0; i < len(str); i++ {
if unicode.MaxASCII < str[i] {
// allow space here because it's technically printable;
// it will be disallowed later by CasefoldName/CasefoldChannel
chr := str[i]
if chr < ' ' || chr > '~' {
return false
}
}

View File

@ -217,20 +217,24 @@ func TestCanonicalizeMaskWildcard(t *testing.T) {
tester("*SHIVARAM*", "*shivaram*!*@*", nil)
}
func validFoldTester(first, second string, equal bool, folder func(string) (string, error), t *testing.T) {
firstFolded, err := folder(first)
if err != nil {
panic(err)
}
secondFolded, err := folder(second)
if err != nil {
panic(err)
}
foundEqual := firstFolded == secondFolded
if foundEqual != equal {
t.Errorf("%s and %s: expected equality %t, but got %t", first, second, equal, foundEqual)
}
}
func TestFoldPermissive(t *testing.T) {
tester := func(first, second string, equal bool) {
firstFolded, err := foldPermissive(first)
if err != nil {
panic(err)
}
secondFolded, err := foldPermissive(second)
if err != nil {
panic(err)
}
foundEqual := firstFolded == secondFolded
if foundEqual != equal {
t.Errorf("%s and %s: expected equality %t, but got %t", first, second, equal, foundEqual)
}
validFoldTester(first, second, equal, foldPermissive, t)
}
tester("SHIVARAM", "shivaram", true)
tester("shIvaram", "shivaraM", true)
@ -250,3 +254,23 @@ func TestFoldPermissiveInvalid(t *testing.T) {
t.Errorf("the null byte should be invalid in identifiers")
}
}
func TestFoldASCII(t *testing.T) {
tester := func(first, second string, equal bool) {
validFoldTester(first, second, equal, foldASCII, t)
}
tester("shivaram", "SHIVARAM", true)
tester("X|Y", "x|y", true)
tester("a != b", "A != B", true)
}
func TestFoldASCIIInvalid(t *testing.T) {
_, err := foldASCII("\x01")
if err == nil {
t.Errorf("control characters should be invalid in identifiers")
}
_, err = foldASCII("\x7F")
if err == nil {
t.Errorf("control characters should be invalid in identifiers")
}
}