fix(libmsg): complete security audit round 2 - fix P0 race + harden input validation

- P0: fix _close_all_clients() race condition (CWE-362) - hold per-connection send_lock
  before close() to prevent messages being sent to wrong client after fd reuse
- P2: add comprehensive input validation for all public APIs
  (timeout/port/workers/max_connections/batch_size etc. bounds checking)
- P2: add UTF-8 payload validation (isValidUtf8) aligning with Python pymsg behavior,
  rejecting overlong encodings, surrogate pairs, truncated/invalid sequences
- P2: document pmsg_recv(int) precondition and recommend deadline mode
- add constructor validation for MsgClient/MsgServer/AsyncQueue/RpcClient/RpcServer
- add add_route() validation for empty route name and null handler
- update security audit report with round 2 findings and fixes (8 issues total, 100% fixed)
This commit is contained in:
qing committed 2026-08-07 21:01:34 +08:00
1 parent 9256a8157b
commit 3c50a862d3
6 files changed
+351 -76

No files matched your search

+16 -1
View File
@@ -41,6 +41,8 @@
#include <exception>
#include <functional>
#include <mutex>
#include <stdexcept>
#include <string>
#include <thread>
#include <utility>
#include <vector>
@@ -87,7 +89,20 @@ public:
_running(false),
_stop(false),
_wakeup(false),
_dropped(0) {}
_dropped(0) {
if (!_handler) {
throw std::invalid_argument("AsyncQueue: handler must not be null");
}
if (config.batch_size == 0) {
throw std::invalid_argument("AsyncQueue: batch_size must be > 0");
}
if (config.flush_interval <= 0 || config.flush_interval > 300.0) {
throw std::invalid_argument("AsyncQueue: flush_interval must be in (0, 300] seconds");
}
if (config.poll_timeout <= 0 || config.poll_timeout > 5.0) {
throw std::invalid_argument("AsyncQueue: poll_timeout must be in (0, 5] seconds");
}
}
/// 析构函数:自动 shutdown(),保证队列中剩余数据被 flush。
~AsyncQueue() { shutdown(); }