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 c6d94e6
Show file tree
Hide file tree
Showing 3 changed files with 44 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.
21 changes: 18 additions & 3 deletions livekit/sip.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,24 @@ func (p *SIPOutboundTrunkInfo) AsTrunkInfo() *SIPTrunkInfo {
}

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 header == "" {
return errors.New("header name is empty")
}
if strings.ContainsFunc(header, func(r rune) bool {
if r >= 'a' && r <= 'z' {
return false
} else if r >= 'A' && r <= 'Z' {
return false
} else if r >= '0' && r <= '9' {
return false
}
switch r {
case '-':
return false
}
return true
}) {
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 c6d94e6

Please sign in to comment.