Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(panic): fix panic when censoring utf-8 #63

Closed
wants to merge 1 commit into from
Closed
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
79 changes: 40 additions & 39 deletions goaway.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,62 +143,63 @@ func (g *ProfanityDetector) indexToRune(s string, index int) int {
count := 0
for i := range s {
if i == index {
return count
break
}
if i < index {
count++
}
count++
}
return -1
return count
}

// Censor takes in a string (word or sentence) and tries to censor all profanities found.
func (g *ProfanityDetector) Censor(s string) string {
censored := []rune(s)
var originalIndexes []int
s, originalIndexes = g.sanitize(s, true)
// Check for false negatives
for _, word := range g.falseNegatives {
currentIndex := 0
for currentIndex != -1 {
if foundIndex := strings.Index(s[currentIndex:], word); foundIndex != -1 {
for i := 0; i < len([]rune(word)); i++ {
runeIndex := g.indexToRune(string(censored), currentIndex+foundIndex+i)
censored[originalIndexes[runeIndex]] = '*'
runeWordLength := 0

checkProfanity := func(wordList []string) {
for _, word := range wordList {
currentIndex := 0
runeWordLength = len([]rune(word))
for currentIndex != -1 {
if foundIndex := strings.Index(s[currentIndex:], word); foundIndex != -1 {
for i := 0; i < runeWordLength; i++ {
runeIndex := g.indexToRune(s, currentIndex+foundIndex) + i
if runeIndex < len(originalIndexes) {
censored[originalIndexes[runeIndex]] = '*'
}
}
currentIndex += foundIndex + len([]byte(word))
} else {
break
stephenduke-care marked this conversation as resolved.
Show resolved Hide resolved
}
currentIndex += foundIndex + len([]rune(word))
} else {
break
}
}
}
// Remove false positives
for _, word := range g.falsePositives {
currentIndex := 0
for currentIndex != -1 {
if foundIndex := strings.Index(s[currentIndex:], word); foundIndex != -1 {
foundRuneIndex := g.indexToRune(s, foundIndex)
originalIndexes = append(originalIndexes[:foundRuneIndex], originalIndexes[foundRuneIndex+len(word):]...)
currentIndex += foundIndex + len([]rune(word))
} else {
break
}
}
s = strings.Replace(s, word, "", -1)
}
// Check for profanities
for _, word := range g.profanities {
currentIndex := 0
for currentIndex != -1 {
if foundIndex := strings.Index(s[currentIndex:], word); foundIndex != -1 {
for i := 0; i < len([]rune(word)); i++ {
runeIndex := g.indexToRune(string(censored), currentIndex+foundIndex+i)
censored[originalIndexes[runeIndex]] = '*'

removeFalsePositives := func() {
for _, word := range g.falsePositives {
currentIndex := 0
runeWordLength = len([]rune(word))
for currentIndex != -1 {
if foundIndex := strings.Index(s[currentIndex:], word); foundIndex != -1 {
foundRuneIndex := g.indexToRune(s, foundIndex)
originalIndexes = append(originalIndexes[:foundRuneIndex], originalIndexes[foundRuneIndex+runeWordLength:]...)
currentIndex += foundIndex + len([]byte(word))
} else {
break
}
currentIndex += foundIndex + len([]rune(word))
} else {
break
}
s = strings.Replace(s, word, "", -1)
}
}

checkProfanity(g.falseNegatives)
removeFalsePositives()
checkProfanity(g.profanities)

return string(censored)
}

Expand Down
21 changes: 21 additions & 0 deletions goaway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,24 @@ func TestSanitizeWithoutSanitizingLeetSpeak(t *testing.T) {
t.Errorf("Expected '%s', got '%s'", expectedString, sanitizedString)
}
}

func TestDefaultDriver_UTF8(t *testing.T) {
detector := NewProfanityDetector().WithCustomDictionary(
[]string{"anal", "あほ"}, // profanities
[]string{"あほほ"}, // falsePositives
[]string{"あほほし"}, // falseNegatives
)

unsanitizedString := "いい加減にしろ あほほし あほほ あほ anal ほ"
expectedString := "いい加減にしろ **** あほほ ** **** ほ"

isProfane := detector.IsProfane(unsanitizedString)
if !isProfane {
t.Error("Expected false, got false from sentence", unsanitizedString)
}

sanitizedString := detector.Censor(unsanitizedString)
if sanitizedString != expectedString {
t.Errorf("Expected '%s', got '%s'", expectedString, sanitizedString)
}
}