mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 14:01:42 +03:00
fix(database-builder): quote names and passwords in generated SQL
The database name, account and server address are prefilled from URL query parameters and were pasted into the script as is, so a crafted link could add statements to the SQL a user copies and runs (e.g. a database name containing `` `; DROP DATABASE prod; -- ``). A password containing `'` also produced invalid SQL. - Quote identifiers and string literals per dialect. - PostgreSQL passwords containing a backslash are emitted as E'' strings, which parse the same whatever standard_conforming_strings is set to. - Oracle has no escape for `"` in quoted names or passwords, so either one containing `"` shows a validation error instead of generating SQL. Plain Oracle names stay unquoted since quoting makes them case-sensitive. - Newlines in values that land in SQL comments are flattened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0203c2388a
commit
1b8d4879ac
2 files changed
+57
-17
No files matched your search
@@ -8151,6 +8151,8 @@ tools:
|
|||||||
tag-replication-client: REPLICATION CLIENT
|
tag-replication-client: REPLICATION CLIENT
|
||||||
tag-replication-slave: REPLICATION SLAVE
|
tag-replication-slave: REPLICATION SLAVE
|
||||||
tag-generate-sql-instructions: Generate SQL Instructions
|
tag-generate-sql-instructions: Generate SQL Instructions
|
||||||
|
error-oracle-password-double-quote: Oracle passwords cannot contain double quotes (")
|
||||||
|
error-oracle-account-double-quote: Oracle account names cannot contain double quotes (")
|
||||||
database-table-generator:
|
database-table-generator:
|
||||||
title: Database Table Generator
|
title: Database Table Generator
|
||||||
description: Generate SQL CREATE TABLE for common databases
|
description: Generate SQL CREATE TABLE for common databases
|
||||||
|
|||||||
@@ -14,12 +14,41 @@ const password = ref('');
|
|||||||
const permissions = ref<string[]>([]);
|
const permissions = ref<string[]>([]);
|
||||||
const sqlOutput = ref('');
|
const sqlOutput = ref('');
|
||||||
|
|
||||||
|
// Oracle quoted identifiers and passwords cannot contain `"`, and there is no way to escape it
|
||||||
|
const accountError = computed(() =>
|
||||||
|
dbType.value === 'oracle' && account.value.includes('"')
|
||||||
|
? t('tools.database-builder.texts.error-oracle-account-double-quote')
|
||||||
|
: undefined,
|
||||||
|
);
|
||||||
|
const passwordError = computed(() =>
|
||||||
|
dbType.value === 'oracle' && password.value.includes('"')
|
||||||
|
? t('tools.database-builder.texts.error-oracle-password-double-quote')
|
||||||
|
: undefined,
|
||||||
|
);
|
||||||
|
|
||||||
function generateRandomPassword(length = 12) {
|
function generateRandomPassword(length = 12) {
|
||||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*';
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*';
|
||||||
return multiRandFromArray([...chars], length).join('');
|
return multiRandFromArray([...chars], length).join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Quote user input so a quote, backtick or bracket cannot end the literal or identifier early
|
||||||
|
const sqlString = (value: string) => `'${value.replace(/'/g, "''")}'`;
|
||||||
|
// E'' treats backslashes as escapes whatever standard_conforming_strings is set to
|
||||||
|
const pgString = (value: string) =>
|
||||||
|
value.includes('\\') ? `E'${value.replace(/\\/g, '\\\\').replace(/'/g, "''")}'` : sqlString(value);
|
||||||
|
const mysqlString = (value: string) => `'${value.replace(/\\/g, '\\\\').replace(/'/g, "''")}'`;
|
||||||
|
const mysqlIdentifier = (value: string) => `\`${value.replace(/`/g, '``')}\``;
|
||||||
|
const pgIdentifier = (value: string) => `"${value.replace(/"/g, '""')}"`;
|
||||||
|
const sqlServerIdentifier = (value: string) => `[${value.replace(/]/g, ']]')}]`;
|
||||||
|
// Quoting makes Oracle names case-sensitive, so plain names stay unquoted (names with `"` are rejected by accountError)
|
||||||
|
const oracleIdentifier = (value: string) => (/^[A-Za-z][\w$#]*$/.test(value) ? value : `"${value}"`);
|
||||||
|
const sqlComment = (value: string) => value.replace(/[\r\n]+/g, ' ');
|
||||||
|
|
||||||
function generateSQL() {
|
function generateSQL() {
|
||||||
|
if (accountError.value || passwordError.value) {
|
||||||
|
sqlOutput.value = '';
|
||||||
|
return;
|
||||||
|
}
|
||||||
const pwd = password.value || generateRandomPassword();
|
const pwd = password.value || generateRandomPassword();
|
||||||
const perms = permissions.value.length > 0 ? permissions.value.join(', ') : 'ALL PRIVILEGES';
|
const perms = permissions.value.length > 0 ? permissions.value.join(', ') : 'ALL PRIVILEGES';
|
||||||
|
|
||||||
@@ -28,44 +57,44 @@ function generateSQL() {
|
|||||||
switch (dbType.value) {
|
switch (dbType.value) {
|
||||||
case 'mysql':
|
case 'mysql':
|
||||||
sql = `
|
sql = `
|
||||||
CREATE DATABASE IF NOT EXISTS \`${dbName.value}\`;
|
CREATE DATABASE IF NOT EXISTS ${mysqlIdentifier(dbName.value)};
|
||||||
CREATE USER IF NOT EXISTS '${account.value}'@'${serverAddress.value || '%'}' IDENTIFIED BY '${pwd}';
|
CREATE USER IF NOT EXISTS ${mysqlString(account.value)}@${mysqlString(serverAddress.value || '%')} IDENTIFIED BY ${mysqlString(pwd)};
|
||||||
GRANT ${perms} ON \`${dbName.value}\`.* TO '${account.value}'@'${serverAddress.value || '%'}';
|
GRANT ${perms} ON ${mysqlIdentifier(dbName.value)}.* TO ${mysqlString(account.value)}@${mysqlString(serverAddress.value || '%')};
|
||||||
FLUSH PRIVILEGES;
|
FLUSH PRIVILEGES;
|
||||||
`.trim();
|
`.trim();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'postgresql':
|
case 'postgresql':
|
||||||
sql = `
|
sql = `
|
||||||
CREATE DATABASE "${dbName.value}";
|
CREATE DATABASE ${pgIdentifier(dbName.value)};
|
||||||
CREATE ROLE "${account.value}" LOGIN PASSWORD '${pwd}';
|
CREATE ROLE ${pgIdentifier(account.value)} LOGIN PASSWORD ${pgString(pwd)};
|
||||||
GRANT ${perms} ON DATABASE "${dbName.value}" TO "${account.value}";
|
GRANT ${perms} ON DATABASE ${pgIdentifier(dbName.value)} TO ${pgIdentifier(account.value)};
|
||||||
`.trim();
|
`.trim();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'sqlserver':
|
case 'sqlserver':
|
||||||
sql = `
|
sql = `
|
||||||
CREATE DATABASE [${dbName.value}];
|
CREATE DATABASE ${sqlServerIdentifier(dbName.value)};
|
||||||
CREATE LOGIN [${account.value}] WITH PASSWORD = '${pwd}';
|
CREATE LOGIN ${sqlServerIdentifier(account.value)} WITH PASSWORD = ${sqlString(pwd)};
|
||||||
USE [${dbName.value}];
|
USE ${sqlServerIdentifier(dbName.value)};
|
||||||
CREATE USER [${account.value}] FOR LOGIN [${account.value}];
|
CREATE USER ${sqlServerIdentifier(account.value)} FOR LOGIN ${sqlServerIdentifier(account.value)};
|
||||||
GRANT ${perms} TO [${account.value}];
|
GRANT ${perms} TO ${sqlServerIdentifier(account.value)};
|
||||||
`.trim();
|
`.trim();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'oracle':
|
case 'oracle':
|
||||||
sql = `
|
sql = `
|
||||||
CREATE USER ${account.value} IDENTIFIED BY "${pwd}";
|
CREATE USER ${oracleIdentifier(account.value)} IDENTIFIED BY "${pwd}";
|
||||||
GRANT ${perms} TO ${account.value};
|
GRANT ${perms} TO ${oracleIdentifier(account.value)};
|
||||||
-- Oracle typically uses schemas; adjust database creation as needed
|
-- Oracle typically uses schemas; adjust database creation as needed
|
||||||
-- for quota, you may want: ALTER USER ${account.value} QUOTA UNLIMITED ON USERS;
|
-- for quota, you may want: ALTER USER ${sqlComment(oracleIdentifier(account.value))} QUOTA UNLIMITED ON USERS;
|
||||||
`.trim();
|
`.trim();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'sqlite':
|
case 'sqlite':
|
||||||
sql = `
|
sql = `
|
||||||
-- SQLite does not support user management or GRANT statements.
|
-- SQLite does not support user management or GRANT statements.
|
||||||
-- Database is created as a file: ${dbName.value}.db
|
-- Database is created as a file: ${sqlComment(dbName.value)}.db
|
||||||
-- Permissions are handled at the OS/file system level.
|
-- Permissions are handled at the OS/file system level.
|
||||||
`.trim();
|
`.trim();
|
||||||
break;
|
break;
|
||||||
@@ -99,11 +128,20 @@ GRANT ${perms} TO ${account.value};
|
|||||||
<NInput v-model:value="serverAddress" :placeholder="t('tools.database-builder.texts.placeholder-127-0-0-1')" />
|
<NInput v-model:value="serverAddress" :placeholder="t('tools.database-builder.texts.placeholder-127-0-0-1')" />
|
||||||
</NFormItem>
|
</NFormItem>
|
||||||
|
|
||||||
<NFormItem :label="t('tools.database-builder.texts.label-account')">
|
<NFormItem
|
||||||
|
:label="t('tools.database-builder.texts.label-account')"
|
||||||
|
:feedback="accountError"
|
||||||
|
:validation-status="accountError ? 'error' : undefined"
|
||||||
|
>
|
||||||
<NInput v-model:value="account" :placeholder="t('tools.database-builder.texts.placeholder-test')" />
|
<NInput v-model:value="account" :placeholder="t('tools.database-builder.texts.placeholder-test')" />
|
||||||
</NFormItem>
|
</NFormItem>
|
||||||
|
|
||||||
<NFormItem :label="t('tools.database-builder.texts.label-password-leave-empty-to-generate')" label-width="auto">
|
<NFormItem
|
||||||
|
:label="t('tools.database-builder.texts.label-password-leave-empty-to-generate')"
|
||||||
|
label-width="auto"
|
||||||
|
:feedback="passwordError"
|
||||||
|
:validation-status="passwordError ? 'error' : undefined"
|
||||||
|
>
|
||||||
<NInput
|
<NInput
|
||||||
v-model:value="password"
|
v-model:value="password"
|
||||||
type="password"
|
type="password"
|
||||||
|
|||||||
Reference in new issue
Block a user