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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions docs/site/content/en/docs/cli/hls.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
6 changes: 6 additions & 0 deletions internal/hls/catalog_redirect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion internal/hls/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 26 additions & 3 deletions internal/hls/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 25 additions & 46 deletions internal/hls/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down
39 changes: 39 additions & 0 deletions internal/integration/relay_optin_paths_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading