fix: validate and clamp container log tail parameter (closes #24)
- Add sanitizeTail() helper that validates tail is numeric and positive - Clamp values to max 500 - Default to 500 when empty, non-numeric, zero, or negative - Add comprehensive test cases
This commit is contained in:
@@ -373,6 +373,28 @@ func (h *Handlers) HandleAppDeployments() http.HandlerFunc {
|
||||
// defaultLogTail is the default number of log lines to fetch.
|
||||
const defaultLogTail = "500"
|
||||
|
||||
// maxLogTail is the maximum allowed value for the tail parameter.
|
||||
const maxLogTail = 500
|
||||
|
||||
// sanitizeTail validates and clamps the tail query parameter.
|
||||
// It returns a numeric string clamped to maxLogTail, or the default if invalid.
|
||||
func sanitizeTail(raw string) string {
|
||||
if raw == "" {
|
||||
return defaultLogTail
|
||||
}
|
||||
|
||||
n, err := strconv.Atoi(raw)
|
||||
if err != nil || n < 1 {
|
||||
return defaultLogTail
|
||||
}
|
||||
|
||||
if n > maxLogTail {
|
||||
n = maxLogTail
|
||||
}
|
||||
|
||||
return strconv.Itoa(n)
|
||||
}
|
||||
|
||||
// HandleAppLogs returns the container logs handler.
|
||||
func (h *Handlers) HandleAppLogs() http.HandlerFunc {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
@@ -394,10 +416,7 @@ func (h *Handlers) HandleAppLogs() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
tail := request.URL.Query().Get("tail")
|
||||
if tail == "" {
|
||||
tail = defaultLogTail
|
||||
}
|
||||
tail := sanitizeTail(request.URL.Query().Get("tail"))
|
||||
|
||||
logs, logsErr := h.docker.ContainerLogs(
|
||||
request.Context(),
|
||||
|
||||
Reference in New Issue
Block a user