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

@@ -57,9 +57,9 @@ func mountUploads(r *Router, app *backpack.App) error {
if !app.Config.Bool("storage.uploads.serve") {
return nil
}
prefix := strings.TrimSuffix(strings.TrimSpace(app.Config.String("storage.uploads.public_path_prefix")), "/")
if prefix == "" {
prefix = defaultUploadsPrefix
prefix, err := uploadsPrefix(app.Config.String("storage.uploads.public_path_prefix"))
if err != nil {
return err
}
for _, rt := range r.routes {
if rt.path == prefix || strings.HasPrefix(rt.path, prefix+"/") {
@@ -74,6 +74,34 @@ func mountUploads(r *Router, app *backpack.App) error {
return nil
}
// uploadsPrefix returns the route prefix for a storage.uploads.public_path_prefix
// value: trimmed, without a trailing slash, defaultUploadsPrefix when empty.
// The route needs an absolute URL path: a leading slash, no scheme or host,
// no empty, "." or ".." segment, no query, fragment, whitespace or ServeMux
// wildcard braces. Anything else is refused, because ServeMux would read a
// value without a leading slash as a host pattern (every upload then 404s)
// and panic on a URL.
func uploadsPrefix(raw string) (string, error) {
value := strings.TrimSpace(raw)
if value == "" {
return defaultUploadsPrefix, nil
}
prefix := strings.TrimSuffix(value, "/")
bad := !strings.HasPrefix(prefix, "/") || strings.ContainsAny(prefix, "{}?# \t\r\n\\")
if !bad {
for _, seg := range strings.Split(prefix[1:], "/") {
if seg == "" || seg == "." || seg == ".." {
bad = true
break
}
}
}
if bad {
return "", fmt.Errorf("surf: storage.uploads.serve needs storage.uploads.public_path_prefix to be an absolute URL path such as /storage/uploads (no scheme, host, query or wildcard), got %q", raw)
}
return prefix, nil
}
// uploadsHandler resolves the published bucket and database on every request
// and answers 404 while either is missing.
func uploadsHandler(app *backpack.App, prefix string) http.HandlerFunc {