Browse Source

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

A silence request with neither field fell through to forever, so a client that forgot it silenced the alert with no expiry and no signal. One of duration or until is now required and forever has to be asked for by name. The frontend already sends one of the two, so only accidental requests change behaviour.

Closes #2223
Nacho 5 ngày trước cách đây
mục cha
commit
868557a5a5

+ 1 - 0
CHANGELOG.md

@@ -21,6 +21,7 @@ y este proyecto se adhiere a [Versionado Semántico](https://semver.org/lang/es/
 
 ### Fixed
 
+- `POST /api/alerts/silence` requires `duration` or `until`. A body with neither fell through to `forever`, so a client that forgot the field silenced the alert with no expiry and no signal. `forever` still exists, but only when it is asked for by name (#2223).
 - A push with a bad token for a slug that was never registered no longer leaves two rows in kv. Any host that can reach the ingest could fill the table with invented names, and nothing cleans those keys: the badge exists for agents that exist (#2219).
 - The minimum signal filter of the device list applies on the click that sets it. It was read inside the memo but missing from its dependencies, so the list only changed with the next poll, and never on a static one (#2220).
 - The alert card of the home summary no longer lists read alerts and drops one as soon as it is marked read. When the filter leaves it empty it says so, offers one click back to the whole list and a link to the alert log, and its badge no longer subtracts the same read alert twice (#2217).

+ 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")