@marcel91200 / caddy Mirror · read-only / commits / 70998929
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 23:30 UTC

core: Check for nil event origin (#7047)

* fix: crash - null check on event origin * chore: use accessor instead of property

Laurin committed Jun 5, 2025 at 21:10 UTC 7099892958fbee17f5e087c768f4db1940303fa8
2 files changed +26 -1
caddy.go
+7 -1
@@ -1104,9 +1104,15 @@ func (e Event) Origin() Module { return e.origin } // Returns the module t
1104 // CloudEvents spec.
1105 func (e Event) CloudEvent() CloudEvent {
1106 dataJSON, _ := json.Marshal(e.Data)
1107 + var source string
1108 + if e.Origin() == nil {
1109 + source = "caddy"
1110 + } else {
1111 + source = string(e.Origin().CaddyModule().ID)
1112 + }
1113 return CloudEvent{
1114 ID: e.id.String(),
1109 - Source: e.origin.CaddyModule().String(),
1115 + Source: source,
1116 SpecVersion: "1.0",
1117 Type: e.name,
1118 Time: e.ts,
caddy_test.go
+19
@@ -15,6 +15,7 @@
15 package caddy
16
17 import (
18 + "context"
19 "testing"
20 "time"
21 )
@@ -72,3 +73,21 @@ func TestParseDuration(t *testing.T) {
73 }
74 }
75 }
76 +
77 +func TestEvent_CloudEvent_NilOrigin(t *testing.T) {
78 + ctx, _ := NewContext(Context{Context: context.Background()}) // module will be nil by default
79 + event, err := NewEvent(ctx, "started", nil)
80 + if err != nil {
81 + t.Fatalf("NewEvent() error = %v", err)
82 + }
83 +
84 + // This should not panic
85 + ce := event.CloudEvent()
86 +
87 + if ce.Source != "caddy" {
88 + t.Errorf("Expected CloudEvent Source to be 'caddy', got '%s'", ce.Source)
89 + }
90 + if ce.Type != "started" {
91 + t.Errorf("Expected CloudEvent Type to be 'started', got '%s'", ce.Type)
92 + }
93 +}