### 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)
## 问题 / Problem
部分摄像机(实测:宇视/Uniview)推流时,在 `@setDataFrame` 之前插入了 **2 个多余的 AMF0 number(共
18 字节)**。抓包结果:
Some publishers (verified with a Uniview camera) insert **two redundant
AMF0 numbers (18 bytes)** before `@setDataFrame`. Captured bytes:
```
44 00 00 28 00 00 a9 12 <- chunk header, type 0x12 (AMF0_DATA), body_size 169
00 0000000000000000 <- AMF0 number 0.0 (多余 / redundant)
00 0000000000000000 <- AMF0 number 0.0 (多余 / redundant)
02 00 0d "@setDataFrame" <- 按标准应从这里开始 / message should start here
02 00 0a "onMetaData" ...
```
`RtmpSession::onRtmpChunk()` 在 `MSG_DATA` 分支中无条件把首个 AMF 元素当作字符串读取,遇到
`0x00`(number)标记时 `amf.cpp` 抛出 `Expected a string`,异常未被捕获,推流连接被断开。
`RtmpSession::onRtmpChunk()` reads the first AMF element as a string
unconditionally in the `MSG_DATA` branch. With a `0x00` (number) marker,
`amf.cpp` throws `Expected a string`; the exception is not caught and
the publisher is dropped.
nginx-rtmp 可以正常接收同一路流,ZLMediaKit 无法接收。
nginx-rtmp accepts the very same stream; ZLMediaKit does not.
### 修复前日志 / Log before the fix
```
RtmpSession.cpp:130 | publish 回复时间:2ms
TcpServer.cpp:210 | on err: 6(Expected a string)
RtmpSession.cpp:28 | RTMP推流器(__defaultVhost__/live/stream)断开:Expected a string,耗时(s):0
```
## 修改内容 / Changes
新增 `amfLoadLeadingString()`,跳过前置的非字符串 AMF 值后再取首个字符串。该写法参考了 `FlvSplitter`
中已有的防御式解析(先 `load<AMFValue>()`
再判断类型),并把它统一成一个公共函数。`AMFDecoder::remain()` 用于限定循环边界,避免缓冲区读完后再次抛异常。
Add `amfLoadLeadingString()`, which skips leading non-string AMF values
before returning the first string. This generalizes the defensive
parsing already present in `FlvSplitter` (`load<AMFValue>()` followed by
a type check) into one shared helper. `AMFDecoder::remain()` bounds the
loop so it does not throw once the buffer is exhausted.
- `amf.h` / `amf.cpp` — 新增 `remain()` 与 `amfLoadLeadingString()` / add
`remain()` and `amfLoadLeadingString()`
- `RtmpSession.cpp` — 推流侧,本 issue 的根因 / publish side, the root cause
here
- `RtmpPlayer.cpp` — 拉流侧存在同样的崩溃风险 / pull side has the identical failure
mode
- `FlvSplitter.cpp` — 原本不会断开,但会静默丢弃 metadata;现在能正确解析 / did not
disconnect before, but silently dropped the metadata; now parses it
## 验证 / Verification
用真实摄像机推流抓取了 17.8 MB 原始 RTMP 字节流,分别回放到修改前后的 MediaServer:
A 17.8 MB raw RTMP capture from the live camera was replayed against
MediaServer before and after the change:
| 场景 / Scenario | 修改前 / Before | 修改后 / After |
|---|---|---|
| 原始流(含多余 AMF 值)/ raw stream with redundant AMF values | `Expected a
string`,推流被断开 / dropped | 全部回放完成,无断开 / replayed fully, no disconnect |
| 去掉那 18 字节的标准流 / same stream with the 18 bytes removed | 正常 / OK | 正常 /
OK(回归测试 / regression) |
对照实验:**仅**从原始抓包中删除那 18 字节,其余字节完全不变,修改前的版本即可正常收流 —— 说明问题确实由这段多余的 AMF 值引起。
Control experiment: removing **only** those 18 bytes from the capture,
leaving every other byte untouched, makes the unpatched build work —
confirming those bytes are the cause.
修复后的日志 / Log after the fix:
```
MultiMediaSourceMuxer.cpp | stream: rtmp://__defaultVhost__/live/stream , codec info: H264[1920/1080/25]
MediaSource.cpp | 媒体注册:rtmp://__defaultVhost__/live/stream
MediaSource.cpp | 媒体注册:rtsp/hls/ts/fmp4://__defaultVhost__/live/stream
```
摄像机实时推流也已验证:拉取 HTTP-FLV 后用 ffmpeg 解码无报错(H264 1920x1080 25fps)。
Also verified end to end with the live camera: pulling HTTP-FLV and
decoding it with ffmpeg produces no errors (H264 1920x1080 25fps).
## 行为变化说明 / Note on behavior change
`RtmpPlayer` 中被修改的分支同时处理
`MSG_CMD`。命令消息的首个元素本来就是字符串,正常情况下行为不变;但如果收到首元素非字符串的畸形命令消息,以前会断开连接,现在会打印
`can not support cmd:` 警告并继续。符合标准的流行为完全不变(已通过上面的回归测试验证)。
The modified branch in `RtmpPlayer` also handles `MSG_CMD`. The first
element of a command message is already a string, so normal behavior is
unchanged; for a malformed command message whose first element is not a
string, the connection used to be dropped and now a `can not support
cmd:` warning is logged instead. Standard-compliant streams are
unaffected, as shown by the regression test above.