Skip to content

Commit

Permalink
Relax header validation for SIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Dec 17, 2024
1 parent bc38834 commit 5f4c748
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-boats-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"github.com/livekit/protocol": patch
---

Relax SIP header validation.
8 changes: 5 additions & 3 deletions livekit/sip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package livekit
import (
"errors"
"fmt"
"regexp"
"slices"
"strings"
)
Expand Down Expand Up @@ -102,10 +103,11 @@ func (p *SIPOutboundTrunkInfo) AsTrunkInfo() *SIPTrunkInfo {
}
}

var reHeaders = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9\-]*$`)

func validateHeader(header string) error {
v := strings.ToLower(header)
if !strings.HasPrefix(v, "x-") {
return fmt.Errorf("only X-* headers are allowed: %s", header)
if !reHeaders.MatchString(header) {
return fmt.Errorf("invalid header name: %q", header)
}
return nil
}
Expand Down
21 changes: 21 additions & 0 deletions livekit/sip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ func TestSIPValidate(t *testing.T) {
"From": "from",
},
},
exp: true,
},
{
name: "inbound invalid header",
req: &SIPInboundTrunkInfo{
Numbers: []string{"+1111"},
HeadersToAttributes: map[string]string{
"From ": "from",
},
},
exp: false,
},
{
Expand Down Expand Up @@ -165,6 +175,17 @@ func TestSIPValidate(t *testing.T) {
"From": "from",
},
},
exp: true,
},
{
name: "outbound invalid header",
req: &SIPOutboundTrunkInfo{
Address: "sip.example.com",
Numbers: []string{"+2222"},
HeadersToAttributes: map[string]string{
"From ": "from",
},
},
exp: false,
},
}
Expand Down

0 comments on commit 5f4c748

Please sign in to comment.