Input sanitisation in userpages and user settings
This commit is contained in:
16
common/sanitisation.go
Normal file
16
common/sanitisation.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// SanitiseString removes all control codes from a string.
|
||||
func SanitiseString(s string) string {
|
||||
n := make([]rune, 0, len(s))
|
||||
for _, c := range s {
|
||||
if !unicode.Is(unicode.Other, c) {
|
||||
n = append(n, c)
|
||||
}
|
||||
}
|
||||
return string(n)
|
||||
}
|
41
common/sanitisation_test.go
Normal file
41
common/sanitisation_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package common
|
||||
|
||||
import "testing"
|
||||
|
||||
const pen = "I trattori di palmizio 나는 펜이있다. 私はリンゴを持っています。" +
|
||||
"啊! 苹果笔。 у меня есть ручка, Tôi có dứa. අන්නාසි පෑන"
|
||||
|
||||
func TestSanitiseString(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
arg string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
"Normal",
|
||||
pen,
|
||||
pen,
|
||||
},
|
||||
{
|
||||
"Arabic (rtl)",
|
||||
"أناناس",
|
||||
"أناناس",
|
||||
},
|
||||
{
|
||||
"Null",
|
||||
"A\x00B",
|
||||
"AB",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := SanitiseString(tt.arg); got != tt.want {
|
||||
t.Errorf("%q. SanitiseString() = %v, want %v", tt.name, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSanitiseString(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
SanitiseString(pen)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user