Sync shared snapshot from 9e58b6d
This commit is contained in:
1 parent
36ab01aab1
commit
5a4bf45bc7
10 files changed
+393
-51
No files matched your search
@@ -135,7 +135,23 @@ func (c *Service) streamMediaIdentifier(w http.ResponseWriter, r *http.Request,
|
||||
writeError(w, http.StatusNotFound, "media not found")
|
||||
return
|
||||
}
|
||||
c.carrierNode.Proxy(w, r, "/media/"+upstream)
|
||||
c.carrierNode.Proxy(w, r, streamUpstreamPath(upstream, r.URL.Query()))
|
||||
}
|
||||
|
||||
// streamUpstreamPath is the CarrierNode request path for a stream. CarrierNode answers with a live
|
||||
// H.264 copy when the query carries quality (a height) and start (seconds), which is how browsers get
|
||||
// containers they cannot decode. Those two parameters are the only ones forwarded.
|
||||
func streamUpstreamPath(upstream string, query url.Values) string {
|
||||
forward := url.Values{}
|
||||
for _, key := range []string{"quality", "start"} {
|
||||
if value := strings.TrimSpace(query.Get(key)); value != "" {
|
||||
forward.Set(key, value)
|
||||
}
|
||||
}
|
||||
if len(forward) == 0 {
|
||||
return "/media/" + upstream
|
||||
}
|
||||
return "/media/" + upstream + "?" + forward.Encode()
|
||||
}
|
||||
|
||||
func (c *Service) thumbnailMediaByQuery(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -911,21 +927,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
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -124,3 +125,20 @@ func TestOnlyModeratorsCanSeeHiddenMedia(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamUpstreamPathForwardsOnlyLiveTranscodeOptions(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
query url.Values
|
||||
want string
|
||||
}{
|
||||
{"plain stream", url.Values{"mediaKey": {"abc"}}, "/media/Videos/clip.mp4"},
|
||||
{"blank options are dropped", url.Values{"quality": {" "}, "start": {""}}, "/media/Videos/clip.mp4"},
|
||||
{"quality and start are forwarded", url.Values{"mediaKey": {"abc"}, "quality": {"1080"}, "start": {" 12.5 "}}, "/media/Videos/clip.mp4?quality=1080&start=12.5"},
|
||||
{"quality alone", url.Values{"quality": {"720"}}, "/media/Videos/clip.mp4?quality=720"},
|
||||
} {
|
||||
if got := streamUpstreamPath("Videos/clip.mp4", tc.query); got != tc.want {
|
||||
t.Errorf("%s: streamUpstreamPath() = %q, want %q", tc.name, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user