fix(14.2): WR-04 validate storage.uploads.public_path_prefix at boot

The prefix was only trimmed. Without a leading slash ServeMux read its first
segment as a host pattern, so every upload silently answered 404, and a CDN
URL panicked in ServeMux and surfaced as a misleading route conflict. With
storage.uploads.serve on, BuildRouter now refuses a prefix that is not an
absolute URL path (scheme, host, query, fragment, whitespace, wildcard
braces, empty, . or .. segments, or a bare /) with an error naming the key.
This commit is contained in:
2026-10-12 11:21:38 +02:00
parent 41a03b9987
commit 01d2548ab3
4 changed files with 70 additions and 5 deletions

View File

@@ -159,6 +159,41 @@ func TestUploadsMount(t *testing.T) {
}
})
t.Run("a prefix that is not an absolute URL path fails BuildRouter", func(t *testing.T) {
for _, prefix := range []string{
"storage/uploads", // no leading slash: ServeMux reads "storage" as a host
"storage/app/uploads/public", // the WinterCMS disk path, not its URL
"https://cdn.example.com/uploads", // a CDN origin
"//cdn.example.com/uploads", // a scheme-relative URL
"/storage//uploads",
"/storage/{id}/uploads",
"/storage/uploads?x=1",
"/storage/uploads#top",
"/storage/../uploads",
"/storage/./uploads",
"/storage/up loads",
"/",
} {
app := backpack.New(uploadsConfig(t, map[string]any{"storage.uploads.serve": true, "storage.uploads.public_path_prefix": prefix}))
_, err := BuildRouter(app, nil)
if err == nil || !strings.Contains(err.Error(), "storage.uploads.public_path_prefix") || !strings.Contains(err.Error(), "absolute URL path") {
t.Fatalf("prefix %q: err = %v, want a refusal naming storage.uploads.public_path_prefix", prefix, err)
}
}
// Valid prefixes, with or without a trailing slash, and an invalid
// one while serve is off (the key then only feeds attach.PublicURL).
for _, prefix := range []string{"/storage/uploads", "/storage/app/uploads/public/", "/media", " /files "} {
app := backpack.New(uploadsConfig(t, map[string]any{"storage.uploads.serve": true, "storage.uploads.public_path_prefix": prefix}))
if _, err := BuildRouter(app, nil); err != nil {
t.Fatalf("prefix %q: %v", prefix, err)
}
}
app := backpack.New(uploadsConfig(t, map[string]any{"storage.uploads.public_path_prefix": "https://cdn.example.com/uploads"}))
if _, err := BuildRouter(app, nil); err != nil {
t.Fatalf("serve off with a CDN prefix: %v", err)
}
})
t.Run("a plugin route at or under the prefix fails BuildRouter", func(t *testing.T) {
for _, path := range []string{"/storage/uploads/{path...}", "/storage/uploads/avatars/{id}", "/storage/uploads"} {
app := backpack.New(uploadsConfig(t, map[string]any{"storage.uploads.serve": true}))