Add name validation to backend handlers

This commit is contained in:
2025-10-22 18:50:51 +02:00
parent c6053f6afd
commit 3b8bc658e3

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"llamactl/pkg/backends" "llamactl/pkg/backends"
"llamactl/pkg/instance" "llamactl/pkg/instance"
"llamactl/pkg/validation"
"net/http" "net/http"
"os/exec" "os/exec"
"strings" "strings"
@@ -22,13 +23,16 @@ func (h *Handler) LlamaCppProxy(onDemandStart bool) http.HandlerFunc {
// Get the instance name from the URL parameter // Get the instance name from the URL parameter
name := chi.URLParam(r, "name") name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Instance name cannot be empty", http.StatusBadRequest) // Validate instance name at the entry point
validatedName, err := validation.ValidateInstanceName(name)
if err != nil {
http.Error(w, "Invalid instance name: "+err.Error(), http.StatusBadRequest)
return return
} }
// Route to the appropriate inst based on instance name // Route to the appropriate inst based on instance name
inst, err := h.InstanceManager.GetInstance(name) inst, err := h.InstanceManager.GetInstance(validatedName)
if err != nil { if err != nil {
http.Error(w, "Invalid instance: "+err.Error(), http.StatusBadRequest) http.Error(w, "Invalid instance: "+err.Error(), http.StatusBadRequest)
return return
@@ -66,7 +70,7 @@ func (h *Handler) LlamaCppProxy(onDemandStart bool) http.HandlerFunc {
} }
// If on-demand start is enabled, start the instance // If on-demand start is enabled, start the instance
if _, err := h.InstanceManager.StartInstance(name); err != nil { if _, err := h.InstanceManager.StartInstance(validatedName); err != nil {
http.Error(w, "Failed to start instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to start instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
@@ -85,7 +89,7 @@ func (h *Handler) LlamaCppProxy(onDemandStart bool) http.HandlerFunc {
} }
// Strip the "/llama-cpp/<name>" prefix from the request URL // Strip the "/llama-cpp/<name>" prefix from the request URL
prefix := fmt.Sprintf("/llama-cpp/%s", name) prefix := fmt.Sprintf("/llama-cpp/%s", validatedName)
r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix) r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix)
// Update the last request time for the instance // Update the last request time for the instance