Update shared package
This commit is contained in:
1 parent
fc01c93aeb
commit
7f21bb9073
2 files changed
+37
-13
No files matched your search
+9
-11
@@ -911,21 +911,19 @@ func urlPath(value string) string {
|
|||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
// upstreamMediaPath returns the escaped CarrierNode path for a stored file path. It reports false
|
// upstreamMediaPath returns the CarrierNode path for a stored file path. It reports false only when
|
||||||
// when the stored path is not a safe relative path, or when it contains a query or fragment marker
|
// the stored path is not a safe relative path.
|
||||||
// that CarrierNode would read as the end of the 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) {
|
func upstreamMediaPath(filePath string) (string, bool) {
|
||||||
relative := urlPath(filePath)
|
relative := urlPath(filePath)
|
||||||
if !files.SafeRelative(relative) || strings.ContainsAny(relative, "?#") {
|
if !files.SafeRelative(relative) {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
// Escape each segment so percent-escapes and other reserved characters in a stored name
|
return (&url.URL{Path: relative}).EscapedPath(), true
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// canonicalMediaID returns the numeric media id that identifier spells exactly. Leading zeros, signs
|
// canonicalMediaID returns the numeric media id that identifier spells exactly. Leading zeros, signs
|
||||||
|
|||||||
@@ -1,17 +1,39 @@
|
|||||||
package media
|
package media
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUpstreamMediaPathRefusesQueryFragmentAndTraversal(t *testing.T) {
|
func TestUpstreamMediaPathRefusesTraversal(t *testing.T) {
|
||||||
for _, stored := range []string{"x?y.mp4", "a#b.mp4", "../x.mp4", "media/../../x.mp4", `..\x.mp4`} {
|
for _, stored := range []string{"../x.mp4", "media/../../x.mp4", `..\x.mp4`} {
|
||||||
if _, ok := upstreamMediaPath(stored); ok {
|
if _, ok := upstreamMediaPath(stored); ok {
|
||||||
t.Errorf("upstreamMediaPath(%q) accepted, want refused", stored)
|
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) {
|
func TestUpstreamMediaPathEscapesEachSegment(t *testing.T) {
|
||||||
cases := map[string]string{
|
cases := map[string]string{
|
||||||
"clips/clip.mp4": "clips/clip.mp4",
|
"clips/clip.mp4": "clips/clip.mp4",
|
||||||
@@ -19,6 +41,10 @@ func TestUpstreamMediaPathEscapesEachSegment(t *testing.T) {
|
|||||||
"100%.mp4": "100%25.mp4",
|
"100%.mp4": "100%25.mp4",
|
||||||
"media/a b/c.mp4": "a%20b/c.mp4",
|
"media/a b/c.mp4": "a%20b/c.mp4",
|
||||||
"/leading.mp4": "leading.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 {
|
for stored, want := range cases {
|
||||||
got, ok := upstreamMediaPath(stored)
|
got, ok := upstreamMediaPath(stored)
|
||||||
|
|||||||
Reference in new issue
Block a user