Panic in EvalAlertContextRules when a context expression returns nil

Đang mở Phù hợp với người mới
#4,847 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 4 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go
Lĩnh vực
backend

Hướng nghiên cứu

Panic nằm trong nhánh default của EvalAlertContextRules trong pkg/alertcontext/alertcontext.go (khoảng dòng 214), được đến từ EventToContext và NewAlert. Hãy đọc nhánh đó trước: bảo vệ trước một output nil (hoặc !r.IsValid()) trước IsZero(), và giới hạn IsNil() cho các kind nillable. Thêm một case vào các test của pkg/alertcontext với một biểu thức trả về nil và xác nhận rằng nó bị bỏ qua thay vì panic.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

kind/bug needs/triage

What happened

crowdsec crashes with a panic when an alert context expression evaluates to nil:

error: reflect: call of reflect.Value.IsZero on zero Value
reflect.Value.IsZero({0x0?, 0x0?, ...})
	reflect/value.go:1746
github.com/crowdsecurity/crowdsec/pkg/alertcontext.EvalAlertContextRules(...)
	github.com/crowdsecurity/crowdsec/pkg/alertcontext/alertcontext.go:214
github.com/crowdsecurity/crowdsec/pkg/alertcontext.EventToContext(...)
	github.com/crowdsecurity/crowdsec/pkg/alertcontext/alertcontext.go:257
github.com/crowdsecurity/crowdsec/pkg/leakybucket.NewAlert(...)
	github.com/crowdsecurity/crowdsec/pkg/leakybucket/overflows.go:371

The whole agent goes down, so every bucket overflow that hits the expression kills crowdsec. With systemd restarting it, this ends in a crash loop, and bouncers can no longer reach the LAPI.

How to reproduce

Add a context file such as /etc/crowdsec/contexts/example.yaml:

context:
  example:
  - "get(evt.Unmarshaled.caddy.resp_headers, 'X-Example') != nil ? evt.Unmarshaled.caddy.resp_headers['X-Example'][0] : nil"

Trigger any scenario overflow for an event where the header is missing.

Cause

In pkg/alertcontext/alertcontext.go the default branch does:

r := reflect.ValueOf(output)
if r.IsZero() || r.IsNil() {
    continue
}

For output == nil, reflect.ValueOf returns the zero reflect.Value, and IsZero() panics on it. The IsNil() check shows that nil was meant to be skipped. The same code is still on master.

Suggested fix

Check output == nil (or !r.IsValid()) before calling IsZero(), for example:

default:
    if output == nil {
        continue
    }
    r := reflect.ValueOf(output)
    if r.IsZero() {
        continue
    }

Note that r.IsNil() can also panic for kinds that are not nillable (e.g. structs, bools), so it should probably be guarded by a kind check as well.

Workaround

Return '' instead of nil in the expression. Empty strings are skipped in the string branch.

Environment

  • crowdsec v1.8.2 (Debian package, v1.8.2-debian-pragmatic-amd64), also seen with v1.8.1
  • Ubuntu 26.04, amd64
Ngôn ngữ chính
Go
Star
15.1k
Fork
726
Merge trung bình
4 ngày 23 giờ
Pull request đã merge (30 ngày)
27

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của crowdsecurity/crowdsec

Tất cả issue của crowdsecurity/crowdsec

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.