@carbonorm/carbonnode 3.5.5 → 3.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/orm/builders/ConditionBuilder.d.ts +3 -2
- package/dist/index.cjs.js +46 -26
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +46 -26
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/api/orm/builders/ConditionBuilder.ts +45 -21
- package/src/api/orm/builders/JoinBuilder.ts +1 -3
- package/src/api/orm/queries/DeleteQueryBuilder.ts +1 -1
- package/src/api/orm/queries/PostQueryBuilder.ts +0 -1
- package/src/api/orm/queries/SelectQueryBuilder.ts +1 -1
- package/src/api/orm/queries/UpdateQueryBuilder.ts +1 -1
|
@@ -8,7 +8,35 @@ export abstract class ConditionBuilder<
|
|
|
8
8
|
G extends OrmGenerics
|
|
9
9
|
> extends AggregateBuilder<G> {
|
|
10
10
|
|
|
11
|
-
protected
|
|
11
|
+
protected aliasMap: Record<string, string> = {};
|
|
12
|
+
|
|
13
|
+
protected initAlias(baseTable: string, joins?: any): void {
|
|
14
|
+
this.aliasMap = { [baseTable]: baseTable };
|
|
15
|
+
|
|
16
|
+
if (!joins) return;
|
|
17
|
+
|
|
18
|
+
for (const joinType in joins) {
|
|
19
|
+
for (const raw in joins[joinType]) {
|
|
20
|
+
const [table, alias] = raw.split(' ');
|
|
21
|
+
this.aliasMap[alias || table] = table;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected isColumnRef(ref: string): boolean {
|
|
27
|
+
if (typeof ref !== 'string' || !ref.includes('.')) return false;
|
|
28
|
+
|
|
29
|
+
const [prefix, column] = ref.split('.', 2);
|
|
30
|
+
const tableName = this.aliasMap[prefix] || prefix;
|
|
31
|
+
const table = this.config.C6?.TABLES?.[tableName];
|
|
32
|
+
if (!table) return false;
|
|
33
|
+
|
|
34
|
+
const fullKey = `${tableName}.${column}`;
|
|
35
|
+
if (table.COLUMNS && (fullKey in table.COLUMNS)) return true;
|
|
36
|
+
if (table.COLUMNS && Object.values(table.COLUMNS).includes(column)) return true;
|
|
37
|
+
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
12
40
|
|
|
13
41
|
abstract build(table: string): SqlBuilderResult;
|
|
14
42
|
|
|
@@ -27,17 +55,6 @@ export abstract class ConditionBuilder<
|
|
|
27
55
|
C6C.ST_DISTANCE_SPHERE
|
|
28
56
|
]);
|
|
29
57
|
|
|
30
|
-
private isTableReference(val: any): boolean {
|
|
31
|
-
if (typeof val !== 'string' || !val.includes('.')) return false;
|
|
32
|
-
const [prefix, column] = val.split('.');
|
|
33
|
-
const tableName = this.aliasMappings[prefix] ?? prefix;
|
|
34
|
-
return (
|
|
35
|
-
typeof this.config.C6?.TABLES[tableName] === 'object' &&
|
|
36
|
-
val in this.config.C6.TABLES[tableName].COLUMNS &&
|
|
37
|
-
this.config.C6.TABLES[tableName].COLUMNS[val] === column
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
58
|
private validateOperator(op: string) {
|
|
42
59
|
if (!this.OPERATORS.has(op)) {
|
|
43
60
|
throw new Error(`Invalid or unsupported SQL operator detected: '${op}'`);
|
|
@@ -83,8 +100,14 @@ export abstract class ConditionBuilder<
|
|
|
83
100
|
}
|
|
84
101
|
}
|
|
85
102
|
|
|
103
|
+
const leftIsCol = this.isColumnRef(column);
|
|
104
|
+
const rightIsCol = typeof value === 'string' && this.isColumnRef(value);
|
|
105
|
+
|
|
106
|
+
if (!leftIsCol && !rightIsCol) {
|
|
107
|
+
throw new Error(`Potential SQL injection detected: '${column} ${op} ${value}'`);
|
|
108
|
+
}
|
|
109
|
+
|
|
86
110
|
this.validateOperator(op);
|
|
87
|
-
const leftIsRef: boolean = this.isTableReference(column);
|
|
88
111
|
|
|
89
112
|
if (op === C6C.MATCH_AGAINST && Array.isArray(value)) {
|
|
90
113
|
const [search, mode] = value;
|
|
@@ -109,7 +132,7 @@ export abstract class ConditionBuilder<
|
|
|
109
132
|
break;
|
|
110
133
|
}
|
|
111
134
|
|
|
112
|
-
if (!
|
|
135
|
+
if (!leftIsCol) {
|
|
113
136
|
throw new Error(`MATCH_AGAINST requires a table reference as the left operand. Column '${column}' is not a valid table reference.`);
|
|
114
137
|
}
|
|
115
138
|
const matchClause = `(MATCH(${column}) ${againstClause})`;
|
|
@@ -118,9 +141,11 @@ export abstract class ConditionBuilder<
|
|
|
118
141
|
}
|
|
119
142
|
|
|
120
143
|
if ((op === C6C.IN || op === C6C.NOT_IN) && Array.isArray(value)) {
|
|
121
|
-
const placeholders = value.map(v =>
|
|
144
|
+
const placeholders = value.map(v =>
|
|
145
|
+
this.isColumnRef(v) ? v : this.addParam(params, column, v)
|
|
146
|
+
).join(', ');
|
|
122
147
|
const normalized = op.replace('_', ' ');
|
|
123
|
-
if (!
|
|
148
|
+
if (!leftIsCol) {
|
|
124
149
|
throw new Error(`IN operator requires a table reference as the left operand. Column '${column}' is not a valid table reference.`);
|
|
125
150
|
}
|
|
126
151
|
return `( ${column} ${normalized} (${placeholders}) )`;
|
|
@@ -131,23 +156,22 @@ export abstract class ConditionBuilder<
|
|
|
131
156
|
throw new Error(`BETWEEN operator requires an array of two values`);
|
|
132
157
|
}
|
|
133
158
|
const [start, end] = value;
|
|
134
|
-
if (!
|
|
159
|
+
if (!leftIsCol) {
|
|
135
160
|
throw new Error(`BETWEEN operator requires a table reference as the left operand. Column '${column}' is not a valid table reference.`);
|
|
136
161
|
}
|
|
137
162
|
return `(${column}) ${op.replace('_', ' ')} ${this.addParam(params, column, start)} AND ${this.addParam(params, column, end)}`;
|
|
138
163
|
}
|
|
139
164
|
|
|
140
|
-
const rightIsRef: boolean = this.isTableReference(value);
|
|
141
165
|
|
|
142
|
-
if (
|
|
166
|
+
if (leftIsCol && rightIsCol) {
|
|
143
167
|
return `(${column}) ${op} ${value}`;
|
|
144
168
|
}
|
|
145
169
|
|
|
146
|
-
if (
|
|
170
|
+
if (leftIsCol && !rightIsCol) {
|
|
147
171
|
return `(${column}) ${op} ${this.addParam(params, column, value)}`;
|
|
148
172
|
}
|
|
149
173
|
|
|
150
|
-
if (
|
|
174
|
+
if (rightIsCol) {
|
|
151
175
|
return `(${this.addParam(params, column, column)}) ${op} ${value}`;
|
|
152
176
|
}
|
|
153
177
|
|
|
@@ -11,9 +11,7 @@ export abstract class JoinBuilder<G extends OrmGenerics> extends ConditionBuilde
|
|
|
11
11
|
|
|
12
12
|
for (const raw in joinArgs[joinType]) {
|
|
13
13
|
const [table, alias] = raw.split(' ');
|
|
14
|
-
|
|
15
|
-
this.aliasMappings[alias] = table;
|
|
16
|
-
}
|
|
14
|
+
this.aliasMap[alias || table] = table;
|
|
17
15
|
const onClause = this.buildBooleanJoinedConditions(joinArgs[joinType][raw], true, params);
|
|
18
16
|
const joinSql = alias ? `\`${table}\` AS \`${alias}\`` : `\`${table}\``;
|
|
19
17
|
sql += ` ${joinKind} JOIN ${joinSql} ON ${onClause}`;
|
|
@@ -6,8 +6,8 @@ export class DeleteQueryBuilder<G extends OrmGenerics> extends JoinBuilder<G> {
|
|
|
6
6
|
build(
|
|
7
7
|
table: string
|
|
8
8
|
): SqlBuilderResult {
|
|
9
|
-
this.aliasMappings = {};
|
|
10
9
|
const params = this.useNamedParams ? {} : [];
|
|
10
|
+
this.initAlias(table, this.request.JOIN);
|
|
11
11
|
|
|
12
12
|
let sql = `DELETE \`${table}\` FROM \`${table}\``;
|
|
13
13
|
|
|
@@ -14,7 +14,6 @@ export class PostQueryBuilder<G extends OrmGenerics> extends ConditionBuilder<G>
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
build(table: string) {
|
|
17
|
-
this.aliasMappings = {};
|
|
18
17
|
const verb = C6C.REPLACE in this.request ? C6C.REPLACE : C6C.INSERT;
|
|
19
18
|
const body = verb in this.request ? this.request[verb] : this.request;
|
|
20
19
|
const keys = Object.keys(body);
|
|
@@ -8,8 +8,8 @@ export class SelectQueryBuilder<G extends OrmGenerics> extends PaginationBuilder
|
|
|
8
8
|
table: string,
|
|
9
9
|
isSubSelect: boolean = false
|
|
10
10
|
): SqlBuilderResult {
|
|
11
|
-
this.aliasMappings = {};
|
|
12
11
|
const args = this.request;
|
|
12
|
+
this.initAlias(table, args.JOIN);
|
|
13
13
|
const params = this.useNamedParams ? {} : [];
|
|
14
14
|
const selectList = args.SELECT ?? ['*'];
|
|
15
15
|
const selectFields = selectList
|
|
@@ -8,9 +8,9 @@ export class UpdateQueryBuilder<G extends OrmGenerics> extends PaginationBuilder
|
|
|
8
8
|
build(
|
|
9
9
|
table: string,
|
|
10
10
|
): SqlBuilderResult {
|
|
11
|
-
this.aliasMappings = {};
|
|
12
11
|
const args = this.request;
|
|
13
12
|
const params = this.useNamedParams ? {} : [];
|
|
13
|
+
this.initAlias(table, args.JOIN);
|
|
14
14
|
let sql = `UPDATE \`${table}\``;
|
|
15
15
|
|
|
16
16
|
if (args.JOIN) {
|