(t *testing.T)
| 1017 | } |
| 1018 | |
| 1019 | func TestFSRootEnforcement(t *testing.T) { |
| 1020 | t.Parallel() |
| 1021 | |
| 1022 | memFS := fstest.MapFS{ |
| 1023 | "public/index.html": {Data: []byte("<h1>Public</h1>")}, |
| 1024 | "secret/admin.json": {Data: []byte(`{"admin": true, "key": "s3cret"}`)}, |
| 1025 | "public/nested/info": {Data: []byte("nested")}, |
| 1026 | } |
| 1027 | |
| 1028 | // Skip dirfs subtests on Windows: the FS handler pools open file handles |
| 1029 | // (via bigFileReader) which prevents t.TempDir cleanup. |
| 1030 | // The mapfs subtests exercise the same root enforcement logic. |
| 1031 | var tmpDir string |
| 1032 | if runtime.GOOS != "windows" { |
| 1033 | tmpDir = t.TempDir() |
| 1034 | if err := os.MkdirAll(filepath.Join(tmpDir, "public"), 0o755); err != nil { |
| 1035 | t.Fatalf("cannot create public dir: %v", err) |
| 1036 | } |
| 1037 | if err := os.MkdirAll(filepath.Join(tmpDir, "secret"), 0o755); err != nil { |
| 1038 | t.Fatalf("cannot create secret dir: %v", err) |
| 1039 | } |
| 1040 | if err := os.WriteFile(filepath.Join(tmpDir, "public", "index.html"), []byte("<h1>Public</h1>"), 0o644); err != nil { |
| 1041 | t.Fatalf("cannot create public index: %v", err) |
| 1042 | } |
| 1043 | if err := os.WriteFile(filepath.Join(tmpDir, "secret", "admin.json"), []byte(`{"admin": true, "key": "s3cret"}`), 0o644); err != nil { |
| 1044 | t.Fatalf("cannot create secret admin file: %v", err) |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | type testCase struct { |
| 1049 | name string |
| 1050 | root string |
| 1051 | filesystem fs.FS |
| 1052 | pathRewrite PathRewriteFunc |
| 1053 | } |
| 1054 | |
| 1055 | cases := make([]testCase, 0, 9) |
| 1056 | for _, root := range []string{"public", "public/", "./public", "/public"} { |
| 1057 | cases = append(cases, testCase{ |
| 1058 | name: "mapfs/" + root, |
| 1059 | root: root, |
| 1060 | filesystem: memFS, |
| 1061 | }) |
| 1062 | if tmpDir != "" { |
| 1063 | cases = append(cases, testCase{ |
| 1064 | name: "dirfs/" + root, |
| 1065 | root: root, |
| 1066 | filesystem: os.DirFS(tmpDir), |
| 1067 | }) |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | cases = append(cases, testCase{ |
| 1072 | name: "mapfs/pathrewrite-no-leading-slash", |
| 1073 | root: "./public/", |
| 1074 | filesystem: memFS, |
| 1075 | pathRewrite: func(ctx *RequestCtx) []byte { |
| 1076 | return bytes.TrimPrefix(ctx.Path(), []byte("/")) |
nothing calls this directly
no test coverage detected
searching dependent graphs…