Skip to content

NullPointerException in ServerSentEvent when trying to set id or event properties #36634

@drzpk

Description

@drzpk

This commit added a guard against invalid characters in SSE. It triggers null pointer exceptions when null values are used in id and event properties:

java.lang.NullPointerException: Cannot invoke "String.indexOf(int)" because "content" is null
	at org.springframework.http.codec.ServerSentEvent$BuilderImpl.checkEvent(ServerSentEvent.java:270)
	Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below: 
Original Stack Trace:
		at org.springframework.http.codec.ServerSentEvent$BuilderImpl.checkEvent(ServerSentEvent.java:270)
		at org.springframework.http.codec.ServerSentEvent$BuilderImpl.id(ServerSentEvent.java:257)
		at io.modelcontextprotocol.server.transport.WebFluxStreamableServerTransportProvider$WebFluxStreamableMcpSessionTransport.lambda$sendMessage$1(WebFluxStreamableServerTransportProvider.java:361)
		at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:196)

These properties are marked as @Nullable so it should be possible to use null values in the builder.

I came across this issue after updating spring-web to 6.2.17, before that I was using 6.2.10 without any problems.

For context, I have a Spring-AI-based MCP server with streamable HTTP transport (spring-ai:1.1.4). Spring AI 2.x introduced a fix on their end, but for those stuck with 1.x for now, so the workaround is to use SSE transport instead of streamable HTTP.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: regressionA bug that is also a regression

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions