@nocobase/plugin-collection-sql 1.6.0-alpha.2 → 1.6.0-alpha.20

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.
@@ -8,10 +8,10 @@
8
8
  */
9
9
 
10
10
  module.exports = {
11
- "@nocobase/client": "1.6.0-alpha.2",
12
- "@nocobase/server": "1.6.0-alpha.2",
13
- "@nocobase/database": "1.6.0-alpha.2",
14
- "@nocobase/actions": "1.6.0-alpha.2",
11
+ "@nocobase/client": "1.6.0-alpha.20",
12
+ "@nocobase/server": "1.6.0-alpha.20",
13
+ "@nocobase/database": "1.6.0-alpha.20",
14
+ "@nocobase/actions": "1.6.0-alpha.20",
15
15
  "sequelize": "6.35.2",
16
- "@nocobase/utils": "1.6.0-alpha.2"
16
+ "@nocobase/utils": "1.6.0-alpha.20"
17
17
  };
@@ -43,6 +43,7 @@ module.exports = __toCommonJS(plugin_exports);
43
43
  var import_server = require("@nocobase/server");
44
44
  var import_sql_collection = require("./sql-collection");
45
45
  var import_sql = __toESM(require("./resources/sql"));
46
+ var import_utils = require("./utils");
46
47
  class PluginCollectionSQLServer extends import_server.Plugin {
47
48
  async beforeLoad() {
48
49
  this.app.db.collectionFactory.registerCollectionType(import_sql_collection.SQLCollection, {
@@ -61,6 +62,20 @@ class PluginCollectionSQLServer extends import_server.Plugin {
61
62
  name: `pm.data-source-manager.collection-sql `,
62
63
  actions: ["sqlCollection:*"]
63
64
  });
65
+ this.app.resourceManager.use(async (ctx, next) => {
66
+ const { resourceName, actionName } = ctx.action;
67
+ if (resourceName === "collections" && actionName === "create") {
68
+ const { sql } = ctx.action.params.values || {};
69
+ if (sql) {
70
+ try {
71
+ (0, import_utils.checkSQL)(sql);
72
+ } catch (e) {
73
+ ctx.throw(400, ctx.t(e.message));
74
+ }
75
+ }
76
+ }
77
+ return next();
78
+ });
64
79
  }
65
80
  }
66
81
  var plugin_default = PluginCollectionSQLServer;
@@ -30,7 +30,9 @@ __export(sql_exports, {
30
30
  });
31
31
  module.exports = __toCommonJS(sql_exports);
32
32
  var import_sql_collection = require("../sql-collection");
33
+ var import_utils = require("../utils");
33
34
  const updateCollection = async (ctx, transaction) => {
35
+ var _a;
34
36
  const { filterByTk, values } = ctx.action.params;
35
37
  const repo = ctx.db.getRepository("collections");
36
38
  const collection = await repo.findOne({
@@ -41,15 +43,21 @@ const updateCollection = async (ctx, transaction) => {
41
43
  });
42
44
  const existFields = await collection.getFields({ transaction });
43
45
  const deletedFields = existFields.filter((field) => {
44
- var _a;
45
- return !((_a = values.fields) == null ? void 0 : _a.find((f) => f.name === field.name));
46
+ var _a2;
47
+ return !((_a2 = values.fields) == null ? void 0 : _a2.find((f) => f.name === field.name));
46
48
  });
47
49
  for (const field of deletedFields) {
48
50
  await field.destroy({ transaction });
49
51
  }
50
52
  const upRes = await repo.update({
51
53
  filterByTk,
52
- values,
54
+ values: {
55
+ ...values,
56
+ fields: (_a = values.fields) == null ? void 0 : _a.map((f) => {
57
+ delete f.key;
58
+ return f;
59
+ })
60
+ },
53
61
  updateAssociationValues: ["fields"],
54
62
  transaction
55
63
  });
@@ -59,13 +67,14 @@ var sql_default = {
59
67
  name: "sqlCollection",
60
68
  actions: {
61
69
  execute: async (ctx, next) => {
62
- let { sql } = ctx.action.params.values || {};
70
+ const { sql } = ctx.action.params.values || {};
63
71
  if (!sql) {
64
72
  ctx.throw(400, ctx.t("Please enter a SQL statement"));
65
73
  }
66
- sql = sql.trim().split(";").shift();
67
- if (!/^select/i.test(sql) && !/^with([\s\S]+)select([\s\S]+)/i.test(sql)) {
68
- ctx.throw(400, ctx.t("Only supports SELECT statements or WITH clauses"));
74
+ try {
75
+ (0, import_utils.checkSQL)(sql);
76
+ } catch (e) {
77
+ ctx.throw(400, ctx.t(e.message));
69
78
  }
70
79
  const tmpCollection = new import_sql_collection.SQLCollection({ name: "tmp", sql }, { database: ctx.db });
71
80
  const model = tmpCollection.model;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ export declare const checkSQL: (sql: string) => void;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var utils_exports = {};
28
+ __export(utils_exports, {
29
+ checkSQL: () => checkSQL
30
+ });
31
+ module.exports = __toCommonJS(utils_exports);
32
+ const checkSQL = (sql) => {
33
+ const dangerKeywords = [
34
+ // PostgreSQL
35
+ "pg_read_file",
36
+ "pg_read_binary_file",
37
+ "pg_stat_file",
38
+ "pg_ls_dir",
39
+ "pg_logdir_ls",
40
+ "pg_terminate_backend",
41
+ "pg_cancel_backend",
42
+ "current_setting",
43
+ "set_config",
44
+ "pg_reload_conf",
45
+ "pg_sleep",
46
+ "generate_series",
47
+ // MySQL
48
+ "LOAD_FILE",
49
+ "BENCHMARK",
50
+ "@@global.",
51
+ "@@session.",
52
+ // SQLite
53
+ "sqlite3_load_extension",
54
+ "load_extension"
55
+ ];
56
+ sql = sql.trim().split(";").shift();
57
+ if (!/^select/i.test(sql) && !/^with([\s\S]+)select([\s\S]+)/i.test(sql)) {
58
+ throw new Error("Only supports SELECT statements or WITH clauses");
59
+ }
60
+ if (dangerKeywords.some((keyword) => sql.toLowerCase().includes(keyword.toLowerCase()))) {
61
+ throw new Error("SQL statements contain dangerous keywords");
62
+ }
63
+ };
64
+ // Annotate the CommonJS export names for ESM import in node:
65
+ 0 && (module.exports = {
66
+ checkSQL
67
+ });
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "displayName.zh-CN": "数据表: SQL",
5
5
  "description": "Provides SQL collection template",
6
6
  "description.zh-CN": "提供 SQL 数据表模板",
7
- "version": "1.6.0-alpha.2",
7
+ "version": "1.6.0-alpha.20",
8
8
  "homepage": "https://docs-cn.nocobase.com/handbook/collection-sql",
9
9
  "homepage.zh-CN": "https://docs-cn.nocobase.com/handbook/collection-sql",
10
10
  "main": "dist/server/index.js",
@@ -17,5 +17,5 @@
17
17
  "keywords": [
18
18
  "Collections"
19
19
  ],
20
- "gitHead": "08bbc34c21727fc0ad0880f397a42bf7741091ee"
20
+ "gitHead": "c127664eb2b900edd5c18c9344046cd663a06c3b"
21
21
  }