Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix: validate event and id fields in format_sse_event to prevent SSE protocol injection#15651

Closed
hemanthvnp wants to merge 2 commits into
fastapi:masterfrom
hemanthvnp:fix/validate-format-sse-event-fields
Closed

Fix: validate event and id fields in format_sse_event to prevent SSE protocol injection#15651
hemanthvnp wants to merge 2 commits into
fastapi:masterfrom
hemanthvnp:fix/validate-format-sse-event-fields

Conversation

@hemanthvnp

Copy link
Copy Markdown

Closes the gap left by #15588.

ServerSentEvent already validates event and id for newlines and
null characters, but the public format_sse_event() function did not
apply the same checks, allowing SSE protocol injection when
user-controlled input is passed directly:

format_sse_event(event="legit\ndata: injected", data_str="safe")
# → b'event: legit\ndata: injected\ndata: safe\n\n'

fastapi/sse.pycall the two existing validators at the top of
format_sse_event():


_check_event_single_line(event)
_check_id_valid(id)
(The helpers already guard against None, so no extra if wrappers
are needed.)

tests/test_sse.pyeight new parametrised tests covering \n,
\r, \r\n, and \0 for both event and id, plus an end-to-end
injection scenario. All 32 tests pass.

Discussed in: https://github.com/fastapi/fastapi/discussions/15648

…protocol injection

PR fastapi#15588 added validation to ServerSentEvent, but the public
format_sse_event() function accepted multi-line values for event and id
without raising an error. A newline in either field splits into extra
SSE lines, allowing callers who pass user-supplied data directly to
format_sse_event() to unintentionally inject arbitrary SSE fields.

Reuse the existing _check_event_single_line and _check_id_valid helpers
at the top of format_sse_event so the two entry points enforce the same
rules. Add eight parametrised tests covering \n, \r, \r\n and \0 in
both fields, plus an end-to-end injection scenario.
Copilot AI review requested due to automatic review settings May 31, 2026 05:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR strengthens Server-Sent Events (SSE) safety by validating event and id inputs in format_sse_event, and adds regression tests to ensure newline/null-character injection is rejected.

Changes:

  • Add protocol-safety validation for event (single-line) and id (single-line + no null chars) in format_sse_event.
  • Update format_sse_event documentation to reflect the validation rules.
  • Add tests covering multiline and null-character rejection cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
fastapi/sse.py Documents and enforces validation rules for event/id in format_sse_event.
tests/test_sse.py Adds validation tests ensuring malformed event/id values raise ValueError.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread fastapi/sse.py
Comment on lines +211 to +212
_check_event_single_line(event)
_check_id_valid(id)
Comment thread tests/test_sse.py
Comment on lines +345 to +347
def test_format_sse_event_rejects_null_id():
with pytest.raises(ValueError, match="null"):
format_sse_event(id="has\0null")
@codspeed-hq

codspeed-hq Bot commented May 31, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing hemanthvnp:fix/validate-format-sse-event-fields (d6ef333) with master (1990ecb)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (ee22a4b) during the generation of this report, so 1990ecb was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@hemanthvnp

Copy link
Copy Markdown
Author

Could a maintainer please add the appropriate label so the check-labels CI passes? Thank you!

@hemanthvnp

Copy link
Copy Markdown
Author

Closing this as I opened it prematurely based on a non-team-member reply. Will re-open once a team member confirms in the discussion. Apologies for the process misstep.

@hemanthvnp hemanthvnp closed this May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants