Skip to content

Commit 57cf0e9

Browse files
authored
Forbid query execution on explicit session without attach (#1870)
1 parent ec18660 commit 57cf0e9

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

ydb/core/kqp/proxy_service/kqp_proxy_service.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ class TKqpProxyService : public TActorBootstrapped<TKqpProxyService> {
626626
const auto queryAction = ev->Get()->GetAction();
627627
TKqpRequestInfo requestInfo(traceId);
628628
ui64 requestId = PendingRequests.RegisterRequest(ev->Sender, ev->Cookie, traceId, TKqpEvents::EvQueryRequest);
629+
bool explicitSession = true;
629630
if (ev->Get()->GetSessionId().empty()) {
630631
TProcessResult<TKqpSessionInfo*> result;
631632
if (!CreateNewSessionWorker(requestInfo, TString(DefaultKikimrPublicClusterName), false,
@@ -634,7 +635,7 @@ class TKqpProxyService : public TActorBootstrapped<TKqpProxyService> {
634635
ReplyProcessError(result.YdbStatus, result.Error, requestId);
635636
return;
636637
}
637-
638+
explicitSession = false;
638639
ev->Get()->SetSessionId(result.Value->SessionId);
639640
}
640641

@@ -650,6 +651,16 @@ class TKqpProxyService : public TActorBootstrapped<TKqpProxyService> {
650651
dbCounters = Counters->GetDbCounters(database);
651652
}
652653

654+
if (queryType == NKikimrKqp::QUERY_TYPE_SQL_GENERIC_QUERY ||
655+
queryType == NKikimrKqp::QUERY_TYPE_SQL_GENERIC_CONCURRENT_QUERY) {
656+
657+
if (explicitSession && sessionInfo && !sessionInfo->AttachedRpcId) {
658+
TString error = "Attempt to execute query on explicit session without attach";
659+
ReplyProcessError(Ydb::StatusIds::BAD_REQUEST, error, requestId);
660+
return;
661+
}
662+
}
663+
653664
PendingRequests.SetSessionId(requestId, sessionId, dbCounters);
654665
Counters->ReportQueryRequest(dbCounters, ev->Get()->GetRequestSize(), ev->Get()->GetParametersSize(), ev->Get()->GetQuerySize());
655666
Counters->ReportQueryAction(dbCounters, queryAction);

ydb/services/ydb/ydb_query_ut.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,45 @@ Y_UNIT_TEST_SUITE(YdbQueryService) {
6868
UNIT_ASSERT(allDoneOk);
6969
}
7070

71+
Y_UNIT_TEST(TestForbidExecuteWithoutAttach) {
72+
TKikimrWithGrpcAndRootSchema server;
73+
74+
ui16 grpc = server.GetPort();
75+
TString location = TStringBuilder() << "localhost:" << grpc;
76+
77+
auto clientConfig = NGRpcProxy::TGRpcClientConfig(location);
78+
79+
TString sessionId = CreateQuerySession(clientConfig);
80+
81+
UNIT_ASSERT(sessionId);
82+
83+
NYdbGrpc::TGRpcClientLow clientLow;
84+
85+
std::shared_ptr<grpc::Channel> channel;
86+
channel = grpc::CreateChannel("localhost:" + ToString(grpc), grpc::InsecureChannelCredentials());
87+
88+
{
89+
std::unique_ptr<Ydb::Query::V1::QueryService::Stub> stub;
90+
stub = Ydb::Query::V1::QueryService::NewStub(channel);
91+
grpc::ClientContext context;
92+
Ydb::Query::ExecuteQueryRequest request;
93+
request.set_session_id(sessionId);
94+
request.set_exec_mode(Ydb::Query::EXEC_MODE_EXECUTE);
95+
request.mutable_tx_control()->mutable_begin_tx()->mutable_serializable_read_write();
96+
request.mutable_tx_control()->set_commit_tx(true);
97+
request.mutable_query_content()->set_text("SELECT 42");
98+
Ydb::Query::ExecuteQueryResponsePart response;
99+
auto reader = stub->ExecuteQuery(&context, request);
100+
bool res = true;
101+
while (res) {
102+
res = reader->Read(&response);
103+
if (res) {
104+
UNIT_ASSERT_VALUES_EQUAL(response.status(), Ydb::StatusIds::BAD_REQUEST);
105+
}
106+
}
107+
}
108+
}
109+
71110
Y_UNIT_TEST(TestCreateDropAttachSession) {
72111
TKikimrWithGrpcAndRootSchema server;
73112

0 commit comments

Comments
 (0)