|
1 | 1 | // Copyright 2024 the Deno authors. All rights reserved. MIT license. |
2 | 2 |
|
3 | 3 | #include "support.h" |
| 4 | +#include "v8/third_party/inspector_protocol/crdtp/cbor.h" |
4 | 5 | #include "v8/third_party/inspector_protocol/crdtp/dispatch.h" |
5 | 6 | #include "v8/third_party/inspector_protocol/crdtp/frontend_channel.h" |
6 | 7 | #include "v8/third_party/inspector_protocol/crdtp/json.h" |
| 8 | +#include "v8/third_party/inspector_protocol/crdtp/parser_handler.h" |
7 | 9 |
|
8 | 10 | using namespace support; |
9 | 11 | using namespace v8_crdtp; |
@@ -259,10 +261,40 @@ Serializable* crdtp__CreateResponse(int call_id, Serializable* params) { |
259 | 261 | return CreateResponse(call_id, std::move(params_ptr)).release(); |
260 | 262 | } |
261 | 263 |
|
| 264 | +// Owns a copy of the method string, since upstream Notification stores only |
| 265 | +// a raw const char* pointer which leads to use-after-free when the Rust |
| 266 | +// CString is dropped before AppendSerialized is called. |
| 267 | +class OwnedNotification : public Serializable { |
| 268 | + public: |
| 269 | + OwnedNotification(const char* method, std::unique_ptr<Serializable> params) |
| 270 | + : method_(method), params_(std::move(params)) {} |
| 271 | + |
| 272 | + void AppendSerialized(std::vector<uint8_t>* out) const override { |
| 273 | + Status status; |
| 274 | + std::unique_ptr<ParserHandler> encoder = cbor::NewCBOREncoder(out, &status); |
| 275 | + encoder->HandleMapBegin(); |
| 276 | + encoder->HandleString8(SpanFrom("method")); |
| 277 | + encoder->HandleString8(SpanFrom(method_)); |
| 278 | + encoder->HandleString8(SpanFrom("params")); |
| 279 | + if (params_) { |
| 280 | + params_->AppendSerialized(out); |
| 281 | + } else { |
| 282 | + encoder->HandleMapBegin(); |
| 283 | + encoder->HandleMapEnd(); |
| 284 | + } |
| 285 | + encoder->HandleMapEnd(); |
| 286 | + assert(status.ok()); |
| 287 | + } |
| 288 | + |
| 289 | + private: |
| 290 | + std::string method_; |
| 291 | + std::unique_ptr<Serializable> params_; |
| 292 | +}; |
| 293 | + |
262 | 294 | Serializable* crdtp__CreateNotification(const char* method, |
263 | 295 | Serializable* params) { |
264 | 296 | std::unique_ptr<Serializable> params_ptr(params); |
265 | | - return CreateNotification(method, std::move(params_ptr)).release(); |
| 297 | + return new OwnedNotification(method, std::move(params_ptr)); |
266 | 298 | } |
267 | 299 |
|
268 | 300 | Serializable* crdtp__CreateErrorNotification( |
|
0 commit comments