73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
package files
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestResolveBlocksTraversal(t *testing.T) {
|
|
root := filepath.Join(t.TempDir(), "project")
|
|
for _, requested := range []string{
|
|
"../../etc/passwd",
|
|
`..\..\secret`,
|
|
"/../../outside",
|
|
"public/../secret",
|
|
"public//index.html",
|
|
"public/./index.html",
|
|
} {
|
|
if _, err := Resolve(root, requested); !errors.Is(err, ErrOutsideRoot) {
|
|
t.Errorf("Resolve(%q) error = %v, want ErrOutsideRoot", requested, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInspectRegularRejectsDirectoriesAndReadsFiles(t *testing.T) {
|
|
root := t.TempDir()
|
|
if err := os.MkdirAll(filepath.Join(root, "app"), 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(root, "app", "server.js"), []byte("ok"), 0640); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
info, err := InspectRegular(root, "app/server.js")
|
|
if err != nil || info.Size() != 2 {
|
|
t.Fatalf("InspectRegular(file) = %v, %v", info, err)
|
|
}
|
|
if _, err = InspectRegular(root, "app"); !errors.Is(err, ErrNotRegular) {
|
|
t.Fatalf("InspectRegular(directory) error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestResolveNormalizesInsideRoot(t *testing.T) {
|
|
root := filepath.Join(t.TempDir(), "project")
|
|
got, err := Resolve(root, "public/assets/app.js")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := filepath.Join(root, "public", "assets", "app.js")
|
|
if got != want {
|
|
t.Fatalf("got %q want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestEnsureDirectoryStaysInsidePhysicalProjectPath(t *testing.T) {
|
|
root := t.TempDir()
|
|
if err := os.Mkdir(filepath.Join(root, "app"), 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := EnsureDirectory(root, "app/node_modules", 0770); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info, err := os.Lstat(filepath.Join(root, "app", "node_modules")); err != nil || !info.IsDir() {
|
|
t.Fatalf("compatibility directory was not created: info=%v err=%v", info, err)
|
|
}
|
|
outside := t.TempDir()
|
|
if err := os.Symlink(outside, filepath.Join(root, "linked")); err == nil {
|
|
if err = EnsureDirectory(root, "linked/node_modules", 0770); err == nil {
|
|
t.Fatal("EnsureDirectory followed a symlink outside the project root")
|
|
}
|
|
}
|
|
}
|