diff --git a/remoting/protocol/session_authz_authenticator.cc b/remoting/protocol/session_authz_authenticator.cc index 35dc027b1bb4c..070bd8ec96102 100644 --- a/remoting/protocol/session_authz_authenticator.cc +++ b/remoting/protocol/session_authz_authenticator.cc @@ -118,11 +118,20 @@ void SessionAuthzAuthenticator::ProcessMessage( case SessionAuthzState::WAITING_FOR_SESSION_TOKEN: VerifySessionToken(message, std::move(resume_callback)); break; - case SessionAuthzState::SHARED_SECRET_FETCHED: + case SessionAuthzState::SHARED_SECRET_FETCHED: { DCHECK_EQ(underlying_->state(), WAITING_MESSAGE); + // |resume_callback| may synchronously destroy |this| (via + // JingleSession::ProcessAuthenticationStep -> Close() -> + // ChromotingHost::OnSessionClosed -> clients_.erase()), so guard the + // follow-up call with a weak pointer. + auto weak_this = weak_factory_.GetWeakPtr(); underlying_->ProcessMessage(message, std::move(resume_callback)); + if (!weak_this) { + return; + } StartReauthorizerIfNecessary(); break; + } default: NOTREACHED() << "Unexpected SessionAuthz state: " << static_cast(session_authz_state_); @@ -245,7 +254,15 @@ void SessionAuthzAuthenticator::OnVerifiedSessionToken( WAITING_MESSAGE); session_policies_ = std::move(response->session_policies); verify_token_response_ = std::move(response); + // |resume_callback| may synchronously destroy |this| (via + // JingleSession::ProcessAuthenticationStep -> Close() -> + // ChromotingHost::OnSessionClosed -> clients_.erase()), so guard the + // follow-up call with a weak pointer. + auto weak_this = weak_factory_.GetWeakPtr(); underlying_->ProcessMessage(message, std::move(resume_callback)); + if (!weak_this) { + return; + } StartReauthorizerIfNecessary(); } diff --git a/remoting/protocol/session_authz_authenticator.h b/remoting/protocol/session_authz_authenticator.h index 42b39a3442def..b0f69668aa745 100644 --- a/remoting/protocol/session_authz_authenticator.h +++ b/remoting/protocol/session_authz_authenticator.h @@ -11,6 +11,7 @@ #include #include "base/functional/callback.h" +#include "base/memory/weak_ptr.h" #include "base/time/time.h" #include "remoting/base/constants.h" #include "remoting/base/http_status.h" @@ -148,6 +149,8 @@ class SessionAuthzAuthenticator : public Authenticator { std::string session_id_; std::string host_token_; + + base::WeakPtrFactory weak_factory_{this}; }; } // namespace remoting::protocol