added input sanitization

This commit is contained in:
marko 2024-10-03 16:19:54 +02:00
parent d928a681f9
commit 97e41eece0
4 changed files with 104 additions and 94 deletions

View File

@ -6,6 +6,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"time" "time"
"unicode/utf8"
"github.com/xmppo/go-xmpp" "github.com/xmppo/go-xmpp"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
@ -71,19 +72,30 @@ func (nkbot *NekoUKrovuBot) Listen() {
} }
func (nkbot *NekoUKrovuBot) handleChat(ch *xmpp.Chat) { func (nkbot *NekoUKrovuBot) handleChat(ch *xmpp.Chat) {
src := ch.Remote
txt := ch.Text txt := ch.Text
if src == "sdsads" { if !nkbot.sanitizeInput(txt) {
return return
} }
if nkbot.checkForJelNekoUKrovu(txt) { if nkbot.checkForJelNekoUKrovu(txt) {
n := ping.Run() n := ping.PingLocal255()
nkbot.answer(fmt.Sprintf("%v uredjaja povezano", n)) nkbot.answer(fmt.Sprintf("%v uredjaja povezano", n))
} }
} }
func (nkbot *NekoUKrovuBot) sanitizeInput(input string) bool {
if len(input) > 50 {
return false
}
if !utf8.ValidString(input) {
return false
}
return true
}
func (nkbot *NekoUKrovuBot) checkForJelNekoUKrovu(txt string) bool { func (nkbot *NekoUKrovuBot) checkForJelNekoUKrovu(txt string) bool {
normalizedText := strings.ToLower(txt) normalizedText := strings.ToLower(txt)
@ -99,10 +111,10 @@ func (nkbot *NekoUKrovuBot) checkForJelNekoUKrovu(txt string) bool {
func (nkbot *NekoUKrovuBot) answer(ans string) { func (nkbot *NekoUKrovuBot) answer(ans string) {
chat := xmpp.Chat{ chat := xmpp.Chat{
Remote: "chatbottest@conference.dmz.rs", Remote: "chatbottest@conference.dmz.rs",
Type: "groupchat", Type: "groupchat",
Text: ans, Text: ans,
Stamp: time.Now(), Stamp: time.Now(),
} }
n, err := nkbot.cl.Send(chat) n, err := nkbot.cl.Send(chat)

View File

@ -9,85 +9,84 @@ func TestNekoUKrovuBot_checkForJelNekoUKrovu(t *testing.T) {
txt string txt string
} }
tests := []struct { tests := []struct {
name string name string
args args args args
want bool want bool
}{ }{
{ {
name: "matches 'jel neko u krovu'", name: "matches 'jel neko u krovu'",
args: args{ args: args{
txt: "jel neko u krovu", txt: "jel neko u krovu",
}, },
want: true, want: true,
}, },
{ {
name: "matches 'ima li koga na krovu'", name: "matches 'ima li koga na krovu'",
args: args{ args: args{
txt: "ima li koga na krovu", txt: "ima li koga na krovu",
}, },
want: true, want: true,
}, },
{ {
name: "does not match 'nema nikoga'", name: "does not match 'nema nikoga'",
args: args{ args: args{
txt: "nema nikoga", txt: "nema nikoga",
}, },
want: false, want: false,
}, },
{ {
name: "matches 'koga ima na krovu'", name: "matches 'koga ima na krovu'",
args: args{ args: args{
txt: "koga ima na krovu", txt: "koga ima na krovu",
}, },
want: true, want: true,
}, },
{ {
name: "matches 'neko u krov'", name: "matches 'neko u krov'",
args: args{ args: args{
txt: "neko u krov", txt: "neko u krov",
}, },
want: true, want: true,
}, },
{ {
name: "case insensitive match 'EL NEKO krov'", name: "case insensitive match 'EL NEKO krov'",
args: args{ args: args{
txt: "EL NEKO krov", txt: "EL NEKO krov",
}, },
want: true, want: true,
}, },
{ {
name: "case insensitive match 'jel neko jebeno u krovu'", name: "case insensitive match 'jel neko jebeno u krovu'",
args: args{ args: args{
txt: "jel neko jebeno u krovu", txt: "jel neko jebeno u krovu",
}, },
want: true, want: true,
}, },
{ {
name: "case insensitive match 'buraz jel neko jebeno u krovu'", name: "case insensitive match 'buraz jel neko jebeno u krovu'",
args: args{ args: args{
txt: "buraz jel neko jebeno u krovu", txt: "buraz jel neko jebeno u krovu",
}, },
want: true, want: true,
}, },
{ {
name: "case insensitive match 'neko u krovu?'", name: "case insensitive match 'neko u krovu?'",
args: args{ args: args{
txt: "neko u krovu?", txt: "neko u krovu?",
}, },
want: true, want: true,
}, },
{ {
name: "case insensitive match 'u krovu?'", name: "case insensitive match 'u krovu?'",
args: args{ args: args{
txt: "u krovu?", txt: "u krovu?",
}, },
want: false, want: false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
nkbot := &NekoUKrovuBot{ nkbot := &NekoUKrovuBot{}
}
if got := nkbot.checkForJelNekoUKrovu(tt.args.txt); got != tt.want { if got := nkbot.checkForJelNekoUKrovu(tt.args.txt); got != tt.want {
t.Errorf("NekoUKrovuBot.checkForJelNekoUKrovu() = %v, want %v", got, tt.want) t.Errorf("NekoUKrovuBot.checkForJelNekoUKrovu() = %v, want %v", got, tt.want)
} }

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"log"
"gitea.dmz.rs/bauljamic123arlijam/neko-u-krovu-bot/chatbot" "gitea.dmz.rs/bauljamic123arlijam/neko-u-krovu-bot/chatbot"
"log"
) )
func main() { func main() {

View File

@ -7,7 +7,7 @@ import (
"sync" "sync"
) )
func Run() int { func PingLocal255() int {
localIP, err := getLocalIP() localIP, err := getLocalIP()
if err != nil { if err != nil {
fmt.Println("Error getting local IP:", err) fmt.Println("Error getting local IP:", err)
@ -56,13 +56,12 @@ func getNetworkPrefix(ip net.IP) string {
return fmt.Sprintf("%d.%d.%d.0", ip[0], ip[1], ip[2]) return fmt.Sprintf("%d.%d.%d.0", ip[0], ip[1], ip[2])
} }
func ping(ip string) bool { func ping(ip string) bool {
output, err := exec.Command("ping", "-c", "1", "-W", "1", ip).CombinedOutput() output, err := exec.Command("ping", "-c", "1", "-W", "1", ip).CombinedOutput()
if err != nil { if err != nil {
return false return false
} }
_ = output _ = output
return true return true
} }