Skip to content

Commit

Permalink
Refuse to parse messages with multiple spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
luk3yx committed Dec 8, 2024
1 parent c6fa51f commit d85a22e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions miniirc.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,18 @@ def ircv3_message_parser(msg):
# Get the command and arguments
args = []
c = 1
for i in n[2:]:
for word in n[2:]:
c += 1
if i.startswith(':'):
if word.startswith(':'):
args.append(' '.join(n[c:]))
break
elif word:
args.append(word)
else:
args.append(i)
# RFC 1459 allows multiple spaces to separate parameters, but this
# is very uncommon and could possibly happen if a server tries to
# send an empty parameter
raise ValueError('Ambiguous IRC message')

# Return the parsed data
return cmd, hostmask, tags, args
Expand Down Expand Up @@ -190,6 +195,9 @@ def __init__(self, func):
def _prune_arg(arg):
if arg.startswith(':'):
arg = '\u0703' + arg[1:]
elif not arg:
# Replace the argument with something to prevent misinterpretation
arg = ' '
return arg.replace(' ', '\xa0').replace('\r', '\xa0').replace('\n', '\xa0')


Expand Down Expand Up @@ -298,8 +306,8 @@ def quote(self, *msg, force=None, tags=None):
'draft/message-tags-0.2' not in self.active_caps)):
tags = None
self.debug('>3> ' + repr(tags) if tags else '>>>', *msg)
msg = (' '.join(msg).encode('utf-8').replace(b'\r', b' ')
.replace(b'\n', b' '))
msg = (' '.join(msg).replace('\x00', '\ufffd').encode('utf-8')
.replace(b'\r', b' ') .replace(b'\n', b' '))

if len(msg) + 2 > self.msglen:
msg = msg[:self.msglen - 2]
Expand Down
2 changes: 1 addition & 1 deletion test_miniirc.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test_irc_send():
assert test('a') == ('a', None)
assert test('a', 'Hello world!', 'b') == ('a Hello\xa0world! :b', None)
assert (test('', 'abc def\r\n', ':ghi', ':jkl', tags={'a': 'b'}) ==
(' abc\xa0def\xa0\xa0 \u0703ghi ::jkl', {'a': 'b'}))
('\xa0 abc\xa0def\xa0\xa0 \u0703ghi ::jkl', {'a': 'b'}))


irc_msg_funcs = {
Expand Down

0 comments on commit d85a22e

Please sign in to comment.