Files
Kelx/template_image_policy.go
2026-11-30 16:32:37 +00:00

81 lines
2.6 KiB
Go

package main
import (
"fmt"
"os"
"strings"
)
// Template images (installer AND runtime) are execution content: the node
// pulls and runs whatever reference a template carries, so an arbitrary
// registry is an arbitrary-code primitive. This allowlist is the node agent's
// sink-side mirror of the panel's ingestion check
// (validateTemplateImageRefInput in utils/template-store-trust.js, default
// registries in DEFAULT_TEMPLATE_IMAGE_REGISTRIES) — keep both
// behaviorally equivalent. Enforced at every sink that pulls or runs a
// template-chosen image (create, start, reinstall pull, installer preflight)
// so stored legacy templates cannot bypass the panel either. Operators with
// private registries extend it via ADPANEL_ALLOWED_TEMPLATE_IMAGE_REGISTRIES.
var defaultTemplateImageRegistries = []string{
"docker.io",
"ghcr.io",
"gcr.io",
"quay.io",
"registry.k8s.io",
"public.ecr.aws",
}
func templateImageRegistries() []string {
configured := strings.Split(strings.TrimSpace(os.Getenv("ADPANEL_ALLOWED_TEMPLATE_IMAGE_REGISTRIES")), ",")
var registries []string
for _, entry := range configured {
entry = strings.ToLower(strings.TrimSpace(entry))
if entry != "" {
registries = append(registries, entry)
}
}
if len(registries) == 0 {
return defaultTemplateImageRegistries
}
return registries
}
// templateImageRegistryOf returns the registry of a docker image reference:
// the first path component only when it looks like a host (contains "." or
// ":" or is "localhost"); otherwise the ref is a Docker Hub name. Mirrors
// imageRegistryForRef in utils/template-store-trust.js.
func templateImageRegistryOf(ref string) string {
clean := strings.TrimSpace(ref)
if clean == "" {
return ""
}
// adpanel:reinstall-image-registry - a tag or digest without a slash is
// Docker Hub shorthand, not a registry hostname or registry port.
if separator := strings.Index(clean, "/"); separator > 0 {
registry := strings.ToLower(clean[:separator])
if strings.ContainsAny(registry, ".:") || registry == "localhost" {
return registry
}
}
return "docker.io"
}
func validateTemplateImageRef(ref string) string {
clean := strings.TrimSpace(ref)
if clean == "" {
return "missing docker image"
}
for _, character := range clean {
if character <= 0x20 {
return "docker image reference contains whitespace or control characters"
}
}
registry := templateImageRegistryOf(clean)
for _, allowed := range templateImageRegistries() {
if registry == allowed {
return ""
}
}
return fmt.Sprintf("docker image registry %q is not allowed (allowed: %s)", registry, strings.Join(templateImageRegistries(), ", "))
}