Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"html"
"html/template"
"io"
"log"
"path"
"regexp"
"strings"
Expand Down Expand Up @@ -305,6 +306,19 @@ func Accents(s string) string {
return b.String()
}

// Get alphanumeric strings only
func Alphanumeric(s string) string {

// Make a Regex to say we only want
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
log.Fatal(err)
}
processedString := reg.ReplaceAllString(s, "")

return processedString
}

var (
// If the attribute contains data: or javascript: anywhere, ignore it
// we don't allow this in attributes as it is so frequently used for xss
Expand Down
26 changes: 26 additions & 0 deletions sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,29 @@ func BenchmarkHTMLAllowed(b *testing.B) {
}
}
}

var alphanumerics = []Test{
{"#$ferf*", "ferf"},
{"aall$%", "aall"},
{"EFFFG^&", "EFFFG"},
}

func TestAlphanumeric(t *testing.T) {
for _, test := range alphanumerics {
output := Alphanumeric(test.input)
if output != test.expected {
t.Fatalf(Format, test.input, test.expected, output)
}
}
}

func BenchmarkAlphanumeric(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range alphanumerics {
output := Alphanumeric(test.input)
if output != test.expected {
b.Fatalf(Format, test.input, test.expected, output)
}
}
}
}