We're using cjson.safe in an OpenResty/Lua environment where we execute arbitrary user-provided Lua code in a sandboxed environment. Currently, we're only exposing cjson.array_mt to the sandbox, but we're considering whether it's safe to expose cjson.encode() and cjson.decode().
Our sandbox setup:
- Using
setfenv() to restrict the execution environment to a limited set of allowed functions (string, table, math operations, etc.)
- Execution time limits to prevent infinite loops or long-running code
- Currently only exposing
cjson.array_mt (not encode/decode)
Our concerns:
- Memory exhaustion: Can malicious JSON input cause unbounded memory allocation?
- Resource limits: Are there built-in limits on encode/decode buffer sizes?
- Exception handling: Does
cjson.safe properly handle errors without throwing exceptions that could break the sandbox?
- Best practices: What are the recommended practices for safely exposing JSON encoding/decoding in sandboxed environments?
Questions:
- Are
cjson.safe.encode() and cjson.safe.decode() designed to be safe for sandbox environments?
- Are there any known security vulnerabilities or resource exhaustion risks?
- Should we add additional size limits or validation when exposing these functions?
- Are there any configuration options we should set for sandbox safety?
Environment:
- OpenResty with LuaJIT
- Using
cjson.safe module
- Lua 5.1 compatibility