Skip to content

[Security] Thread-safety: send callback truy cập TcpConnection từ V8 thread #14

Description

@JkarVN

Mô tả

`Response` `send_cb` được gọi từ V8 thread (qua JS `res.send()`), không phải Trantor event loop thread → tiềm ẩn data race khi truy cập `p_conn`.

Vị trí

  • `src/module/builtin/server/server.cpp:82-88` (lambda trong `setRecvMessageCallback`)

Nguyên nhân

```cpp
auto p_res = std::make_unique(
[p_conn](int32_t status, const std::map<std::string, std::string>& headers,
const std::vector<uint8_t>& body) {
std::string http_resp = zane::http::buildResponse(...);
p_conn->send(http_resp.data(), http_resp.size()); // ⚠️ có thể từ V8 thread
}
);
```
`res.send()` trong JS chạy trên V8 thread, không phải Trantor loop thread của `p_conn`. Trantor `TcpConnection` yêu cầu truy cập từ loop thread của nó; gọi từ thread khác là data race (trạng thái buffer, lifecycle).

Tác động

  • Data race → crash, memory corruption, gửi response sai connection.
  • Đặc biệt nguy hiểm khi kết hợp với issue UAF (Promise async): `send` có thể chạy rất lâu sau, trên thread khác.

Fix đề xuất

  • Bọc call về loop thread của connection:
    ```cpp
    p_conn->getLoop()->runInLoop(p_conn, http_resp {
    p_conn->send(http_resp.data(), http_resp.size());
    });
    ```
  • Kiểm tra `p_conn` còn connected trước khi gửi.
  • Cân nhắc dùng `TcpConnectionPtr` (shared_ptr) thay raw `TcpConnection*` để tránh UAF khi connection đóng giữa chừng.

Mức độ: 🟡 Medium

Metadata

Metadata

Assignees

Labels

mediumPriority: mediumsecuritySecurity vulnerability

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions