| 254 | } |
| 255 | |
| 256 | func TestOpenFileRejectsTrailingSeparator(t *testing.T) { |
| 257 | tests := []struct { |
| 258 | name string |
| 259 | path func(root string) string |
| 260 | }{ |
| 261 | { |
| 262 | name: "relative path", |
| 263 | path: func(string) string { |
| 264 | return "output" + string(os.PathSeparator) |
| 265 | }, |
| 266 | }, |
| 267 | { |
| 268 | name: "absolute path", |
| 269 | path: func(root string) string { |
| 270 | return filepath.Join(root, "output") + string(os.PathSeparator) |
| 271 | }, |
| 272 | }, |
| 273 | } |
| 274 | |
| 275 | for _, tt := range tests { |
| 276 | t.Run(tt.name, func(t *testing.T) { |
| 277 | root := t.TempDir() |
| 278 | t.Chdir(root) |
| 279 | path := tt.path(root) |
| 280 | |
| 281 | file, err := safepaths.OpenFile(path, 0o644, 0o755, false) |
| 282 | if file != nil { |
| 283 | _ = file.Close() |
| 284 | } |
| 285 | require.ErrorContains(t, err, "ends in a separator") |
| 286 | _, statErr := os.Lstat(filepath.Join(root, "output")) |
| 287 | require.True(t, os.IsNotExist(statErr)) |
| 288 | assert.NoFileExists(t, filepath.Join(root, "output", "output")) |
| 289 | }) |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | func TestOpenFileAllowsTrustedParentSymlinks(t *testing.T) { |
| 294 | tests := []struct { |