@berthojoris/mcp-mysql-server 1.46.0 → 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 +6 -0
- package/DOCUMENTATIONS.md +26 -2
- package/README.md +13 -1
- 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/index.d.ts +2 -0
- package/dist/mcp-server.js +12 -0
- package/dist/security/databaseQueryScope.d.ts +5 -0
- package/dist/security/databaseQueryScope.js +184 -0
- package/dist/security/securityLayer.js +5 -0
- 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.js +2 -26
- package/dist/tools/migrationTools.js +2 -20
- 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/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,12 @@ All notable changes to the MySQL MCP Server will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.47.0] - 2026-12-01
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- `--databases=<name,...>` and `MCP_ALLOWED_DATABASES` add database names to the schema in the MySQL URL, preserving the same tool permissions across the allowlist.
|
|
12
|
+
- Database-aware tools and qualified SQL now enforce the configured database allowlist; `list_databases` returns only configured databases.
|
|
13
|
+
|
|
8
14
|
## [1.46.0] - 2026-10-26
|
|
9
15
|
|
|
10
16
|
### Security
|
package/DOCUMENTATIONS.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# MySQL MCP Server - Documentation
|
|
2
2
|
|
|
3
|
-
**Last Updated:** 2026-
|
|
4
|
-
**Version:** 1.
|
|
3
|
+
**Last Updated:** 2026-12-01 14:33:14
|
|
4
|
+
**Version:** 1.47.0
|
|
5
5
|
**Total Tools:** 88
|
|
6
6
|
|
|
7
7
|
Comprehensive documentation for the MySQL MCP Server. For quick start, see [README.md](README.md).
|
|
@@ -48,6 +48,30 @@ Configure MySQL MCP with two access-control layers:
|
|
|
48
48
|
**Layer 1 (Permissions)**: Broad operation control
|
|
49
49
|
**Layer 2 (Categories)**: Fine-grained tool filtering
|
|
50
50
|
|
|
51
|
+
### Multiple database access
|
|
52
|
+
|
|
53
|
+
Use `--databases` to add databases to the one in the MySQL URL. The URL database remains the default for unqualified queries. Database-aware tools that accept a `database` argument (including `read_records`, `read_table_schema`, `list_tables`, and schema tools) can target any allowed database. The allowlist also applies to explicitly qualified SQL; cross-database queries are allowed only when every referenced database is in the allowlist. `list_databases` returns only allowed databases.
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"mcpServers": {
|
|
58
|
+
"mysql": {
|
|
59
|
+
"command": "npx",
|
|
60
|
+
"type": "stdio",
|
|
61
|
+
"args": [
|
|
62
|
+
"-y",
|
|
63
|
+
"@berthojoris/mcp-mysql-server",
|
|
64
|
+
"mysql://user:pass@localhost:3306/mydb",
|
|
65
|
+
"list,read,utility",
|
|
66
|
+
"--databases=database_a,database_b"
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
In this example, the server may access `mydb`, `database_a`, and `database_b`, with the same `list,read,utility` permissions. Omit `--databases` to retain single-database access. The option can also be set with `MCP_ALLOWED_DATABASES=database_a,database_b`. The MySQL account must still have grants for each allowed database; MySQL grants remain the database-level security boundary.
|
|
74
|
+
|
|
51
75
|
> **Credentials with special characters**: if the username or password in the connection string contains URL-reserved characters (`@`, `:`, `/`, `?`, `#`, `%`, `&`, etc.), URL-encode them — e.g. the password `p@ss:w0rd` becomes `p%40ss%3Aw0rd`, giving `mysql://user:p%40ss%3Aw0rd@host:3306/db`. The server passes both the username and password through `decodeURIComponent()` before connecting, including when only one of the two is set.
|
|
52
76
|
|
|
53
77
|
### Environment Variables
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
**A production-ready Model Context Protocol (MCP) server for MySQL database integration with AI agents**
|
|
6
6
|
|
|
7
|
-
**Last Updated:** 2026-
|
|
7
|
+
**Last Updated:** 2026-12-01 14:33:14
|
|
8
8
|
|
|
9
9
|
[](https://www.npmjs.com/package/@berthojoris/mcp-mysql-server)
|
|
10
10
|
[](https://www.npmjs.com/package/@berthojoris/mcp-mysql-server)
|
|
@@ -47,6 +47,18 @@ Add to your AI agent config (`.mcp.json`, `.cursor/mcp.json`, etc.):
|
|
|
47
47
|
|
|
48
48
|
For dual-layer and environment-variable configuration, see **[DOCUMENTATIONS.md → Configuration](DOCUMENTATIONS.md#configuration)**. To connect remote clients such as ChatGPT over HTTP, see [Remote Access](#remote-access-chatgpt--other-remote-mcp-clients).
|
|
49
49
|
|
|
50
|
+
To allow additional databases while keeping the same permissions, add `--databases` after the permissions argument. The database in the URL stays the default:
|
|
51
|
+
|
|
52
|
+
```json
|
|
53
|
+
"args": [
|
|
54
|
+
"-y",
|
|
55
|
+
"@berthojoris/mcp-mysql-server",
|
|
56
|
+
"mysql://user:pass@localhost:3306/mydb",
|
|
57
|
+
"list,read,utility",
|
|
58
|
+
"--databases=database_a,database_b"
|
|
59
|
+
]
|
|
60
|
+
```
|
|
61
|
+
|
|
50
62
|
---
|
|
51
63
|
|
|
52
64
|
## Installation
|
package/bin/mcp-mysql.js
CHANGED
|
@@ -17,6 +17,7 @@ const FLAG_ENV = {
|
|
|
17
17
|
transport: "MCP_TRANSPORT",
|
|
18
18
|
host: "MCP_HTTP_HOST",
|
|
19
19
|
port: "MCP_HTTP_PORT",
|
|
20
|
+
databases: "MCP_ALLOWED_DATABASES",
|
|
20
21
|
};
|
|
21
22
|
|
|
22
23
|
const positional = [];
|
|
@@ -37,7 +38,7 @@ for (let i = 0; i < rawArgs.length; i++) {
|
|
|
37
38
|
const envName = FLAG_ENV[flag];
|
|
38
39
|
if (!envName) {
|
|
39
40
|
console.error(`Error: unknown option ${arg}`);
|
|
40
|
-
console.error("Options: --transport=stdio|http, --http, --stdio, --host=<addr>, --port=<n
|
|
41
|
+
console.error("Options: --transport=stdio|http, --http, --stdio, --host=<addr>, --port=<n>, --databases=<name,...>");
|
|
41
42
|
process.exit(1);
|
|
42
43
|
}
|
|
43
44
|
const value = inlineValue !== undefined ? inlineValue : rawArgs[++i];
|
|
@@ -72,7 +73,7 @@ for (let i = 0; i < args.length; i++) {
|
|
|
72
73
|
if (!mysqlUrl) {
|
|
73
74
|
console.error("Error: MySQL connection URL is required");
|
|
74
75
|
console.error(
|
|
75
|
-
"Usage: mcp-mysql mysql://user:password@host:port/dbname [permissions] [categories] [--transport=stdio|http] [--host=<addr>] [--port=<n>]",
|
|
76
|
+
"Usage: mcp-mysql mysql://user:password@host:port/dbname [permissions] [categories] [--databases=<name,...>] [--transport=stdio|http] [--host=<addr>] [--port=<n>]",
|
|
76
77
|
);
|
|
77
78
|
console.error("");
|
|
78
79
|
console.error("Examples:");
|
package/dist/config/config.d.ts
CHANGED
package/dist/config/config.js
CHANGED
|
@@ -14,6 +14,10 @@ exports.dbConfig = {
|
|
|
14
14
|
password: process.env.DB_PASSWORD || '',
|
|
15
15
|
// Empty means no database selected; never default to the mysql system schema
|
|
16
16
|
database: process.env.DB_NAME || '',
|
|
17
|
+
allowedDatabases: (process.env.MCP_ALLOWED_DATABASES || '')
|
|
18
|
+
.split(',')
|
|
19
|
+
.map((database) => database.trim())
|
|
20
|
+
.filter(Boolean),
|
|
17
21
|
};
|
|
18
22
|
exports.default = {
|
|
19
23
|
database: exports.dbConfig,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface DatabaseAccessResult {
|
|
2
|
+
valid: boolean;
|
|
3
|
+
database: string;
|
|
4
|
+
error?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const getAllowedDatabases: () => string[];
|
|
7
|
+
export declare const isDatabaseAllowed: (database: string) => boolean;
|
|
8
|
+
export declare const validateDatabaseAccess: (requestedDatabase?: string) => DatabaseAccessResult;
|
|
9
|
+
export declare const requireAllowedDatabase: (requestedDatabase?: string) => string;
|
|
10
|
+
export declare const quoteDatabaseIdentifier: (database: string) => string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.quoteDatabaseIdentifier = exports.requireAllowedDatabase = exports.validateDatabaseAccess = exports.isDatabaseAllowed = exports.getAllowedDatabases = void 0;
|
|
4
|
+
const config_1 = require("./config");
|
|
5
|
+
const getAllowedDatabases = () => [...new Set([config_1.dbConfig.database, ...config_1.dbConfig.allowedDatabases].filter(Boolean))];
|
|
6
|
+
exports.getAllowedDatabases = getAllowedDatabases;
|
|
7
|
+
const isDatabaseAllowed = (database) => (0, exports.getAllowedDatabases)().includes(database);
|
|
8
|
+
exports.isDatabaseAllowed = isDatabaseAllowed;
|
|
9
|
+
const validateDatabaseAccess = (requestedDatabase) => {
|
|
10
|
+
const database = requestedDatabase || config_1.dbConfig.database;
|
|
11
|
+
if (!database) {
|
|
12
|
+
return {
|
|
13
|
+
valid: false,
|
|
14
|
+
database: "",
|
|
15
|
+
error: "No database specified in connection string or database allowlist.",
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
if (!(0, exports.isDatabaseAllowed)(database)) {
|
|
19
|
+
return {
|
|
20
|
+
valid: false,
|
|
21
|
+
database: "",
|
|
22
|
+
error: `Access denied. Database '${database}' is not in the allowed database list.`,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return { valid: true, database };
|
|
26
|
+
};
|
|
27
|
+
exports.validateDatabaseAccess = validateDatabaseAccess;
|
|
28
|
+
const requireAllowedDatabase = (requestedDatabase) => {
|
|
29
|
+
const result = (0, exports.validateDatabaseAccess)(requestedDatabase);
|
|
30
|
+
if (!result.valid)
|
|
31
|
+
throw new Error(result.error);
|
|
32
|
+
return result.database;
|
|
33
|
+
};
|
|
34
|
+
exports.requireAllowedDatabase = requireAllowedDatabase;
|
|
35
|
+
const quoteDatabaseIdentifier = (database) => `\`${database.replace(/`/g, "``")}\``;
|
|
36
|
+
exports.quoteDatabaseIdentifier = quoteDatabaseIdentifier;
|
package/dist/index.d.ts
CHANGED
|
@@ -40,6 +40,7 @@ export declare class MySQLMCP {
|
|
|
40
40
|
}>;
|
|
41
41
|
readTableSchema(params: {
|
|
42
42
|
table_name: string;
|
|
43
|
+
database?: string;
|
|
43
44
|
}): Promise<{
|
|
44
45
|
status: string;
|
|
45
46
|
data?: import("./validation/schemas").ColumnInfo[];
|
|
@@ -351,6 +352,7 @@ export declare class MySQLMCP {
|
|
|
351
352
|
}>;
|
|
352
353
|
exportTableToCSV(params: {
|
|
353
354
|
table_name: string;
|
|
355
|
+
database?: string;
|
|
354
356
|
filters?: any[];
|
|
355
357
|
pagination?: {
|
|
356
358
|
page: number;
|
package/dist/mcp-server.js
CHANGED
|
@@ -266,6 +266,10 @@ const TOOLS = (0, toolAnnotations_1.withToolAnnotations)([
|
|
|
266
266
|
type: "string",
|
|
267
267
|
description: "Name of the table to read schema from",
|
|
268
268
|
},
|
|
269
|
+
database: {
|
|
270
|
+
type: "string",
|
|
271
|
+
description: "Optional: allowed database name (defaults to the database in the connection URL)",
|
|
272
|
+
},
|
|
269
273
|
},
|
|
270
274
|
required: ["table_name"],
|
|
271
275
|
},
|
|
@@ -298,6 +302,10 @@ const TOOLS = (0, toolAnnotations_1.withToolAnnotations)([
|
|
|
298
302
|
type: "string",
|
|
299
303
|
description: "Name of the table to read from",
|
|
300
304
|
},
|
|
305
|
+
database: {
|
|
306
|
+
type: "string",
|
|
307
|
+
description: "Optional: allowed database name (defaults to the database in the connection URL)",
|
|
308
|
+
},
|
|
301
309
|
filters: {
|
|
302
310
|
type: "array",
|
|
303
311
|
description: "Array of filter conditions",
|
|
@@ -1345,6 +1353,10 @@ const TOOLS = (0, toolAnnotations_1.withToolAnnotations)([
|
|
|
1345
1353
|
type: "string",
|
|
1346
1354
|
description: "Name of the table to export",
|
|
1347
1355
|
},
|
|
1356
|
+
database: {
|
|
1357
|
+
type: "string",
|
|
1358
|
+
description: "Optional: allowed database name (defaults to the database in the connection URL)",
|
|
1359
|
+
},
|
|
1348
1360
|
filters: {
|
|
1349
1361
|
type: "array",
|
|
1350
1362
|
description: "Array of filter conditions",
|
|
@@ -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.
|
|
@@ -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
|