From 56059c0dba6db079798f67a0a4063c4c3921ddb8 Mon Sep 17 00:00:00 2001 From: Blake Oler Date: Wed, 25 Jul 2018 15:04:05 -0400 Subject: SERVER-35180 Safeguard from setting operation session info values while in a direct client --- src/mongo/db/initialize_operation_session_info.cpp | 7 +++++++ src/mongo/db/logical_session_id_test.cpp | 24 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/mongo/db/initialize_operation_session_info.cpp b/src/mongo/db/initialize_operation_session_info.cpp index eb3821f0dce..aec8a4fb584 100644 --- a/src/mongo/db/initialize_operation_session_info.cpp +++ b/src/mongo/db/initialize_operation_session_info.cpp @@ -57,6 +57,13 @@ boost::optional initializeOperationSessionInfo( bool supportsRecoverToStableTimestamp) { auto osi = OperationSessionInfoFromClient::parse("OperationSessionInfo"_sd, requestBody); + if (opCtx->getClient()->isInDirectClient()) { + uassert(50891, + "Invalid to set operation session info in a direct client", + !osi.getSessionId() && !osi.getTxnNumber() && !osi.getAutocommit() && + !osi.getStartTransaction()); + } + if (!requiresAuth) { uassert(ErrorCodes::OperationNotSupportedInTransaction, "This command is not supported in transactions", diff --git a/src/mongo/db/logical_session_id_test.cpp b/src/mongo/db/logical_session_id_test.cpp index 7a314bd61b0..59f3fcb969b 100644 --- a/src/mongo/db/logical_session_id_test.cpp +++ b/src/mongo/db/logical_session_id_test.cpp @@ -368,6 +368,30 @@ TEST_F(LogicalSessionIdTest, InitializeOperationSessionInfo_IgnoresInfoIfNoCache true)); } +TEST_F(LogicalSessionIdTest, InitializeOperationSessionInfo_SendingInfoFailsInDirectClient) { + const std::vector operationSessionParameters{ + {BSON("lsid" << makeLogicalSessionIdForTest().toBSON())}, + {BSON("txnNumber" << 1L)}, + {BSON("autocommit" << true)}, + {BSON("startTransaction" << true)}}; + + + _opCtx->getClient()->setInDirectClient(true); + + for (const auto& param : operationSessionParameters) { + BSONObjBuilder commandBuilder = BSON("count" + << "foo"); + commandBuilder.appendElements(param); + + ASSERT_THROWS_CODE(initializeOperationSessionInfo( + _opCtx.get(), commandBuilder.obj(), true, true, true, true), + AssertionException, + 50891); + } + + _opCtx->getClient()->setInDirectClient(false); +} + TEST_F(LogicalSessionIdTest, ConstructorFromClientWithTooLongName) { auto id = UUID::gen(); -- cgit v1.2.1