Skip to content

Validate C1 before claiming a connection as rtmp - #3766

Open
wwbmmm wants to merge 1 commit into
apache:masterfrom
wwbmmm:oncall/req-20261121-025731-3
Open

wwbmmm wants to merge 1 commit into
apache:masterfrom
wwbmmm:oncall/req-20261121-025731-3

Conversation

@wwbmmm

@wwbmmm wwbmmm commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve N/A

Problem Summary:

IsPossiblyRtmp claimed any connection whose first byte is 0x03 (the rtmp C0 version). RTMP is sniffed before thrift/http/redis/nshead and ParseRtmpMessage permanently binds the parsing context once it matches, so a frame of another protocol co-hosted on the same port that happens to start with 0x03 (for example a thrift request) was captured by the rtmp handshake state machine and the connection was broken.

What is changed and the side effects?

Changed:

  • IsPossiblyRtmp now requires the whole 1537-byte C0+C1 before claiming the connection and validates the version field of C1: zero for the simple handshake, adobe_hs::FP_VERSION or adobe_hs::FMS_VERSION for the complex one. Fewer bytes return NOT_ENOUGH_DATA and an unsupported version returns TRY_OTHERS, so the other protocols sharing the port get a chance.
  • Simplified rtmp (C0 followed by the "BDMS" magic number) is still recognized from the first 5 bytes, because it carries no C1.
  • Reordering the sniff list was not chosen: another protocol would then be able to capture a real rtmp handshake, and validating the content keeps the decision local to the rtmp parser.
  • The handshake path itself (adobe_hs::C1::Load and the fallback to simple handshaking) is untouched.

Side effects:

  • Performance effects: negligible. Only the first 9 bytes are inspected, and when the first byte is 0x03 the parser waits for 1537 buffered bytes before it either claims or rejects the connection.

  • Breaking backward compatibility: none. Client and server keeps working with simple, complex and simplified handshaking.


Check List:

Tests:

  • cmake -S . -B build -DBUILD_UNIT_TESTS=ON -DDOWNLOAD_GTEST=OFF -DBRPC_SYSTEM_GTEST_SOURCE_DIR=/usr/src/googletest && make -j6 brpc_rtmp_unittest
  • ./test/brpc_rtmp_unittest (36 cases, all passed). New cases: payload_starting_with_c0_version_tries_others, simple_handshake_accepted_after_c0c1_complete, complex_handshake_c1_is_accepted.

🤖 This PR was automatically created by brpc-oncall

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

An active moderate finding remains because incomplete RTMP-looking input can pin protocol selection and indefinitely block valid non-RTMP requests.

1 open issue
What changed in this PR

This PR improves RTMP protocol detection on shared ports by validating handshake data before claiming a connection, while preserving support for simplified, simple, and complex handshakes.

Changes:

  • Requires complete C0+C1 data and validates supported C1 version fields before selecting RTMP.
  • Preserves five-byte 03BDMS simplified-handshake detection.
  • Adds tests for ambiguous payloads, fragmented handshakes, and simple/complex handshake acceptance.
File Description
test/​brpc_rtmp_unittest.cpp Expands RTMP parser tests for handshake validation, protocol fallback, fragmentation, and simple/complex handshake acceptance.
src/​brpc/​policy/​rtmp_protocol.cpp Implements C1-aware RTMP sniffing while retaining simplified-handshake detection.

Review effort: Lite

Comment on lines +3487 to 3488
if (len < RTMP_HANDSHAKE_SIZE0 + RTMP_HANDSHAKE_SIZE1) {
return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
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