Skip to content

Commit

Permalink
feat: export ParseMaxMessageLength to limit size of message on parse …
Browse files Browse the repository at this point in the history
…stream #37
  • Loading branch information
emiago committed Dec 8, 2024
1 parent 2ec66d6 commit 184adeb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
5 changes: 1 addition & 4 deletions sip/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ var (
ErrParseReadBodyIncomplete = errors.New("reading body incomplete")
ErrParseMoreMessages = errors.New("Stream has more message")

ParseMaxMessageLength = 65000
ParseMaxHeaderLength = 4096
ParseMaxBodyLength = 61440
ParseMaxNumHeaders = 64
ParseMaxMessageLength = 65535
)

func ParseMessage(msgData []byte) (Message, error) {
Expand Down
4 changes: 4 additions & 0 deletions sip/parser_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (p *ParserStream) ParseSIPStreamEach(data []byte, cb func(msg Message)) (er
}

reader := p.reader
if reader.Len()+len(data) > ParseMaxMessageLength {
return fmt.Errorf("Message exceeds ParseMaxMessageLength")
}

reader.Write(data) // This should append to our already buffer

unparsed := reader.Bytes() // TODO find a better way as we only want to move our offset
Expand Down
31 changes: 31 additions & 0 deletions sip/parser_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,37 @@ func TestParserStreamMultiple(t *testing.T) {
})
}

func TestParserStreamMessageSizeLimit(t *testing.T) {
p := NewParser()
parser := p.NewSIPStream()

lines := []string{
"INVITE sip:192.168.1.254:5060 SIP/2.0",
"Via: SIP/2.0/TCP 192.168.1.155:44861;branch=z9hG4bK954690f3012120bc5d064d3f7b5d8a24;rport",
"Call-ID: 25be1c3be64adb89fa2e86772dd99db1",
"CSeq: 100 INVITE",
"Contact: <sip:192.168.1.155:44861;transport=tcp>;some.tag.here;other-tag=here",
"From: <sip:192.168.1.155>;tag=76fb12e7e2241ed6",
"To: <sip:192.168.1.254:5060>",
"Max-Forwards: 70",
"Allow: INVITE,ACK,CANCEL,BYE,UPDATE,INFO,OPTIONS,REFER,NOTIFY",
"User-Agent: MyUserAgent v2.3.6. b53ee2632df (DEV) Client",
"Supported: replaces,100rel,timer,gruu,path,outbound",
"Session-Expires: 1800",
"Session-ID: e937754d76855249814a9b7f8b3bf556;remote=00000000000000000000000000000000",
"Content-Type: application/sdp",
"Content-Length: 65000",
"",
strings.Repeat("x", 65000),
}

data := []byte(strings.Join(lines, "\r\n"))

_, err := parser.ParseSIPStream(data)
require.Error(t, err)
require.Equal(t, "Message exceeds ParseMaxMessageLength", err.Error())
}

func BenchmarkParserStream(b *testing.B) {
branch := GenerateBranch()
callid := fmt.Sprintf("gotest-%d", time.Now().UnixNano())
Expand Down

0 comments on commit 184adeb

Please sign in to comment.