Parcourir la source

fix #297

add validation for isupport tokens
tags/v1.0.0-rc1
Shivaram Lingamneni il y a 5 ans
Parent
révision
ba2aacaf5b
3 fichiers modifiés avec 57 ajouts et 9 suppressions
  1. 12
    3
      irc/isupport/list.go
  2. 35
    3
      irc/isupport/list_test.go
  3. 10
    3
      irc/server.go

+ 12
- 3
irc/isupport/list.go Voir le fichier

3
 
3
 
4
 package isupport
4
 package isupport
5
 
5
 
6
-import "fmt"
7
-import "sort"
6
+import (
7
+	"fmt"
8
+	"sort"
9
+	"strings"
10
+)
8
 
11
 
9
 const (
12
 const (
10
 	maxLastArgLength = 400
13
 	maxLastArgLength = 400
102
 }
105
 }
103
 
106
 
104
 // RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
107
 // RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
105
-func (il *List) RegenerateCachedReply() {
108
+func (il *List) RegenerateCachedReply() (err error) {
106
 	il.CachedReply = make([][]string, 0)
109
 	il.CachedReply = make([][]string, 0)
107
 	var length int     // Length of the current cache
110
 	var length int     // Length of the current cache
108
 	var cache []string // Token list cache
111
 	var cache []string // Token list cache
116
 
119
 
117
 	for _, name := range tokens {
120
 	for _, name := range tokens {
118
 		token := getTokenString(name, il.Tokens[name])
121
 		token := getTokenString(name, il.Tokens[name])
122
+		if token[0] == ':' || strings.Contains(token, " ") {
123
+			err = fmt.Errorf("bad isupport token (cannot contain spaces or start with :): %s", token)
124
+			continue
125
+		}
119
 
126
 
120
 		if len(token)+length <= maxLastArgLength {
127
 		if len(token)+length <= maxLastArgLength {
121
 			// account for the space separating tokens
128
 			// account for the space separating tokens
136
 	if len(cache) > 0 {
143
 	if len(cache) > 0 {
137
 		il.CachedReply = append(il.CachedReply, cache)
144
 		il.CachedReply = append(il.CachedReply, cache)
138
 	}
145
 	}
146
+
147
+	return
139
 }
148
 }

+ 35
- 3
irc/isupport/list_test.go Voir le fichier

26
 	tListLong.AddNoValue("D")
26
 	tListLong.AddNoValue("D")
27
 	tListLong.AddNoValue("E")
27
 	tListLong.AddNoValue("E")
28
 	tListLong.AddNoValue("F")
28
 	tListLong.AddNoValue("F")
29
-	tListLong.RegenerateCachedReply()
29
+	err := tListLong.RegenerateCachedReply()
30
+	if err != nil {
31
+		t.Error(err)
32
+	}
30
 
33
 
31
 	longReplies := [][]string{
34
 	longReplies := [][]string{
32
 		{"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D"},
35
 		{"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D"},
44
 	tList1.Add("INVEX", "i")
47
 	tList1.Add("INVEX", "i")
45
 	tList1.AddNoValue("EXTBAN")
48
 	tList1.AddNoValue("EXTBAN")
46
 	tList1.Add("RANDKILL", "whenever")
49
 	tList1.Add("RANDKILL", "whenever")
47
-	tList1.RegenerateCachedReply()
50
+	err = tList1.RegenerateCachedReply()
51
+	if err != nil {
52
+		t.Error(err)
53
+	}
48
 
54
 
49
 	expected := [][]string{{"CASEMAPPING=rfc1459-strict", "EXTBAN", "INVEX=i", "RANDKILL=whenever", "SASL=yes"}}
55
 	expected := [][]string{{"CASEMAPPING=rfc1459-strict", "EXTBAN", "INVEX=i", "RANDKILL=whenever", "SASL=yes"}}
50
 	if !reflect.DeepEqual(tList1.CachedReply, expected) {
56
 	if !reflect.DeepEqual(tList1.CachedReply, expected) {
58
 	tList2.AddNoValue("INVEX")
64
 	tList2.AddNoValue("INVEX")
59
 	tList2.Add("EXTBAN", "TestBah")
65
 	tList2.Add("EXTBAN", "TestBah")
60
 	tList2.AddNoValue("STABLEKILL")
66
 	tList2.AddNoValue("STABLEKILL")
61
-	tList2.RegenerateCachedReply()
67
+	err = tList2.RegenerateCachedReply()
68
+	if err != nil {
69
+		t.Error(err)
70
+	}
62
 
71
 
63
 	expected = [][]string{{"CASEMAPPING=ascii", "EXTBAN=TestBah", "INVEX", "SASL=yes", "STABLEKILL"}}
72
 	expected = [][]string{{"CASEMAPPING=ascii", "EXTBAN=TestBah", "INVEX", "SASL=yes", "STABLEKILL"}}
64
 	if !reflect.DeepEqual(tList2.CachedReply, expected) {
73
 	if !reflect.DeepEqual(tList2.CachedReply, expected) {
72
 		t.Error("difference reply does not match expected difference reply")
81
 		t.Error("difference reply does not match expected difference reply")
73
 	}
82
 	}
74
 }
83
 }
84
+
85
+func TestBadToken(t *testing.T) {
86
+	list := NewList()
87
+	list.Add("NETWORK", "Bad Network Name")
88
+	list.Add("SASL", "yes")
89
+	list.Add("CASEMAPPING", "rfc1459-strict")
90
+	list.Add("INVEX", "i")
91
+	list.AddNoValue("EXTBAN")
92
+
93
+	err := list.RegenerateCachedReply()
94
+	if err == nil {
95
+		t.Error("isupport token generation should fail due to space in network name")
96
+	}
97
+
98
+	// should produce a list containing the other, valid params
99
+	numParams := 0
100
+	for _, tokenLine := range list.CachedReply {
101
+		numParams += len(tokenLine)
102
+	}
103
+	if numParams != 4 {
104
+		t.Errorf("expected the other 4 params to be generated, got %v", list.CachedReply)
105
+	}
106
+}

+ 10
- 3
irc/server.go Voir le fichier

147
 }
147
 }
148
 
148
 
149
 // setISupport sets up our RPL_ISUPPORT reply.
149
 // setISupport sets up our RPL_ISUPPORT reply.
150
-func (server *Server) setISupport() {
150
+func (server *Server) setISupport() (err error) {
151
 	maxTargetsString := strconv.Itoa(maxTargets)
151
 	maxTargetsString := strconv.Itoa(maxTargets)
152
 
152
 
153
 	config := server.Config()
153
 	config := server.Config()
192
 		isupport.Add("REGCREDTYPES", "passphrase,certfp")
192
 		isupport.Add("REGCREDTYPES", "passphrase,certfp")
193
 	}
193
 	}
194
 
194
 
195
-	isupport.RegenerateCachedReply()
195
+	err = isupport.RegenerateCachedReply()
196
+	if err != nil {
197
+		return
198
+	}
196
 
199
 
197
 	server.configurableStateMutex.Lock()
200
 	server.configurableStateMutex.Lock()
198
 	server.isupport = isupport
201
 	server.isupport = isupport
199
 	server.configurableStateMutex.Unlock()
202
 	server.configurableStateMutex.Unlock()
203
+	return
200
 }
204
 }
201
 
205
 
202
 func loadChannelList(channel *Channel, list string, maskMode modes.Mode) {
206
 func loadChannelList(channel *Channel, list string, maskMode modes.Mode) {
787
 	// set RPL_ISUPPORT
791
 	// set RPL_ISUPPORT
788
 	var newISupportReplies [][]string
792
 	var newISupportReplies [][]string
789
 	oldISupportList := server.ISupport()
793
 	oldISupportList := server.ISupport()
790
-	server.setISupport()
794
+	err = server.setISupport()
795
+	if err != nil {
796
+		return err
797
+	}
791
 	if oldISupportList != nil {
798
 	if oldISupportList != nil {
792
 		newISupportReplies = oldISupportList.GetDifference(server.ISupport())
799
 		newISupportReplies = oldISupportList.GetDifference(server.ISupport())
793
 	}
800
 	}

Chargement…
Annuler
Enregistrer