Copilot commented on code in PR #3766:
URL: https://github.com/apache/brpc/pull/3766#discussion_r4652637059


##########
src/brpc/policy/rtmp_protocol.cpp:
##########
@@ -3453,12 +3453,44 @@ bool RtmpChunkStream::OnPause(const RtmpMessageHeader& 
mh,
 
 // ============== protocol handlers =============
 
+// Size of C0 plus the magic number of simplified rtmp.
+static const size_t RTMP_SNIFF_SIZE0 = RTMP_HANDSHAKE_SIZE0 + 
MAGIC_NUMBER_SIZE;
+// Size of C0 plus the time and version fields of C1.
+static const size_t RTMP_SNIFF_SIZE1 = RTMP_HANDSHAKE_SIZE0 + 8;
+
 inline ParseResult IsPossiblyRtmp(const butil::IOBuf* source) {
-    const char* p = (const char*)source->fetch1();
-    if (p == nullptr) {
+    // C0 alone is too weak a signature: any payload starting with 0x03(e.g. a
+    // thrift request) would be captured by the RTMP handshake and the
+    // connection is broken. So validate C1 as well before claiming the
+    // connection.
+    const size_t len = source->length();
+    if (len < RTMP_HANDSHAKE_SIZE0) {
+        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+    }
+    char buf[RTMP_SNIFF_SIZE1];
+    // `fetch' may return the internal buffer instead of copying to `buf'.
+    const char* const p = (const char*)source->fetch(
+        buf, std::min<size_t>(len, sizeof(buf)));
+    if ((uint8_t)p[0] != RTMP_DEFAULT_VERSION) {
+        return MakeParseError(PARSE_ERROR_TRY_OTHERS);
+    }
+    if (len < RTMP_SNIFF_SIZE0) {
+        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+    }
+    if (memcmp(p + RTMP_HANDSHAKE_SIZE0, SIMPLIFIED_RTMP_MAGIC_NUMBER,
+               MAGIC_NUMBER_SIZE) == 0) {
+        return MakeMessage(nullptr);
+    }
+    // Both handshakes supported by brpc need the whole C0+C1: the complex one
+    // is verified by adobe_hs::C1::Load, the simple one requires the version
+    // field of C1 to be zero.
+    if (len < RTMP_HANDSHAKE_SIZE0 + RTMP_HANDSHAKE_SIZE1) {
         return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);

Review Comment:
   Returning `PARSE_ERROR_NOT_ENOUGH_DATA` here pins RTMP as the socket's 
preferred handler, so `InputMessengerProcessor` will not probe 
thrift/http/redis on subsequent reads. A valid non-RTMP request that starts 
with `0x03` but is shorter than 1537 bytes can therefore sit buffered 
indefinitely while the client waits for its response; the other protocol only 
gets a chance if later traffic happens to fill the RTMP handshake size. Please 
provide a dispatch path that lets another handler claim a complete message 
before pinning RTMP, or otherwise bound this wait without regressing fragmented 
RTMP handshakes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to