diff --git a/extensions/renderer/guest_view/mime_handler_view/post_message_support.cc b/extensions/renderer/guest_view/mime_handler_view/post_message_support.cc index d14bb357965ae..c2b2317a23891 100644 --- a/extensions/renderer/guest_view/mime_handler_view/post_message_support.cc +++ b/extensions/renderer/guest_view/mime_handler_view/post_message_support.cc @@ -181,12 +181,19 @@ void PostMessageSupport::SetActive() { v8::Isolate* isolate = source->GetAgentGroupScheduler()->Isolate(); v8::HandleScope handle_scope(isolate); v8::Context::Scope context_scope(source->MainWorldScriptContext()); - for (const auto& pending_message : pending_messages_) { + // PostJavaScriptMessage() runs script (structured clone of attacker-supplied + // values) and may re-enter delegate_->GetTargetFrame(), either of which can + // synchronously delete |this| and free |pending_messages_|'s backing buffer. + // Move the queue onto the stack and bail out if |this| goes away. + auto weak_this = weak_factory_.GetWeakPtr(); + std::vector> messages = std::move(pending_messages_); + for (const auto& pending_message : messages) { PostJavaScriptMessage(isolate, v8::Local::New(isolate, pending_message)); + if (!weak_this) { + return; + } } - - pending_messages_.clear(); } } // namespace extensions