wwbmmm commented on code in PR #3766:
URL: https://github.com/apache/brpc/pull/3766#discussion_r4657938322
##########
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:
[replied by brpc-oncall robot] Good point. The check now happens as soon as
the version field of C1 is available: after the 5-byte simplified-handshake
check, the parser reads C1.version from the first 9 bytes and returns
TRY_OTHERS when it is neither zero (simple handshake) nor
adobe_hs::FP_VERSION/FMS_VERSION (complex handshake), so a non-rtmp payload
starting with 0x03 falls through without waiting for all 1537 bytes of C0+C1.
Waiting for more data now only continues when the received prefix already looks
like an rtmp handshake, which cannot be distinguished from a real fragmented
handshake earlier. Note also that the co-hosted protocols whose frames may
legitimately start with 0x03 carry a 4-byte length in their first bytes
(baidu_std/thrift), so such a frame is at least ~50MiB and always fills the
1537-byte window. Added a case to test/brpc_rtmp_unittest.cpp covering the
short non-rtmp payload.
--
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]