Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(transcoder): reject unsafe HLS upload paths (#5182)
Gabe Kangas4c583e03cc61
diff --git a/services/transcoder/fileWriterReceiverService.go b/services/transcoder/fileWriterReceiverService.goindex c52fda432d..ba8eac7e86 100644--- a/services/transcoder/fileWriterReceiverService.go+++ b/services/transcoder/fileWriterReceiverService.go@@ -1,10 +1,13 @@package transcoderimport (+ "errors""io""net""net/http"+ "net/url""os"+ "path""path/filepath""strings"@@ -77,9 +80,20 @@ func (s *FileWriterReceiverService) uploadHandler(w http.ResponseWriter, r *httpreturn}- path := r.URL.Path- writePath := filepath.Join(config.HLSStoragePath, path)- f, err := os.Create(writePath) //nolint: gosec+ relativePath, writePath, err := safeHLSUploadTarget(config.HLSStoragePath, r.URL.EscapedPath())+ if err != nil {+ http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)+ return+ }++ root, err := os.OpenRoot(config.HLSStoragePath)+ if err != nil {+ returnError(err, w)+ return+ }+ defer root.Close()++ f, err := root.Create(relativePath)if err != nil {returnError(err, w)return@@ -97,7 +111,7 @@ func (s *FileWriterReceiverService) uploadHandler(w http.ResponseWriter, r *http}func (s *FileWriterReceiverService) fileWritten(path string) {- if utils.GetRelativePathFromAbsolutePath(path) == "hls/stream.m3u8" {+ if filepath.ToSlash(utils.GetRelativePathFromAbsolutePath(path)) == "hls/stream.m3u8" {s.callbacks.MasterPlaylistWritten(path)} else if strings.HasSuffix(path, ".ts") {s.callbacks.SegmentWritten(path)@@ -106,6 +120,40 @@ func (s *FileWriterReceiverService) fileWritten(path string) {}}+func safeHLSUploadTarget(baseDir string, escapedRequestPath string) (string, string, error) {+ relativePath, err := safeHLSUploadRelativePath(escapedRequestPath)+ if err != nil {+ return "", "", err+ }++ return relativePath, filepath.Join(baseDir, filepath.FromSlash(relativePath)), nil+}++func safeHLSUploadRelativePath(escapedRequestPath string) (string, error) {+ requestPath, err := url.PathUnescape(escapedRequestPath)+ if err != nil {+ return "", errors.New("invalid HLS upload path")+ }++ relativePath, ok := strings.CutPrefix(requestPath, "/")+ if !ok || relativePath == "" || strings.HasPrefix(relativePath, "/") || strings.Contains(relativePath, `\`) {+ return "", errors.New("invalid HLS upload path")+ }++ for _, segment := range strings.Split(relativePath, "/") {+ if segment == "" || segment == "." || segment == ".." {+ return "", errors.New("invalid HLS upload path")+ }+ }++ cleanPath := path.Clean(relativePath)+ if !filepath.IsLocal(filepath.FromSlash(cleanPath)) {+ return "", errors.New("invalid HLS upload path")+ }++ return cleanPath, nil+}+func returnError(err error, w http.ResponseWriter) {log.Debugln(err)http.Error(w, http.StatusText(http.StatusInternalServerError)+": "+err.Error(), http.StatusInternalServerError)diff --git a/services/transcoder/receiverHost_test.go b/services/transcoder/receiverHost_test.goindex 25b670ac82..980b67c2c3 100644--- a/services/transcoder/receiverHost_test.go+++ b/services/transcoder/receiverHost_test.go@@ -2,7 +2,13 @@ package transcoderimport ("net"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "runtime""strconv"+ "strings""testing""github.com/owncast/owncast/config"@@ -47,3 +53,234 @@ func TestReceiverBindHost(t *testing.T) {assertNumericPort(t, cfg.InternalHLSListenerPort)})}++type receiverCallbacks struct {+ segments []string+ variantPlaylists []string+ masterPlaylists []string+}++func (c *receiverCallbacks) SegmentWritten(path string) {+ c.segments = append(c.segments, path)+}++func (c *receiverCallbacks) VariantPlaylistWritten(path string) {+ c.variantPlaylists = append(c.variantPlaylists, path)+}++func (c *receiverCallbacks) MasterPlaylistWritten(path string) {+ c.masterPlaylists = append(c.masterPlaylists, path)+}++func withTestHLSStoragePath(t *testing.T) string {+ t.Helper()++ originalPath := config.HLSStoragePath+ basePath := filepath.Join(t.TempDir(), "hls")+ if err := os.MkdirAll(filepath.Join(basePath, "0"), 0o750); err != nil {+ t.Fatal(err)+ }++ config.HLSStoragePath = basePath+ t.Cleanup(func() {+ config.HLSStoragePath = originalPath+ })++ return basePath+}++func TestReceiverUploadWritesOnlySafeHLSPaths(t *testing.T) {+ basePath := withTestHLSStoragePath(t)+ callbacks := &receiverCallbacks{}+ receiver := &FileWriterReceiverService{callbacks: callbacks}++ cases := []struct {+ target string+ body string+ writePath string+ }{+ {"/stream.m3u8", "master", filepath.Join(basePath, "stream.m3u8")},+ {"/0/stream.m3u8", "variant", filepath.Join(basePath, "0", "stream.m3u8")},+ {"/0/segment.ts", "segment", filepath.Join(basePath, "0", "segment.ts")},+ }++ for _, tc := range cases {+ req := httptest.NewRequest(http.MethodPut, tc.target, strings.NewReader(tc.body))+ res := httptest.NewRecorder()++ receiver.uploadHandler(res, req)++ if res.Code != http.StatusOK {+ t.Fatalf("expected upload status %d for %s, got %d", http.StatusOK, tc.target, res.Code)+ }++ data, err := os.ReadFile(tc.writePath)+ if err != nil {+ t.Fatal(err)+ }+ if string(data) != tc.body {+ t.Fatalf("expected uploaded data %q at %s, got %q", tc.body, tc.writePath, string(data))+ }+ }++ if len(callbacks.masterPlaylists) != 1 || callbacks.masterPlaylists[0] != cases[0].writePath {+ t.Fatalf("expected master playlist callback for %q, got %#v", cases[0].writePath, callbacks.masterPlaylists)+ }+ if len(callbacks.variantPlaylists) != 1 || callbacks.variantPlaylists[0] != cases[1].writePath {+ t.Fatalf("expected variant playlist callback for %q, got %#v", cases[1].writePath, callbacks.variantPlaylists)+ }+ if len(callbacks.segments) != 1 || callbacks.segments[0] != cases[2].writePath {+ t.Fatalf("expected segment callback for %q, got %#v", cases[2].writePath, callbacks.segments)+ }+}++func TestSafeHLSUploadPath(t *testing.T) {+ basePath := withTestHLSStoragePath(t)++ validCases := map[string]string{+ "/stream.m3u8": filepath.Join(basePath, "stream.m3u8"),+ "/0/stream.m3u8": filepath.Join(basePath, "0", "stream.m3u8"),+ "/0/segment.ts": filepath.Join(basePath, "0", "segment.ts"),+ "/audio/segment.aac": filepath.Join(basePath, "audio", "segment.aac"),+ "/v0/part%200.ts": filepath.Join(basePath, "v0", "part 0.ts"),+ }++ for target, want := range validCases {+ t.Run("valid "+target, func(t *testing.T) {+ _, got, err := safeHLSUploadTarget(basePath, target)+ if err != nil {+ t.Fatalf("expected valid path, got error: %v", err)+ }+ if got != want {+ t.Fatalf("expected %q, got %q", want, got)+ }+ })+ }++ invalidCases := []string{+ "",+ "0/segment.ts",+ "/",+ "//tmp/evil.ts",+ "/../evil.ts",+ "/%2e%2e/evil.ts",+ "/%2E%2E/evil.ts",+ "/..%2fevil.ts",+ "/0/../evil.ts",+ "/0/%2e%2e/evil.ts",+ "/0/%2e%2e%2fevil.ts",+ "/0//segment.ts",+ "/0/./segment.ts",+ "/0/%2e/segment.ts",+ "/0%5c..%5cevil.ts",+ "/0\\..\\evil.ts",+ "/%2Ftmp%2Fevil.ts",+ "/bad%ZZpath.ts",+ }++ for _, target := range invalidCases {+ t.Run("invalid "+target, func(t *testing.T) {+ if relativePath, got, err := safeHLSUploadTarget(basePath, target); err == nil {+ t.Fatalf("expected error for %q, got relative path %q and path %q", target, relativePath, got)+ }+ })+ }+}++func TestSafeHLSUploadPathKeepsOriginalBase(t *testing.T) {+ _, got, err := safeHLSUploadTarget(filepath.Join("data", "hls"), "/0/segment.ts")+ if err != nil {+ t.Fatal(err)+ }++ want := filepath.Join("data", "hls", "0", "segment.ts")+ if got != want {+ t.Fatalf("expected %q, got %q", want, got)+ }+}++func TestReceiverUploadCannotEscapeThroughSymlink(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("symlink creation requires privileges on Windows")+ }++ basePath := withTestHLSStoragePath(t)+ outsidePath := filepath.Join(t.TempDir(), "outside")+ if err := os.MkdirAll(outsidePath, 0o750); err != nil {+ t.Fatal(err)+ }+ if err := os.Symlink(outsidePath, filepath.Join(basePath, "link")); err != nil {+ t.Skipf("unable to create symlink: %v", err)+ }++ callbacks := &receiverCallbacks{}+ receiver := &FileWriterReceiverService{callbacks: callbacks}+ req := httptest.NewRequest(http.MethodPut, "/link/evil.ts", strings.NewReader("segment"))+ res := httptest.NewRecorder()++ receiver.uploadHandler(res, req)++ if res.Code == http.StatusOK {+ t.Fatalf("expected symlink escape upload to fail")+ }+ if _, err := os.Stat(filepath.Join(outsidePath, "evil.ts")); !os.IsNotExist(err) {+ t.Fatalf("unsafe upload created outside file: %v", err)+ }+ if len(callbacks.masterPlaylists) != 0 || len(callbacks.variantPlaylists) != 0 || len(callbacks.segments) != 0 {+ t.Fatalf("unsafe upload fired callbacks: %#v", callbacks)+ }+}++func TestReceiverUploadRejectsUnsafePaths(t *testing.T) {+ basePath := withTestHLSStoragePath(t)+ callbacks := &receiverCallbacks{}+ receiver := &FileWriterReceiverService{callbacks: callbacks}++ cases := []string{+ "/../evil.ts",+ "/%2e%2e/evil.ts",+ "/0/../evil.ts",+ "/0/%2e%2e/evil.ts",+ "/%2Ftmp%2Fevil.ts",+ "/0//segment.ts",+ "/0/./segment.ts",+ "/0%5c..%5cevil.ts",+ }++ for _, target := range cases {+ t.Run(target, func(t *testing.T) {+ req := httptest.NewRequest(http.MethodPut, target, strings.NewReader("segment"))+ res := httptest.NewRecorder()++ receiver.uploadHandler(res, req)++ if res.Code != http.StatusBadRequest {+ t.Fatalf("expected upload status %d, got %d", http.StatusBadRequest, res.Code)+ }+ if _, err := os.Stat(filepath.Join(filepath.Dir(basePath), "evil.ts")); !os.IsNotExist(err) {+ t.Fatalf("unsafe upload created outside file: %v", err)+ }+ if len(callbacks.masterPlaylists) != 0 || len(callbacks.variantPlaylists) != 0 || len(callbacks.segments) != 0 {+ t.Fatalf("unsafe upload fired callbacks: %#v", callbacks)+ }+ })+ }+}++func TestReceiverUploadRejectsNonPutMethods(t *testing.T) {+ withTestHLSStoragePath(t)+ callbacks := &receiverCallbacks{}+ receiver := &FileWriterReceiverService{callbacks: callbacks}++ req := httptest.NewRequest(http.MethodPost, "/0/segment.ts", strings.NewReader("segment"))+ res := httptest.NewRecorder()++ receiver.uploadHandler(res, req)++ if res.Code != http.StatusBadRequest {+ t.Fatalf("expected upload status %d, got %d", http.StatusBadRequest, res.Code)+ }+ if len(callbacks.masterPlaylists) != 0 || len(callbacks.variantPlaylists) != 0 || len(callbacks.segments) != 0 {+ t.Fatalf("rejected upload fired callbacks: %#v", callbacks)+ }+}