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:
@@ -709,11 +709,11 @@ AMFDecoder::AMFDecoder(const BufferLikeString &buf_in, size_t pos_in, int versio
|
||||
}
|
||||
|
||||
std::string amfLoadLeadingString(AMFDecoder &dec) {
|
||||
while (dec.remain()) {
|
||||
auto val = dec.load<AMFValue>();
|
||||
if (val.type() == AMF_STRING) {
|
||||
return val.as_string();
|
||||
}
|
||||
// Some non-standard publishers prepend two numbers to metadata. Only skip
|
||||
// that known, fixed-size anomaly: decoding arbitrary values here can turn a
|
||||
// compact strict array into a large AMFValue allocation before it is ignored.
|
||||
for (size_t i = 0; i < 2 && dec.remain() && dec.version == 0 && dec.front() == AMF0_NUMBER; ++i) {
|
||||
dec.load<double>();
|
||||
}
|
||||
return "";
|
||||
return dec.remain() ? dec.load<std::string>() : "";
|
||||
}
|
||||
|
||||
@@ -92,6 +92,8 @@ public:
|
||||
// Number of bytes not yet parsed
|
||||
size_t remain() const;
|
||||
|
||||
friend std::string amfLoadLeadingString(AMFDecoder &dec);
|
||||
|
||||
private:
|
||||
class DepthGuard {
|
||||
public:
|
||||
@@ -122,10 +124,10 @@ private:
|
||||
static constexpr size_t kMaxDepth = 64;
|
||||
};
|
||||
|
||||
// 加载首个字符串, 自动跳过前置的非字符串AMF值; 若不存在字符串则返回空字符串
|
||||
// 加载首个字符串, 自动跳过最多两个前置AMF0 number; 若没有数据则返回空字符串
|
||||
// 兼容某些不规范的推流器(例如宇视摄像机在@setDataFrame前插入多余的AMF number)
|
||||
// Load the first string, skipping any leading non-string AMF values;
|
||||
// returns an empty string if the stream contains none.
|
||||
// Load the first string, skipping at most two leading AMF0 numbers; returns an
|
||||
// empty string if the stream contains no data.
|
||||
// Compatible with some non-standard publishers (e.g. Uniview cameras insert
|
||||
// redundant AMF numbers before @setDataFrame)
|
||||
std::string amfLoadLeadingString(AMFDecoder &dec);
|
||||
|
||||
@@ -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