From 7f21bb90733956e67c0663dbbf5b9a5f6373bd22 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Tue, 17 Nov 2026 13:35:39 +0100 Subject: [PATCH] Update shared package --- backend/media/media.go | 20 +++++++++---------- backend/media/paths_test.go | 40 ++++++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/backend/media/media.go b/backend/media/media.go index 21288b3..2e1cd5e 100644 --- a/backend/media/media.go +++ b/backend/media/media.go @@ -911,21 +911,19 @@ func urlPath(value string) string { return value } -// upstreamMediaPath returns the escaped CarrierNode path for a stored file path. It reports false -// when the stored path is not a safe relative path, or when it contains a query or fragment marker -// that CarrierNode would read as the end of the path. +// upstreamMediaPath returns the CarrierNode path for a stored file path. It reports false only when +// the stored path is not a safe relative path. +// +// url.PathEscape would write a comma as %2C, and the request then keeps that escape as a raw path. +// CarrierNode's router matches a raw path literally, so "Hello, World.mp4" would never be found. A +// URL built from Path alone leaves no raw path, so the name reaches CarrierNode decoded. Query and +// fragment markers are escaped the same way, so they no longer need refusing. func upstreamMediaPath(filePath string) (string, bool) { relative := urlPath(filePath) - if !files.SafeRelative(relative) || strings.ContainsAny(relative, "?#") { + if !files.SafeRelative(relative) { return "", false } - // Escape each segment so percent-escapes and other reserved characters in a stored name - // reach CarrierNode as literal text, not as an escape or a traversal it decodes later. - segments := strings.Split(relative, "/") - for i, segment := range segments { - segments[i] = url.PathEscape(segment) - } - return strings.Join(segments, "/"), true + return (&url.URL{Path: relative}).EscapedPath(), true } // canonicalMediaID returns the numeric media id that identifier spells exactly. Leading zeros, signs diff --git a/backend/media/paths_test.go b/backend/media/paths_test.go index 52c4db8..13fdf2d 100644 --- a/backend/media/paths_test.go +++ b/backend/media/paths_test.go @@ -1,24 +1,50 @@ package media import ( + "net/http" "testing" ) -func TestUpstreamMediaPathRefusesQueryFragmentAndTraversal(t *testing.T) { - for _, stored := range []string{"x?y.mp4", "a#b.mp4", "../x.mp4", "media/../../x.mp4", `..\x.mp4`} { +func TestUpstreamMediaPathRefusesTraversal(t *testing.T) { + for _, stored := range []string{"../x.mp4", "media/../../x.mp4", `..\x.mp4`} { if _, ok := upstreamMediaPath(stored); ok { t.Errorf("upstreamMediaPath(%q) accepted, want refused", stored) } } } +// CarrierNode's router matches a raw request path literally, so a name must reach it with no raw path: +// a %2C for a comma would never match "Hello, World.mp4". Query and fragment markers are escaped, not refused. +func TestUpstreamMediaPathReachesCarrierNodeAsTheLiteralName(t *testing.T) { + for _, stored := range []string{"Hello, World.mp4", "a;b.mp4", "x?y.mp4", "a#b.mp4", "100%.mp4", "dir one/x y.mp4"} { + upstream, ok := upstreamMediaPath(stored) + if !ok { + t.Fatalf("upstreamMediaPath(%q) refused", stored) + } + request, err := http.NewRequest(http.MethodGet, "http://carrier.example/media/"+upstream, nil) + if err != nil { + t.Fatalf("NewRequest for %q: %v", stored, err) + } + if request.URL.RawPath != "" { + t.Errorf("%q produced raw path %q; CarrierNode would match that instead of the name", stored, request.URL.RawPath) + } + if want := "/media/" + urlPath(stored); request.URL.Path != want { + t.Errorf("%q decodes to %q, want %q", stored, request.URL.Path, want) + } + } +} + func TestUpstreamMediaPathEscapesEachSegment(t *testing.T) { cases := map[string]string{ - "clips/clip.mp4": "clips/clip.mp4", - "my clip.mp4": "my%20clip.mp4", - "100%.mp4": "100%25.mp4", - "media/a b/c.mp4": "a%20b/c.mp4", - "/leading.mp4": "leading.mp4", + "clips/clip.mp4": "clips/clip.mp4", + "my clip.mp4": "my%20clip.mp4", + "100%.mp4": "100%25.mp4", + "media/a b/c.mp4": "a%20b/c.mp4", + "/leading.mp4": "leading.mp4", + "Hello, World.mp4": "Hello,%20World.mp4", + "a;b.mp4": "a;b.mp4", + "x?y.mp4": "x%3Fy.mp4", + "a#b.mp4": "a%23b.mp4", } for stored, want := range cases { got, ok := upstreamMediaPath(stored)