Panic in EvalAlertContextRules when a context expression returns nil

オープン 初心者向け
#4,847 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 4 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
go
領域
backend

調査の方向性

panic は default 分岐の EvalAlertContextRules(pkg/alertcontext/alertcontext.go、214 行目付近)にあり、EventToContext と NewAlert から到達します。まずその分岐を読んでください。nil の output(または !r.IsValid())に対して IsZero() の前にガードし、IsNil() を nillable な kind に限定します。nil を返す式を使って pkg/alertcontext のテストにケースを追加し、panic する代わりにスキップされることを確認してください。

索引モデルが issue の本文から書いたものです。

説明

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
主要言語
Go
スター
15.1k
フォーク
726
平均マージ
4日 23時間
マージ済み PR(30日)
27

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

crowdsecurity/crowdsec のほかの issue

crowdsecurity/crowdsec の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。