Browse Source

feat(nocodb): validate column name at api level

pull/5131/head
Wing-Kam Wong 2 years ago
parent
commit
c47787d959
  1. 14
      packages/nocodb/src/lib/meta/api/columnApis.ts
  2. 17
      packages/nocodb/src/lib/meta/api/tableApis.ts

14
packages/nocodb/src/lib/meta/api/columnApis.ts

@ -64,9 +64,23 @@ export async function columnAdd(
const table = await Model.getWithInfo({ const table = await Model.getWithInfo({
id: req.params.tableId, id: req.params.tableId,
}); });
const base = await Base.get(table.base_id); const base = await Base.get(table.base_id);
const project = await base.getProject(); const project = await base.getProject();
const dbDriver = NcConnectionMgrv2.get(base);
const sqlClientType = dbDriver.clientType();
const mxColumnLength = Column.getMaxColumnNameLength(sqlClientType);
if (req.body.column_name.length > mxColumnLength) {
NcError.badRequest(
`Column name ${req.body.column_name} exceeds ${mxColumnLength} characters`
);
}
if ( if (
!isVirtualCol(req.body) && !isVirtualCol(req.body) &&
!(await Column.checkTitleAvailable({ !(await Column.checkTitleAvailable({

17
packages/nocodb/src/lib/meta/api/tableApis.ts

@ -148,10 +148,15 @@ export async function tableCreate(req: Request<any, any, TableReqType>, res) {
} }
const sqlMgr = await ProjectMgrv2.getSqlMgr(project); const sqlMgr = await ProjectMgrv2.getSqlMgr(project);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base); const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
const dbDriver = NcConnectionMgrv2.get(base);
const sqlClientType = dbDriver.clientType();
let tableNameLengthLimit = 255; let tableNameLengthLimit = 255;
const sqlClientType = sqlClient.clientType;
if (sqlClientType === 'mysql2' || sqlClientType === 'mysql') { if (sqlClientType === 'mysql2' || sqlClientType === 'mysql') {
tableNameLengthLimit = 64; tableNameLengthLimit = 64;
} else if (sqlClientType === 'pg') { } else if (sqlClientType === 'pg') {
@ -164,6 +169,16 @@ export async function tableCreate(req: Request<any, any, TableReqType>, res) {
NcError.badRequest(`Table name exceeds ${tableNameLengthLimit} characters`); NcError.badRequest(`Table name exceeds ${tableNameLengthLimit} characters`);
} }
const mxColumnLength = Column.getMaxColumnNameLength(sqlClientType);
for (const column of req.body.columns) {
if (column.column_name.length > mxColumnLength) {
NcError.badRequest(
`Column name ${column.column_name} exceeds ${mxColumnLength} characters`
);
}
}
req.body.columns = req.body.columns?.map((c) => ({ req.body.columns = req.body.columns?.map((c) => ({
...getColumnPropsFromUIDT(c as any, base), ...getColumnPropsFromUIDT(c as any, base),
cn: c.column_name, cn: c.column_name,

Loading…
Cancel
Save