Files

103 lines
3.1 KiB
Go
Raw Permalink Normal View History

package assets
import (
"errors"
"fmt"
"path"
"strings"
)
// ErrInvalidPath marks a virtual asset path that is rejected before any
// source is consulted (traversal, backslash, drive letter, UNC, NUL, ...).
var ErrInvalidPath = errors.New("invalid asset path")
// URL prefixes that address the asset root. "/assets/x" and "assets:x" are
// the same identity "x".
const (
PrefixSlash = "/assets/"
PrefixScheme = "assets:"
)
// IsAssetRef reports whether ref uses one of the two asset reference forms.
func IsAssetRef(ref string) bool {
return strings.HasPrefix(ref, PrefixSlash) || strings.HasPrefix(ref, PrefixScheme)
}
// SplitRef removes query and fragment from a reference. They never take part
// in the lookup.
func SplitRef(ref string) (p, suffix string) {
if i := strings.IndexAny(ref, "?#"); i >= 0 {
return ref[:i], ref[i:]
}
return ref, ""
}
// FromRef turns "/assets/a/b.png?x#y" or "assets:a/b.png" into the canonical
// virtual path "a/b.png". The path is validated with ValidatePath; nothing is
// cleaned silently and nothing is percent-decoded here.
func FromRef(ref string) (string, error) {
p, _ := SplitRef(ref)
switch {
case strings.HasPrefix(p, PrefixSlash):
p = p[len(PrefixSlash):]
case strings.HasPrefix(p, PrefixScheme):
p = p[len(PrefixScheme):]
default:
return "", fmt.Errorf("%w: %q is not an asset reference", ErrInvalidPath, ref)
}
if err := ValidatePath(p); err != nil {
return "", err
}
return p, nil
}
// ValidatePath accepts only canonical, relative, forward-slash paths inside
// the asset root. It rejects instead of cleaning: "a/../b" is an error, not
// "b".
func ValidatePath(p string) error {
bad := func(why string) error { return fmt.Errorf("%w: %q: %s", ErrInvalidPath, p, why) }
switch {
case p == "":
return bad("empty")
case strings.ContainsRune(p, 0):
return bad("NUL byte")
case strings.Contains(p, `\`):
return bad("backslash")
case strings.HasPrefix(p, "/"):
return bad("absolute")
case strings.Contains(p, ":"):
return bad("drive letter or scheme")
case strings.HasSuffix(p, "/"):
return bad("directory")
}
for _, seg := range strings.Split(p, "/") {
switch seg {
case "":
return bad("empty segment")
case ".", "..":
return bad("dot segment")
}
}
if path.Clean(p) != p {
return bad("not canonical")
}
return nil
}
// Join resolves ref relative to the directory of base, both virtual paths.
// Unlike ValidatePath it may use ".." segments, as long as the canonical
// result stays inside the root. This is for references found inside trusted
// asset content (CSS url(), @import), not for paths coming from the browser.
func Join(base, ref string) (string, error) {
if strings.ContainsAny(ref, "\\\x00") {
return "", fmt.Errorf("%w: %q", ErrInvalidPath, ref)
}
joined := path.Join(path.Dir(base), ref)
if joined == "." || joined == ".." || strings.HasPrefix(joined, "../") || strings.HasPrefix(joined, "/") {
return "", fmt.Errorf("%w: %q relative to %q leaves the asset root", ErrInvalidPath, ref, base)
}
if err := ValidatePath(joined); err != nil {
return "", err
}
return joined, nil
}