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)

View File

@ -86,8 +86,7 @@ func TestNekoUKrovuBot_checkForJelNekoUKrovu(t *testing.T) {
} }
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,7 +56,6 @@ 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()