Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-jokes-enter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"github.com/livekit/protocol": patch
---

Enabling soft fail for sip validation
18 changes: 14 additions & 4 deletions livekit/sip_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ var nameAddrHeaders = map[string]bool{
"record-route": true,
"reply-to": true,
"p-asserted-identity": true, // RFC 3325 Section 9.1
"referred-by": true, // RFC 3892 Section 3
}

var softFailureReporter func(err error)

func SetSoftFailureReporter(reporter func(err error)) {
softFailureReporter = reporter
}

// ValidateHeaderName validates a SIP header name per RFC 3261 Section 25.1
Expand Down Expand Up @@ -253,10 +260,13 @@ func ValidateHeaderValue(name, value string) error {

// Convert to lowercase for case-insensitive comparison
lowerName := strings.ToLower(name)
if _, exists := nameAddrHeaders[lowerName]; exists && false {
// TODO: Disabled since all supported headers are forbidden, re-enable when we allow some
if _, exists := nameAddrHeaders[lowerName]; exists {
if err := validateNameAddrHeader(value); err != nil {
return fmt.Errorf("header %s: value: %w", name, err)
err = fmt.Errorf("header %s: value: %w", name, err)
// For now do not actually error out on header values, just note failure.
if cb := softFailureReporter; cb != nil {
cb(err)
}
Comment on lines +267 to +269

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do } else { return err } here? Was considering it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we return error when we know the validation failed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if some customers are using the system with invalid values today, we don't want to cause an outage when this lands. So we "soft-fail" first, allowing us to log the would-be errors for a while before we turn this on.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure how would that be an outage since those calls are already failing.. 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would be enabling this check not just for refer messages.

}
}

Expand Down Expand Up @@ -315,7 +325,7 @@ func validateNameAddrHeader(value string) error {
}
} else {
// This is a bare URI, and should comply with addr-spec, no special characters
if strings.ContainsAny(value, ";,? ") {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A semicolon here would mean that what's after that is header params, not the URI. However, it is still valid syntax

if strings.ContainsAny(value, ",? ") {
return errors.New("bare URI with special characters")
}
}
Expand Down
63 changes: 42 additions & 21 deletions livekit/sip_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,47 @@ func testCaseName(name string, maxLen int, index int) string {

// ValidNameAddrHeaders contains valid Name-addr format headers with parameters
var ValidNameAddrHeaders = []string{
`"Alice Johnson" <sip:u1@example.com>`,
`"Alice \"Ace\" Johnson's device\\" <sip:u2@example.com>`,
// addr-spec schemes (bare)
`sip:u4@exmaple.com`,
`sips:u5@exmaple.com`,
`tel:+1-555-123-4567`,

// bare addr-spec + header params (RFC 3261 §25.1 *( SEMI to-param ))
`sip:user@host;tag=abc`,
`sip:user@host;tag=abc;x-custom=val`,
`sip:r@host;cid="2UWQFN309shb3@ref.example"`, // quoted header param value

// bracketed addr-spec (no display name)
`<sip:u4@exmaple.com>`,
`<sips:u5@exmaple.com>`,
`<tel:+1-555-123-4567>`,
`<sip:u10@example.com;transport=tcp>`, // SIP URI with transport
`<sip:u11@example.com;lr>`, // SIP URI with flag param
`<sip:u12@example.com:5060>`, // SIP URI with port
`<sip:user@host;transport=tcp;lr>`, // 2 uri-params
`<sip:user@[2001:db8::1]:5060>`, // IPv6 + port
`<sip:user@host?X-Custom=val>`, // uri-header
`<sip:user@host?X-Foo=1&X-Bar=2>`, // 2 uri-headers
`<sip:user@host;transport=tcp?X-Custom=val>`, // uri-param + uri-header
`<sip:user@host;transport=tcp;user=phone?X-Foo=1&X-Bar=2>`, // 2 uri-params + 2 uri-headers

// bracketed addr-spec + header params after >
`<sip:user@host>;tag=abc`,
`<sip:user@host>;tag=abc;x-custom=val`,
`<sip:user@host;transport=tcp>;tag=abc`,
`<sip:user@host;transport=tcp;lr>;tag=abc`,
`<sip:user@host?X-Custom=val>;tag=abc`,
`<sip:user@host?X-Foo=1&X-Bar=2>;tag=abc`,
`<sip:user@host;transport=tcp?X-Custom=val>;tag=abc`,
`<sip:user@host;transport=tcp;user=phone?X-Foo=1&X-Bar=2>;tag=abc;x-custom=val`,

// name-addr (display name + bracketed URI)
`Alice <sip:user@host>`,
`"Alice Johnson" <sip:user@host>`,
`Alice Johnson <sip:u3@example.com>`,
`sip:u4@example.com`, // basic SIP URI (no brackets needed)
`sips:u5@example.com`, // secure SIP URI (no brackets needed)
`tel:+1-555-123-4567`, // TEL URI (no brackets needed)
`<sip:u6@example.com>`, // basic SIP URI with brackets
`<sips:u7@example.com>`, // secure SIP URI with brackets
`<tel:+1-555-123-4567>`, // TEL URI with brackets
`Alice <sip:u8@example.com>`, // display name + SIP URI
`"Alice Johnson" <sip:u9@example.com>`, // quoted display name
`<sip:u10@example.com;transport=tcp>`, // SIP URI with transport
`<sip:u11@example.com;lr>`, // SIP URI with flag param
`<sip:u12@example.com:5060>`, // SIP URI with port
`<sip:u13@example.com;transport=tcp;lr>`, // SIP URI with multiple params
`Alice <sip:u14@example.com;transport=tcp>`, // display name + params
`"Alice \"Ace\"" <sip:u15@example.com>`, // quoted display
`<sip:u16@[2001:db8::1]:5060>`, // IPv6 with params
`<sips:u17@192.0.2.4>;expires=60`, // SIPS URI with expires parameter
`Alice <sip:u18@example.com;transport=tcp>`, // display name + params
`"Alice & Bob" <sip:u19@example.com>`, // display name with & symbol
`"Alice \"Ace\" Johnson's device\\" <sip:user@host>`, // quoted + escapes
`"Alice & Bob" <sip:user@host>`, // special char in quoted display
`Alice <sip:user@host;transport=tcp?X-Custom=val>;tag=abc`,
}

// InvalidNameAddrHeaders contains invalid Name-addr format headers
Expand All @@ -163,7 +183,8 @@ var InvalidNameAddrHeaders = []string{
`Alice sip:u13@example.com`, // display name without brackets
`Alice sips:u14@example.com`, // display name without brackets
`Alice & Bob <sip:u15@example.com>`, // display name with & symbol
`sip:u16@example.com;transport=tcp`, // special chars without brackets
`a-value`, // bare token, not a URI
`sip:user@host?X-Custom=val`, // uri-header in bare addr-spec (RFC 3261 §20)
`sip:u17@example.com,transport=tcp`, // comma without brackets
`sip:u18@example.com?transport=tcp`, // question mark without brackets
`<sip:u21@example.com;transport tcp>`, // missing equals sign
Expand Down
Loading