fix(staticweb): add nil check to WithStripPrefix helper

Prevent panic when WithStripPrefix is called with a nil provider by
using reflection to check for typed nil pointers stored in interfaces.
This commit is contained in:
2026-01-03 14:43:31 +02:00
parent 2017465cb8
commit 8504b6d13d

View File

@@ -3,6 +3,7 @@ package staticweb
import (
"io/fs"
"net/http"
"reflect"
)
// FileSystemProvider abstracts the source of files (local, zip, embedded, future: http, s3)
@@ -51,7 +52,10 @@ type PrefixStrippingProvider interface {
// WithStripPrefix is a helper function that sets the strip prefix on a provider
// if it implements PrefixStrippingProvider. Returns the provider for method chaining.
func WithStripPrefix(provider FileSystemProvider, prefix string) FileSystemProvider {
if p, ok := provider.(PrefixStrippingProvider); ok {
if provider == nil || reflect.ValueOf(provider).IsNil() {
return provider
}
if p, ok := provider.(PrefixStrippingProvider); ok && p != nil {
p.WithStripPrefix(prefix)
}
return provider