Files
ZLMediaKit/tests/test_amf.cpp

173 lines
5.1 KiB
C++
Raw Normal View History

fix: limit AMF decoder nesting depth (#4787) ### Motivation - Prevent unbounded recursive-descent parsing in the AMF decoder that allowed deeply nested AMF objects/arrays to exhaust the process stack and cause a pre-auth remote DoS. ### Description - Add an RAII depth guard `AMFDecoder::DepthGuard` and a per-decoder `depth` counter with `kMaxDepth = 64` to `AMFDecoder` in `src/Rtmp/amf.h`/`src/Rtmp/amf.cpp`. - Instrument `load_object()`, `load_ecma()`, and `load_arr()` to create a `DepthGuard` at entry so nesting is incremented and reliably decremented on return or exception, and throw `std::runtime_error("Maximum AMF nesting depth exceeded")` when the limit is exceeded. - Add `tests/test_amf.cpp` to assert that a 64-level nested AMF container is accepted and a 65-level nested container is rejected with the expected error. - Keep non-nested parsing behavior unchanged; the guard only enforces a maximum container nesting depth. ### Testing - Configured the project with tests enabled using `cmake -S . -B build -DENABLE_TESTS=ON`, which generated the `test_amf` target successfully. - Built the test target with `cmake --build build --target test_amf` and performed smoke compiles of modified sources, with `src/Rtmp/amf.cpp` compiling to object (`/tmp/amf.o`) successfully. - Compiled the new test source with `c++ -std=gnu++11 -c tests/test_amf.cpp` successfully and verified `git diff --cached --check` passed. ------ [Codex Task](https://chatgpt.com/codex/cloud/tasks/task_e_6a63c9326b9883209c857c0f4fa5d0cc)
2026-07-27 07:56:18 -07:00
/*
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
*
* Use of this source code is governed by MIT-like license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#include <iostream>
#include <stdexcept>
#include <string>
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)
2026-10-06 20:57:48 -07:00
#include <utility>
fix: limit AMF decoder nesting depth (#4787) ### Motivation - Prevent unbounded recursive-descent parsing in the AMF decoder that allowed deeply nested AMF objects/arrays to exhaust the process stack and cause a pre-auth remote DoS. ### Description - Add an RAII depth guard `AMFDecoder::DepthGuard` and a per-decoder `depth` counter with `kMaxDepth = 64` to `AMFDecoder` in `src/Rtmp/amf.h`/`src/Rtmp/amf.cpp`. - Instrument `load_object()`, `load_ecma()`, and `load_arr()` to create a `DepthGuard` at entry so nesting is incremented and reliably decremented on return or exception, and throw `std::runtime_error("Maximum AMF nesting depth exceeded")` when the limit is exceeded. - Add `tests/test_amf.cpp` to assert that a 64-level nested AMF container is accepted and a 65-level nested container is rejected with the expected error. - Keep non-nested parsing behavior unchanged; the guard only enforces a maximum container nesting depth. ### Testing - Configured the project with tests enabled using `cmake -S . -B build -DENABLE_TESTS=ON`, which generated the `test_amf` target successfully. - Built the test target with `cmake --build build --target test_amf` and performed smoke compiles of modified sources, with `src/Rtmp/amf.cpp` compiling to object (`/tmp/amf.o`) successfully. - Compiled the new test source with `c++ -std=gnu++11 -c tests/test_amf.cpp` successfully and verified `git diff --cached --check` passed. ------ [Codex Task](https://chatgpt.com/codex/cloud/tasks/task_e_6a63c9326b9883209c857c0f4fa5d0cc)
2026-07-27 07:56:18 -07:00
#include "Network/Buffer.h"
#include "Rtmp/amf.h"
using namespace std;
using namespace toolkit;
enum class ContainerType {
Object,
EcmaArray,
StrictArray,
};
static const char *container_name(ContainerType type) {
switch (type) {
case ContainerType::Object:
return "object";
case ContainerType::EcmaArray:
return "ECMA array";
case ContainerType::StrictArray:
return "strict array";
}
return "unknown";
}
static AMFType container_amf_type(ContainerType type) {
switch (type) {
case ContainerType::Object:
return AMF_OBJECT;
case ContainerType::EcmaArray:
return AMF_ECMA_ARRAY;
case ContainerType::StrictArray:
return AMF_STRICT_ARRAY;
}
return AMF_UNDEFINED;
}
static string make_nested_container(ContainerType type, size_t depth) {
string result;
for (size_t i = 0; i < depth; ++i) {
switch (type) {
case ContainerType::Object:
result.append("\x03\x00\x01x", 4);
break;
case ContainerType::EcmaArray:
result.append("\x08\x00\x00\x00\x01\x00\x01x", 8);
break;
case ContainerType::StrictArray:
result.append("\x0A\x00\x00\x00\x01", 5);
break;
}
}
result.append("\x05", 1);
if (type != ContainerType::StrictArray) {
for (size_t i = 0; i < depth; ++i) {
result.append("\x00\x00\x09", 3);
}
}
return result;
}
static bool accepts_nesting_limit(ContainerType type) {
BufferLikeString input(make_nested_container(type, 64));
AMFDecoder decoder(input, 0);
try {
return decoder.load<AMFValue>().type() == container_amf_type(type);
} catch (const runtime_error &) {
return false;
}
}
static bool rejects_excessive_nesting(ContainerType type) {
BufferLikeString input(make_nested_container(type, 65));
AMFDecoder decoder(input, 0);
try {
decoder.load<AMFValue>();
} catch (const runtime_error &ex) {
return string(ex.what()) == "Maximum AMF nesting depth exceeded";
}
return false;
}
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)
2026-10-06 20:57:48 -07:00
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";
}
fix: limit AMF decoder nesting depth (#4787) ### Motivation - Prevent unbounded recursive-descent parsing in the AMF decoder that allowed deeply nested AMF objects/arrays to exhaust the process stack and cause a pre-auth remote DoS. ### Description - Add an RAII depth guard `AMFDecoder::DepthGuard` and a per-decoder `depth` counter with `kMaxDepth = 64` to `AMFDecoder` in `src/Rtmp/amf.h`/`src/Rtmp/amf.cpp`. - Instrument `load_object()`, `load_ecma()`, and `load_arr()` to create a `DepthGuard` at entry so nesting is incremented and reliably decremented on return or exception, and throw `std::runtime_error("Maximum AMF nesting depth exceeded")` when the limit is exceeded. - Add `tests/test_amf.cpp` to assert that a 64-level nested AMF container is accepted and a 65-level nested container is rejected with the expected error. - Keep non-nested parsing behavior unchanged; the guard only enforces a maximum container nesting depth. ### Testing - Configured the project with tests enabled using `cmake -S . -B build -DENABLE_TESTS=ON`, which generated the `test_amf` target successfully. - Built the test target with `cmake --build build --target test_amf` and performed smoke compiles of modified sources, with `src/Rtmp/amf.cpp` compiling to object (`/tmp/amf.o`) successfully. - Compiled the new test source with `c++ -std=gnu++11 -c tests/test_amf.cpp` successfully and verified `git diff --cached --check` passed. ------ [Codex Task](https://chatgpt.com/codex/cloud/tasks/task_e_6a63c9326b9883209c857c0f4fa5d0cc)
2026-07-27 07:56:18 -07:00
int main() {
const ContainerType types[] = {
ContainerType::Object,
ContainerType::EcmaArray,
ContainerType::StrictArray,
};
for (auto type : types) {
if (!accepts_nesting_limit(type)) {
cerr << "AMF " << container_name(type) << " at the nesting limit was not decoded" << endl;
return 1;
}
if (!rejects_excessive_nesting(type)) {
cerr << "AMF " << container_name(type) << " beyond the nesting limit was not rejected" << endl;
return 2;
}
}
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)
2026-10-06 20:57:48 -07:00
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;
}
fix: limit AMF decoder nesting depth (#4787) ### Motivation - Prevent unbounded recursive-descent parsing in the AMF decoder that allowed deeply nested AMF objects/arrays to exhaust the process stack and cause a pre-auth remote DoS. ### Description - Add an RAII depth guard `AMFDecoder::DepthGuard` and a per-decoder `depth` counter with `kMaxDepth = 64` to `AMFDecoder` in `src/Rtmp/amf.h`/`src/Rtmp/amf.cpp`. - Instrument `load_object()`, `load_ecma()`, and `load_arr()` to create a `DepthGuard` at entry so nesting is incremented and reliably decremented on return or exception, and throw `std::runtime_error("Maximum AMF nesting depth exceeded")` when the limit is exceeded. - Add `tests/test_amf.cpp` to assert that a 64-level nested AMF container is accepted and a 65-level nested container is rejected with the expected error. - Keep non-nested parsing behavior unchanged; the guard only enforces a maximum container nesting depth. ### Testing - Configured the project with tests enabled using `cmake -S . -B build -DENABLE_TESTS=ON`, which generated the `test_amf` target successfully. - Built the test target with `cmake --build build --target test_amf` and performed smoke compiles of modified sources, with `src/Rtmp/amf.cpp` compiling to object (`/tmp/amf.o`) successfully. - Compiled the new test source with `c++ -std=gnu++11 -c tests/test_amf.cpp` successfully and verified `git diff --cached --check` passed. ------ [Codex Task](https://chatgpt.com/codex/cloud/tasks/task_e_6a63c9326b9883209c857c0f4fa5d0cc)
2026-07-27 07:56:18 -07:00
return 0;
}