Skip to content

Commit 9beac2d

Browse files
authored
Merge pull request #109 from ergochat/cmdvalidation.1
ircmsg: validate that commands are ASCII
2 parents 36a8aad + 2a1f341 commit 9beac2d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

ircmsg/message.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ func trimInitialSpaces(str string) string {
196196
return str[i:]
197197
}
198198

199+
func isASCII(str string) bool {
200+
for i := 0; i < len(str); i++ {
201+
if str[i] > 127 {
202+
return false
203+
}
204+
}
205+
return true
206+
}
207+
199208
func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg Message, err error) {
200209
// remove either \n or \r\n from the end of the line:
201210
line = strings.TrimSuffix(line, "\n")
@@ -265,11 +274,16 @@ func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg Messa
265274
commandEnd = len(line)
266275
paramStart = len(line)
267276
}
268-
// normalize command to uppercase:
269-
ircmsg.Command = strings.ToUpper(line[:commandEnd])
270-
if len(ircmsg.Command) == 0 {
277+
baseCommand := line[:commandEnd]
278+
if len(baseCommand) == 0 {
271279
return ircmsg, ErrorLineIsEmpty
272280
}
281+
// technically this must be either letters or a 3-digit numeric:
282+
if !isASCII(baseCommand) {
283+
return ircmsg, ErrorLineContainsBadChar
284+
}
285+
// normalize command to uppercase:
286+
ircmsg.Command = strings.ToUpper(baseCommand)
273287
line = line[paramStart:]
274288

275289
for {

ircmsg/message_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,14 @@ type testparseerror struct {
112112

113113
var decodetesterrors = []testparseerror{
114114
{"", ErrorLineIsEmpty},
115+
{"\n", ErrorLineIsEmpty},
115116
{"\r\n", ErrorLineIsEmpty},
116117
{"\r\n ", ErrorLineContainsBadChar},
117118
{"\r\n ", ErrorLineContainsBadChar},
118119
{" \r\n", ErrorLineIsEmpty},
119120
{" \r\n ", ErrorLineContainsBadChar},
120121
{" \r\n ", ErrorLineContainsBadChar},
122+
{"\xff\r\n", ErrorLineContainsBadChar},
121123
{"@tags=tesa\r\n", ErrorLineIsEmpty},
122124
{"@tags=tested \r\n", ErrorLineIsEmpty},
123125
{":dan- \r\n", ErrorLineIsEmpty},

0 commit comments

Comments
 (0)