Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

ircmsg: validate that commands are ASCII #109

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
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
Loading