package runtime import ( "path" "strings" "git.sendico.io/tech/sxchnge/product/app/api" "git.sendico.io/tech/sxchnge/shared/errors" ) func validMount(value string) bool { if value == "" || value == "/" || !strings.HasPrefix(value, "/") || path.Clean(value) != value { return false } for _, c := range value { if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || strings.ContainsRune("/-._~", c) { continue } return false } return true } func overlaps(a, b string) bool { return a == b || strings.HasPrefix(a, b+"/") || strings.HasPrefix(b, a+"/") } func resolveMounts(configured []api.Mount, endpoint string, supported map[string]api.Installer) ([]api.Mount, error) { if !validMount(endpoint) { return nil, merrors.InvalidArgument("a canonical absolute API endpoint is required", "api_endpoint") } if configured == nil { configured = []api.Mount{{Version: "v1", Path: endpoint}} } seen := map[string]bool{} for i, mount := range configured { if supported[mount.Version] == nil { return nil, merrors.InvalidArgument("unknown or unavailable API version: "+mount.Version, "api.versions") } if seen[mount.Version] { return nil, merrors.InvalidArgument("duplicate API version: "+mount.Version, "api.versions") } seen[mount.Version] = true if !validMount(mount.Path) { return nil, merrors.InvalidArgument("invalid API mount: "+mount.Path, "api.versions") } for _, reserved := range []string{"/metrics", endpoint + "/metrics", endpoint + "/health"} { if mount.Path == reserved || strings.HasPrefix(mount.Path, reserved+"/") { return nil, merrors.InvalidArgument("API mount conflicts with operational endpoint", "api.versions") } } for _, previous := range configured[:i] { if overlaps(mount.Path, previous.Path) { return nil, merrors.InvalidArgument("overlapping API mounts", "api.versions") } } } return configured, nil }