3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-26 05:49:25 +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" "fmt"
"regexp" "regexp"
"strings" "strings"
"unicode"
"github.com/oragono/confusables" "github.com/oragono/confusables"
"golang.org/x/text/cases" "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) { func foldASCII(str string) (result string, err error) {
if !IsPureASCII(str) { if !IsPrintableASCII(str) {
return "", errInvalidCharacter return "", errInvalidCharacter
} }
return strings.ToLower(str), nil return strings.ToLower(str), nil
} }
func IsPureASCII(str string) bool { func IsPrintableASCII(str string) bool {
for i := 0; i < len(str); i++ { 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 return false
} }
} }

View File

@ -217,13 +217,12 @@ func TestCanonicalizeMaskWildcard(t *testing.T) {
tester("*SHIVARAM*", "*shivaram*!*@*", nil) tester("*SHIVARAM*", "*shivaram*!*@*", nil)
} }
func TestFoldPermissive(t *testing.T) { func validFoldTester(first, second string, equal bool, folder func(string) (string, error), t *testing.T) {
tester := func(first, second string, equal bool) { firstFolded, err := folder(first)
firstFolded, err := foldPermissive(first)
if err != nil { if err != nil {
panic(err) panic(err)
} }
secondFolded, err := foldPermissive(second) secondFolded, err := folder(second)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -231,6 +230,11 @@ func TestFoldPermissive(t *testing.T) {
if foundEqual != equal { if foundEqual != equal {
t.Errorf("%s and %s: expected equality %t, but got %t", first, second, equal, foundEqual) 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) {
validFoldTester(first, second, equal, foldPermissive, t)
} }
tester("SHIVARAM", "shivaram", true) tester("SHIVARAM", "shivaram", true)
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") 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")
}
}