mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-11-24 00:47:21 +08:00
fix(rtmp): bound leading AMF compatibility parsing (#4842)
### Motivation - Prevent a memory-amplification DoS in RTMP DATA/CMD handling where the compatibility helper decoded arbitrary leading AMF values (including large strict arrays) while searching for the first string. - Support the observed non-standard publisher case (two leading AMF0 numbers) while avoiding unbounded recursive decoding and attacker-controlled allocations. ### Description - Replace the unbounded `amfLoadLeadingString()` behavior that repeatedly `load<AMFValue>()` with a bounded compatibility rule that skips at most two AMF0 `Number` values and then requires a string (`amfLoadLeadingString` in `src/Rtmp/amf.cpp` and declaration in `src/Rtmp/amf.h`). - Add `friend` linkage for the helper in `AMFDecoder` and update header comments to document the tightened compatibility semantics. - Extend `tests/test_amf.cpp` with regression checks covering: zero/one/two leading numbers, rejection of complex leading values (e.g. strict array), excessive leading numbers, and AMF3 string handling. - No new source files added and existing file UTF-8 BOMs were preserved. ### Testing - Ran configuration and unit build: `cmake -S . -B build -DENABLE_TESTS=ON -DCMAKE_BUILD_TYPE=Debug` then `cmake --build build --target test_amf -j8` and the build completed successfully. - Executed the test binary via CTest: `ctest --test-dir build -R '^test_amf$' --output-on-failure` and `test_amf` passed (1/1 tests passed). - Ran `git diff --check` and ensured no trailing whitespace or check errors after changes. ------ [Codex Task](https://chatgpt.com/codex/cloud/tasks/task_e_6abb8adc6f5c8320a84e8b198e28f68b)
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "Network/Buffer.h"
|
||||
#include "Rtmp/amf.h"
|
||||
@@ -93,6 +94,39 @@ static bool rejects_excessive_nesting(ContainerType type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool accepts_leading_numbers(size_t count) {
|
||||
string data;
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
data.append("\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9);
|
||||
}
|
||||
data.append("\x02\x00\x0AonMetaData", 13);
|
||||
BufferLikeString input(std::move(data));
|
||||
AMFDecoder decoder(input, 0);
|
||||
return amfLoadLeadingString(decoder) == "onMetaData";
|
||||
}
|
||||
|
||||
static bool rejects_leading_value(const string &value) {
|
||||
BufferLikeString input(value + string("\x02\x00\x0AonMetaData", 13));
|
||||
AMFDecoder decoder(input, 0);
|
||||
try {
|
||||
amfLoadLeadingString(decoder);
|
||||
} catch (const runtime_error &ex) {
|
||||
return string(ex.what()) == "Expected a string";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool accepts_amf3_string(bool switched_from_amf0) {
|
||||
string data;
|
||||
if (switched_from_amf0) {
|
||||
data.push_back('\x11');
|
||||
}
|
||||
data.append("\x06\x15onMetaData", 12);
|
||||
BufferLikeString input(std::move(data));
|
||||
AMFDecoder decoder(input, 0, switched_from_amf0 ? 0 : 3);
|
||||
return amfLoadLeadingString(decoder) == "onMetaData";
|
||||
}
|
||||
|
||||
int main() {
|
||||
const ContainerType types[] = {
|
||||
ContainerType::Object,
|
||||
@@ -111,5 +145,28 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t count = 0; count <= 2; ++count) {
|
||||
if (!accepts_leading_numbers(count)) {
|
||||
cerr << "AMF metadata with " << count << " leading numbers was not decoded" << endl;
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
// Only the observed, bounded compatibility prefix is accepted. In
|
||||
// particular, complex values must not be materialized just to find a later
|
||||
// string, and an arbitrary number of scalar values must not be skipped.
|
||||
if (!rejects_leading_value(string("\x0A\x00\x00\x00\x01\x05", 6))) {
|
||||
cerr << "complex leading AMF value was not rejected" << endl;
|
||||
return 4;
|
||||
}
|
||||
if (!rejects_leading_value(string(27, '\0'))) {
|
||||
cerr << "more than two leading AMF numbers were not rejected" << endl;
|
||||
return 5;
|
||||
}
|
||||
if (!accepts_amf3_string(false) || !accepts_amf3_string(true)) {
|
||||
cerr << "standard AMF3 string was not decoded" << endl;
|
||||
return 6;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user