| 1 |
// Copyright 2012-2024 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package server |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"fmt" |
| 19 |
"testing" |
| 20 |
"time" |
| 21 |
|
| 22 |
"github.com/nats-io/jwt/v2" |
| 23 |
) |
| 24 |
|
| 25 |
func dummyClient() *client { |
| 26 |
opts := defaultServerOptions |
| 27 |
return &client{srv: New(&opts), msubs: -1, mpay: -1, mcl: MAX_CONTROL_LINE_SIZE} |
| 28 |
} |
| 29 |
|
| 30 |
func dummyRouteClient() *client { |
| 31 |
opts := defaultServerOptions |
| 32 |
return &client{srv: New(&opts), kind: ROUTER, mcl: MAX_CONTROL_LINE_SIZE} |
| 33 |
} |
| 34 |
|
| 35 |
func TestParsePing(t *testing.T) { |
| 36 |
c := dummyClient() |
| 37 |
if c.state != OP_START { |
| 38 |
t.Fatalf("Expected OP_START vs %d\n", c.state) |
| 39 |
} |
| 40 |
ping := []byte("PING\r\n") |
| 41 |
err := c.parse(ping[:1]) |
| 42 |
if err != nil || c.state != OP_P { |
| 43 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 44 |
} |
| 45 |
err = c.parse(ping[1:2]) |
| 46 |
if err != nil || c.state != OP_PI { |
| 47 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 48 |
} |
| 49 |
err = c.parse(ping[2:3]) |
| 50 |
if err != nil || c.state != OP_PIN { |
| 51 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 52 |
} |
| 53 |
err = c.parse(ping[3:4]) |
| 54 |
if err != nil || c.state != OP_PING { |
| 55 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 56 |
} |
| 57 |
err = c.parse(ping[4:5]) |
| 58 |
if err != nil || c.state != OP_PING { |
| 59 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 60 |
} |
| 61 |
err = c.parse(ping[5:6]) |
| 62 |
if err != nil || c.state != OP_START { |
| 63 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 64 |
} |
| 65 |
err = c.parse(ping) |
| 66 |
if err != nil || c.state != OP_START { |
| 67 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 68 |
} |
| 69 |
// Should tolerate spaces |
| 70 |
ping = []byte("PING \r") |
| 71 |
err = c.parse(ping) |
| 72 |
if err != nil || c.state != OP_PING { |
| 73 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 74 |
} |
| 75 |
c.state = OP_START |
| 76 |
ping = []byte("PING \r \n") |
| 77 |
err = c.parse(ping) |
| 78 |
if err != nil || c.state != OP_START { |
| 79 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestParsePong(t *testing.T) { |
| 84 |
c := dummyClient() |
| 85 |
if c.state != OP_START { |
| 86 |
t.Fatalf("Expected OP_START vs %d\n", c.state) |
| 87 |
} |
| 88 |
pong := []byte("PONG\r\n") |
| 89 |
err := c.parse(pong[:1]) |
| 90 |
if err != nil || c.state != OP_P { |
| 91 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 92 |
} |
| 93 |
err = c.parse(pong[1:2]) |
| 94 |
if err != nil || c.state != OP_PO { |
| 95 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 96 |
} |
| 97 |
err = c.parse(pong[2:3]) |
| 98 |
if err != nil || c.state != OP_PON { |
| 99 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 100 |
} |
| 101 |
err = c.parse(pong[3:4]) |
| 102 |
if err != nil || c.state != OP_PONG { |
| 103 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 104 |
} |
| 105 |
err = c.parse(pong[4:5]) |
| 106 |
if err != nil || c.state != OP_PONG { |
| 107 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 108 |
} |
| 109 |
err = c.parse(pong[5:6]) |
| 110 |
if err != nil || c.state != OP_START { |
| 111 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 112 |
} |
| 113 |
if c.ping.out != 0 { |
| 114 |
t.Fatalf("Unexpected ping.out value: %d vs 0\n", c.ping.out) |
| 115 |
} |
| 116 |
err = c.parse(pong) |
| 117 |
if err != nil || c.state != OP_START { |
| 118 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 119 |
} |
| 120 |
if c.ping.out != 0 { |
| 121 |
t.Fatalf("Unexpected ping.out value: %d vs 0\n", c.ping.out) |
| 122 |
} |
| 123 |
// Should tolerate spaces |
| 124 |
pong = []byte("PONG \r") |
| 125 |
err = c.parse(pong) |
| 126 |
if err != nil || c.state != OP_PONG { |
| 127 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 128 |
} |
| 129 |
c.state = OP_START |
| 130 |
pong = []byte("PONG \r \n") |
| 131 |
err = c.parse(pong) |
| 132 |
if err != nil || c.state != OP_START { |
| 133 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 134 |
} |
| 135 |
if c.ping.out != 0 { |
| 136 |
t.Fatalf("Unexpected ping.out value: %d vs 0\n", c.ping.out) |
| 137 |
} |
| 138 |
|
| 139 |
// Should be adjusting c.pout (Pings Outstanding): reset to 0 |
| 140 |
c.state = OP_START |
| 141 |
c.ping.out = 10 |
| 142 |
pong = []byte("PONG\r\n") |
| 143 |
err = c.parse(pong) |
| 144 |
if err != nil || c.state != OP_START { |
| 145 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 146 |
} |
| 147 |
if c.ping.out != 0 { |
| 148 |
t.Fatalf("Unexpected ping.out: %d vs 0\n", c.ping.out) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func TestParseConnect(t *testing.T) { |
| 153 |
c := dummyClient() |
| 154 |
connect := []byte("CONNECT {\"verbose\":false,\"pedantic\":true,\"tls_required\":false}\r\n") |
| 155 |
err := c.parse(connect) |
| 156 |
if err != nil || c.state != OP_START { |
| 157 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 158 |
} |
| 159 |
// Check saved state |
| 160 |
if c.as != 8 { |
| 161 |
t.Fatalf("ArgStart state incorrect: 8 vs %d\n", c.as) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func TestParseSub(t *testing.T) { |
| 166 |
c := dummyClient() |
| 167 |
sub := []byte("SUB foo 1\r") |
| 168 |
err := c.parse(sub) |
| 169 |
if err != nil || c.state != SUB_ARG { |
| 170 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 171 |
} |
| 172 |
// Check saved state |
| 173 |
if c.as != 4 { |
| 174 |
t.Fatalf("ArgStart state incorrect: 4 vs %d\n", c.as) |
| 175 |
} |
| 176 |
if c.drop != 1 { |
| 177 |
t.Fatalf("Drop state incorrect: 1 vs %d\n", c.as) |
| 178 |
} |
| 179 |
if !bytes.Equal(sub[c.as:], []byte("foo 1\r")) { |
| 180 |
t.Fatalf("Arg state incorrect: %s\n", sub[c.as:]) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func TestParsePub(t *testing.T) { |
| 185 |
c := dummyClient() |
| 186 |
|
| 187 |
pub := []byte("PUB foo 5\r\nhello\r") |
| 188 |
err := c.parse(pub) |
| 189 |
if err != nil || c.state != MSG_END_N { |
| 190 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 191 |
} |
| 192 |
if !bytes.Equal(c.pa.subject, []byte("foo")) { |
| 193 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", string(c.pa.subject)) |
| 194 |
} |
| 195 |
if c.pa.reply != nil { |
| 196 |
t.Fatalf("Did not parse reply correctly: 'nil' vs '%s'\n", string(c.pa.reply)) |
| 197 |
} |
| 198 |
if c.pa.size != 5 { |
| 199 |
t.Fatalf("Did not parse msg size correctly: 5 vs %d\n", c.pa.size) |
| 200 |
} |
| 201 |
|
| 202 |
// Clear snapshots |
| 203 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 204 |
|
| 205 |
pub = []byte("PUB foo.bar INBOX.22 11\r\nhello world\r") |
| 206 |
err = c.parse(pub) |
| 207 |
if err != nil || c.state != MSG_END_N { |
| 208 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 209 |
} |
| 210 |
if !bytes.Equal(c.pa.subject, []byte("foo.bar")) { |
| 211 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", string(c.pa.subject)) |
| 212 |
} |
| 213 |
if !bytes.Equal(c.pa.reply, []byte("INBOX.22")) { |
| 214 |
t.Fatalf("Did not parse reply correctly: 'INBOX.22' vs '%s'\n", string(c.pa.reply)) |
| 215 |
} |
| 216 |
if c.pa.size != 11 { |
| 217 |
t.Fatalf("Did not parse msg size correctly: 11 vs %d\n", c.pa.size) |
| 218 |
} |
| 219 |
|
| 220 |
// Clear snapshots |
| 221 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 222 |
|
| 223 |
// This is the case when data has more bytes than expected by size. |
| 224 |
pub = []byte("PUB foo.bar 11\r\nhello world hello world\r") |
| 225 |
err = c.parse(pub) |
| 226 |
if err == nil { |
| 227 |
t.Fatalf("Expected an error parsing longer than expected message body") |
| 228 |
} |
| 229 |
if c.msgBuf != nil { |
| 230 |
t.Fatalf("Did not expect a c.msgBuf to be non-nil") |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// https://www.twistlock.com/labs-blog/finding-dos-vulnerability-nats-go-fuzz-cve-2019-13126/ |
| 235 |
func TestParsePubSizeOverflow(t *testing.T) { |
| 236 |
c := dummyClient() |
| 237 |
|
| 238 |
pub := []byte("PUB foo 3333333333333333333333333333333333333333333333333333333333333333\r\n") |
| 239 |
if err := c.parse(pub); err == nil { |
| 240 |
t.Fatalf("Expected an error") |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func TestParsePubArg(t *testing.T) { |
| 245 |
c := dummyClient() |
| 246 |
|
| 247 |
for _, test := range []struct { |
| 248 |
arg string |
| 249 |
subject string |
| 250 |
reply string |
| 251 |
size int |
| 252 |
szb string |
| 253 |
}{ |
| 254 |
{arg: "a 2", subject: "a", reply: "", size: 2, szb: "2"}, |
| 255 |
{arg: "a 222", subject: "a", reply: "", size: 222, szb: "222"}, |
| 256 |
{arg: "foo 22", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 257 |
{arg: " foo 22", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 258 |
{arg: "foo 22 ", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 259 |
{arg: "foo 22", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 260 |
{arg: " foo 22 ", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 261 |
{arg: " foo 22 ", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 262 |
{arg: "foo bar 22", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 263 |
{arg: " foo bar 22", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 264 |
{arg: "foo bar 22 ", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 265 |
{arg: "foo bar 22", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 266 |
{arg: " foo bar 22 ", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 267 |
{arg: " foo bar 22 ", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 268 |
{arg: " foo bar 2222 ", subject: "foo", reply: "bar", size: 2222, szb: "2222"}, |
| 269 |
{arg: " foo 2222 ", subject: "foo", reply: "", size: 2222, szb: "2222"}, |
| 270 |
{arg: "a\t2", subject: "a", reply: "", size: 2, szb: "2"}, |
| 271 |
{arg: "a\t222", subject: "a", reply: "", size: 222, szb: "222"}, |
| 272 |
{arg: "foo\t22", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 273 |
{arg: "\tfoo\t22", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 274 |
{arg: "foo\t22\t", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 275 |
{arg: "foo\t\t\t22", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 276 |
{arg: "\tfoo\t22\t", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 277 |
{arg: "\tfoo\t\t\t22\t", subject: "foo", reply: "", size: 22, szb: "22"}, |
| 278 |
{arg: "foo\tbar\t22", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 279 |
{arg: "\tfoo\tbar\t22", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 280 |
{arg: "foo\tbar\t22\t", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 281 |
{arg: "foo\t\tbar\t\t22", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 282 |
{arg: "\tfoo\tbar\t22\t", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 283 |
{arg: "\t \tfoo\t \t \tbar\t \t22\t \t", subject: "foo", reply: "bar", size: 22, szb: "22"}, |
| 284 |
{arg: "\t\tfoo\t\t\tbar\t\t2222\t\t", subject: "foo", reply: "bar", size: 2222, szb: "2222"}, |
| 285 |
{arg: "\t \tfoo\t \t \t\t\t2222\t \t", subject: "foo", reply: "", size: 2222, szb: "2222"}, |
| 286 |
} { |
| 287 |
t.Run(test.arg, func(t *testing.T) { |
| 288 |
if err := c.processPub([]byte(test.arg)); err != nil { |
| 289 |
t.Fatalf("Unexpected parse error: %v\n", err) |
| 290 |
} |
| 291 |
if !bytes.Equal(c.pa.subject, []byte(test.subject)) { |
| 292 |
t.Fatalf("Mismatched subject: '%s'\n", c.pa.subject) |
| 293 |
} |
| 294 |
if !bytes.Equal(c.pa.reply, []byte(test.reply)) { |
| 295 |
t.Fatalf("Mismatched reply subject: '%s'\n", c.pa.reply) |
| 296 |
} |
| 297 |
if !bytes.Equal(c.pa.szb, []byte(test.szb)) { |
| 298 |
t.Fatalf("Bad size buf: '%s'\n", c.pa.szb) |
| 299 |
} |
| 300 |
if c.pa.size != test.size { |
| 301 |
t.Fatalf("Bad size: %d\n", c.pa.size) |
| 302 |
} |
| 303 |
}) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
func TestParsePubBadSize(t *testing.T) { |
| 308 |
c := dummyClient() |
| 309 |
// Setup localized max payload |
| 310 |
c.mpay = 32768 |
| 311 |
if err := c.processPub([]byte("foo 2222222222222222")); err == nil { |
| 312 |
t.Fatalf("Expected parse error for size too large") |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
func TestParseLeafMsgBadSize(t *testing.T) { |
| 317 |
c := dummyClient() |
| 318 |
c.kind = LEAF |
| 319 |
c.leaf = &leaf{} |
| 320 |
c.mpay = 16 |
| 321 |
require_Error(t, c.processLeafMsgArgs([]byte("foo 17")), ErrMaxPayload) |
| 322 |
} |
| 323 |
|
| 324 |
func TestParseLeafHeaderMsgBadSize(t *testing.T) { |
| 325 |
c := dummyClient() |
| 326 |
c.kind = LEAF |
| 327 |
c.leaf = &leaf{} |
| 328 |
c.mpay = 16 |
| 329 |
require_Error(t, c.processLeafHeaderMsgArgs([]byte("foo 4 17")), ErrMaxPayload) |
| 330 |
} |
| 331 |
|
| 332 |
func TestParseHeaderPub(t *testing.T) { |
| 333 |
c := dummyClient() |
| 334 |
c.headers = true |
| 335 |
|
| 336 |
hpub := []byte("HPUB foo 12 17\r\nname:derek\r\nHELLO\r") |
| 337 |
if err := c.parse(hpub); err != nil || c.state != MSG_END_N { |
| 338 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 339 |
} |
| 340 |
if !bytes.Equal(c.pa.subject, []byte("foo")) { |
| 341 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'", c.pa.subject) |
| 342 |
} |
| 343 |
if c.pa.reply != nil { |
| 344 |
t.Fatalf("Did not parse reply correctly: 'nil' vs '%s'", c.pa.reply) |
| 345 |
} |
| 346 |
if c.pa.hdr != 12 { |
| 347 |
t.Fatalf("Did not parse msg header size correctly: 12 vs %d", c.pa.hdr) |
| 348 |
} |
| 349 |
if !bytes.Equal(c.pa.hdb, []byte("12")) { |
| 350 |
t.Fatalf("Did not parse or capture the header size as bytes correctly: %q", c.pa.hdb) |
| 351 |
} |
| 352 |
if c.pa.size != 17 { |
| 353 |
t.Fatalf("Did not parse msg size correctly: 17 vs %d", c.pa.size) |
| 354 |
} |
| 355 |
|
| 356 |
// Clear snapshots |
| 357 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 358 |
|
| 359 |
hpub = []byte("HPUB foo INBOX.22 12 17\r\nname:derek\r\nHELLO\r") |
| 360 |
if err := c.parse(hpub); err != nil || c.state != MSG_END_N { |
| 361 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 362 |
} |
| 363 |
if !bytes.Equal(c.pa.subject, []byte("foo")) { |
| 364 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'", c.pa.subject) |
| 365 |
} |
| 366 |
if !bytes.Equal(c.pa.reply, []byte("INBOX.22")) { |
| 367 |
t.Fatalf("Did not parse reply correctly: 'INBOX.22' vs '%s'", c.pa.reply) |
| 368 |
} |
| 369 |
if c.pa.hdr != 12 { |
| 370 |
t.Fatalf("Did not parse msg header size correctly: 12 vs %d", c.pa.hdr) |
| 371 |
} |
| 372 |
if !bytes.Equal(c.pa.hdb, []byte("12")) { |
| 373 |
t.Fatalf("Did not parse or capture the header size as bytes correctly: %q", c.pa.hdb) |
| 374 |
} |
| 375 |
if c.pa.size != 17 { |
| 376 |
t.Fatalf("Did not parse msg size correctly: 17 vs %d", c.pa.size) |
| 377 |
} |
| 378 |
|
| 379 |
// Clear snapshots |
| 380 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 381 |
|
| 382 |
hpub = []byte("HPUB foo INBOX.22 0 5\r\nHELLO\r") |
| 383 |
if err := c.parse(hpub); err != nil || c.state != MSG_END_N { |
| 384 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 385 |
} |
| 386 |
if !bytes.Equal(c.pa.subject, []byte("foo")) { |
| 387 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject) |
| 388 |
} |
| 389 |
if !bytes.Equal(c.pa.reply, []byte("INBOX.22")) { |
| 390 |
t.Fatalf("Did not parse reply correctly: 'INBOX.22' vs '%s'\n", c.pa.reply) |
| 391 |
} |
| 392 |
if c.pa.hdr != 0 { |
| 393 |
t.Fatalf("Did not parse msg header size correctly: 0 vs %d\n", c.pa.hdr) |
| 394 |
} |
| 395 |
if !bytes.Equal(c.pa.hdb, []byte("0")) { |
| 396 |
t.Fatalf("Did not parse or capture the header size as bytes correctly: %q", c.pa.hdb) |
| 397 |
} |
| 398 |
if c.pa.size != 5 { |
| 399 |
t.Fatalf("Did not parse msg size correctly: 5 vs %d\n", c.pa.size) |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
func TestParseHeaderPubArg(t *testing.T) { |
| 404 |
c := dummyClient() |
| 405 |
c.headers = true |
| 406 |
|
| 407 |
for _, test := range []struct { |
| 408 |
arg string |
| 409 |
subject string |
| 410 |
reply string |
| 411 |
hdr int |
| 412 |
size int |
| 413 |
szb string |
| 414 |
}{ |
| 415 |
{arg: "a 2 4", subject: "a", reply: "", hdr: 2, size: 4, szb: "4"}, |
| 416 |
{arg: "a 22 222", subject: "a", reply: "", hdr: 22, size: 222, szb: "222"}, |
| 417 |
{arg: "foo 3 22", subject: "foo", reply: "", hdr: 3, size: 22, szb: "22"}, |
| 418 |
{arg: " foo 1 22", subject: "foo", reply: "", hdr: 1, size: 22, szb: "22"}, |
| 419 |
{arg: "foo 0 22 ", subject: "foo", reply: "", hdr: 0, size: 22, szb: "22"}, |
| 420 |
{arg: "foo 0 22", subject: "foo", reply: "", hdr: 0, size: 22, szb: "22"}, |
| 421 |
{arg: " foo 1 22 ", subject: "foo", reply: "", hdr: 1, size: 22, szb: "22"}, |
| 422 |
{arg: " foo 3 22 ", subject: "foo", reply: "", hdr: 3, size: 22, szb: "22"}, |
| 423 |
{arg: "foo bar 1 22", subject: "foo", reply: "bar", hdr: 1, size: 22, szb: "22"}, |
| 424 |
{arg: " foo bar 11 22", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"}, |
| 425 |
{arg: "foo bar 11 22 ", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"}, |
| 426 |
{arg: "foo bar 11 22", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"}, |
| 427 |
{arg: " foo bar 11 22 ", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"}, |
| 428 |
{arg: " foo bar 11 22 ", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"}, |
| 429 |
{arg: " foo bar 22 2222 ", subject: "foo", reply: "bar", hdr: 22, size: 2222, szb: "2222"}, |
| 430 |
{arg: " foo 1 2222 ", subject: "foo", reply: "", hdr: 1, size: 2222, szb: "2222"}, |
| 431 |
{arg: "a\t2\t22", subject: "a", reply: "", hdr: 2, size: 22, szb: "22"}, |
| 432 |
{arg: "a\t2\t\t222", subject: "a", reply: "", hdr: 2, size: 222, szb: "222"}, |
| 433 |
{arg: "foo\t2 22", subject: "foo", reply: "", hdr: 2, size: 22, szb: "22"}, |
| 434 |
{arg: "\tfoo\t11\t 22", subject: "foo", reply: "", hdr: 11, size: 22, szb: "22"}, |
| 435 |
{arg: "foo\t11\t22\t", subject: "foo", reply: "", hdr: 11, size: 22, szb: "22"}, |
| 436 |
{arg: "foo\t\t\t11 22", subject: "foo", reply: "", hdr: 11, size: 22, szb: "22"}, |
| 437 |
{arg: "\tfoo\t11\t \t 22\t", subject: "foo", reply: "", hdr: 11, size: 22, szb: "22"}, |
| 438 |
{arg: "\tfoo\t\t\t11 22\t", subject: "foo", reply: "", hdr: 11, size: 22, szb: "22"}, |
| 439 |
{arg: "foo\tbar\t2 22", subject: "foo", reply: "bar", hdr: 2, size: 22, szb: "22"}, |
| 440 |
{arg: "\tfoo\tbar\t11\t22", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"}, |
| 441 |
{arg: "foo\tbar\t11\t\t22\t ", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"}, |
| 442 |
{arg: "foo\t\tbar\t\t11\t\t\t22", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"}, |
| 443 |
{arg: "\tfoo\tbar\t11\t22\t", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"}, |
| 444 |
{arg: "\t \tfoo\t \t \tbar\t \t11\t 22\t \t", subject: "foo", reply: "bar", hdr: 11, size: 22, szb: "22"}, |
| 445 |
{arg: "\t\tfoo\t\t\tbar\t\t22\t\t\t2222\t\t", subject: "foo", reply: "bar", hdr: 22, size: 2222, szb: "2222"}, |
| 446 |
{arg: "\t \tfoo\t \t \t\t\t11\t\t 2222\t \t", subject: "foo", reply: "", hdr: 11, size: 2222, szb: "2222"}, |
| 447 |
} { |
| 448 |
t.Run(test.arg, func(t *testing.T) { |
| 449 |
if err := c.processHeaderPub([]byte(test.arg), nil); err != nil { |
| 450 |
t.Fatalf("Unexpected parse error: %v\n", err) |
| 451 |
} |
| 452 |
if !bytes.Equal(c.pa.subject, []byte(test.subject)) { |
| 453 |
t.Fatalf("Mismatched subject: '%s'\n", c.pa.subject) |
| 454 |
} |
| 455 |
if !bytes.Equal(c.pa.reply, []byte(test.reply)) { |
| 456 |
t.Fatalf("Mismatched reply subject: '%s'\n", c.pa.reply) |
| 457 |
} |
| 458 |
if !bytes.Equal(c.pa.szb, []byte(test.szb)) { |
| 459 |
t.Fatalf("Bad size buf: '%s'\n", c.pa.szb) |
| 460 |
} |
| 461 |
if c.pa.hdr != test.hdr { |
| 462 |
t.Fatalf("Bad header size: %d\n", c.pa.hdr) |
| 463 |
} |
| 464 |
if c.pa.size != test.size { |
| 465 |
t.Fatalf("Bad size: %d\n", c.pa.size) |
| 466 |
} |
| 467 |
}) |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
func TestParseRoutedHeaderMsg(t *testing.T) { |
| 472 |
c := dummyRouteClient() |
| 473 |
c.route = &route{} |
| 474 |
|
| 475 |
pub := []byte("HMSG $foo foo 10 8\r\nXXXhello\r") |
| 476 |
if err := c.parse(pub); err == nil { |
| 477 |
t.Fatalf("Expected an error") |
| 478 |
} |
| 479 |
|
| 480 |
// Clear snapshots |
| 481 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 482 |
|
| 483 |
pub = []byte("HMSG $foo foo 3 8\r\nXXXhello\r") |
| 484 |
err := c.parse(pub) |
| 485 |
if err != nil || c.state != MSG_END_N { |
| 486 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 487 |
} |
| 488 |
if !bytes.Equal(c.pa.account, []byte("$foo")) { |
| 489 |
t.Fatalf("Did not parse account correctly: '$foo' vs '%s'\n", c.pa.account) |
| 490 |
} |
| 491 |
if !bytes.Equal(c.pa.subject, []byte("foo")) { |
| 492 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject) |
| 493 |
} |
| 494 |
if c.pa.reply != nil { |
| 495 |
t.Fatalf("Did not parse reply correctly: 'nil' vs '%s'\n", c.pa.reply) |
| 496 |
} |
| 497 |
if c.pa.hdr != 3 { |
| 498 |
t.Fatalf("Did not parse header size correctly: 3 vs %d\n", c.pa.hdr) |
| 499 |
} |
| 500 |
if c.pa.size != 8 { |
| 501 |
t.Fatalf("Did not parse msg size correctly: 8 vs %d\n", c.pa.size) |
| 502 |
} |
| 503 |
|
| 504 |
// Clear snapshots |
| 505 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 506 |
|
| 507 |
pub = []byte("HMSG $G foo.bar INBOX.22 3 14\r\nOK:hello world\r") |
| 508 |
err = c.parse(pub) |
| 509 |
if err != nil || c.state != MSG_END_N { |
| 510 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 511 |
} |
| 512 |
if !bytes.Equal(c.pa.account, []byte("$G")) { |
| 513 |
t.Fatalf("Did not parse account correctly: '$G' vs '%s'\n", c.pa.account) |
| 514 |
} |
| 515 |
if !bytes.Equal(c.pa.subject, []byte("foo.bar")) { |
| 516 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject) |
| 517 |
} |
| 518 |
if !bytes.Equal(c.pa.reply, []byte("INBOX.22")) { |
| 519 |
t.Fatalf("Did not parse reply correctly: 'INBOX.22' vs '%s'\n", c.pa.reply) |
| 520 |
} |
| 521 |
if c.pa.hdr != 3 { |
| 522 |
t.Fatalf("Did not parse header size correctly: 3 vs %d\n", c.pa.hdr) |
| 523 |
} |
| 524 |
if c.pa.size != 14 { |
| 525 |
t.Fatalf("Did not parse msg size correctly: 14 vs %d\n", c.pa.size) |
| 526 |
} |
| 527 |
|
| 528 |
// Clear snapshots |
| 529 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 530 |
|
| 531 |
pub = []byte("HMSG $G foo.bar + reply baz 3 14\r\nOK:hello world\r") |
| 532 |
err = c.parse(pub) |
| 533 |
if err != nil || c.state != MSG_END_N { |
| 534 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 535 |
} |
| 536 |
if !bytes.Equal(c.pa.account, []byte("$G")) { |
| 537 |
t.Fatalf("Did not parse account correctly: '$G' vs '%s'\n", c.pa.account) |
| 538 |
} |
| 539 |
if !bytes.Equal(c.pa.subject, []byte("foo.bar")) { |
| 540 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject) |
| 541 |
} |
| 542 |
if !bytes.Equal(c.pa.reply, []byte("reply")) { |
| 543 |
t.Fatalf("Did not parse reply correctly: 'reply' vs '%s'\n", c.pa.reply) |
| 544 |
} |
| 545 |
if len(c.pa.queues) != 1 { |
| 546 |
t.Fatalf("Expected 1 queue, got %d", len(c.pa.queues)) |
| 547 |
} |
| 548 |
if !bytes.Equal(c.pa.queues[0], []byte("baz")) { |
| 549 |
t.Fatalf("Did not parse queues correctly: 'baz' vs '%q'\n", c.pa.queues[0]) |
| 550 |
} |
| 551 |
if c.pa.hdr != 3 { |
| 552 |
t.Fatalf("Did not parse header size correctly: 3 vs %d\n", c.pa.hdr) |
| 553 |
} |
| 554 |
if c.pa.size != 14 { |
| 555 |
t.Fatalf("Did not parse msg size correctly: 14 vs %d\n", c.pa.size) |
| 556 |
} |
| 557 |
|
| 558 |
// Clear snapshots |
| 559 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 560 |
|
| 561 |
pub = []byte("HMSG $G foo.bar | baz 3 14\r\nOK:hello world\r") |
| 562 |
err = c.parse(pub) |
| 563 |
if err != nil || c.state != MSG_END_N { |
| 564 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 565 |
} |
| 566 |
if !bytes.Equal(c.pa.account, []byte("$G")) { |
| 567 |
t.Fatalf("Did not parse account correctly: '$G' vs '%s'\n", c.pa.account) |
| 568 |
} |
| 569 |
if !bytes.Equal(c.pa.subject, []byte("foo.bar")) { |
| 570 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject) |
| 571 |
} |
| 572 |
if !bytes.Equal(c.pa.reply, []byte("")) { |
| 573 |
t.Fatalf("Did not parse reply correctly: '' vs '%s'\n", c.pa.reply) |
| 574 |
} |
| 575 |
if len(c.pa.queues) != 1 { |
| 576 |
t.Fatalf("Expected 1 queue, got %d", len(c.pa.queues)) |
| 577 |
} |
| 578 |
if !bytes.Equal(c.pa.queues[0], []byte("baz")) { |
| 579 |
t.Fatalf("Did not parse queues correctly: 'baz' vs '%q'\n", c.pa.queues[0]) |
| 580 |
} |
| 581 |
if c.pa.hdr != 3 { |
| 582 |
t.Fatalf("Did not parse header size correctly: 3 vs %d\n", c.pa.hdr) |
| 583 |
} |
| 584 |
if c.pa.size != 14 { |
| 585 |
t.Fatalf("Did not parse msg size correctly: 14 vs %d\n", c.pa.size) |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
func TestParseRouteMsg(t *testing.T) { |
| 590 |
c := dummyRouteClient() |
| 591 |
c.route = &route{} |
| 592 |
|
| 593 |
pub := []byte("MSG $foo foo 5\r\nhello\r") |
| 594 |
err := c.parse(pub) |
| 595 |
if err == nil { |
| 596 |
t.Fatalf("Expected an error, got none") |
| 597 |
} |
| 598 |
pub = []byte("RMSG $foo foo 5\r\nhello\r") |
| 599 |
err = c.parse(pub) |
| 600 |
if err != nil || c.state != MSG_END_N { |
| 601 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 602 |
} |
| 603 |
if !bytes.Equal(c.pa.account, []byte("$foo")) { |
| 604 |
t.Fatalf("Did not parse account correctly: '$foo' vs '%s'\n", c.pa.account) |
| 605 |
} |
| 606 |
if !bytes.Equal(c.pa.subject, []byte("foo")) { |
| 607 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject) |
| 608 |
} |
| 609 |
if c.pa.reply != nil { |
| 610 |
t.Fatalf("Did not parse reply correctly: 'nil' vs '%s'\n", c.pa.reply) |
| 611 |
} |
| 612 |
if c.pa.size != 5 { |
| 613 |
t.Fatalf("Did not parse msg size correctly: 5 vs %d\n", c.pa.size) |
| 614 |
} |
| 615 |
|
| 616 |
// Clear snapshots |
| 617 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 618 |
|
| 619 |
pub = []byte("RMSG $G foo.bar INBOX.22 11\r\nhello world\r") |
| 620 |
err = c.parse(pub) |
| 621 |
if err != nil || c.state != MSG_END_N { |
| 622 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 623 |
} |
| 624 |
if !bytes.Equal(c.pa.account, []byte("$G")) { |
| 625 |
t.Fatalf("Did not parse account correctly: '$G' vs '%s'\n", c.pa.account) |
| 626 |
} |
| 627 |
if !bytes.Equal(c.pa.subject, []byte("foo.bar")) { |
| 628 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject) |
| 629 |
} |
| 630 |
if !bytes.Equal(c.pa.reply, []byte("INBOX.22")) { |
| 631 |
t.Fatalf("Did not parse reply correctly: 'INBOX.22' vs '%s'\n", c.pa.reply) |
| 632 |
} |
| 633 |
if c.pa.size != 11 { |
| 634 |
t.Fatalf("Did not parse msg size correctly: 11 vs %d\n", c.pa.size) |
| 635 |
} |
| 636 |
|
| 637 |
// Clear snapshots |
| 638 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 639 |
|
| 640 |
pub = []byte("RMSG $G foo.bar + reply baz 11\r\nhello world\r") |
| 641 |
err = c.parse(pub) |
| 642 |
if err != nil || c.state != MSG_END_N { |
| 643 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 644 |
} |
| 645 |
if !bytes.Equal(c.pa.account, []byte("$G")) { |
| 646 |
t.Fatalf("Did not parse account correctly: '$G' vs '%s'\n", c.pa.account) |
| 647 |
} |
| 648 |
if !bytes.Equal(c.pa.subject, []byte("foo.bar")) { |
| 649 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject) |
| 650 |
} |
| 651 |
if !bytes.Equal(c.pa.reply, []byte("reply")) { |
| 652 |
t.Fatalf("Did not parse reply correctly: 'reply' vs '%s'\n", c.pa.reply) |
| 653 |
} |
| 654 |
if len(c.pa.queues) != 1 { |
| 655 |
t.Fatalf("Expected 1 queue, got %d", len(c.pa.queues)) |
| 656 |
} |
| 657 |
if !bytes.Equal(c.pa.queues[0], []byte("baz")) { |
| 658 |
t.Fatalf("Did not parse queues correctly: 'baz' vs '%q'\n", c.pa.queues[0]) |
| 659 |
} |
| 660 |
|
| 661 |
// Clear snapshots |
| 662 |
c.argBuf, c.msgBuf, c.state = nil, nil, OP_START |
| 663 |
|
| 664 |
pub = []byte("RMSG $G foo.bar | baz 11\r\nhello world\r") |
| 665 |
err = c.parse(pub) |
| 666 |
if err != nil || c.state != MSG_END_N { |
| 667 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 668 |
} |
| 669 |
if !bytes.Equal(c.pa.account, []byte("$G")) { |
| 670 |
t.Fatalf("Did not parse account correctly: '$G' vs '%s'\n", c.pa.account) |
| 671 |
} |
| 672 |
if !bytes.Equal(c.pa.subject, []byte("foo.bar")) { |
| 673 |
t.Fatalf("Did not parse subject correctly: 'foo' vs '%s'\n", c.pa.subject) |
| 674 |
} |
| 675 |
if !bytes.Equal(c.pa.reply, []byte("")) { |
| 676 |
t.Fatalf("Did not parse reply correctly: '' vs '%s'\n", c.pa.reply) |
| 677 |
} |
| 678 |
if len(c.pa.queues) != 1 { |
| 679 |
t.Fatalf("Expected 1 queue, got %d", len(c.pa.queues)) |
| 680 |
} |
| 681 |
if !bytes.Equal(c.pa.queues[0], []byte("baz")) { |
| 682 |
t.Fatalf("Did not parse queues correctly: 'baz' vs '%q'\n", c.pa.queues[0]) |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
func TestParseMsgSpace(t *testing.T) { |
| 687 |
c := dummyRouteClient() |
| 688 |
|
| 689 |
// Ivan bug he found |
| 690 |
if err := c.parse([]byte("MSG \r\n")); err == nil { |
| 691 |
t.Fatalf("Expected parse error for MSG <SPC>") |
| 692 |
} |
| 693 |
|
| 694 |
c = dummyClient() |
| 695 |
|
| 696 |
// Anything with an M from a client should parse error |
| 697 |
if err := c.parse([]byte("M")); err == nil { |
| 698 |
t.Fatalf("Expected parse error for M* from a client") |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
func TestParsePingNoAuthUserExceptionsAndRestrictions(t *testing.T) { |
| 703 |
opts := defaultServerOptions |
| 704 |
s := New(&opts) |
| 705 |
s.opts.NoAuthUser = "foo" |
| 706 |
s.users = map[string]*User{ |
| 707 |
"foo": {Username: "foo"}, |
| 708 |
} |
| 709 |
|
| 710 |
newAuthPendingClient := func(kind int, gw *gateway, route *route, leaf *leaf, ws *websocket) *client { |
| 711 |
c := &client{ |
| 712 |
srv: s, kind: kind, |
| 713 |
gw: gw, route: route, leaf: leaf, ws: ws, |
| 714 |
atmr: time.AfterFunc(time.Minute, func() {}), |
| 715 |
} |
| 716 |
c.flags.set(expectConnect) |
| 717 |
t.Cleanup(func() { |
| 718 |
c.mu.Lock() |
| 719 |
defer c.mu.Unlock() |
| 720 |
c.clearAuthTimer() |
| 721 |
}) |
| 722 |
return c |
| 723 |
} |
| 724 |
|
| 725 |
for _, test := range []struct { |
| 726 |
name string |
| 727 |
kind int |
| 728 |
gw *gateway |
| 729 |
route *route |
| 730 |
leaf *leaf |
| 731 |
}{ |
| 732 |
{name: "Leaf", kind: LEAF, leaf: &leaf{}}, |
| 733 |
{name: "Route", kind: ROUTER, route: &route{}}, |
| 734 |
{name: "Gateway", kind: GATEWAY, gw: &gateway{}}, |
| 735 |
} { |
| 736 |
t.Run(test.name+"NotAllowed", func(t *testing.T) { |
| 737 |
c := newAuthPendingClient(test.kind, test.gw, test.route, test.leaf, nil) |
| 738 |
require_Error(t, c.parse([]byte("PING\r\n")), ErrAuthentication) |
| 739 |
require_False(t, c.flags.isSet(connectReceived)) |
| 740 |
}) |
| 741 |
} |
| 742 |
|
| 743 |
for _, test := range []struct { |
| 744 |
name string |
| 745 |
kind int |
| 746 |
ws *websocket |
| 747 |
}{ |
| 748 |
{name: "Client", kind: CLIENT}, |
| 749 |
{name: "WebSocket", kind: CLIENT, ws: &websocket{}}, |
| 750 |
} { |
| 751 |
t.Run(test.name+"Allowed", func(t *testing.T) { |
| 752 |
c := newAuthPendingClient(test.kind, nil, nil, nil, test.ws) |
| 753 |
require_NoError(t, c.parse([]byte("PING\r\n"))) |
| 754 |
require_True(t, c.flags.isSet(connectReceived)) |
| 755 |
}) |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
func TestParsePingNoAuthUserEnforcesUserRestrictions(t *testing.T) { |
| 760 |
newServer := func(user *User) *Server { |
| 761 |
opts := defaultServerOptions |
| 762 |
s := New(&opts) |
| 763 |
s.opts.NoAuthUser = user.Username |
| 764 |
s.users = map[string]*User{user.Username: user} |
| 765 |
return s |
| 766 |
} |
| 767 |
|
| 768 |
newAuthPendingClient := func(s *Server, ws *websocket) *client { |
| 769 |
c := &client{ |
| 770 |
srv: s, kind: CLIENT, ws: ws, |
| 771 |
atmr: time.AfterFunc(time.Minute, func() {}), |
| 772 |
} |
| 773 |
c.flags.set(expectConnect) |
| 774 |
t.Cleanup(func() { |
| 775 |
c.mu.Lock() |
| 776 |
defer c.mu.Unlock() |
| 777 |
c.clearAuthTimer() |
| 778 |
}) |
| 779 |
return c |
| 780 |
} |
| 781 |
|
| 782 |
t.Run("AllowedConnectionTypes", func(t *testing.T) { |
| 783 |
// no_auth_user restricted to WebSocket connections only. |
| 784 |
s := newServer(&User{ |
| 785 |
Username: "foo", |
| 786 |
AllowedConnectionTypes: map[string]struct{}{jwt.ConnectionTypeWebsocket: {}}, |
| 787 |
}) |
| 788 |
|
| 789 |
// A standard TCP client must be rejected, just like the CONNECT path. |
| 790 |
c := newAuthPendingClient(s, nil) |
| 791 |
require_Error(t, c.parse([]byte("PING\r\n")), ErrAuthentication) |
| 792 |
require_False(t, c.flags.isSet(connectReceived)) |
| 793 |
|
| 794 |
// A WebSocket client satisfies the restriction and is allowed. |
| 795 |
c = newAuthPendingClient(s, &websocket{}) |
| 796 |
require_NoError(t, c.parse([]byte("PING\r\n"))) |
| 797 |
require_True(t, c.flags.isSet(connectReceived)) |
| 798 |
}) |
| 799 |
|
| 800 |
t.Run("ProxyRequired", func(t *testing.T) { |
| 801 |
// no_auth_user that may only connect through a trusted proxy. A |
| 802 |
// connection reaching the pre-CONNECT fast path never carries a proxy |
| 803 |
// signature, so it must always be rejected. |
| 804 |
s := newServer(&User{Username: "foo", ProxyRequired: true}) |
| 805 |
|
| 806 |
for _, ws := range []*websocket{nil, {}} { |
| 807 |
c := newAuthPendingClient(s, ws) |
| 808 |
require_Error(t, c.parse([]byte("PING\r\n")), ErrAuthentication) |
| 809 |
require_False(t, c.flags.isSet(connectReceived)) |
| 810 |
} |
| 811 |
}) |
| 812 |
} |
| 813 |
|
| 814 |
func TestParsePingAllowedAfterConnectForNonClients(t *testing.T) { |
| 815 |
for _, test := range []struct { |
| 816 |
name string |
| 817 |
kind int |
| 818 |
gw *gateway |
| 819 |
route *route |
| 820 |
leaf *leaf |
| 821 |
}{ |
| 822 |
{name: "Leaf", kind: LEAF, leaf: &leaf{}}, |
| 823 |
{name: "Route", kind: ROUTER, route: &route{}}, |
| 824 |
{name: "Gateway", kind: GATEWAY, gw: &gateway{}}, |
| 825 |
} { |
| 826 |
t.Run(test.name, func(t *testing.T) { |
| 827 |
c := &client{ |
| 828 |
srv: func() *Server { |
| 829 |
opts := defaultServerOptions |
| 830 |
return New(&opts) |
| 831 |
}(), |
| 832 |
kind: test.kind, |
| 833 |
gw: test.gw, |
| 834 |
route: test.route, |
| 835 |
leaf: test.leaf, |
| 836 |
} |
| 837 |
c.flags.set(connectReceived) |
| 838 |
|
| 839 |
require_NoError(t, c.parse([]byte("PING\r\n"))) |
| 840 |
require_Equal(t, c.state, OP_START) |
| 841 |
}) |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
func TestShouldFail(t *testing.T) { |
| 846 |
wrongProtos := []string{ |
| 847 |
"xxx", |
| 848 |
"Px", "PIx", "PINx", " PING", |
| 849 |
"POx", "PONx", |
| 850 |
"+x", "+Ox", |
| 851 |
"-x", "-Ex", "-ERx", "-ERRx", |
| 852 |
"Cx", "COx", "CONx", "CONNx", "CONNEx", "CONNECx", "CONNECx", "CONNECT \r\n", |
| 853 |
"PUx", "PUB foo\r\n", "PUB \r\n", "PUB foo bar \r\n", |
| 854 |
"PUB foo 2\r\nok \r\n", "PUB foo 2\r\nok\r \n", |
| 855 |
"Sx", "SUx", "SUB\r\n", "SUB \r\n", "SUB foo\r\n", |
| 856 |
"SUB foo bar baz 22\r\n", |
| 857 |
"Ux", "UNx", "UNSx", "UNSUx", "UNSUBx", "UNSUBUNSUB 1\r\n", "UNSUB_2\r\n", |
| 858 |
"UNSUB_UNSUB_UNSUB 2\r\n", "UNSUB_\t2\r\n", "UNSUB\r\n", "UNSUB \r\n", |
| 859 |
"UNSUB \t \r\n", |
| 860 |
"Ix", "INx", "INFx", "INFO \r\n", |
| 861 |
} |
| 862 |
for _, proto := range wrongProtos { |
| 863 |
c := dummyClient() |
| 864 |
if err := c.parse([]byte(proto)); err == nil { |
| 865 |
t.Fatalf("Should have received a parse error for: %v", proto) |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
// Special case for MSG, type needs to not be client. |
| 870 |
wrongProtos = []string{"Mx", "MSx", "MSGx", "MSG \r\n"} |
| 871 |
for _, proto := range wrongProtos { |
| 872 |
c := dummyClient() |
| 873 |
c.kind = ROUTER |
| 874 |
if err := c.parse([]byte(proto)); err == nil { |
| 875 |
t.Fatalf("Should have received a parse error for: %v", proto) |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
func TestProtoSnippet(t *testing.T) { |
| 881 |
sample := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") |
| 882 |
|
| 883 |
tests := []struct { |
| 884 |
input int |
| 885 |
expected string |
| 886 |
}{ |
| 887 |
{0, `"abcdefghijklmnopqrstuvwxyzABCDEF"`}, |
| 888 |
{1, `"bcdefghijklmnopqrstuvwxyzABCDEFG"`}, |
| 889 |
{2, `"cdefghijklmnopqrstuvwxyzABCDEFGH"`}, |
| 890 |
{3, `"defghijklmnopqrstuvwxyzABCDEFGHI"`}, |
| 891 |
{4, `"efghijklmnopqrstuvwxyzABCDEFGHIJ"`}, |
| 892 |
{5, `"fghijklmnopqrstuvwxyzABCDEFGHIJK"`}, |
| 893 |
{6, `"ghijklmnopqrstuvwxyzABCDEFGHIJKL"`}, |
| 894 |
{7, `"hijklmnopqrstuvwxyzABCDEFGHIJKLM"`}, |
| 895 |
{8, `"ijklmnopqrstuvwxyzABCDEFGHIJKLMN"`}, |
| 896 |
{9, `"jklmnopqrstuvwxyzABCDEFGHIJKLMNO"`}, |
| 897 |
{10, `"klmnopqrstuvwxyzABCDEFGHIJKLMNOP"`}, |
| 898 |
{11, `"lmnopqrstuvwxyzABCDEFGHIJKLMNOPQ"`}, |
| 899 |
{12, `"mnopqrstuvwxyzABCDEFGHIJKLMNOPQR"`}, |
| 900 |
{13, `"nopqrstuvwxyzABCDEFGHIJKLMNOPQRS"`}, |
| 901 |
{14, `"opqrstuvwxyzABCDEFGHIJKLMNOPQRST"`}, |
| 902 |
{15, `"pqrstuvwxyzABCDEFGHIJKLMNOPQRSTU"`}, |
| 903 |
{16, `"qrstuvwxyzABCDEFGHIJKLMNOPQRSTUV"`}, |
| 904 |
{17, `"rstuvwxyzABCDEFGHIJKLMNOPQRSTUVW"`}, |
| 905 |
{18, `"stuvwxyzABCDEFGHIJKLMNOPQRSTUVWX"`}, |
| 906 |
{19, `"tuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY"`}, |
| 907 |
{20, `"uvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"`}, |
| 908 |
{21, `"vwxyzABCDEFGHIJKLMNOPQRSTUVWXY"`}, |
| 909 |
{22, `"wxyzABCDEFGHIJKLMNOPQRSTUVWXY"`}, |
| 910 |
{23, `"xyzABCDEFGHIJKLMNOPQRSTUVWXY"`}, |
| 911 |
{24, `"yzABCDEFGHIJKLMNOPQRSTUVWXY"`}, |
| 912 |
{25, `"zABCDEFGHIJKLMNOPQRSTUVWXY"`}, |
| 913 |
{26, `"ABCDEFGHIJKLMNOPQRSTUVWXY"`}, |
| 914 |
{27, `"BCDEFGHIJKLMNOPQRSTUVWXY"`}, |
| 915 |
{28, `"CDEFGHIJKLMNOPQRSTUVWXY"`}, |
| 916 |
{29, `"DEFGHIJKLMNOPQRSTUVWXY"`}, |
| 917 |
{30, `"EFGHIJKLMNOPQRSTUVWXY"`}, |
| 918 |
{31, `"FGHIJKLMNOPQRSTUVWXY"`}, |
| 919 |
{32, `"GHIJKLMNOPQRSTUVWXY"`}, |
| 920 |
{33, `"HIJKLMNOPQRSTUVWXY"`}, |
| 921 |
{34, `"IJKLMNOPQRSTUVWXY"`}, |
| 922 |
{35, `"JKLMNOPQRSTUVWXY"`}, |
| 923 |
{36, `"KLMNOPQRSTUVWXY"`}, |
| 924 |
{37, `"LMNOPQRSTUVWXY"`}, |
| 925 |
{38, `"MNOPQRSTUVWXY"`}, |
| 926 |
{39, `"NOPQRSTUVWXY"`}, |
| 927 |
{40, `"OPQRSTUVWXY"`}, |
| 928 |
{41, `"PQRSTUVWXY"`}, |
| 929 |
{42, `"QRSTUVWXY"`}, |
| 930 |
{43, `"RSTUVWXY"`}, |
| 931 |
{44, `"STUVWXY"`}, |
| 932 |
{45, `"TUVWXY"`}, |
| 933 |
{46, `"UVWXY"`}, |
| 934 |
{47, `"VWXY"`}, |
| 935 |
{48, `"WXY"`}, |
| 936 |
{49, `"XY"`}, |
| 937 |
{50, `"Y"`}, |
| 938 |
{51, `""`}, |
| 939 |
{52, `""`}, |
| 940 |
{53, `""`}, |
| 941 |
{54, `""`}, |
| 942 |
} |
| 943 |
|
| 944 |
for _, tt := range tests { |
| 945 |
got := protoSnippet(tt.input, PROTO_SNIPPET_SIZE, sample) |
| 946 |
if tt.expected != got { |
| 947 |
t.Errorf("Expected protocol snippet to be %s when start=%d but got %s\n", tt.expected, tt.input, got) |
| 948 |
} |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
func TestParseOK(t *testing.T) { |
| 953 |
c := dummyClient() |
| 954 |
if c.state != OP_START { |
| 955 |
t.Fatalf("Expected OP_START vs %d\n", c.state) |
| 956 |
} |
| 957 |
okProto := []byte("+OK\r\n") |
| 958 |
err := c.parse(okProto[:1]) |
| 959 |
if err != nil || c.state != OP_PLUS { |
| 960 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 961 |
} |
| 962 |
err = c.parse(okProto[1:2]) |
| 963 |
if err != nil || c.state != OP_PLUS_O { |
| 964 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 965 |
} |
| 966 |
err = c.parse(okProto[2:3]) |
| 967 |
if err != nil || c.state != OP_PLUS_OK { |
| 968 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 969 |
} |
| 970 |
err = c.parse(okProto[3:4]) |
| 971 |
if err != nil || c.state != OP_PLUS_OK { |
| 972 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 973 |
} |
| 974 |
err = c.parse(okProto[4:5]) |
| 975 |
if err != nil || c.state != OP_START { |
| 976 |
t.Fatalf("Unexpected: %d : %v\n", c.state, err) |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
func TestMaxControlLine(t *testing.T) { |
| 981 |
for _, test := range []struct { |
| 982 |
name string |
| 983 |
kind int |
| 984 |
shouldFail bool |
| 985 |
}{ |
| 986 |
{"client", CLIENT, true}, |
| 987 |
{"leaf", LEAF, false}, |
| 988 |
{"route", ROUTER, false}, |
| 989 |
{"gateway", GATEWAY, false}, |
| 990 |
} { |
| 991 |
t.Run(test.name, func(t *testing.T) { |
| 992 |
pub := []byte("PUB foo.bar.baz 2\r\nok\r\n") |
| 993 |
|
| 994 |
setupClient := func() *client { |
| 995 |
c := dummyClient() |
| 996 |
c.setNoReconnect() |
| 997 |
c.flags.set(connectReceived) |
| 998 |
c.kind = test.kind |
| 999 |
switch test.kind { |
| 1000 |
case ROUTER: |
| 1001 |
c.route = &route{} |
| 1002 |
case GATEWAY: |
| 1003 |
c.gw = &gateway{outbound: false, connected: true, insim: make(map[string]*insie)} |
| 1004 |
} |
| 1005 |
c.mcl = 8 |
| 1006 |
return c |
| 1007 |
} |
| 1008 |
|
| 1009 |
c := setupClient() |
| 1010 |
// First try with a partial: |
| 1011 |
// PUB foo.bar.baz 2\r\nok\r\n |
| 1012 |
// .............^ |
| 1013 |
err := c.parse(pub[:14]) |
| 1014 |
switch test.shouldFail { |
| 1015 |
case true: |
| 1016 |
if !ErrorIs(err, ErrMaxControlLine) { |
| 1017 |
t.Fatalf("Expected an error parsing longer than expected control line") |
| 1018 |
} |
| 1019 |
case false: |
| 1020 |
if err != nil { |
| 1021 |
t.Fatalf("Should not have failed, got %v", err) |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
// Now with full protocol (no split) and we should still enforce. |
| 1026 |
c = setupClient() |
| 1027 |
err = c.parse(pub) |
| 1028 |
switch test.shouldFail { |
| 1029 |
case true: |
| 1030 |
if !ErrorIs(err, ErrMaxControlLine) { |
| 1031 |
t.Fatalf("Expected an error parsing longer than expected control line") |
| 1032 |
} |
| 1033 |
case false: |
| 1034 |
if err != nil { |
| 1035 |
t.Fatalf("Should not have failed, got %v", err) |
| 1036 |
} |
| 1037 |
} |
| 1038 |
}) |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
func TestMaxControlLineNonClientUpperBound(t *testing.T) { |
| 1043 |
for _, test := range []struct { |
| 1044 |
name string |
| 1045 |
kind int |
| 1046 |
}{ |
| 1047 |
{"leaf", LEAF}, |
| 1048 |
{"route", ROUTER}, |
| 1049 |
{"gateway", GATEWAY}, |
| 1050 |
} { |
| 1051 |
t.Run(test.name, func(t *testing.T) { |
| 1052 |
setupClient := func() *client { |
| 1053 |
c := dummyClient() |
| 1054 |
c.setNoReconnect() |
| 1055 |
c.flags.set(connectReceived) |
| 1056 |
c.kind = test.kind |
| 1057 |
switch test.kind { |
| 1058 |
case ROUTER: |
| 1059 |
c.route = &route{} |
| 1060 |
case GATEWAY: |
| 1061 |
c.gw = &gateway{outbound: false, connected: true, insim: make(map[string]*insie)} |
| 1062 |
} |
| 1063 |
// mcl is the CLIENT default; non-CLIENT uses 16 times that instead |
| 1064 |
c.mcl = MAX_CONTROL_LINE_SIZE |
| 1065 |
return c |
| 1066 |
} |
| 1067 |
|
| 1068 |
for _, off := range []int{-1, 0, 1} { |
| 1069 |
t.Run(fmt.Sprintf("%d", off), func(t *testing.T) { |
| 1070 |
// Build a PUB arg that exceeds the max control line. |
| 1071 |
// Feed it in two chunks so argBuf accumulates across parse calls |
| 1072 |
// (replicates the split-buffer trickle-feed exploit path). |
| 1073 |
defaultMaxControlLineSize := MAX_CONTROL_LINE_SIZE * 16 |
| 1074 |
subject := bytes.Repeat([]byte("x"), defaultMaxControlLineSize+off) |
| 1075 |
chunk1 := append([]byte("PUB "), subject[:defaultMaxControlLineSize/2]...) |
| 1076 |
chunk2 := subject[defaultMaxControlLineSize/2:] |
| 1077 |
|
| 1078 |
c := setupClient() |
| 1079 |
// First chunk — no error expected yet (argBuf < limit) |
| 1080 |
if err := c.parse(chunk1); err != nil { |
| 1081 |
t.Fatalf("unexpected error on first chunk: %v", err) |
| 1082 |
} |
| 1083 |
// Second chunk pushes argBuf over max control line |
| 1084 |
err := c.parse(chunk2) |
| 1085 |
if off <= 0 && err != nil { |
| 1086 |
t.Fatalf("unexpected error on second chunk: %v", err) |
| 1087 |
} else if off > 0 && !ErrorIs(err, ErrMaxControlLine) { |
| 1088 |
t.Fatalf("expected ErrMaxControlLine after oversized accumulation, got: %v", err) |
| 1089 |
} |
| 1090 |
}) |
| 1091 |
} |
| 1092 |
}) |
| 1093 |
} |
| 1094 |
} |