From 57ba9fd9fc32c8a2f3cf3f6942ce55aea20aa53a Mon Sep 17 00:00:00 2001 From: Aloento <11802769+Aloento@users.noreply.github.com> Date: Tue, 1 Dec 2026 11:40:10 +0100 Subject: [PATCH] [Fix] Guard ReOpenIncident with a version predicate ReOpenIncident cleared end_date without checking the caller's version, so a stale copy could reopen an incident modified after it was read. The update now matches on the loaded version, bumps it, and reports ErrVersionConflict when the row no longer matches. The patch handler maps that conflict to 409 instead of 500. --- internal/api/v2/v2.go | 6 ++++ internal/db/db.go | 24 +++++++++++---- tests/reopen_version_test.go | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 tests/reopen_version_test.go diff --git a/internal/api/v2/v2.go b/internal/api/v2/v2.go index ac8eaa5..c3bce5e 100644 --- a/internal/api/v2/v2.go +++ b/internal/api/v2/v2.go @@ -1215,6 +1215,12 @@ func reopenIncident( ) err := dbInst.ReOpenIncident(c.Request.Context(), storedIncident) if err != nil { + if errors.Is(err, db.ErrVersionConflict) { + logger.Warn("incident reopen failed: version conflict", + zap.Uint("event_id", storedIncident.ID)) + apiErrors.RaiseConflictErr(c, apiErrors.ErrVersionConflict) + return err + } logger.Error("incident reopen failed: database error", zap.Uint("event_id", storedIncident.ID), zap.Error(err)) apiErrors.RaiseInternalErr(c, err) diff --git a/internal/db/db.go b/internal/db/db.go index 12d2ceb..ab47a4f 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -520,13 +520,27 @@ func (db *DB) AddComponentToIncident(ctx context.Context, inc *Incident, comp *C // ReOpenIncident the special function if you need to NULL your end_date. func (db *DB) ReOpenIncident(ctx context.Context, inc *Incident) error { - err := db.e.Incident.UpdateOneID(int(inc.ID)). + if inc.Version == nil { + return errors.New("version is required for incident modification") + } + + expectedVersion := *inc.Version + newVersion := expectedVersion + 1 + + affected, err := db.e.Incident.Update(). + Where(incident.IDEQ(int(inc.ID)), incident.VersionEQ(expectedVersion)). ClearEndDate(). - Exec(ctx) - if ent.IsNotFound(err) { - return nil + SetVersion(newVersion). + Save(ctx) + if err != nil { + return err + } + if affected == 0 { + return ErrVersionConflict } - return err + + inc.Version = &newVersion + return nil } // GetEventsByComponentID retrieves all public events associated with a specific component ID. diff --git a/tests/reopen_version_test.go b/tests/reopen_version_test.go new file mode 100644 index 0000000..564acf3 --- /dev/null +++ b/tests/reopen_version_test.go @@ -0,0 +1,58 @@ +package tests + +import ( + "context" + "testing" + "time" + + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/stackmon/otc-status-dashboard/internal/db" + "github.com/stackmon/otc-status-dashboard/internal/event" +) + +// TestReOpenIncidentVersionPredicate checks that reopening an incident clears +// end_date only when the supplied version still matches the stored one. +func TestReOpenIncidentVersionPredicate(t *testing.T) { + ctx := context.Background() + d, _ := newNotifDB(t) + + text := "reopen probe " + uuid.NewString() + start := time.Now().UTC() + end := start.Add(time.Hour) + impact := 2 + + id, err := d.SaveIncident(ctx, &db.Incident{ + Text: &text, + StartDate: &start, + EndDate: &end, + Impact: &impact, + Status: event.IncidentResolved, + Type: event.TypeIncident, + }) + require.NoError(t, err) + + stale, err := d.GetIncident(ctx, int(id)) + require.NoError(t, err) + version := *stale.Version + stale.Version = intPtr(version - 1) + + require.ErrorIs(t, d.ReOpenIncident(ctx, stale), db.ErrVersionConflict) + + unchanged, err := d.GetIncident(ctx, int(id)) + require.NoError(t, err) + require.NotNil(t, unchanged.EndDate, "a conflicting reopen must not clear end_date") + assert.Equal(t, version, *unchanged.Version) + + fresh, err := d.GetIncident(ctx, int(id)) + require.NoError(t, err) + require.NoError(t, d.ReOpenIncident(ctx, fresh)) + + reopened, err := d.GetIncident(ctx, int(id)) + require.NoError(t, err) + assert.Nil(t, reopened.EndDate) + assert.Equal(t, version+1, *reopened.Version) + assert.Equal(t, version+1, *fresh.Version) +}