Skip to content

Commit 4176fda

Browse files
committed
fix(rtmp): fail-fast on chunk stream desynchronization via repeated unknown data types
When a publisher sends corrupted data (e.g., zero-fill from encoder crash), the decoder would process multiple garbage packets with invalid data types before eventually hitting a null-header error. This adds early detection that closes the connection after 3 consecutive unknown data types, reducing wasted decode cycles on misaligned data and producing clearer diagnostics.
1 parent adfc7e9 commit 4176fda

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

common/src/main/java/org/red5/server/net/rtmp/codec/RTMPProtocolDecoder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public List<Object> decodeBuffer(RTMPConnection conn, IoBuffer buffer) {
120120
}
121121
int remaining;
122122
int iterations = 0;
123+
int consecutiveUnknownTypes = 0;
123124
final int maxIterations = buffer.remaining() + 1;
124125
while ((remaining = buffer.remaining()) > 0) {
125126
if (++iterations > maxIterations) {
@@ -148,6 +149,17 @@ public List<Object> decodeBuffer(RTMPConnection conn, IoBuffer buffer) {
148149
//log.trace("Has decoded object");
149150
if (decodedObject != null) {
150151
result.add(decodedObject);
152+
// detect chunk stream desynchronization via repeated unknown data types
153+
if (decodedObject instanceof Packet) {
154+
byte dataType = ((Packet) decodedObject).getHeader().getDataType();
155+
if (dataType < TYPE_CHUNK_SIZE || dataType > TYPE_AGGREGATE) {
156+
if (++consecutiveUnknownTypes >= 3) {
157+
throw new ProtocolException("Chunk stream desynchronization detected: " + consecutiveUnknownTypes + " consecutive unknown data types (last: 0x" + String.format("%02X", dataType) + ")");
158+
}
159+
} else {
160+
consecutiveUnknownTypes = 0;
161+
}
162+
}
151163
}
152164
} else if (state.canContinueDecoding()) {
153165
//log.trace("Can continue decoding");

0 commit comments

Comments
 (0)