on main help sign in
fix(event): prevent panic on non-string telemetry keys (#2502)
commit8e743cbfeb8e0dbb2b3a605a93c2acc754024d27 authoriceymoss <114280774+iceymoss@users.noreply.github.com> dateSat 28 Mar 2026, 04:02 (7 months ago) parent086d839
2 files changed+35−1
internal/event/event.go+5−1 internal/event/event_test.go+30
internal/event/event.go+5−1
@@ -127,7 +127,11 @@ func pairsToProps(props ...any) posthog.Properties {
127127 }
128128
129129 for i := 0; i < len(props); i += 2 {
130− key := props[i].(string)
130+ key, ok := props[i].(string)
131+ if !ok {
132+ slog.Error("Event property key must be a string", "key", props[i], "index", i)
133+ continue
134+ }
131135 value := props[i+1]
132136 p = p.Set(key, value)
133137 }
internal/event/event_test.go+30
@@ -4,7 +4,10 @@ package event
44// scenarios. These tests will not log anything.
55
66import (
7+ "reflect"
78 "testing"
9+
10+ "github.com/posthog/posthog-go"
811)
912
1013func TestError(t *testing.T) {
@@ -59,6 +62,33 @@ func TestError(t *testing.T) {
5962 })
6063}
6164
65+func TestPairsToProps(t *testing.T) {
66+ t.Run("sets valid key value pairs", func(t *testing.T) {
67+ got := pairsToProps("foo", "bar", "count", 3)
68+ want := posthog.NewProperties().
69+ Set("foo", "bar").
70+ Set("count", 3)
71+ if !reflect.DeepEqual(got, want) {
72+ t.Fatalf("pairsToProps() = %#v, want %#v", got, want)
73+ }
74+ })
75+
76+ t.Run("returns empty properties for odd pairs", func(t *testing.T) {
77+ got := pairsToProps("foo", "bar", "count")
78+ if len(got) != 0 {
79+ t.Fatalf("pairsToProps() should return empty properties, got %#v", got)
80+ }
81+ })
82+
83+ t.Run("ignores non-string key and continues", func(t *testing.T) {
84+ got := pairsToProps(123, "bad", "ok", true)
85+ want := posthog.NewProperties().Set("ok", true)
86+ if !reflect.DeepEqual(got, want) {
87+ t.Fatalf("pairsToProps() = %#v, want %#v", got, want)
88+ }
89+ })
90+}
91+
6292// newDefaultTestError creates a test error that mimics runtime panic
6393// errors. This helps us testing that the Error function can handle various
6494// error types, including those that might be passed from a panic recovery