• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4541

19 Jul 2025 01:13AM UTC coverage: 56.753% (-1.6%) from 58.31%
#4541

push

travis-ci

web-flow
fix: subquery memleak (#32024)

124299 of 282344 branches covered (44.02%)

Branch coverage included in aggregate %.

181106 of 255787 relevant lines covered (70.8%)

24937406.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.81
/source/libs/parser/src/parAuthenticator.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "catalog.h"
17
#include "cmdnodes.h"
18
#include "parInt.h"
19

20
typedef struct SAuthCxt {
21
  SParseContext*   pParseCxt;
22
  SParseMetaCache* pMetaCache;
23
  int32_t          errCode;
24
} SAuthCxt;
25

26
typedef struct SSelectAuthCxt {
27
  SAuthCxt*    pAuthCxt;
28
  SSelectStmt* pSelect;
29
} SSelectAuthCxt;
30

31
typedef struct SAuthRewriteCxt {
32
  STableNode* pTarget;
33
} SAuthRewriteCxt;
34

35
static int32_t authQuery(SAuthCxt* pCxt, SNode* pStmt);
36

37
static int32_t setUserAuthInfo(SParseContext* pCxt, const char* pDbName, const char* pTabName, AUTH_TYPE type,
2,725✔
38
                               bool isView, bool effective, SUserAuthInfo* pAuth) {
39
  if (effective) {
2,725✔
40
    snprintf(pAuth->user, sizeof(pAuth->user), "%s", pCxt->pEffectiveUser ? pCxt->pEffectiveUser : "");
68!
41
  } else {
42
    snprintf(pAuth->user, sizeof(pAuth->user), "%s", pCxt->pUser);
2,657✔
43
  }
44

45
  if (NULL == pTabName) {
2,725✔
46
    int32_t code = tNameSetDbName(&pAuth->tbName, pCxt->acctId, pDbName, strlen(pDbName));
242✔
47
    if (TSDB_CODE_SUCCESS != code) return code;
242!
48
  } else {
49
    toName(pCxt->acctId, pDbName, pTabName, &pAuth->tbName);
2,483✔
50
  }
51
  pAuth->type = type;
2,725✔
52
  pAuth->isView = isView;
2,725✔
53
  return TSDB_CODE_SUCCESS;
2,725✔
54
}
55

56
static int32_t checkAuthImpl(SAuthCxt* pCxt, const char* pDbName, const char* pTabName, AUTH_TYPE type, SNode** pCond,
1,428,791✔
57
                             bool isView, bool effective) {
58
  SParseContext* pParseCxt = pCxt->pParseCxt;
1,428,791✔
59
  if (pParseCxt->isSuperUser) {
1,428,791✔
60
    return TSDB_CODE_SUCCESS;
1,426,083✔
61
  }
62

63
  AUTH_RES_TYPE auth_res_type = isView ? AUTH_RES_VIEW : AUTH_RES_BASIC;
2,708✔
64
  SUserAuthInfo authInfo = {0};
2,708✔
65
  int32_t       code = setUserAuthInfo(pCxt->pParseCxt, pDbName, pTabName, type, isView, effective, &authInfo);
2,708✔
66
  if (TSDB_CODE_SUCCESS != code) return code;
2,725!
67
  SUserAuthRes authRes = {0};
2,725✔
68
  if (NULL != pCxt->pMetaCache) {
2,725✔
69
    code = getUserAuthFromCache(pCxt->pMetaCache, &authInfo, &authRes);
2,128✔
70
#ifdef TD_ENTERPRISE
71
    if (isView && TSDB_CODE_PAR_INTERNAL_ERROR == code) {
2,128✔
72
      authInfo.isView = false;
101✔
73
      code = getUserAuthFromCache(pCxt->pMetaCache, &authInfo, &authRes);
101✔
74
    }
75
#endif
76
  } else {
77
    SRequestConnInfo conn = {.pTrans = pParseCxt->pTransporter,
597✔
78
                             .requestId = pParseCxt->requestId,
597✔
79
                             .requestObjRefId = pParseCxt->requestRid,
597✔
80
                             .mgmtEps = pParseCxt->mgmtEpSet};
81
    code = catalogChkAuth(pParseCxt->pCatalog, &conn, &authInfo, &authRes);
597✔
82
  }
83
  if (TSDB_CODE_SUCCESS == code && NULL != pCond) {
2,725!
84
    *pCond = authRes.pCond[auth_res_type];
1,391✔
85
  }
86
  return TSDB_CODE_SUCCESS == code ? (authRes.pass[auth_res_type] ? TSDB_CODE_SUCCESS : TSDB_CODE_PAR_PERMISSION_DENIED)
2,725✔
87
                                   : code;
2,725!
88
}
89

90
static int32_t checkAuth(SAuthCxt* pCxt, const char* pDbName, const char* pTabName, AUTH_TYPE type, SNode** pCond) {
1,428,046✔
91
  return checkAuthImpl(pCxt, pDbName, pTabName, type, pCond, false, false);
1,428,046✔
92
}
93

94
static int32_t checkEffectiveAuth(SAuthCxt* pCxt, const char* pDbName, const char* pTabName, AUTH_TYPE type,
44✔
95
                                  SNode** pCond) {
96
  return checkAuthImpl(pCxt, pDbName, pTabName, type, NULL, false, true);
44✔
97
}
98

99
static int32_t checkViewAuth(SAuthCxt* pCxt, const char* pDbName, const char* pTabName, AUTH_TYPE type, SNode** pCond) {
690✔
100
  return checkAuthImpl(pCxt, pDbName, pTabName, type, NULL, true, false);
690✔
101
}
102

103
static int32_t checkViewEffectiveAuth(SAuthCxt* pCxt, const char* pDbName, const char* pTabName, AUTH_TYPE type,
24✔
104
                                      SNode** pCond) {
105
  return checkAuthImpl(pCxt, pDbName, pTabName, type, NULL, true, true);
24✔
106
}
107

108
static EDealRes authSubquery(SAuthCxt* pCxt, SNode* pStmt) {
214,891✔
109
  return TSDB_CODE_SUCCESS == authQuery(pCxt, pStmt) ? DEAL_RES_CONTINUE : DEAL_RES_ERROR;
214,891!
110
}
111

112
static int32_t mergeStableTagCond(SNode** pWhere, SNode* pTagCond) {
×
113
  SLogicConditionNode* pLogicCond = NULL;
×
114
  int32_t              code = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&pLogicCond);
×
115
  if (NULL == pLogicCond) {
×
116
    return code;
×
117
  }
118
  pLogicCond->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
119
  pLogicCond->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
×
120
  pLogicCond->condType = LOGIC_COND_TYPE_AND;
×
121
  code = nodesListMakeStrictAppend(&pLogicCond->pParameterList, pTagCond);
×
122
  if (TSDB_CODE_SUCCESS == code) {
×
123
    code = nodesListMakeAppend(&pLogicCond->pParameterList, *pWhere);
×
124
  }
125
  if (TSDB_CODE_SUCCESS == code) {
×
126
    *pWhere = (SNode*)pLogicCond;
×
127
  } else {
128
    nodesDestroyNode((SNode*)pLogicCond);
×
129
  }
130
  return code;
×
131
}
132

133
EDealRes rewriteAuthTable(SNode* pNode, void* pContext) {
63✔
134
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
63✔
135
    SColumnNode*     pCol = (SColumnNode*)pNode;
21✔
136
    SAuthRewriteCxt* pCxt = (SAuthRewriteCxt*)pContext;
21✔
137
    tstrncpy(pCol->tableName, pCxt->pTarget->tableName, TSDB_TABLE_NAME_LEN);
21✔
138
    tstrncpy(pCol->tableAlias, pCxt->pTarget->tableAlias, TSDB_TABLE_NAME_LEN);
21✔
139
  }
140

141
  return DEAL_RES_CONTINUE;
63✔
142
}
143

144
static int32_t rewriteAppendStableTagCond(SNode** pWhere, SNode* pTagCond, STableNode* pTable) {
21✔
145
  SNode*  pTagCondCopy = NULL;
21✔
146
  int32_t code = nodesCloneNode(pTagCond, &pTagCondCopy);
21✔
147
  if (NULL == pTagCondCopy) {
21!
148
    return code;
×
149
  }
150

151
  SAuthRewriteCxt cxt = {.pTarget = pTable};
21✔
152
  nodesWalkExpr(pTagCondCopy, rewriteAuthTable, &cxt);
21✔
153

154
  if (NULL == *pWhere) {
21!
155
    *pWhere = pTagCondCopy;
21✔
156
    return TSDB_CODE_SUCCESS;
21✔
157
  }
158

159
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(*pWhere) &&
×
160
      LOGIC_COND_TYPE_AND == ((SLogicConditionNode*)*pWhere)->condType) {
×
161
    return nodesListStrictAppend(((SLogicConditionNode*)*pWhere)->pParameterList, pTagCondCopy);
×
162
  }
163

164
  return mergeStableTagCond(pWhere, pTagCondCopy);
×
165
}
166

167
static EDealRes authSelectImpl(SNode* pNode, void* pContext) {
14,054,132✔
168
  SSelectAuthCxt* pCxt = pContext;
14,054,132✔
169
  SAuthCxt*       pAuthCxt = pCxt->pAuthCxt;
14,054,132✔
170
  bool            isView = false;
14,054,132✔
171
  if (QUERY_NODE_REAL_TABLE == nodeType(pNode)) {
14,054,132✔
172
    SNode*      pTagCond = NULL;
1,101,984✔
173
    STableNode* pTable = (STableNode*)pNode;
1,101,984✔
174
#ifdef TD_ENTERPRISE
175
    SName name = {0};
1,101,984✔
176
    toName(pAuthCxt->pParseCxt->acctId, pTable->dbName, pTable->tableName, &name);
1,101,984✔
177
    STableMeta* pTableMeta = NULL;
1,101,989✔
178
    toName(pAuthCxt->pParseCxt->acctId, pTable->dbName, pTable->tableName, &name);
1,101,989✔
179
    int32_t code = getTargetMetaImpl(pAuthCxt->pParseCxt, pAuthCxt->pMetaCache, &name, &pTableMeta, true);
1,101,989✔
180
    if (TSDB_CODE_SUCCESS == code && TSDB_VIEW_TABLE == pTableMeta->tableType) {
1,101,944✔
181
      isView = true;
503✔
182
    }
183
    taosMemoryFree(pTableMeta);
1,101,944!
184
#endif
185
    if (!isView) {
1,101,975✔
186
      pAuthCxt->errCode = checkAuth(pAuthCxt, pTable->dbName, pTable->tableName, AUTH_TYPE_READ, &pTagCond);
1,101,472✔
187
      if (TSDB_CODE_SUCCESS != pAuthCxt->errCode && NULL != pAuthCxt->pParseCxt->pEffectiveUser) {
1,101,461✔
188
        pAuthCxt->errCode = checkEffectiveAuth(pAuthCxt, pTable->dbName, pTable->tableName, AUTH_TYPE_READ, NULL);
44✔
189
      }
190
      if (TSDB_CODE_SUCCESS == pAuthCxt->errCode && NULL != pTagCond) {
1,101,461✔
191
        pAuthCxt->errCode = rewriteAppendStableTagCond(&pCxt->pSelect->pWhere, pTagCond, pTable);
21✔
192
      }
193
    } else {
194
      pAuthCxt->errCode = checkViewAuth(pAuthCxt, pTable->dbName, pTable->tableName, AUTH_TYPE_READ, NULL);
503✔
195
      if (TSDB_CODE_SUCCESS != pAuthCxt->errCode && NULL != pAuthCxt->pParseCxt->pEffectiveUser) {
503✔
196
        pAuthCxt->errCode = checkViewEffectiveAuth(pAuthCxt, pTable->dbName, pTable->tableName, AUTH_TYPE_READ, NULL);
24✔
197
      }
198
    }
199
    return TSDB_CODE_SUCCESS == pAuthCxt->errCode ? DEAL_RES_CONTINUE : DEAL_RES_ERROR;
1,101,964✔
200
  } else if (QUERY_NODE_TEMP_TABLE == nodeType(pNode)) {
12,952,148✔
201
    return authSubquery(pAuthCxt, ((STempTableNode*)pNode)->pSubquery);
214,891✔
202
  }
203
  return DEAL_RES_CONTINUE;
12,737,257✔
204
}
205

206
static int32_t authSelect(SAuthCxt* pCxt, SSelectStmt* pSelect) {
1,223,445✔
207
  SSelectAuthCxt cxt = {.pAuthCxt = pCxt, .pSelect = pSelect};
1,223,445✔
208
  nodesWalkSelectStmt(pSelect, SQL_CLAUSE_FROM, authSelectImpl, &cxt);
1,223,445✔
209
  return pCxt->errCode;
1,223,449✔
210
}
211

212
static int32_t authSetOperator(SAuthCxt* pCxt, SSetOperator* pSetOper) {
66,030✔
213
  int32_t code = authQuery(pCxt, pSetOper->pLeft);
66,030✔
214
  if (TSDB_CODE_SUCCESS == code) {
66,030!
215
    code = authQuery(pCxt, pSetOper->pRight);
66,030✔
216
  }
217
  return code;
66,030✔
218
}
219

220
static int32_t authDropUser(SAuthCxt* pCxt, SDropUserStmt* pStmt) {
172✔
221
  if (!pCxt->pParseCxt->isSuperUser || 0 == strcmp(pStmt->userName, TSDB_DEFAULT_USER)) {
172!
222
    return TSDB_CODE_PAR_PERMISSION_DENIED;
1✔
223
  }
224
  return TSDB_CODE_SUCCESS;
171✔
225
}
226

227
static int32_t authDelete(SAuthCxt* pCxt, SDeleteStmt* pDelete) {
112,077✔
228
  SNode*      pTagCond = NULL;
112,077✔
229
  STableNode* pTable = (STableNode*)pDelete->pFromTable;
112,077✔
230
  int32_t     code = checkAuth(pCxt, pTable->dbName, pTable->tableName, AUTH_TYPE_WRITE, &pTagCond);
112,077✔
231
  if (TSDB_CODE_SUCCESS == code && NULL != pTagCond) {
112,078!
232
    code = rewriteAppendStableTagCond(&pDelete->pWhere, pTagCond, pTable);
×
233
  }
234
  return code;
112,078✔
235
}
236

237
static int32_t authInsert(SAuthCxt* pCxt, SInsertStmt* pInsert) {
134✔
238
  SNode*      pTagCond = NULL;
134✔
239
  STableNode* pTable = (STableNode*)pInsert->pTable;
134✔
240
  // todo check tag condition for subtable
241
  int32_t code = checkAuth(pCxt, pTable->dbName, pTable->tableName, AUTH_TYPE_WRITE, &pTagCond);
134✔
242
  if (TSDB_CODE_SUCCESS == code) {
134!
243
    code = authQuery(pCxt, pInsert->pQuery);
134✔
244
  }
245
  return code;
134✔
246
}
247

248
static int32_t authShowTables(SAuthCxt* pCxt, SShowStmt* pStmt) {
1,441✔
249
  return checkAuth(pCxt, ((SValueNode*)pStmt->pDbName)->literal, NULL, AUTH_TYPE_READ_OR_WRITE, NULL);
1,441✔
250
}
251

252
static int32_t authShowVtables(SAuthCxt* pCxt, SShowStmt* pStmt) { return authShowTables(pCxt, pStmt); }
117✔
253

254
static int32_t authShowUsage(SAuthCxt* pCxt, SShowStmt* pStmt) {
×
255
  return checkAuth(pCxt, ((SValueNode*)pStmt->pDbName)->literal, NULL, AUTH_TYPE_READ_OR_WRITE, NULL);
×
256
}
257

258
static int32_t authShowCreateTable(SAuthCxt* pCxt, SShowCreateTableStmt* pStmt) {
373✔
259
  SNode* pTagCond = NULL;
373✔
260
  // todo check tag condition for subtable
261
  return checkAuth(pCxt, pStmt->dbName, pStmt->tableName, AUTH_TYPE_READ, &pTagCond);
373✔
262
}
263

264
static int32_t authShowCreateView(SAuthCxt* pCxt, SShowCreateViewStmt* pStmt) {
×
265
#ifndef TD_ENTERPRISE
266
  return TSDB_CODE_OPS_NOT_SUPPORT;
267
#endif
268

269
  return TSDB_CODE_SUCCESS;
×
270
}
271

272
static int32_t authCreateTable(SAuthCxt* pCxt, SCreateTableStmt* pStmt) {
20,159✔
273
  SNode* pTagCond = NULL;
20,159✔
274
  // todo check tag condition for subtable
275
  return checkAuth(pCxt, pStmt->dbName, NULL, AUTH_TYPE_WRITE, &pTagCond);
20,159✔
276
}
277

278
static int32_t authCreateVTable(SAuthCxt* pCxt, SCreateVTableStmt* pStmt) {
202✔
279
  PAR_ERR_RET(checkAuth(pCxt, pStmt->dbName, NULL, AUTH_TYPE_WRITE, NULL));
202✔
280
  SNode* pCol = NULL;
186✔
281
  FOREACH(pCol, pStmt->pCols) {
2,162!
282
    SColumnDefNode* pColDef = (SColumnDefNode*)pCol;
1,988✔
283
    if (NULL == pColDef) {
1,988!
284
      PAR_ERR_RET(TSDB_CODE_PAR_INVALID_COLUMN);
×
285
    }
286
    SColumnOptions* pOptions = (SColumnOptions*)pColDef->pOptions;
1,988✔
287
    if (pOptions && pOptions->hasRef) {
1,988!
288
      PAR_ERR_RET(checkAuth(pCxt, pOptions->refDb, pOptions->refTable, AUTH_TYPE_READ, NULL));
1,084✔
289
    }
290
  }
291
  return TSDB_CODE_SUCCESS;
174✔
292
}
293

294
static int32_t authCreateVSubTable(SAuthCxt* pCxt, SCreateVSubTableStmt* pStmt) {
226✔
295
  int32_t    code = TSDB_CODE_SUCCESS;
226✔
296
  SNode*     pNode = NULL;
226✔
297
  SNodeList* pTmpList = pStmt->pSpecificColRefs ? pStmt->pSpecificColRefs : pStmt->pColRefs;
226✔
298
  PAR_ERR_RET(checkAuth(pCxt, pStmt->dbName, NULL, AUTH_TYPE_WRITE, NULL));
226✔
299
  if (NULL == pTmpList) {
210✔
300
    // no column reference
301
    return TSDB_CODE_SUCCESS;
5✔
302
  }
303

304
  FOREACH(pNode, pTmpList) {
1,112!
305
    SColumnRefNode* pColRef = (SColumnRefNode*)pNode;
919✔
306
    if (NULL == pColRef) {
919!
307
      PAR_ERR_RET(TSDB_CODE_PAR_INVALID_COLUMN);
×
308
    }
309
    PAR_ERR_RET(checkAuth(pCxt, pColRef->refDbName, pColRef->refTableName, AUTH_TYPE_READ, NULL));
919✔
310
  }
311
  return code;
193✔
312
}
313

314
static int32_t authCreateStream(SAuthCxt* pCxt, SCreateStreamStmt* pStmt) {
×
315
  int32_t   code = TSDB_CODE_SUCCESS;
×
316

317
  if (IS_SYS_DBNAME(pStmt->streamDbName)) {
×
318
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
319
  }
320
  if (IS_SYS_DBNAME(pStmt->targetDbName)) {
×
321
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
322
  }
323
  if (pStmt->pTrigger) {
×
324
    SStreamTriggerNode *pTrigger = (SStreamTriggerNode*)pStmt->pTrigger;
×
325
    STableNode* pTriggerTable = (STableNode*)pTrigger->pTrigerTable;
×
326
    if (pTriggerTable && IS_SYS_DBNAME(pTriggerTable->dbName)) {
×
327
      return TSDB_CODE_PAR_PERMISSION_DENIED;
×
328
    }
329
  }
330
  return code;
×
331
}
332

333
static int32_t authCreateMultiTable(SAuthCxt* pCxt, SCreateMultiTablesStmt* pStmt) {
103,470✔
334
  int32_t code = TSDB_CODE_SUCCESS;
103,470✔
335
  SNode*  pNode = NULL;
103,470✔
336
  FOREACH(pNode, pStmt->pSubTables) {
256,678!
337
    if (pNode->type == QUERY_NODE_CREATE_SUBTABLE_CLAUSE) {
153,234!
338
      SCreateSubTableClause* pClause = (SCreateSubTableClause*)pNode;
153,234✔
339
      code = checkAuth(pCxt, pClause->dbName, NULL, AUTH_TYPE_WRITE, NULL);
153,234✔
340
      if (TSDB_CODE_SUCCESS != code) {
153,209✔
341
        break;
1✔
342
      }
343
    } else {
344
      SCreateSubTableFromFileClause* pClause = (SCreateSubTableFromFileClause*)pNode;
×
345
      code = checkAuth(pCxt, pClause->useDbName, NULL, AUTH_TYPE_WRITE, NULL);
×
346
      if (TSDB_CODE_SUCCESS != code) {
×
347
        break;
×
348
      }
349
    }
350
  }
351
  return code;
103,445✔
352
}
353

354
static int32_t authDropTable(SAuthCxt* pCxt, SDropTableStmt* pStmt) {
16,993✔
355
  int32_t code = TSDB_CODE_SUCCESS;
16,993✔
356
  if (pStmt->withOpt && !pCxt->pParseCxt->isSuperUser) {
16,993✔
357
    return TSDB_CODE_PAR_PERMISSION_DENIED;
2✔
358
  }
359
  SNode* pNode = NULL;
16,991✔
360
  FOREACH(pNode, pStmt->pTables) {
34,890!
361
    SDropTableClause* pClause = (SDropTableClause*)pNode;
17,899✔
362
    code = checkAuth(pCxt, pClause->dbName, pClause->tableName, AUTH_TYPE_WRITE, NULL);
17,899✔
363
    if (TSDB_CODE_SUCCESS != code) {
17,899!
364
      break;
×
365
    }
366
  }
367
  return code;
16,991✔
368
}
369

370
static int32_t authDropStable(SAuthCxt* pCxt, SDropSuperTableStmt* pStmt) {
592✔
371
  if (pStmt->withOpt && !pCxt->pParseCxt->isSuperUser) {
592✔
372
    return TSDB_CODE_PAR_PERMISSION_DENIED;
1✔
373
  }
374
  return checkAuth(pCxt, pStmt->dbName, pStmt->tableName, AUTH_TYPE_WRITE, NULL);
591✔
375
}
376

377
static int32_t authDropVtable(SAuthCxt* pCxt, SDropVirtualTableStmt* pStmt) {
110✔
378
  if (pStmt->withOpt && !pCxt->pParseCxt->isSuperUser) {
110!
379
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
380
  }
381
  return checkAuth(pCxt, pStmt->dbName, pStmt->tableName, AUTH_TYPE_WRITE, NULL);
110✔
382
}
383

384
static int32_t authAlterTable(SAuthCxt* pCxt, SAlterTableStmt* pStmt) {
17,121✔
385
  SNode* pTagCond = NULL;
17,121✔
386
  // todo check tag condition for subtable
387
  return checkAuth(pCxt, pStmt->dbName, pStmt->tableName, AUTH_TYPE_WRITE, NULL);
17,121✔
388
}
389

390
static int32_t authAlterVTable(SAuthCxt* pCxt, SAlterTableStmt* pStmt) {
577✔
391
  PAR_ERR_RET(checkAuth(pCxt, pStmt->dbName, pStmt->tableName, AUTH_TYPE_WRITE, NULL));
577✔
392
  if (pStmt->alterType == TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF ||
489✔
393
      pStmt->alterType == TSDB_ALTER_TABLE_ALTER_COLUMN_REF) {
444✔
394
    PAR_ERR_RET(checkAuth(pCxt, pStmt->dbName, pStmt->refTableName, AUTH_TYPE_READ, NULL));
189✔
395
  }
396
  PAR_RET(TSDB_CODE_SUCCESS);
457!
397
}
398

399
static int32_t authCreateView(SAuthCxt* pCxt, SCreateViewStmt* pStmt) {
279✔
400
#ifndef TD_ENTERPRISE
401
  return TSDB_CODE_OPS_NOT_SUPPORT;
402
#endif
403
  return checkAuth(pCxt, pStmt->dbName, NULL, AUTH_TYPE_WRITE, NULL);
279✔
404
}
405

406
static int32_t authDropView(SAuthCxt* pCxt, SDropViewStmt* pStmt) {
187✔
407
#ifndef TD_ENTERPRISE
408
  return TSDB_CODE_OPS_NOT_SUPPORT;
409
#endif
410
  return checkViewAuth(pCxt, pStmt->dbName, pStmt->viewName, AUTH_TYPE_ALTER, NULL);
187✔
411
}
412

413
static int32_t authQuery(SAuthCxt* pCxt, SNode* pStmt) {
1,738,590✔
414
  switch (nodeType(pStmt)) {
1,738,590!
415
    case QUERY_NODE_SET_OPERATOR:
66,030✔
416
      return authSetOperator(pCxt, (SSetOperator*)pStmt);
66,030✔
417
    case QUERY_NODE_SELECT_STMT:
1,223,451✔
418
      return authSelect(pCxt, (SSelectStmt*)pStmt);
1,223,451✔
419
    case QUERY_NODE_DROP_USER_STMT:
172✔
420
      return authDropUser(pCxt, (SDropUserStmt*)pStmt);
172✔
421
    case QUERY_NODE_DELETE_STMT:
112,079✔
422
      return authDelete(pCxt, (SDeleteStmt*)pStmt);
112,079✔
423
    case QUERY_NODE_INSERT_STMT:
134✔
424
      return authInsert(pCxt, (SInsertStmt*)pStmt);
134✔
425
    case QUERY_NODE_CREATE_TABLE_STMT:
20,159✔
426
      return authCreateTable(pCxt, (SCreateTableStmt*)pStmt);
20,159✔
427
    case QUERY_NODE_CREATE_VIRTUAL_TABLE_STMT:
202✔
428
      return authCreateVTable(pCxt, (SCreateVTableStmt*)pStmt);
202✔
429
    case QUERY_NODE_CREATE_VIRTUAL_SUBTABLE_STMT:
226✔
430
      return authCreateVSubTable(pCxt, (SCreateVSubTableStmt*)pStmt);
226✔
431
    case QUERY_NODE_CREATE_MULTI_TABLES_STMT:
103,472✔
432
      return authCreateMultiTable(pCxt, (SCreateMultiTablesStmt*)pStmt);
103,472✔
433
    case QUERY_NODE_CREATE_STREAM_STMT:
×
434
      return authCreateStream(pCxt, (SCreateStreamStmt*)pStmt);
×
435
    case QUERY_NODE_DROP_TABLE_STMT:
16,993✔
436
      return authDropTable(pCxt, (SDropTableStmt*)pStmt);
16,993✔
437
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:
592✔
438
      return authDropStable(pCxt, (SDropSuperTableStmt*)pStmt);
592✔
439
    case QUERY_NODE_DROP_VIRTUAL_TABLE_STMT:
110✔
440
      return authDropVtable(pCxt, (SDropVirtualTableStmt*)pStmt);
110✔
441
    case QUERY_NODE_ALTER_TABLE_STMT:
17,121✔
442
    case QUERY_NODE_ALTER_SUPER_TABLE_STMT:
443
      return authAlterTable(pCxt, (SAlterTableStmt*)pStmt);
17,121✔
444
    case QUERY_NODE_ALTER_VIRTUAL_TABLE_STMT:
577✔
445
      return authAlterVTable(pCxt, (SAlterTableStmt*)pStmt);
577✔
446
    case QUERY_NODE_SHOW_DNODES_STMT:
1,380✔
447
    case QUERY_NODE_SHOW_MNODES_STMT:
448
    case QUERY_NODE_SHOW_MODULES_STMT:
449
    case QUERY_NODE_SHOW_QNODES_STMT:
450
    case QUERY_NODE_SHOW_SNODES_STMT:
451
    case QUERY_NODE_SHOW_BACKUP_NODES_STMT:
452
    case QUERY_NODE_SHOW_CLUSTER_STMT:
453
    case QUERY_NODE_SHOW_LICENCES_STMT:
454
    case QUERY_NODE_SHOW_VGROUPS_STMT:
455
    case QUERY_NODE_SHOW_DB_ALIVE_STMT:
456
    case QUERY_NODE_SHOW_CLUSTER_ALIVE_STMT:
457
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
458
    case QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT:
459
    case QUERY_NODE_SHOW_DNODE_VARIABLES_STMT:
460
    case QUERY_NODE_SHOW_VNODES_STMT:
461
    case QUERY_NODE_SHOW_SCORES_STMT:
462
    case QUERY_NODE_SHOW_USERS_STMT:
463
    case QUERY_NODE_SHOW_USERS_FULL_STMT:
464
    case QUERY_NODE_SHOW_USER_PRIVILEGES_STMT:
465
    case QUERY_NODE_SHOW_GRANTS_FULL_STMT:
466
    case QUERY_NODE_SHOW_GRANTS_LOGS_STMT:
467
    case QUERY_NODE_SHOW_CLUSTER_MACHINES_STMT:
468
    case QUERY_NODE_SHOW_ARBGROUPS_STMT:
469
    case QUERY_NODE_SHOW_ENCRYPTIONS_STMT:
470
    case QUERY_NODE_SHOW_MOUNTS_STMT:
471
      return !pCxt->pParseCxt->enableSysInfo ? TSDB_CODE_PAR_PERMISSION_DENIED : TSDB_CODE_SUCCESS;
1,380✔
472
    case QUERY_NODE_SHOW_USAGE_STMT:
7✔
473
    case QUERY_NODE_SHOW_ANODES_STMT:
474
    case QUERY_NODE_SHOW_ANODES_FULL_STMT:
475
      return TSDB_CODE_SUCCESS;
7✔
476
    case QUERY_NODE_SHOW_TABLES_STMT:
1,324✔
477
    case QUERY_NODE_SHOW_STABLES_STMT:
478
      return authShowTables(pCxt, (SShowStmt*)pStmt);
1,324✔
479
    case QUERY_NODE_SHOW_VTABLES_STMT:
117✔
480
      return authShowVtables(pCxt, (SShowStmt*)pStmt);
117✔
481
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
373✔
482
    case QUERY_NODE_SHOW_CREATE_VTABLE_STMT:
483
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
484
      return authShowCreateTable(pCxt, (SShowCreateTableStmt*)pStmt);
373✔
485
      //    case QUERY_NODE_SHOW_CREATE_VIEW_STMT:
486
      //      return authShowCreateView(pCxt, (SShowCreateViewStmt*)pStmt);
487
    case QUERY_NODE_CREATE_VIEW_STMT:
279✔
488
      return authCreateView(pCxt, (SCreateViewStmt*)pStmt);
279✔
489
    case QUERY_NODE_DROP_VIEW_STMT:
187✔
490
      return authDropView(pCxt, (SDropViewStmt*)pStmt);
187✔
491
    default:
173,605✔
492
      break;
173,605✔
493
  }
494

495
  return TSDB_CODE_SUCCESS;
173,605✔
496
}
497

498
int32_t authenticate(SParseContext* pParseCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) {
1,391,478✔
499
  SAuthCxt cxt = {.pParseCxt = pParseCxt, .pMetaCache = pMetaCache, .errCode = TSDB_CODE_SUCCESS};
1,391,478✔
500
  return authQuery(&cxt, pQuery->pRoot);
1,391,478✔
501
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc