mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-22 11:59:40 +01:00
restrict ASCII mode to printable characters only
This commit is contained in:
parent
781bb6b051
commit
4391b1ba5a
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -217,20 +217,24 @@ func TestCanonicalizeMaskWildcard(t *testing.T) {
|
|||||||
tester("*SHIVARAM*", "*shivaram*!*@*", nil)
|
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) {
|
func TestFoldPermissive(t *testing.T) {
|
||||||
tester := func(first, second string, equal bool) {
|
tester := func(first, second string, equal bool) {
|
||||||
firstFolded, err := foldPermissive(first)
|
validFoldTester(first, second, equal, foldPermissive, t)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user