Repository navigation
Description
Discovered while fixing #5950. That fix deliberately left this alone, because it needs a design decision rather than a mechanical change.
What happens
ResponseFilteringWriter.writeErrorResponse (pkg/authz/response_filter.go) is reached from two places. On the JSON path it works. On the SSE path — writeSSEDataLine, via the two call sites around lines 237 and 241 — it writes a bare JSON object directly into a text/event-stream body, with no data: prefix and no blank-line terminator.
Per the WHATWG event-stream grammar, that line parses as a field named {"jsonrpc" with the rest as its value. An unrecognized field is ignored. So the client never sees the error at all: it sees a stream that simply ends with no response to its request, and then hangs until its own timeout expires.
That is worse than a malformed error, and worse for diagnosis than obvious garbage would be, because nothing in the stream indicates a failure occurred.
Two compounding problems in the same function:
- The
WriteHeader(http.StatusInternalServerError)is usually a no-op here. By the timewriteSSEDataLineruns, earlier lines have already been written throughrfw.ResponseWriter, andFlush()(around line 125) may already have committed the headers, so Go logssuperfluous response.WriteHeader calland drops it. Even when it does land,Content-Typeis stilltext/event-streamwith a JSON body, so an SSE client ignores it regardless. writeSSEDataLinereturnsstop=true, soprocessSSEResponsereturns immediately and skips the linesep reconciliation further down — the very code whose comment says it exists so as not to break SSE parsing. If truncation lands between adata:line and its terminating blank line, the client holds a partial event indefinitely.
Why it matters
The SSE path is the common transport for streamable HTTP, so this is the realistic branch rather than an exotic one. It fires when response filtering fails — a malformed tools list from a backend, or a frame carrying a result outside a clean Response — so it is low-frequency but not theoretical. A hang is the worst of the available failure modes, because the caller cannot distinguish it from a dead server.
Note that #5950 makes the payload itself conformant JSON-RPC. That is an improvement everywhere except here, where it means the bytes now look correct to anyone grepping for the bug while remaining unreadable by any SSE client. Closed PR #5955 was a live demonstration of that trap.
What needs deciding
Not obvious, hence a separate issue:
- Emit
data: <json>\n\nand terminate the stream cleanly? - Abandon the stream instead, and if so how does the client tell that apart from success?
- The filter has already failed closed at this point, so what does the client actually deserve — an error it can correlate, or just a clean close?
Whatever the answer, the mid-stream WriteHeader should go, and the truncation interaction with the linesep reconciliation needs handling.
Activity
Frame SSE filter-failure errors as SSE events
Parse SSE responses per event, not per line
Tighten SSE filter comments and close test gaps
Close two SSE filter bypasses found in review
Close two SSE filter bypasses found in review
Frame SSE filter-failure errors as SSE events (#6087)
Parse SSE responses per event, not per line
Tighten SSE filter comments and close test gaps
Close two SSE filter bypasses found in review
Match the client's SSE line model, not the spec's
Match the client's SSE line model, not the spec's
Parse SSE responses per event, not per line (#6088)