Dispatcher panics when a failed delivery returns a response body larger than 768 bytes
Describe the bug
When a subscriber or trigger destination returns a non-2xx response with a body longer than 768 bytes, and the delivery has a dead letter sink (or a reply), the dispatcher panics with runtime error: index out of range in encoding/base64.(*Encoding).Encode.
dispatchExecutionInfoTransformers in pkg/kncloudevents/event_dispatcher.go caps the destination buffer at KnativeErrorDataExtensionMaxLength (1024) but encodes the whole response body into it:
encodedLen := base64.StdEncoding.EncodedLen(bodyLen)
if encodedLen > attributes.KnativeErrorDataExtensionMaxLength {
encodedLen = attributes.KnativeErrorDataExtensionMaxLength
}
encodedBuf := make([]byte, encodedLen)
base64.StdEncoding.Encode(encodedBuf, httpResponseBody)
base64.Encode always writes EncodedLen(len(src)) bytes, so it panics if the buffer is shorter than that.
Effects:
- In the in-memory channel dispatcher, the fanout runs each subscription in its own goroutine, so the panic is not recovered and the dispatcher process exits.
- In the broker filter,
net/httprecovers the panic, the connection is dropped, and the event never reaches the dead letter sink.
Expected behavior
The event goes to the dead letter sink, and knativeerrordata holds the base64-encoded response body truncated to 1024 characters.
To Reproduce
- Create a Trigger or Subscription with
delivery.deadLetterSinkset. - Point the subscriber at a service that returns HTTP 500 with a body of 2000 bytes (for example, an HTML error page).
- Send an event. The dispatcher logs
panic: runtime error: index out of rangeand the dead letter sink receives nothing.
The unit test case invalid destination with a large response body and delivery option in TestSendEvent reproduces this on main:
panic: runtime error: index out of range [3] with length 0
encoding/base64.(*Encoding).Encode(...)
knative.dev/eventing/pkg/kncloudevents.dispatchExecutionInfoTransformers(...)
pkg/kncloudevents/event_dispatcher.go:593
Knative release version
main (2153953). The same code has been in place since at least the kncloudevents refactor (#7092).
Additional context Bodies of 768 bytes or fewer are not affected, because their encoded length fits in the buffer.
Opened #9507 for this. /assign