@berthojoris/mcp-mysql-server 1.45.2 → 1.47.0
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/CHANGELOG.md +19 -0
- package/DOCUMENTATIONS.md +38 -8
- package/README.md +19 -2
- package/bin/mcp-mysql.js +3 -2
- package/dist/config/config.d.ts +1 -0
- package/dist/config/config.js +4 -0
- package/dist/config/databaseScope.d.ts +10 -0
- package/dist/config/databaseScope.js +36 -0
- package/dist/config/featureConfig.js +5 -5
- package/dist/index.d.ts +2 -0
- package/dist/mcp-server.js +17 -9
- package/dist/optimization/queryOptimizer.d.ts +13 -5
- package/dist/optimization/queryOptimizer.js +85 -58
- package/dist/security/databaseQueryScope.d.ts +5 -0
- package/dist/security/databaseQueryScope.js +184 -0
- package/dist/security/securityLayer.js +8 -2
- package/dist/tools/analysisTools.js +2 -26
- package/dist/tools/constraintTools.js +2 -26
- package/dist/tools/crudTools.d.ts +1 -0
- package/dist/tools/crudTools.js +6 -1
- package/dist/tools/dataExportTools.d.ts +1 -0
- package/dist/tools/dataExportTools.js +7 -21
- package/dist/tools/databaseTools.d.ts +1 -0
- package/dist/tools/databaseTools.js +23 -62
- package/dist/tools/fulltextSearchTools.js +2 -26
- package/dist/tools/indexTools.js +2 -26
- package/dist/tools/maintenanceTools.d.ts +3 -1
- package/dist/tools/maintenanceTools.js +24 -36
- package/dist/tools/migrationTools.js +2 -20
- package/dist/tools/queryTools.js +9 -0
- package/dist/tools/relationalSeederTools.js +2 -9
- package/dist/tools/smartQueryBuilderTools.d.ts +2 -0
- package/dist/tools/smartQueryBuilderTools.js +10 -29
- package/dist/tools/storedProcedureTools.js +2 -28
- package/dist/tools/toolArgumentValidation.js +22 -1
- package/dist/tools/triggerTools.js +2 -26
- package/dist/tools/utilityTools.js +6 -1
- package/dist/tools/viewTools.js +2 -26
- package/dist/validation/schemas.d.ts +6 -0
- package/dist/validation/schemas.js +3 -1
- package/manifest.json +6 -10
- package/package.json +1 -1
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateDatabaseQueryScope = void 0;
|
|
4
|
+
const databaseScope_1 = require("../config/databaseScope");
|
|
5
|
+
const tokenize = (sql) => {
|
|
6
|
+
const tokens = [];
|
|
7
|
+
let index = 0;
|
|
8
|
+
while (index < sql.length) {
|
|
9
|
+
const char = sql[index];
|
|
10
|
+
if (/\s/.test(char)) {
|
|
11
|
+
index++;
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
if (char === "'" || char === '"' || char === "`") {
|
|
15
|
+
const quote = char;
|
|
16
|
+
let value = "";
|
|
17
|
+
index++;
|
|
18
|
+
while (index < sql.length) {
|
|
19
|
+
if (sql[index] === quote) {
|
|
20
|
+
if (sql[index + 1] === quote) {
|
|
21
|
+
value += quote;
|
|
22
|
+
index += 2;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
index++;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
if (sql[index] === "\\" && quote !== "`") {
|
|
29
|
+
value += sql[index + 1] || "";
|
|
30
|
+
index += 2;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
value += sql[index++];
|
|
34
|
+
}
|
|
35
|
+
if (quote !== "'")
|
|
36
|
+
tokens.push({ value, identifier: true });
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (/[A-Za-z_$]/.test(char)) {
|
|
40
|
+
const start = index++;
|
|
41
|
+
while (index < sql.length && /[A-Za-z0-9_$]/.test(sql[index]))
|
|
42
|
+
index++;
|
|
43
|
+
tokens.push({ value: sql.slice(start, index), identifier: true });
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
tokens.push({ value: char, identifier: false });
|
|
47
|
+
index++;
|
|
48
|
+
}
|
|
49
|
+
return tokens;
|
|
50
|
+
};
|
|
51
|
+
const RELATION_END = new Set([
|
|
52
|
+
"WHERE", "GROUP", "ORDER", "HAVING", "LIMIT", "OFFSET", "UNION", "SET",
|
|
53
|
+
"VALUES", "VALUE", "ON", "USING", "RETURNING", "WINDOW", "FOR", "LOCK",
|
|
54
|
+
]);
|
|
55
|
+
const checkTargetAt = (tokens, start) => {
|
|
56
|
+
let index = start;
|
|
57
|
+
while (["LOW_PRIORITY", "IGNORE", "QUICK", "TEMPORARY", "IF", "NOT", "EXISTS", "ONLY"].includes((tokens[index]?.value || "").toUpperCase())) {
|
|
58
|
+
index++;
|
|
59
|
+
}
|
|
60
|
+
const first = tokens[index];
|
|
61
|
+
if (!first?.identifier || first.value === "")
|
|
62
|
+
return undefined;
|
|
63
|
+
if (tokens[index + 1]?.value === "." && tokens[index + 2]?.identifier) {
|
|
64
|
+
return first.value;
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
};
|
|
68
|
+
/** Restricts explicitly named schemas while leaving ordinary table aliases alone. */
|
|
69
|
+
const validateDatabaseQueryScope = (sql) => {
|
|
70
|
+
const tokens = tokenize(sql);
|
|
71
|
+
const disallowed = new Set();
|
|
72
|
+
let inFromClause = false;
|
|
73
|
+
let expectRelation = false;
|
|
74
|
+
let expectBareDatabase = false;
|
|
75
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
76
|
+
const value = tokens[index].value.toUpperCase();
|
|
77
|
+
if (RELATION_END.has(value)) {
|
|
78
|
+
inFromClause = false;
|
|
79
|
+
expectRelation = false;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (value === "FROM") {
|
|
83
|
+
inFromClause = true;
|
|
84
|
+
expectRelation = true;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (value === "JOIN" || value === "UPDATE" || value === "INTO" || value === "REFERENCES" || value === "TO" || value === "DESCRIBE" || value === "DESC") {
|
|
88
|
+
expectRelation = true;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (value === "TRUNCATE") {
|
|
92
|
+
expectRelation = true;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (value === "TABLE") {
|
|
96
|
+
expectRelation = true;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (value === "," && inFromClause) {
|
|
100
|
+
expectRelation = true;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if ((value === "DATABASE" || value === "SCHEMA") && ["CREATE", "ALTER", "DROP"].includes((tokens[index - 1]?.value || "").toUpperCase())) {
|
|
104
|
+
expectRelation = true;
|
|
105
|
+
expectBareDatabase = true;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (["VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "EVENT"].includes(value) &&
|
|
109
|
+
["CREATE", "ALTER", "DROP"].includes((tokens[index - 1]?.value || "").toUpperCase())) {
|
|
110
|
+
expectRelation = true;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (expectRelation) {
|
|
114
|
+
let bareIndex = index;
|
|
115
|
+
if (expectBareDatabase) {
|
|
116
|
+
while (["IF", "NOT", "EXISTS"].includes((tokens[bareIndex]?.value || "").toUpperCase())) {
|
|
117
|
+
bareIndex++;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const database = expectBareDatabase && tokens[bareIndex]?.identifier
|
|
121
|
+
? tokens[bareIndex].value
|
|
122
|
+
: checkTargetAt(tokens, index);
|
|
123
|
+
if (database)
|
|
124
|
+
disallowed.add(database);
|
|
125
|
+
expectRelation = false;
|
|
126
|
+
expectBareDatabase = false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (/^\s*CREATE\s+(?:(?:UNIQUE|FULLTEXT|SPATIAL)\s+)?INDEX\b/i.test(sql) ||
|
|
130
|
+
/^\s*CREATE\s+TRIGGER\b/i.test(sql)) {
|
|
131
|
+
const onIndex = tokens.findIndex((token) => token.value.toUpperCase() === "ON");
|
|
132
|
+
if (onIndex >= 0) {
|
|
133
|
+
const database = checkTargetAt(tokens, onIndex + 1);
|
|
134
|
+
if (database)
|
|
135
|
+
disallowed.add(database);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (/^\s*CREATE\s+TABLE\b/i.test(sql)) {
|
|
139
|
+
const likeIndex = tokens.findIndex((token) => token.value.toUpperCase() === "LIKE");
|
|
140
|
+
if (likeIndex >= 0) {
|
|
141
|
+
const database = checkTargetAt(tokens, likeIndex + 1);
|
|
142
|
+
if (database)
|
|
143
|
+
disallowed.add(database);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// A schema-qualified function call also accesses that schema, though it is
|
|
147
|
+
// not introduced by a table keyword.
|
|
148
|
+
for (let index = 0; index + 3 < tokens.length; index++) {
|
|
149
|
+
if (tokens[index].identifier && tokens[index + 1].value === "." &&
|
|
150
|
+
tokens[index + 2].identifier && tokens[index + 3].value === "(") {
|
|
151
|
+
disallowed.add(tokens[index].value);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
for (let index = 0; index + 4 < tokens.length; index++) {
|
|
155
|
+
if (tokens[index].identifier && tokens[index + 1].value === "." &&
|
|
156
|
+
tokens[index + 2].identifier && tokens[index + 3].value === "." &&
|
|
157
|
+
(tokens[index + 4].identifier || tokens[index + 4].value === "*")) {
|
|
158
|
+
disallowed.add(tokens[index].value);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// SHOW TABLES FROM db uses a bare database name rather than db.table.
|
|
162
|
+
if (/^\s*SHOW\s+(?:FULL\s+)?(?:TABLES|TABLE\s+STATUS)\b/i.test(sql)) {
|
|
163
|
+
const showFrom = tokens.map((token, index) => token.value.toUpperCase() === "FROM" ? index : -1).filter((index) => index >= 0).pop();
|
|
164
|
+
if (showFrom !== undefined && tokens[showFrom + 1]?.identifier && tokens[showFrom + 2]?.value !== ".") {
|
|
165
|
+
disallowed.add(tokens[showFrom + 1].value);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (/^\s*SHOW\s+(?:FULL\s+)?(?:COLUMNS|FIELDS|INDEX|INDEXES|KEYS)\b/i.test(sql)) {
|
|
169
|
+
const showFroms = tokens.map((token, index) => token.value.toUpperCase() === "FROM" ? index : -1).filter((index) => index >= 0);
|
|
170
|
+
const databaseFrom = showFroms.length > 1 ? showFroms[showFroms.length - 1] : -1;
|
|
171
|
+
if (databaseFrom >= 0 && tokens[databaseFrom + 1]?.identifier && tokens[databaseFrom + 2]?.value !== ".") {
|
|
172
|
+
disallowed.add(tokens[databaseFrom + 1].value);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const denied = [...disallowed].filter((database) => !(0, databaseScope_1.isDatabaseAllowed)(database));
|
|
176
|
+
if (denied.length) {
|
|
177
|
+
return {
|
|
178
|
+
valid: false,
|
|
179
|
+
error: `Access denied. SQL references database(s) outside the allowed list: ${denied.join(", ")}.`,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
return { valid: true };
|
|
183
|
+
};
|
|
184
|
+
exports.validateDatabaseQueryScope = validateDatabaseQueryScope;
|
|
@@ -7,6 +7,7 @@ exports.SecurityLayer = void 0;
|
|
|
7
7
|
const ajv_1 = __importDefault(require("ajv"));
|
|
8
8
|
const featureConfig_1 = require("../config/featureConfig");
|
|
9
9
|
const maskingLayer_1 = require("./maskingLayer");
|
|
10
|
+
const databaseQueryScope_1 = require("./databaseQueryScope");
|
|
10
11
|
class SecurityLayer {
|
|
11
12
|
constructor(featureConfig) {
|
|
12
13
|
this.ajv = new ajv_1.default();
|
|
@@ -187,6 +188,10 @@ class SecurityLayer {
|
|
|
187
188
|
if (!dangerousCheck.valid) {
|
|
188
189
|
return dangerousCheck;
|
|
189
190
|
}
|
|
191
|
+
const databaseScopeCheck = (0, databaseQueryScope_1.validateDatabaseQueryScope)(query);
|
|
192
|
+
if (!databaseScopeCheck.valid) {
|
|
193
|
+
return databaseScopeCheck;
|
|
194
|
+
}
|
|
190
195
|
// MySQL 8.0.18+ actually executes the statement wrapped by EXPLAIN ANALYZE,
|
|
191
196
|
// including multi-table UPDATE/DELETE. Such queries are authorized as the
|
|
192
197
|
// inner statement instead of being treated as read-only information queries.
|
|
@@ -444,8 +449,9 @@ class SecurityLayer {
|
|
|
444
449
|
*/
|
|
445
450
|
detectDangerousKeywords(cleanQuery, allowAdvancedQueries) {
|
|
446
451
|
// Remove string literals to avoid false positives, and unwrap quoted
|
|
447
|
-
// identifiers so `mysql`.`user` is still detected
|
|
448
|
-
|
|
452
|
+
// identifiers so `mysql`.`user` is still detected. MySQL allows whitespace
|
|
453
|
+
// around the qualifier dot, so `mysql . user` is joined to mysql.user.
|
|
454
|
+
const queryWithoutStrings = (this.maskQuotedContent(cleanQuery, true) ?? cleanQuery.replace(/`/g, "")).replace(/\s*\.\s*/g, ".");
|
|
449
455
|
for (const keyword of this.dangerousKeywords) {
|
|
450
456
|
// Enhanced regex with word boundaries and case insensitivity
|
|
451
457
|
const keywordRegex = new RegExp(`\\b${keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, 'i');
|
|
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.AnalysisTools = void 0;
|
|
7
|
+
const databaseScope_1 = require("../config/databaseScope");
|
|
7
8
|
const connection_1 = __importDefault(require("../db/connection"));
|
|
8
|
-
const config_1 = require("../config/config");
|
|
9
9
|
class AnalysisTools {
|
|
10
10
|
constructor(security) {
|
|
11
11
|
this.db = connection_1.default.getInstance();
|
|
@@ -15,31 +15,7 @@ class AnalysisTools {
|
|
|
15
15
|
* Validate database access - ensures only the connected database can be accessed
|
|
16
16
|
*/
|
|
17
17
|
validateDatabaseAccess(requestedDatabase) {
|
|
18
|
-
|
|
19
|
-
if (!connectedDatabase) {
|
|
20
|
-
return {
|
|
21
|
-
valid: false,
|
|
22
|
-
database: "",
|
|
23
|
-
error: "No database specified in connection string. Cannot access any database.",
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
if (!requestedDatabase) {
|
|
27
|
-
return {
|
|
28
|
-
valid: true,
|
|
29
|
-
database: connectedDatabase,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
if (requestedDatabase !== connectedDatabase) {
|
|
33
|
-
return {
|
|
34
|
-
valid: false,
|
|
35
|
-
database: "",
|
|
36
|
-
error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
return {
|
|
40
|
-
valid: true,
|
|
41
|
-
database: connectedDatabase,
|
|
42
|
-
};
|
|
18
|
+
return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
|
|
43
19
|
}
|
|
44
20
|
clampNumber(value, fallback, min, max) {
|
|
45
21
|
const numeric = Number(value ?? fallback);
|
|
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ConstraintTools = void 0;
|
|
7
|
+
const databaseScope_1 = require("../config/databaseScope");
|
|
7
8
|
const connection_1 = __importDefault(require("../db/connection"));
|
|
8
|
-
const config_1 = require("../config/config");
|
|
9
9
|
class ConstraintTools {
|
|
10
10
|
constructor(security) {
|
|
11
11
|
this.db = connection_1.default.getInstance();
|
|
@@ -15,31 +15,7 @@ class ConstraintTools {
|
|
|
15
15
|
* Validate database access - ensures only the connected database can be accessed
|
|
16
16
|
*/
|
|
17
17
|
validateDatabaseAccess(requestedDatabase) {
|
|
18
|
-
|
|
19
|
-
if (!connectedDatabase) {
|
|
20
|
-
return {
|
|
21
|
-
valid: false,
|
|
22
|
-
database: "",
|
|
23
|
-
error: "No database specified in connection string. Cannot access any database.",
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
if (!requestedDatabase) {
|
|
27
|
-
return {
|
|
28
|
-
valid: true,
|
|
29
|
-
database: connectedDatabase,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
if (requestedDatabase !== connectedDatabase) {
|
|
33
|
-
return {
|
|
34
|
-
valid: false,
|
|
35
|
-
database: "",
|
|
36
|
-
error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
return {
|
|
40
|
-
valid: true,
|
|
41
|
-
database: connectedDatabase,
|
|
42
|
-
};
|
|
18
|
+
return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
|
|
43
19
|
}
|
|
44
20
|
/**
|
|
45
21
|
* List all foreign keys for a table
|
package/dist/tools/crudTools.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.CrudTools = void 0;
|
|
7
7
|
const connection_1 = __importDefault(require("../db/connection"));
|
|
8
|
+
const databaseScope_1 = require("../config/databaseScope");
|
|
8
9
|
const schemas_1 = require("../validation/schemas");
|
|
9
10
|
const inputValidation_1 = require("../validation/inputValidation");
|
|
10
11
|
class CrudTools {
|
|
@@ -105,6 +106,10 @@ class CrudTools {
|
|
|
105
106
|
}
|
|
106
107
|
try {
|
|
107
108
|
const { table_name, filters, pagination, sorting } = params;
|
|
109
|
+
const databaseAccess = (0, databaseScope_1.validateDatabaseAccess)(params.database);
|
|
110
|
+
if (!databaseAccess.valid) {
|
|
111
|
+
return { status: "error", error: databaseAccess.error };
|
|
112
|
+
}
|
|
108
113
|
// Enhanced validation using new validation functions
|
|
109
114
|
const tableNameValidation = (0, inputValidation_1.validateTableName)(table_name);
|
|
110
115
|
if (!tableNameValidation.valid) {
|
|
@@ -207,7 +212,7 @@ class CrudTools {
|
|
|
207
212
|
}
|
|
208
213
|
// Build the LIMIT clause if pagination is provided
|
|
209
214
|
let limitClause = "";
|
|
210
|
-
const escapedTableName = this.security.escapeIdentifier(sanitizedTableName)
|
|
215
|
+
const escapedTableName = `${(0, databaseScope_1.quoteDatabaseIdentifier)(databaseAccess.database)}.${this.security.escapeIdentifier(sanitizedTableName)}`;
|
|
211
216
|
if (pagination) {
|
|
212
217
|
const offset = (pagination.page - 1) * pagination.limit;
|
|
213
218
|
limitClause = `LIMIT ${offset}, ${pagination.limit}`;
|
|
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.DataExportTools = void 0;
|
|
7
|
+
const databaseScope_1 = require("../config/databaseScope");
|
|
7
8
|
const connection_1 = __importDefault(require("../db/connection"));
|
|
8
|
-
const config_1 = require("../config/config");
|
|
9
9
|
class DataExportTools {
|
|
10
10
|
constructor(security) {
|
|
11
11
|
this.db = connection_1.default.getInstance();
|
|
@@ -15,25 +15,7 @@ class DataExportTools {
|
|
|
15
15
|
* Validate database access
|
|
16
16
|
*/
|
|
17
17
|
validateDatabaseAccess(requestedDatabase) {
|
|
18
|
-
|
|
19
|
-
if (!connectedDatabase) {
|
|
20
|
-
return {
|
|
21
|
-
valid: false,
|
|
22
|
-
database: "",
|
|
23
|
-
error: "No database configured. Please specify a database in your connection settings.",
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
if (requestedDatabase && requestedDatabase !== connectedDatabase) {
|
|
27
|
-
return {
|
|
28
|
-
valid: false,
|
|
29
|
-
database: "",
|
|
30
|
-
error: `Access denied: You are connected to '${connectedDatabase}' but requested '${requestedDatabase}'. Cross-database access is not permitted.`,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
return {
|
|
34
|
-
valid: true,
|
|
35
|
-
database: connectedDatabase,
|
|
36
|
-
};
|
|
18
|
+
return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
|
|
37
19
|
}
|
|
38
20
|
/**
|
|
39
21
|
* Escape string value for SQL INSERT statements
|
|
@@ -99,6 +81,10 @@ class DataExportTools {
|
|
|
99
81
|
return { status: "error", error: readPermission.error };
|
|
100
82
|
}
|
|
101
83
|
const { table_name, filters = [], pagination, sorting, include_headers = true, } = params;
|
|
84
|
+
const databaseAccess = this.validateDatabaseAccess(params.database);
|
|
85
|
+
if (!databaseAccess.valid) {
|
|
86
|
+
return { status: "error", error: databaseAccess.error };
|
|
87
|
+
}
|
|
102
88
|
// Validate table name
|
|
103
89
|
const tableValidation = this.security.validateIdentifier(table_name);
|
|
104
90
|
if (!tableValidation.valid) {
|
|
@@ -197,7 +183,7 @@ class DataExportTools {
|
|
|
197
183
|
}
|
|
198
184
|
// Construct the query
|
|
199
185
|
const escapedTableName = this.security.escapeIdentifier(table_name);
|
|
200
|
-
const query = `SELECT * FROM ${escapedTableName} ${whereClause} ${orderByClause} ${limitClause}`;
|
|
186
|
+
const query = `SELECT * FROM ${(0, databaseScope_1.quoteDatabaseIdentifier)(databaseAccess.database)}.${escapedTableName} ${whereClause} ${orderByClause} ${limitClause}`;
|
|
201
187
|
// Execute query
|
|
202
188
|
const results = await this.db.query(query, whereParams);
|
|
203
189
|
// If no results, return empty CSV
|
|
@@ -6,7 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.DatabaseTools = void 0;
|
|
7
7
|
const connection_1 = __importDefault(require("../db/connection"));
|
|
8
8
|
const schemas_1 = require("../validation/schemas");
|
|
9
|
-
const
|
|
9
|
+
const databaseScope_1 = require("../config/databaseScope");
|
|
10
|
+
const quoteDatabase = (database) => `\`${database.replace(/`/g, "``")}\``;
|
|
10
11
|
class DatabaseTools {
|
|
11
12
|
constructor() {
|
|
12
13
|
this.db = connection_1.default.getInstance();
|
|
@@ -17,26 +18,16 @@ class DatabaseTools {
|
|
|
17
18
|
*/
|
|
18
19
|
async listDatabases() {
|
|
19
20
|
try {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (!config_1.dbConfig.database) {
|
|
21
|
+
const databases = (0, databaseScope_1.getAllowedDatabases)();
|
|
22
|
+
if (databases.length === 0) {
|
|
23
23
|
return {
|
|
24
24
|
status: "error",
|
|
25
|
-
error: "No database specified in connection string
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
// Verify the database exists and is accessible
|
|
29
|
-
const results = await this.db.query("SELECT DATABASE() as current_database");
|
|
30
|
-
const currentDatabase = results[0]?.current_database;
|
|
31
|
-
if (!currentDatabase) {
|
|
32
|
-
return {
|
|
33
|
-
status: "error",
|
|
34
|
-
error: "No database selected. Please ensure your connection string includes a valid database name.",
|
|
25
|
+
error: "No database specified in the connection string or database allowlist.",
|
|
35
26
|
};
|
|
36
27
|
}
|
|
37
28
|
return {
|
|
38
29
|
status: "success",
|
|
39
|
-
data:
|
|
30
|
+
data: databases,
|
|
40
31
|
};
|
|
41
32
|
}
|
|
42
33
|
catch (error) {
|
|
@@ -58,26 +49,11 @@ class DatabaseTools {
|
|
|
58
49
|
};
|
|
59
50
|
}
|
|
60
51
|
try {
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
return {
|
|
65
|
-
status: "error",
|
|
66
|
-
error: "No database specified in connection string. Cannot access other databases.",
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
if (params.database !== config_1.dbConfig.database) {
|
|
70
|
-
return {
|
|
71
|
-
status: "error",
|
|
72
|
-
error: `Access denied. You can only access the connected database '${config_1.dbConfig.database}'. Requested database '${params.database}' is not allowed.`,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
let query = "SHOW TABLES";
|
|
77
|
-
// If database is specified and validated, use it
|
|
78
|
-
if (params.database) {
|
|
79
|
-
query = `SHOW TABLES FROM \`${params.database}\``;
|
|
52
|
+
const databaseAccess = (0, databaseScope_1.validateDatabaseAccess)(params.database);
|
|
53
|
+
if (!databaseAccess.valid) {
|
|
54
|
+
return { status: "error", error: databaseAccess.error };
|
|
80
55
|
}
|
|
56
|
+
const query = `SHOW TABLES FROM ${quoteDatabase(databaseAccess.database)}`;
|
|
81
57
|
const results = await this.db.query(query);
|
|
82
58
|
const tables = results.map((row) => {
|
|
83
59
|
// Extract the table name from the first column (which might have different names)
|
|
@@ -109,6 +85,10 @@ class DatabaseTools {
|
|
|
109
85
|
};
|
|
110
86
|
}
|
|
111
87
|
try {
|
|
88
|
+
const databaseAccess = (0, databaseScope_1.validateDatabaseAccess)(params.database);
|
|
89
|
+
if (!databaseAccess.valid) {
|
|
90
|
+
return { status: "error", error: databaseAccess.error };
|
|
91
|
+
}
|
|
112
92
|
const query = `
|
|
113
93
|
SELECT
|
|
114
94
|
COLUMN_NAME as column_name,
|
|
@@ -126,10 +106,7 @@ class DatabaseTools {
|
|
|
126
106
|
ORDER BY
|
|
127
107
|
ORDINAL_POSITION
|
|
128
108
|
`;
|
|
129
|
-
const results = await this.db.query(query, [
|
|
130
|
-
config_1.dbConfig.database,
|
|
131
|
-
params.table_name,
|
|
132
|
-
]);
|
|
109
|
+
const results = await this.db.query(query, [databaseAccess.database, params.table_name]);
|
|
133
110
|
return {
|
|
134
111
|
status: "success",
|
|
135
112
|
data: results,
|
|
@@ -149,19 +126,11 @@ class DatabaseTools {
|
|
|
149
126
|
async getDatabaseSummary(params) {
|
|
150
127
|
try {
|
|
151
128
|
// Security validation
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
error: `Access denied. You can only access the connected database '${config_1.dbConfig.database}'.`,
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
const database = params.database || config_1.dbConfig.database;
|
|
159
|
-
if (!database) {
|
|
160
|
-
return {
|
|
161
|
-
status: "error",
|
|
162
|
-
error: "No database specified and none connected.",
|
|
163
|
-
};
|
|
129
|
+
const access = (0, databaseScope_1.validateDatabaseAccess)(params.database);
|
|
130
|
+
if (!access.valid) {
|
|
131
|
+
return { status: "error", error: access.error };
|
|
164
132
|
}
|
|
133
|
+
const database = access.database;
|
|
165
134
|
const maxTables = params.max_tables ? Math.min(Math.max(params.max_tables, 1), 500) : undefined;
|
|
166
135
|
const includeRelationships = params.include_relationships ?? true;
|
|
167
136
|
// Get total table count first
|
|
@@ -287,19 +256,11 @@ class DatabaseTools {
|
|
|
287
256
|
async getSchemaERD(params) {
|
|
288
257
|
try {
|
|
289
258
|
// Security validation
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
error: `Access denied. You can only access the connected database '${config_1.dbConfig.database}'.`,
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
const database = params.database || config_1.dbConfig.database;
|
|
297
|
-
if (!database) {
|
|
298
|
-
return {
|
|
299
|
-
status: "error",
|
|
300
|
-
error: "No database specified and none connected.",
|
|
301
|
-
};
|
|
259
|
+
const access = (0, databaseScope_1.validateDatabaseAccess)(params.database);
|
|
260
|
+
if (!access.valid) {
|
|
261
|
+
return { status: "error", error: access.error };
|
|
302
262
|
}
|
|
263
|
+
const database = access.database;
|
|
303
264
|
// Get tables and columns
|
|
304
265
|
const columnsQuery = `
|
|
305
266
|
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_KEY
|
|
@@ -4,39 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.FulltextSearchTools = void 0;
|
|
7
|
+
const databaseScope_1 = require("../config/databaseScope");
|
|
7
8
|
const connection_1 = __importDefault(require("../db/connection"));
|
|
8
|
-
const config_1 = require("../config/config");
|
|
9
9
|
class FulltextSearchTools {
|
|
10
10
|
constructor(security) {
|
|
11
11
|
this.db = connection_1.default.getInstance();
|
|
12
12
|
this.security = security;
|
|
13
13
|
}
|
|
14
14
|
validateDatabaseAccess(requestedDatabase) {
|
|
15
|
-
|
|
16
|
-
if (!connectedDatabase) {
|
|
17
|
-
return {
|
|
18
|
-
valid: false,
|
|
19
|
-
database: "",
|
|
20
|
-
error: "No database specified in connection string. Cannot access any database.",
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
if (!requestedDatabase) {
|
|
24
|
-
return {
|
|
25
|
-
valid: true,
|
|
26
|
-
database: connectedDatabase,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
if (requestedDatabase !== connectedDatabase) {
|
|
30
|
-
return {
|
|
31
|
-
valid: false,
|
|
32
|
-
database: "",
|
|
33
|
-
error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
valid: true,
|
|
38
|
-
database: connectedDatabase,
|
|
39
|
-
};
|
|
15
|
+
return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
|
|
40
16
|
}
|
|
41
17
|
async createFulltextIndex(params) {
|
|
42
18
|
try {
|
package/dist/tools/indexTools.js
CHANGED
|
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.IndexTools = void 0;
|
|
7
|
+
const databaseScope_1 = require("../config/databaseScope");
|
|
7
8
|
const connection_1 = __importDefault(require("../db/connection"));
|
|
8
|
-
const config_1 = require("../config/config");
|
|
9
9
|
class IndexTools {
|
|
10
10
|
constructor(security) {
|
|
11
11
|
this.db = connection_1.default.getInstance();
|
|
@@ -15,31 +15,7 @@ class IndexTools {
|
|
|
15
15
|
* Validate database access - ensures only the connected database can be accessed
|
|
16
16
|
*/
|
|
17
17
|
validateDatabaseAccess(requestedDatabase) {
|
|
18
|
-
|
|
19
|
-
if (!connectedDatabase) {
|
|
20
|
-
return {
|
|
21
|
-
valid: false,
|
|
22
|
-
database: "",
|
|
23
|
-
error: "No database specified in connection string. Cannot access any database.",
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
if (!requestedDatabase) {
|
|
27
|
-
return {
|
|
28
|
-
valid: true,
|
|
29
|
-
database: connectedDatabase,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
if (requestedDatabase !== connectedDatabase) {
|
|
33
|
-
return {
|
|
34
|
-
valid: false,
|
|
35
|
-
database: "",
|
|
36
|
-
error: `Access denied. You can only access the connected database '${connectedDatabase}'. Requested database '${requestedDatabase}' is not allowed.`,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
return {
|
|
40
|
-
valid: true,
|
|
41
|
-
database: connectedDatabase,
|
|
42
|
-
};
|
|
18
|
+
return (0, databaseScope_1.validateDatabaseAccess)(requestedDatabase);
|
|
43
19
|
}
|
|
44
20
|
/**
|
|
45
21
|
* List all indexes for a table
|
|
@@ -78,7 +78,9 @@ export declare class MaintenanceTools {
|
|
|
78
78
|
error?: string;
|
|
79
79
|
}>;
|
|
80
80
|
/**
|
|
81
|
-
* Flush table (close and reopen)
|
|
81
|
+
* Flush table(s) of the connected database (close and reopen).
|
|
82
|
+
* WITH READ LOCK is not supported: the lock would stay on a pooled
|
|
83
|
+
* connection and, without a table name, block writes server-wide.
|
|
82
84
|
*/
|
|
83
85
|
flushTable(params: {
|
|
84
86
|
table_name?: string;
|