Skip to content

Commit

Permalink
Merge pull request #109 from ergochat/cmdvalidation.1
Browse files Browse the repository at this point in the history
ircmsg: validate that commands are ASCII
  • Loading branch information
slingamn authored Sep 27, 2024
2 parents 36a8aad + 2a1f341 commit 9beac2d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 17 additions & 3 deletions ircmsg/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ func trimInitialSpaces(str string) string {
return str[i:]
}

func isASCII(str string) bool {
for i := 0; i < len(str); i++ {
if str[i] > 127 {
return false
}
}
return true
}

func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg Message, err error) {
// remove either \n or \r\n from the end of the line:
line = strings.TrimSuffix(line, "\n")
Expand Down Expand Up @@ -265,11 +274,16 @@ func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg Messa
commandEnd = len(line)
paramStart = len(line)
}
// normalize command to uppercase:
ircmsg.Command = strings.ToUpper(line[:commandEnd])
if len(ircmsg.Command) == 0 {
baseCommand := line[:commandEnd]
if len(baseCommand) == 0 {
return ircmsg, ErrorLineIsEmpty
}
// technically this must be either letters or a 3-digit numeric:
if !isASCII(baseCommand) {
return ircmsg, ErrorLineContainsBadChar
}
// normalize command to uppercase:
ircmsg.Command = strings.ToUpper(baseCommand)
line = line[paramStart:]

for {
Expand Down
2 changes: 2 additions & 0 deletions ircmsg/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ type testparseerror struct {

var decodetesterrors = []testparseerror{
{"", ErrorLineIsEmpty},
{"\n", ErrorLineIsEmpty},
{"\r\n", ErrorLineIsEmpty},
{"\r\n ", ErrorLineContainsBadChar},
{"\r\n ", ErrorLineContainsBadChar},
{" \r\n", ErrorLineIsEmpty},
{" \r\n ", ErrorLineContainsBadChar},
{" \r\n ", ErrorLineContainsBadChar},
{"\xff\r\n", ErrorLineContainsBadChar},
{"@tags=tesa\r\n", ErrorLineIsEmpty},
{"@tags=tested \r\n", ErrorLineIsEmpty},
{":dan- \r\n", ErrorLineIsEmpty},
Expand Down

0 comments on commit 9beac2d

Please sign in to comment.