diff --git a/config-sample.yaml b/config-sample.yaml index a0e90d417..0310c16ec 100644 --- a/config-sample.yaml +++ b/config-sample.yaml @@ -402,6 +402,9 @@ keys: # # and make the handler join arbitrary multicast groups and republish whatever it receives into a # # room, using the ingress as a relay for streams on the handler's local network. # enable_udp_url_pull: false +# # Allow http, https and srt URL pull ingresses whose host is, or resolves to, a loopback, private, +# # link-local, CGNAT, unspecified or multicast address. Disabled by default. +# allow_private_url_pull: false # Region of the current node. Required if using regionaware node selector # region: us-west-2 diff --git a/pkg/config/config.go b/pkg/config/config.go index 034ccb982..88e18ff99 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -544,6 +544,9 @@ type IngressConfig struct { // room, using the ingress as a relay for streams on the handler's local network the caller has // no direct access to. EnableUDPURLPull bool `yaml:"enable_udp_url_pull,omitempty"` + // Allow http, https and srt URL pull ingresses whose host is, or resolves to, a loopback, + // private, link-local, CGNAT, unspecified or multicast address. Disabled by default. + AllowPrivateURLPull bool `yaml:"allow_private_url_pull,omitempty"` } type SIPConfig struct{} diff --git a/pkg/service/export_test.go b/pkg/service/export_test.go new file mode 100644 index 000000000..fb9f38028 --- /dev/null +++ b/pkg/service/export_test.go @@ -0,0 +1,28 @@ +// Copyright 2026 LiveKit, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +package service + +import ( + "context" + "net/netip" + "testing" +) + +func SetLookupNetIP(t *testing.T, fn func(ctx context.Context, network, host string) ([]netip.Addr, error)) { + prev := lookupNetIP + lookupNetIP = fn + t.Cleanup(func() { lookupNetIP = prev }) +} diff --git a/pkg/service/ingress.go b/pkg/service/ingress.go index a4abb641b..62204087f 100644 --- a/pkg/service/ingress.go +++ b/pkg/service/ingress.go @@ -17,7 +17,10 @@ package service import ( "context" "fmt" + "net" + "net/netip" "net/url" + "strings" "github.com/livekit/protocol/ingress" "github.com/livekit/protocol/livekit" @@ -137,6 +140,11 @@ func (s *IngressService) CreateIngressWithUrl(ctx context.Context, urlStr string } switch urlObj.Scheme { case "http", "https", "srt": + if !s.conf.AllowPrivateURLPull { + if err := checkURLPullHost(ctx, urlObj.Hostname()); err != nil { + return nil, err + } + } case "udp": if !s.conf.EnableUDPURLPull { return nil, ingress.ErrInvalidIngress("udp url pull is not enabled") @@ -428,3 +436,35 @@ func (s *IngressService) DeleteIngress(ctx context.Context, req *livekit.DeleteI return info, nil } + +var lookupNetIP = net.DefaultResolver.LookupNetIP + +var cgnatPrefix = netip.MustParsePrefix("100.64.0.0/10") + +func checkURLPullHost(ctx context.Context, host string) error { + host = strings.ToLower(strings.TrimSuffix(host, ".")) + if host == "localhost" || strings.HasSuffix(host, ".localhost") { + return ingress.ErrInvalidIngress("url host is not allowed") + } + + addrs, err := lookupNetIP(ctx, "ip", host) + if err != nil || len(addrs) == 0 { + return ingress.ErrInvalidIngress(fmt.Sprintf("cannot resolve url host %s", host)) + } + for _, addr := range addrs { + if !isPublicAddr(addr) { + return ingress.ErrInvalidIngress("url host is not allowed") + } + } + return nil +} + +func isPublicAddr(addr netip.Addr) bool { + addr = addr.Unmap() + if addr.Is4() && addr.As4()[0] == 0 { + return false + } + return addr.IsGlobalUnicast() && + !addr.IsPrivate() && + !cgnatPrefix.Contains(addr) +} diff --git a/pkg/service/ingress_test.go b/pkg/service/ingress_test.go index f391e4482..7a7869d6e 100644 --- a/pkg/service/ingress_test.go +++ b/pkg/service/ingress_test.go @@ -16,6 +16,9 @@ package service_test import ( "context" + "errors" + "net/netip" + "strings" "testing" "github.com/stretchr/testify/require" @@ -31,6 +34,10 @@ import ( ) func TestCreateURLPullIngressScheme(t *testing.T) { + service.SetLookupNetIP(t, func(context.Context, string, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("93.184.215.14")}, nil + }) + newService := func(enableUDP bool) (*service.IngressService, *servicefakes.FakeIngressLauncher) { launcher := &servicefakes.FakeIngressLauncher{} launcher.LaunchPullIngressCalls(func(_ context.Context, info *livekit.IngressInfo) (*livekit.IngressInfo, error) { @@ -97,6 +104,132 @@ func TestCreateURLPullIngressScheme(t *testing.T) { }) } +func TestCreateURLPullIngressHost(t *testing.T) { + newService := func(allowPrivate bool) (*service.IngressService, *servicefakes.FakeIngressLauncher) { + launcher := &servicefakes.FakeIngressLauncher{} + launcher.LaunchPullIngressCalls(func(_ context.Context, info *livekit.IngressInfo) (*livekit.IngressInfo, error) { + return info, nil + }) + + svc := service.NewIngressServiceWithIngressLauncher( + &config.IngressConfig{AllowPrivateURLPull: allowPrivate}, + "nodeID", + nil, + nil, + &servicefakes.FakeIngressStore{}, + nil, + nil, + launcher, + ) + return svc, launcher + } + + ctx := service.WithGrants(context.Background(), &auth.ClaimGrants{Video: &auth.VideoGrant{IngressAdmin: true}}, "") + + create := func(svc *service.IngressService, url string) (*livekit.IngressInfo, error) { + return svc.CreateIngress(ctx, &livekit.CreateIngressRequest{ + InputType: livekit.IngressInput_URL_INPUT, + Url: url, + RoomName: "testroom", + ParticipantIdentity: "ingress", + }) + } + + t.Run("resolved internal addresses rejected", func(t *testing.T) { + svc, launcher := newService(false) + for _, url := range []string{ + "http://localhost/live", + "http://127.0.0.1/live", + "http://[::1]/live", + "http://169.254.169.254/latest/meta-data/", + "http://100.64.0.1/live", + "http://0.1.2.3/live", + } { + _, err := create(svc, url) + require.Error(t, err, url) + } + require.Zero(t, launcher.LaunchPullIngressCallCount()) + }) + + service.SetLookupNetIP(t, func(_ context.Context, _, host string) ([]netip.Addr, error) { + switch strings.ToLower(host) { + case "public.example", "localhost", "localhost.", "foo.localhost": + return []netip.Addr{netip.MustParseAddr("93.184.215.14")}, nil + case "internal.example": + return []netip.Addr{netip.MustParseAddr("10.1.2.3")}, nil + case "mixed.example": + return []netip.Addr{netip.MustParseAddr("93.184.215.14"), netip.MustParseAddr("169.254.169.254")}, nil + } + if addr, err := netip.ParseAddr(host); err == nil { + return []netip.Addr{addr}, nil + } + return nil, errors.New("no such host") + }) + + rejected := []string{ + "http://127.0.0.1/live", + "http://127.1.2.3:8080/live", + "http://[::1]/live", + "http://localhost/live", + "http://LOCALHOST./live", + "http://foo.localhost/live", + "http://10.0.0.1/live", + "http://172.16.5.4/live", + "https://192.168.1.1/live", + "http://[fd00::1]/live", + "http://169.254.169.254/latest/meta-data/", + "http://[fe80::1]/live", + "http://100.64.0.1/live", + "http://0.0.0.0/live", + "http://0.1.2.3/live", + "http://[::]/live", + "http://224.0.0.1/live", + "http://255.255.255.255/live", + "http://[::ffff:127.0.0.1]/live", + "http://[::ffff:10.0.0.1]/live", + "http://[::ffff:100.64.0.1]/live", + "http://[::ffff:0.1.2.3]/live", + "http://internal.example/live", + "http://mixed.example/live", + "http://unresolvable.example/live", + "srt://127.0.0.1:9000", + "srt://10.0.0.1:9000?mode=caller", + "srt://:9000?mode=listener", + } + + t.Run("internal hosts rejected", func(t *testing.T) { + svc, launcher := newService(false) + for _, url := range rejected { + _, err := create(svc, url) + require.Error(t, err, url) + } + require.Zero(t, launcher.LaunchPullIngressCallCount()) + }) + + t.Run("public hosts accepted", func(t *testing.T) { + for _, url := range []string{ + "http://93.184.215.14/live", + "https://public.example/live.m3u8", + "http://[2606:2800:21f:cb07:6820:80da:af6b:8b2c]/live", + "srt://public.example:9000", + "http://100.128.0.1/live", + } { + svc, _ := newService(false) + info, err := create(svc, url) + require.NoError(t, err, url) + require.Equal(t, url, info.Url) + } + }) + + t.Run("internal hosts accepted when allowed", func(t *testing.T) { + for _, url := range rejected { + svc, _ := newService(true) + _, err := create(svc, url) + require.NoError(t, err, url) + } + }) +} + // unusedIngressClient is a non-nil rpc.IngressClient for handlers that must not // reach the ingress service: any call on it panics on the nil embedded interfaces. type unusedIngressClient struct {