Просмотр исходного кода

fix(alerts): require duration or until to silence an alert (#2223)

A request with neither field fell through to the forever case, so a client that forgot it silenced the alert with no expiry and no signal. Both are now required and forever has to be asked for by name, which only changes requests that got the behaviour by accident: the frontend always sends duration or until.
gnacho 5 дней назад
Родитель
Сommit
ca438d369b
2 измененных файлов с 40 добавлено и 3 удалено
  1. 27 0
      server-go/internal/httpapi/alerts_api_test.go
  2. 13 3
      server-go/internal/httpapi/data.go

+ 27 - 0
server-go/internal/httpapi/alerts_api_test.go

@@ -340,3 +340,30 @@ func TestAlertsSnoozeEndpoint(t *testing.T) {
 		res.Body.Close()
 	}
 }
+
+// #2223: un cuerpo sin duration ni until no puede posponer para siempre.
+// forever sigue disponible, pero solo cuando se pide por su nombre.
+func TestAlertsSilenceRequiresExpiry(t *testing.T) {
+	ts := makeTestServer(t)
+	_, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
+
+	res := doJSON(t, "POST", ts.URL, "/api/alerts/silence", cookie, `{"id":"alert-handshake-wg"}`)
+	if res.StatusCode != 400 {
+		t.Fatalf("silence sin duración: %d, esperaba 400", res.StatusCode)
+	}
+	res.Body.Close()
+
+	for _, a := range alertItems(t, get(t, ts.URL, "/api/alerts", cookie)) {
+		if a["id"] == "alert-handshake-wg" {
+			if snoozed, _ := a["snoozed"].(bool); snoozed {
+				t.Fatalf("se pospuso una alerta sin duración: %v", a)
+			}
+		}
+	}
+
+	res = doJSON(t, "POST", ts.URL, "/api/alerts/silence", cookie, `{"id":"alert-handshake-wg","duration":"forever"}`)
+	if res.StatusCode != 200 {
+		t.Fatalf("forever explícito: %d", res.StatusCode)
+	}
+	res.Body.Close()
+}

+ 13 - 3
server-go/internal/httpapi/data.go

@@ -504,8 +504,9 @@ func (s *server) handleAlertsDismiss(w http.ResponseWriter, r *http.Request) {
 
 // handleAlertsSilence (POST): body {"id":"...","duration":"1h|2h|8h|24h|7d|forever"}
 // o {"id":"...","until":<unix segundos>} → pospone las alertas con la misma
-// dedup key (category|title|routerId). Desde #2185 la alerta no se oculta:
-// queda marcada como pospuesta en el feed.
+// dedup key (category|title|routerId). Uno de los dos campos es obligatorio
+// (#2223): sin ellos no se pospone nada, forever se pide por nombre. Desde
+// #2185 la alerta no se oculta: queda marcada como pospuesta en el feed.
 func (s *server) handleAlertsSilence(w http.ResponseWriter, r *http.Request) {
 	var body struct {
 		ID       string `json:"id"`
@@ -520,8 +521,17 @@ func (s *server) handleAlertsSilence(w http.ResponseWriter, r *http.Request) {
 		writeError(w, http.StatusBadRequest, "invalid_input", "id is required")
 		return
 	}
+	// #2223: sin duración ni fecha el switch caía en forever, así que un
+	// cliente que olvidaba el campo posponía la alerta para siempre en
+	// silencio. Uno de los dos es obligatorio; forever se pide por nombre.
+	if body.Duration == "" && body.Until <= 0 {
+		writeError(w, http.StatusBadRequest, "invalid_input", `duration or until is required ("forever" silences without expiry)`)
+		return
+	}
 	var dur time.Duration
 	switch body.Duration {
+	case "":
+		// hasta: lo fija until más abajo
 	case "1h":
 		dur = time.Hour
 	case "2h":
@@ -532,7 +542,7 @@ func (s *server) handleAlertsSilence(w http.ResponseWriter, r *http.Request) {
 		dur = 24 * time.Hour
 	case "7d":
 		dur = 7 * 24 * time.Hour
-	case "forever", "":
+	case "forever":
 		dur = 0
 	default:
 		writeError(w, http.StatusBadRequest, "invalid_input", "duration must be 1h, 2h, 8h, 24h, 7d or forever")