fix(security): harden server and service deletion lifecycle (#1220)

* test: TDD regression tests for GHSA-jx78-55p5-rwv5 stream quota enforcement

* Apply remaining changes

* fix: update action SHA allowlist and test assertions to match dependabot bump

* fix: close GHSA-jx78-55p5-rwv5 incomplete fix of GHSA-qjpp-gffx-2wm9

Finding 1 (Moderate): nil-guard reporterServer in delayCheck and notifyCheck.
ServerShared has its own lock independent of serviceResponseDataStoreLock, so
m := ServerShared.GetList() taken inside the worker can return a nil entry for
the reporter if the server was concurrently deleted. Previously this caused an
unrecovered SIGSEGV in the worker goroutine (and in the gRPC layer with no
recovery interceptor), taking down the whole instance.

Finding 2 (Low): nil-guard ss.services[id] in ServiceSentinel.Delete().
A caller-supplied id that is absent from the registry caused
ss.services[id].CronJobID to panic, aborting the Delete loop and leaving every
subsequent valid id as a zombie service (DB row deleted, in-memory entry kept,
cron probe still running).

Regression tests added for both findings following the existing
servicesentinel_lifecycle_test.go patterns.

* Apply remaining changes

* chore: replace commit hashes with version tags in test.yml

* fix(server): serialize authoritative lifecycle changes

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(service): bind reports to reporter lifecycle

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(rpc): reject results from stale task streams

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(agentcompat): allow version-tagged actions

* fix(agentcompat): allow literal checkout refs

* refactor(agentcompat): remove SHA resolver policy

* test(agentcompat): remove resolver SHA fixtures

* test(agentcompat): remove mutable ref fixtures

* test(agentcompat): use tagged actions in secure fixtures

* test(agentcompat): update credential fixtures for tags

* test(agentcompat): update reusable action fixtures

* test(agentcompat): update artifact redaction fixtures

* test(agentcompat): finish artifact fixture tag migration

* test(agentcompat): update workflow validation fixtures

* test(agentcompat): update dependency workflow fixture

* ci(agentcompat): stop pinning cross-repository revisions

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: naiba <hi@nai.ba>
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
Copilot
2026-08-01 15:45:20 +08:00
committed by GitHub
co-authored by Sisyphus naiba
parent bb941e4d73
commit 9ec6164f58
46 changed files with 1064 additions and 520 deletions
@@ -6,6 +6,7 @@ import (
"testing"
"github.com/nezhahq/nezha/model"
pb "github.com/nezhahq/nezha/proto"
"github.com/nezhahq/nezha/service/singleton"
)
@@ -44,3 +45,91 @@ func TestRequestTaskCleanupDetachesStreamFromCurrentServerAfterEdit(t *testing.T
t.Fatalf("edited server must report offline after the agent stream dropped, got %T", got)
}
}
func TestRequestTaskRejectsResultWhenServerDeletedAfterRecv(t *testing.T) {
reporter := requestTaskSecurityServer(7, 200, "10101010-1010-1010-1010-101010101010")
cronTask := requestTaskSecurityCron(42, 200, model.CronCoverAll, nil)
setupRequestTaskSecurityFixture(t, []*model.Server{reporter}, []*model.Cron{cronTask}, map[uint64]model.UserInfo{
200: {Role: model.RoleMember},
}, map[string]uint64{"reporter-secret": 200})
stream := requestTaskSecurityAuthedStream("reporter-secret", reporter.UUID)
stream.results = []*pb.TaskResult{cronTaskResult(cronTask.ID, true)}
stream.onResult = func() {
singleton.ServerShared.Delete([]uint64{reporter.ID})
}
err := NewNezhaHandler().RequestTask(stream)
if !errors.Is(err, ErrRequestTaskStreamSuperseded) {
t.Fatalf("expected stale RequestTask stream error, got %v", err)
}
assertCronResultNotUpdated(t, cronTask.ID)
}
func TestRequestTaskRejectsResultWhenNewerStreamSupersedesOld(t *testing.T) {
reporter := requestTaskSecurityServer(7, 200, "20202020-2020-2020-2020-202020202020")
cronTask := requestTaskSecurityCron(42, 200, model.CronCoverAll, nil)
setupRequestTaskSecurityFixture(t, []*model.Server{reporter}, []*model.Cron{cronTask}, map[uint64]model.UserInfo{
200: {Role: model.RoleMember},
}, map[string]uint64{"reporter-secret": 200})
current, ok := singleton.ServerShared.Get(reporter.ID)
if !ok {
t.Fatalf("server %d not found", reporter.ID)
}
newer := &requestTaskSecurityStream{ctx: context.Background()}
stream := requestTaskSecurityAuthedStream("reporter-secret", reporter.UUID)
stream.results = []*pb.TaskResult{cronTaskResult(cronTask.ID, true)}
stream.onResult = func() {
current.SetTaskStream(newer)
}
err := NewNezhaHandler().RequestTask(stream)
if !errors.Is(err, ErrRequestTaskStreamSuperseded) {
t.Fatalf("expected superseded RequestTask stream error, got %v", err)
}
if got := current.GetTaskStream(); got != newer {
t.Fatalf("old stream cleanup must preserve newer stream, got %T", got)
}
assertCronResultNotUpdated(t, cronTask.ID)
}
func TestRequestTaskAcceptsResultAfterServerPointerReplacementWithSameStream(t *testing.T) {
reporter := requestTaskSecurityServer(7, 200, "30303030-3030-3030-3030-303030303030")
cronTask := requestTaskSecurityCron(42, 200, model.CronCoverAll, nil)
setupRequestTaskSecurityFixture(t, []*model.Server{reporter}, []*model.Cron{cronTask}, map[uint64]model.UserInfo{
200: {Role: model.RoleMember},
}, map[string]uint64{"reporter-secret": 200})
old, ok := singleton.ServerShared.Get(reporter.ID)
if !ok {
t.Fatalf("server %d not found", reporter.ID)
}
stream := requestTaskSecurityAuthedStream("reporter-secret", reporter.UUID)
stream.results = []*pb.TaskResult{cronTaskResult(cronTask.ID, true)}
stream.onResult = func() {
replacement := &model.Server{Common: model.Common{ID: old.ID, UserID: old.UserID}, UUID: old.UUID, Name: "replacement"}
replacement.CopyFromRunningServer(old)
singleton.ServerShared.Update(replacement, "")
}
err := NewNezhaHandler().RequestTask(stream)
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected RequestTask to finish after accepted result, got %v", err)
}
if !cronLastResult(t, cronTask.ID) {
t.Fatal("result on a replacement server that inherited the stream must be accepted")
}
}
func assertCronResultNotUpdated(t *testing.T, cronID uint64) {
t.Helper()
var cronTask model.Cron
if err := singleton.DB.First(&cronTask, cronID).Error; err != nil {
t.Fatal(err)
}
if cronTask.LastResult || !cronTask.LastExecutedAt.IsZero() {
t.Fatalf("stale RequestTask result must not mutate cron, got last_result=%t last_executed_at=%s", cronTask.LastResult, cronTask.LastExecutedAt)
}
}