Skip to content

lua: bound MessagePack reads when pushing values to Lua - #12597

Open
edsiper wants to merge 2 commits into
masterfrom
fix/lua-pushmpack-bounds
Open

edsiper wants to merge 2 commits into
masterfrom
fix/lua-pushmpack-bounds

Conversation

@edsiper

@edsiper edsiper commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Problem

flb_lua_pushmpack() pushes a MessagePack value read with mpack onto the Lua
stack. For strings, binaries and extensions it advanced reader->data by the
declared length without checking it against the remaining data, and it
ignored reader errors. A truncated value read past the end of the buffer.
Nesting had no limit, and nested tables were created without growing the Lua
stack.

Change

  • Read the bytes with mpack_read_bytes_inplace(), which checks the length,
    and fail on any reader error.
  • Limit nesting to 512 levels, the same limit used for JSON packing.
  • Grow the Lua stack for every nested table.
  • On failure return -1 and leave the Lua stack as it was.

Valid input decodes as before. The function keeps its signature.

Testing

New lua_pushmpack_invalid case in tests/internal/lua.c:

  • a truncated string;
  • an array missing elements;
  • a truncated binary inside a map;
  • 512 and 513 levels of nesting;
  • a valid value after the rejected ones.

It fails on master and passes with this change.

ctest --test-dir build -R lua                          # 2/2 passed
valgrind --leak-check=full build/bin/flb-it-lua         # clean
pytest scenarios/filter_lua                             # 9 passed
VALGRIND=1 VALGRIND_STRICT=1 pytest scenarios/filter_lua  # 9 passed, no errors

Summary by CodeRabbit

  • Bug Fixes
    • Invalid or incomplete MessagePack data now fails cleanly without leaving partial decoded data behind.
    • Deeply nested input is handled with a safety limit, while valid data within that limit continues to decode successfully.
    • Decoding remains usable after an invalid input is rejected.

flb_lua_pushmpack() read strings, binaries and extensions by advancing the
reader position by the declared length, without checking it against the
remaining data, and ignored reader errors, so truncated input read past
the end of the buffer. Nesting was not limited and the Lua stack was not
grown for nested tables.

Read the bytes through the reader, which checks the length, fail on reader
errors, limit nesting to 512 levels as JSON packing does, grow the Lua
stack for every nested table, and leave the stack unchanged on failure.

Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T20:39:19.948801Z 2c64e63 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 74fbf10c-b5a7-4262-833c-028450067f4b
📥 Commits

Reviewing files that changed from the base of the PR and between f5374f7 and 2c64e63.

📒 Files selected for processing: 2
  • src/flb_lua.c
  • tests/internal/lua.c

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

📝 Walkthrough

MessagePack decoding into Lua now uses a recursive helper with a 512-level nesting limit. It checks reader and Lua stack errors, propagates failures, and restores the original Lua stack state when decoding fails. Tests cover truncated inputs and the nesting boundary.

Changes: MessagePack-to-Lua Decoding

Layer / File(s) Summary
Recursive decoding and failure handling
src/flb_lua.c
A recursive helper decodes values with a 512-level depth limit. The decoder checks reader errors and Lua stack capacity. The public wrapper restores the original Lua stack top if decoding fails.
Decoder failure and depth tests
tests/internal/lua.c
Tests cover truncated inputs, the 512- and 513-level nesting cases, unchanged stack state after failures, and valid decoding after a failure.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 2c64e

No merge-blocking issue was identified. The decoder’s failure handling and depth limit are covered by the new tests.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: adding bounds to MessagePack reads when pushing values into Lua.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 2
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch was successfully deployed

1 active deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant