Files
CollabTable/shared/backend/mediacore/paths_test.go
T

101 lines
3.3 KiB
Go
Raw Normal View History

package mediacore
import (
"errors"
"os"
"path/filepath"
"testing"
)
func TestSafeRelativeRejectsTraversalAndAbsolutePaths(t *testing.T) {
for _, value := range []string{"", ".", "..", "../x", "a/../../x", "sub/../a.mp4x", "/etc/passwd", `\etc\passwd`, `C:\x`, "a\x00b"} {
if SafeRelative(value) {
t.Errorf("SafeRelative(%q) = true, want false", value)
}
}
for _, value := range []string{"a.mp4", "sub/b.mp4", "v1..2/clip.mp4"} {
if !SafeRelative(value) {
t.Errorf("SafeRelative(%q) = false, want true", value)
}
}
}
func TestResolveUnderRootsStaysInsideTheRoots(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "sub"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "a.mp4"), []byte("a"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "sub", "b.mp4"), []byte("b"), 0o600); err != nil {
t.Fatal(err)
}
outside := filepath.Join(t.TempDir(), "secret.txt")
if err := os.WriteFile(outside, []byte("s"), 0o600); err != nil {
t.Fatal(err)
}
_ = os.Symlink(outside, filepath.Join(root, "link.txt"))
for value, want := range map[string]string{
"a.mp4": filepath.Join(root, "a.mp4"),
"/media/a.mp4": filepath.Join(root, "a.mp4"),
"sub/b.mp4": filepath.Join(root, "sub", "b.mp4"),
"/media/sub/b.mp4": filepath.Join(root, "sub", "b.mp4"),
} {
got, err := ResolveUnderRoots(value, []string{root})
if err != nil || got != want {
t.Errorf("ResolveUnderRoots(%q) = %q, %v; want %q", value, got, err, want)
}
}
for _, value := range []string{"../secret.txt", "sub", "missing.mp4", "link.txt", "", "a\x00b"} {
if _, err := ResolveUnderRoots(value, []string{root}); err == nil {
t.Errorf("ResolveUnderRoots(%q) resolved, want an error", value)
}
}
if _, err := ResolveUnderRoots("missing.mp4", []string{root}); !errors.Is(err, os.ErrNotExist) {
t.Errorf("missing file error = %v, want os.ErrNotExist", err)
}
}
func TestUpstreamMediaPathRefusesQueryFragmentAndTraversal(t *testing.T) {
for _, stored := range []string{"x?y.mp4", "a#b.mp4", "../x.mp4", "media/../../x.mp4", `..\x.mp4`} {
if _, ok := upstreamMediaPath(stored); ok {
t.Errorf("upstreamMediaPath(%q) accepted, want refused", stored)
}
}
}
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",
}
for stored, want := range cases {
got, ok := upstreamMediaPath(stored)
if !ok || got != want {
t.Errorf("upstreamMediaPath(%q) = %q, %v; want %q", stored, got, ok, want)
}
}
}
func TestCanonicalMediaIDAcceptsOnlyCanonicalDecimals(t *testing.T) {
for _, value := range []string{"", "007", "+1", "-1", "4a", " 4", "4 ", "99999999999999999999"} {
if id, ok := canonicalMediaID(value); ok {
t.Errorf("canonicalMediaID(%q) = %d, want refused", value, id)
}
}
if id, ok := canonicalMediaID("42"); !ok || id != 42 {
t.Errorf("canonicalMediaID(42) = %d, %v; want 42, true", id, ok)
}
if mediaIDArg("abc") != nil {
t.Error("mediaIDArg(non-numeric) is not NULL")
}
if mediaIDArg("42") != int64(42) {
t.Error("mediaIDArg(42) is not 42")
}
}