refactor(backend): rename mediacore package to media
This commit is contained in:
1 parent
e8cb7c6c68
commit
75c08beec6
34 files changed
+837
-656
No files matched your search
@@ -0,0 +1,60 @@
|
||||
package files
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user