From e0943feb242337deb179846c39edef10d3818cca Mon Sep 17 00:00:00 2001 From: daichiDeskTop Date: Wed, 18 Nov 2026 03:45:40 +0900 Subject: [PATCH] fix(hls): refuse an opt-in path with a . or .. segment The relay grants and routes exact paths, so a grant for /.hls/acme/ announces /.hls/acme/../victim/cam. The egress cleaned that to /victim/cam and, reading every broadcast, served it: through the opt-in's redirect, or by reading the cleaned path directly when the opt-in answers NOT_FOUND. Such paths are now refused, and sameBroadcast no longer matches a redirect target that has one. Doubled and trailing slashes are still cleaned away. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KPWMJNAzsnsiqRPREa2JqY --- CHANGELOG.md | 4 ++ docs/site/content/en/docs/cli/hls.md | 5 ++ internal/hls/catalog_redirect_test.go | 6 ++ internal/hls/feed.go | 6 +- internal/hls/mux.go | 29 +++++++- internal/hls/mux_test.go | 71 +++++++------------ .../integration/relay_optin_paths_test.go | 39 ++++++++++ 7 files changed, 110 insertions(+), 50 deletions(-) create mode 100644 internal/integration/relay_optin_paths_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index b9e2dcde..6fd7f78a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Security + +- **`qumo hls` refuses an opt-in path with a `.` or `..` segment (`internal/hls`).** The relay grants `/.hls/acme/../victim/cam` to a `/.hls/acme/` opt-in, and the egress cleaned it to `/victim/cam` and served that, so one tenant could put another's broadcast on HLS. Redirect targets with such segments no longer match either (`sameBroadcast`). Doubled and trailing slashes are still cleaned away. + ### Added - **A relay signs its key-set and usage requests, and `qumo dns` its feed reads, with its peer identity (`internal/reqsign`, #601).** With `PEER_CERT_FILE` and `PEER_KEY_FILE` (an ECDSA key; `qumo dns` takes the same two settings), each request carries `X-Relay-Certificate`, `X-Relay-Timestamp` and `X-Relay-Signature`: the key's ECDSA signature (ASN.1, base64) over SHA-256 of `qumo-relay-request`, the method, the request URI (path and query), the timestamp and the hex SHA-256 of the body, one per line, signed again on every retry. The receiver then knows which relay is calling from a key only that relay holds, and a captured request can't be changed or used for long, where a bearer token copied from a node could be reused until it is replaced. The bearer token (`QUMO_RELAY_TOKEN`, `QUMO_DNS_FEED_TOKEN`) is still sent beside it. A 401 is logged with the server's `Date` beside the local time. Funnel, HLS and ingest, which have no peer identity, are unchanged. diff --git a/docs/site/content/en/docs/cli/hls.md b/docs/site/content/en/docs/cli/hls.md index f6823b7d..2f1e0ec6 100644 --- a/docs/site/content/en/docs/cli/hls.md +++ b/docs/site/content/en/docs/cli/hls.md @@ -74,6 +74,11 @@ token of its own: bearer publishes `/acme/room/42/cam`: the egress subscribes with its own grant. Issue an opt-in only for a broadcast the app means to serve over HTTP. +- **No `.` or `..` segments.** The relay grants paths as written, so + `/.hls/acme/../victim/cam` is covered by an opt-in rooted at `/.hls/acme/` + even though it would clean to `/victim/cam`. The egress ignores any opt-in + path with a `.` or `..` segment. A doubled or trailing slash is still + cleaned away. ```bash # an opt-in for /acme/room/42/cam, signed by the app's key confined to /acme/ diff --git a/internal/hls/catalog_redirect_test.go b/internal/hls/catalog_redirect_test.go index 10e50ce6..41450b03 100644 --- a/internal/hls/catalog_redirect_test.go +++ b/internal/hls/catalog_redirect_test.go @@ -268,6 +268,12 @@ func TestSameBroadcast(t *testing.T) { "a doubled slash": {a: "/acme//cam", b: "/acme/cam", want: true}, "another broadcast": {a: "/other/cam", b: "/acme/cam", want: false}, "beneath the broadcast": {a: "/acme/cam/hd", b: "/acme/cam", want: false}, + // The relay routes /victim/../acme/cam beneath /victim/: a redirect + // there is not to /acme/cam, however it cleans. + "climbing out of another's directory": {a: "/victim/../acme/cam", b: "/acme/cam", want: false}, + "a . segment": {a: "/acme/./cam", b: "/acme/cam", want: false}, + "a .. segment on either side": {a: "/acme/cam", b: "/acme/x/../cam", want: false}, + "a name with dots": {a: "/acme/cam..hd", b: "/acme/cam..hd", want: true}, } for name, tt := range tests { t.Run(name, func(t *testing.T) { diff --git a/internal/hls/feed.go b/internal/hls/feed.go index 730bfc2d..e21b7082 100644 --- a/internal/hls/feed.go +++ b/internal/hls/feed.go @@ -374,7 +374,11 @@ func (g *broadcastGate) acquire(ctx context.Context, broadcast string) (func(), // them into ledger tracks until the announcement ends. func handleAnnouncement(ctx context.Context, session *moqt.Session, ann *moqt.Announcement, store store.Store, mux *streamMux, gate *broadcastGate, window uint64, cfg feedConfig) { annPath := string(ann.BroadcastPath()) - realBroadcast := broadcastPathFromAnnounce(annPath, announcePrefix) + realBroadcast, ok := broadcastPathFromAnnounce(annPath, announcePrefix) + if !ok { + slog.Warn("hls: refusing opt-in with a . or .. segment", "announce", annPath) + return + } slog.Info("hls: received announcement", "announce", annPath, "broadcast", realBroadcast) annCtx, cancelAnn := context.WithCancel(ctx) diff --git a/internal/hls/mux.go b/internal/hls/mux.go index 36287a7f..9c96ee51 100644 --- a/internal/hls/mux.go +++ b/internal/hls/mux.go @@ -20,21 +20,44 @@ import ( // "/.hls/live/cam1" -> "/live/cam1" // "/.hls/cam" -> "/cam" // "/.hls" -> "/" -func broadcastPathFromAnnounce(annPath, prefix string) string { +// +// It reports false for a path with a "." or ".." segment. The relay grants +// and routes exact paths, so /.hls/acme/../victim/cam is announced under a +// grant for /.hls/acme/, yet cleans to /victim/cam, which the egress can read +// and would serve. +func broadcastPathFromAnnounce(annPath, prefix string) (string, bool) { cleanPrefix := strings.TrimSuffix(prefix, "/") p := strings.TrimPrefix(annPath, cleanPrefix) if !strings.HasPrefix(p, "/") { p = "/" + p } - return path.Clean(p) + if hasDotSegment(p) { + return "", false + } + return path.Clean(p), true } // sameBroadcast reports whether a and b name one broadcast, however spelled: -// the egress cleans the paths it derives, a publisher may not. +// the egress cleans the paths it derives, a publisher may not. Paths with a +// "." or ".." segment name none: cleaning one can climb into another +// directory than the one the relay routes it in. func sameBroadcast(a, b moqt.BroadcastPath) bool { + if hasDotSegment(string(a)) || hasDotSegment(string(b)) { + return false + } return path.Clean(string(a)) == path.Clean(string(b)) } +// hasDotSegment reports whether p has a "." or ".." segment. +func hasDotSegment(p string) bool { + for seg := range strings.SplitSeq(p, "/") { + if seg == "." || seg == ".." { + return true + } + } + return false +} + // trackInfo holds metadata describing a media track in an HLS master playlist. type trackInfo struct { Name string diff --git a/internal/hls/mux_test.go b/internal/hls/mux_test.go index 8259504b..cabf8348 100644 --- a/internal/hls/mux_test.go +++ b/internal/hls/mux_test.go @@ -10,55 +10,34 @@ import ( "github.com/stretchr/testify/require" ) +// An opt-in's broadcast is its path with the prefix stripped, cleaned of +// doubled and trailing slashes; one with a "." or ".." segment names none, as +// cleaning it could climb out of the directory the relay granted it in. func Test_broadcastPathFromAnnounce(t *testing.T) { - tests := []struct { - name string - annPath string - prefix string - expected string + tests := map[string]struct { + annPath, prefix string + want string + wantOK bool }{ - { - name: "standard .hls prefix with trailing slash", - annPath: "/.hls/live/cam1", - prefix: "/.hls/", - expected: "/live/cam1", - }, - { - name: "standard .hls prefix without trailing slash", - annPath: "/.hls/live/cam1", - prefix: "/.hls", - expected: "/live/cam1", - }, - { - name: "single segment path", - annPath: "/.hls/cam", - prefix: "/.hls/", - expected: "/cam", - }, - { - name: "hls/live under .hls prefix", - annPath: "/.hls/hls/live", - prefix: "/.hls/", - expected: "/hls/live", - }, - { - name: "custom prefix", - annPath: "/custom/stream/room1", - prefix: "/custom/stream/", - expected: "/room1", - }, - { - name: "bare prefix", - annPath: "/.hls", - prefix: "/.hls/", - expected: "/", - }, + "standard .hls prefix with trailing slash": {annPath: "/.hls/live/cam1", prefix: "/.hls/", want: "/live/cam1", wantOK: true}, + "standard .hls prefix without trailing slash": {annPath: "/.hls/live/cam1", prefix: "/.hls", want: "/live/cam1", wantOK: true}, + "single segment path": {annPath: "/.hls/cam", prefix: "/.hls/", want: "/cam", wantOK: true}, + "hls/live under .hls prefix": {annPath: "/.hls/hls/live", prefix: "/.hls/", want: "/hls/live", wantOK: true}, + "custom prefix": {annPath: "/custom/stream/room1", prefix: "/custom/stream/", want: "/room1", wantOK: true}, + "bare prefix": {annPath: "/.hls", prefix: "/.hls/", want: "/", wantOK: true}, + "a trailing slash": {annPath: "/.hls/live/cam1/", prefix: "/.hls/", want: "/live/cam1", wantOK: true}, + "a doubled slash": {annPath: "/.hls/live//cam1", prefix: "/.hls/", want: "/live/cam1", wantOK: true}, + "a name with dots": {annPath: "/.hls/live/cam1..hd", prefix: "/.hls/", want: "/live/cam1..hd", wantOK: true}, + "a . segment": {annPath: "/.hls/live/./cam1", prefix: "/.hls/", wantOK: false}, + "climbing into another's directory": {annPath: "/.hls/acme/../victim/cam", prefix: "/.hls/", wantOK: false}, + "climbing out of the prefix": {annPath: "/.hls/../victim/cam", prefix: "/.hls/", wantOK: false}, + "a trailing ..": {annPath: "/.hls/acme/..", prefix: "/.hls/", wantOK: false}, } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := broadcastPathFromAnnounce(tt.annPath, tt.prefix) - assert.Equal(t, tt.expected, got) + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + got, ok := broadcastPathFromAnnounce(tt.annPath, tt.prefix) + assert.Equal(t, tt.wantOK, ok) + assert.Equal(t, tt.want, got) }) } } diff --git a/internal/integration/relay_optin_paths_test.go b/internal/integration/relay_optin_paths_test.go new file mode 100644 index 00000000..dc7d7575 --- /dev/null +++ b/internal/integration/relay_optin_paths_test.go @@ -0,0 +1,39 @@ +//go:build integration + +package integration + +import ( + "context" + "testing" + "time" + + "github.com/qumo-dev/gomoqt/moqt" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/qumo-dev/qumo/internal/auth" + "github.com/qumo-dev/qumo/token" +) + +// The relay grants and routes a broadcast by its exact path: a grant for +// /.hls/acme/ announces /.hls/acme/../victim/cam, which cleans to an opt-in +// for /victim/cam. So the HLS egress, which may read every broadcast, must +// refuse an opt-in path with a "." or ".." segment rather than clean it +// (broadcastPathFromAnnounce). +func TestRelay_RoutesOptInDotSegmentsAsGranted(t *testing.T) { + key, _, keysFile := writeKeys(t) + verifier, err := auth.NewVerifier(auth.VerifierConfig{Keys: keysFile}) + require.NoError(t, err) + addr, srv := startAuthRelay(t, verifier.Authorize, nil) + credential, err := token.Sign(key, grantOn(".hls/acme", []string{"/"}, token.ActionPublish), time.Minute) + require.NoError(t, err) + + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + const optIn = moqt.BroadcastPath("/.hls/acme/../victim/cam") + mux := moqt.NewTrackMux(0) + mux.PublishFunc(ctx, optIn, func(tw *moqt.TrackWriter) { <-tw.Context().Done() }) + dialOver(t, "https://"+addr+"/?jwt="+credential, nil, mux) + + assert.Eventually(t, routed(srv, optIn), 3*time.Second, 50*time.Millisecond) +}