Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(sql-editor): only select statements are allowed to execute (#323)
Yunwei Xiaoa8a13b305cb3
diff --git a/frontend/package.json b/frontend/package.jsonindex 4b07b38ab2..fff513d263 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -24,6 +24,7 @@"moment": "^2.29.1","monaco-editor": "^0.31.1","naive-ui": "^2.23.1",+ "node-sql-parser": "^4.0.3","postcss": "^8.3.5","slug": "^4.0.2","splitpanes": "^3.0.6",diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vueindex 6d4ece76d4..04f7e944f5 100644--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue@@ -8,7 +8,6 @@ import type { editor as Editor } from "monaco-editor";import setupMonaco from "./setupMonaco";import sqlFormatter from "./sqlFormatter";-import { SqlDialect } from "../../types";const props = defineProps({diff --git a/frontend/src/components/MonacoEditor/sqlParser.ts b/frontend/src/components/MonacoEditor/sqlParser.tsnew file mode 100644index 0000000000..3a62296328--- /dev/null+++ b/frontend/src/components/MonacoEditor/sqlParser.ts@@ -0,0 +1,27 @@+import { Parser, AST } from "node-sql-parser";++type ParseResult = {+ data: AST | AST[] | null;+ error: Error | null;+};++const parseSQL = (sql: string): ParseResult => {+ const parser = new Parser();++ try {+ const ast = parser.astify(sql);+ return { data: ast, error: null };+ } catch (error) {+ return { data: null, error: error as Error };+ }+};++export const isSelectStatement = (sql: string) => {+ const { data } = parseSQL(sql);+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment+ // @ts-ignore+ // if parser returns the null AST. it's an invalid sql statement+ return data?.type.toLowerCase() === "select" || data === null;+};++export default parseSQL;diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.tsnew file mode 100644index 0000000000..61e99fda0a--- /dev/null+++ b/frontend/src/composables/useExecuteSQL.ts@@ -0,0 +1,40 @@+import { isEmpty } from "lodash-es";+import { isSelectStatement } from "../components/MonacoEditor/sqlParser";++const useExecuteSQL = async (store: any) => {+ const queryStatement = store.state.sqlEditor.queryStatement;+ const selectedStatement = store.state.sqlEditor.selectedStatement;+ const sqlStatement = selectedStatement || queryStatement;++ if (!isEmpty(sqlStatement) && !isSelectStatement(sqlStatement)) {+ store.dispatch("notification/pushNotification", {+ module: "bytebase",+ style: "CRITICAL",+ title: "Only SELECT statements are allowed",+ });+ return;+ }++ try {+ const res = await store.dispatch("sqlEditor/executeQuery", {+ statement: sqlStatement,+ });++ if (res.error) {+ store.dispatch("notification/pushNotification", {+ module: "bytebase",+ style: "CRITICAL",+ title: res.error,+ });+ return;+ }+ } catch (error) {+ store.dispatch("notification/pushNotification", {+ module: "bytebase",+ style: "CRITICAL",+ title: error,+ });+ }+};++export { useExecuteSQL };diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.tsindex ec9efb4e39..216520f2ff 100644--- a/frontend/src/store/modules/sqlEditor.ts+++ b/frontend/src/store/modules/sqlEditor.ts@@ -133,7 +133,7 @@ const actions = {{ root: true });commit(types.SET_QUERY_RESULT, res.data);- return res.data;+ return res;},async fetchConnectionByInstanceIdAndDatabaseId({ commit, dispatch }: any,diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vueindex 9afe60aa66..cd1c11d0a0 100644--- a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue@@ -3,8 +3,8 @@<div class="actions-left w-1/2"><NButtontype="primary"- @click="handleExecuteQueries":disabled="isEmptyStatement"+ @click="handleExecuteQueries"><mdi:play class="h-5 w-5" /> {{ $t("common.run") }} (⌘+⏎)</NButton>@@ -48,6 +48,7 @@ import {SqlEditorActions,ConnectionContext,} from "../../../types";+import { useExecuteSQL } from "../../../composables/useExecuteSQL";const { connectionTree, connectionContext } =useNamespacedState<SqlEditorState>("sqlEditor", [@@ -58,32 +59,16 @@ const { isEmptyStatement } = useNamespacedGetters<SqlEditorGetters>("sqlEditor",["isEmptyStatement", "connectionInfo"]);-const { executeQuery, setConnectionContext } =- useNamespacedActions<SqlEditorActions>("sqlEditor", [- "executeQuery",- "setConnectionContext",- ]);+const { setConnectionContext } = useNamespacedActions<SqlEditorActions>(+ "sqlEditor",+ ["setConnectionContext"]+);const selectedConnection = ref();const isSeletedDatabase = ref(false);const store = useStore();-const handleExecuteQueries = async () => {- try {- const res = await executeQuery();- store.dispatch("notification/pushNotification", {- module: "bytebase",- style: "SUCCESS",- title: "Query executed successfully!",- });- } catch (error) {- store.dispatch("notification/pushNotification", {- module: "bytebase",- style: "CRITICAL",- title: error,- });- }-};+const handleExecuteQueries = () => useExecuteSQL(store);watch(() => connectionTree.value,diff --git a/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue b/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vueindex d839d45a34..5126018b61 100644--- a/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue+++ b/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue@@ -14,15 +14,15 @@ import { useNamespacedActions } from "vuex-composition-helpers";import { useStore } from "vuex";import { SqlEditorActions } from "../../../types";+import { useExecuteSQL } from "../../../composables/useExecuteSQL";const store = useStore();const sqlCode = ref("");-const { setSqlEditorState, executeQuery } =- useNamespacedActions<SqlEditorActions>("sqlEditor", [- "setSqlEditorState",- "executeQuery",- ]);+const { setSqlEditorState } = useNamespacedActions<SqlEditorActions>(+ "sqlEditor",+ ["setSqlEditorState"]+);const handleChange = debounce((value: string) => {setSqlEditorState({@@ -36,22 +36,5 @@ const handleChangeSelection = debounce((value: string) => {});}, 300);-const handleRunQuery = async (statement: string) => {- try {- const res = await executeQuery({- statement,- });- store.dispatch("notification/pushNotification", {- module: "bytebase",- style: "SUCCESS",- title: "Query executed successfully!",- });- } catch (error) {- store.dispatch("notification/pushNotification", {- module: "bytebase",- style: "CRITICAL",- title: error,- });- }-};+const handleRunQuery = () => useExecuteSQL(store);</script>diff --git a/frontend/vite.config.ts b/frontend/vite.config.tsindex 4e8dc06cf4..a51742d62c 100644--- a/frontend/vite.config.ts+++ b/frontend/vite.config.ts@@ -16,6 +16,9 @@ export default defineConfig(() => {process.env["GITPOD_WORKSPACE_ID"] !== null &&process.env["GITPOD_WORKSPACE_ID"] !== undefined;return {+ define: {+ global: {},+ },plugins: [vue(),// https://github.com/intlify/vite-plugin-vue-i18ndiff --git a/frontend/yarn.lock b/frontend/yarn.lockindex df3ea842bc..b42f53ac4b 100644--- a/frontend/yarn.lock+++ b/frontend/yarn.lock@@ -913,6 +913,11 @@ balanced-match@^1.0.0:resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==+big-integer@^1.6.48:+ version "1.6.51"+ resolved "https://registry.yarnpkg.com/big-integer/-/big-integer-1.6.51.tgz#0df92a5d9880560d3ff2d5fd20245c889d130686"+ integrity sha512-GPEid2Y9QU1Exl1rpO9B2IPJGHPSupF5GnVIP0blYvNOMer2bTvSWs1jGOUg04hTmu67nmLsQ9TBo1puaotBHg==+binary-extensions@^2.0.0:version "2.2.0"resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"@@ -3190,6 +3195,13 @@ node-releases@^2.0.1:resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.1.tgz#3d1d395f204f1f2f29a54358b9fb678765ad2fc5"integrity sha512-CqyzN6z7Q6aMeF/ktcMVTzhAHCEpf8SOarwpzpf8pNBY2k5/oM34UHldUwp8VKI7uxct2HxSRdJjBaZeESzcxA==+node-sql-parser@^4.0.3:+ version "4.0.3"+ resolved "https://registry.yarnpkg.com/node-sql-parser/-/node-sql-parser-4.0.3.tgz#8782b6f8cd2577e73ceec6318898723b2e89c455"+ integrity sha512-OySEmtUfwQbXDJiN4t6lQdAh9PNg5P2cE8l3KtOqGarVHDnJUK80miGP9lAc/IzVvKstvKvc0VY0XN2DIcl1RQ==+ dependencies:+ big-integer "^1.6.48"+version "1.0.10"resolved "https://registry.yarnpkg.com/nopt/-/nopt-1.0.10.tgz#6ddd21bd2a31417b92727dd585f8a6f37608ebee"