doomiwork 4.1.2 → 4.1.3

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.
@@ -129,7 +129,7 @@ module.exports.loadController=(app, routes)=> {
129
129
  if (modules.length > 0) {
130
130
  for (const modulePath of modules) {
131
131
  //if (app.logger) app.logger.trace('initlize controller [%s] for baseUrl [%s]', modulePath, baseUrl);
132
- console.log('initlize controller [%s] for baseUrl [%s]', modulePath, baseUrl);
132
+ // console.log('initlize controller [%s] for baseUrl [%s]', modulePath, baseUrl);
133
133
  const controllModule = require(modulePath);
134
134
  const controller = new controllModule(app);
135
135
  app.use(baseUrl, controller.router);
@@ -182,11 +182,12 @@ class controller {
182
182
  */
183
183
  async getListData(req, dataKey, configFile = 0) {
184
184
  if (this.logger) this.logger.trace("准备获取dataconfig文件中对应的 %s list数据",dataKey)
185
- const listinfo = getListInfo(req, dataKey, configFile,this._daoModel);
185
+ const ignorefilter = await this._redisHelper.get('key:ignorefilter');
186
+ const listinfo = getListInfo(req, dataKey, configFile, this._daoModel, ignorefilter==1);
186
187
  if (!listinfo) return { successed: false, errcode: -10, errmsg:`缺失${dataKey}对应的查询语句`};
187
188
 
188
189
  ////直接操作数据库之前,可由子类再次Handler
189
- let beforeData = await this.beforeAccessDB(req, listinfo.sql,null, "list", this);
190
+ let beforeData = await this.beforeAccessDB(req, listinfo.sql, listinfo.params, "list", this);
190
191
  ////如果返回Null,则表示不加载数据了
191
192
  if (beforeData.canceled === true) return { successed: false, errorcode: 1, errmsg: "操作取消" };
192
193
  ////操作数据库
@@ -101,17 +101,17 @@ class mysqlDao extends dao{
101
101
  /**
102
102
  * 删除记录
103
103
  */
104
- deleteSql(id) {
105
- const ids = (id + '').trim().split(',').map(x => '\'' + x + '\'');
104
+ deleteSql() {
105
+ // const ids = (id + '').trim().split(',').map(x => '\'' + x + '\'');
106
106
  ////如果是逻辑删除,则只将记录的删除状态设置为1
107
107
  if(this.tableoption.logicDelete) {
108
108
 
109
- let sqlDelete = `update ${this.tableoption.tableName} set #DELETEBY# #DELETEDATE# ${this.tableoption.logicDeleteField || 'rec_isdeleted'} =1 where ${this.tableoption.primaryKey} in (?) ${this.tableoption.forcefilter}`;
109
+ let sqlDelete = `update ${this.tableoption.tableName} set #DELETEBY# #DELETEDATE# ${this.tableoption.logicDeleteField || 'rec_isdeleted'} =1 where ${this.tableoption.primaryKey} =? ${this.tableoption.forcefilter}`;
110
110
  sqlDelete = sqlDelete.replace('#DELETEBY#', this.tableoption.logDeleteBy?`${this.tableoption.logDeleteBy}=?,`:'')
111
111
  sqlDelete = sqlDelete.replace('#DELETEDATE#', this.tableoption.logDeleteDate ? `${this.tableoption.logDeleteDate}=now(),` : '');
112
112
  return sqlDelete;
113
113
  }
114
- return `delete from ${this.tableoption.tableName} where ${this.tableoption.primaryKey} in (?) ${this.tableoption.forcefilter}`;
114
+ return `delete from ${this.tableoption.tableName} where ${this.tableoption.primaryKey} =? ${this.tableoption.forcefilter}`;
115
115
  }
116
116
 
117
117
  /**
@@ -131,9 +131,9 @@ class mysqlDao extends dao{
131
131
  async delete(Sql, id,userid) {
132
132
  const ids = (id + '').trim().split(',').map(x => '\'' + x + '\'');
133
133
  if (this.tableoption.logicDelete && this.tableoption.logDeleteBy){
134
- return this.executeSql(Sql, [userid, ids.join(',')]);
134
+ return this.executeSql(Sql, [userid, id]); //ids.join(',')]);
135
135
  }
136
- return this.executeSql(Sql, ids.join(','));
136
+ return this.executeSql(Sql, id); //ids.join(','));
137
137
  ///
138
138
  //return this.executeSql(Sql, id);
139
139
  /*if (result.successed && this.tableoption.logicDelete && userid && (this.tableoption.logDeleteBy || this.tableoption.logDeleteDate)){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doomiwork",
3
- "version": "4.1.2",
3
+ "version": "4.1.3",
4
4
  "description": "doomisoft nodejs web framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,226 @@
1
+ const { parseKeyValue, validatorParamsType } = require('./keywordparse');
2
+ const dataconfig = require('../configuration/dataconfig').getCurrent();
3
+ const mysql = require('mysql');
4
+ const ORDER_STRING = ['asc', 'desc']
5
+ const FORBID_SQL_KEYWORD = /;|(--)|(\bWHERE\b)|(\bCASE\b)|(\bWHEN\b)|(\bSLEEP\b)|(\bSHOW\b)|(\bCOUNT\(\b)|(\bCREATE\b)|(\bCALL\b)|(\bBY\b)|(\bORDER\b)|(\bJOIN\b)|(\bUNION\b)|(\bFROM\b)|(\bSELECT\b)|(\bDROP\b)|(\bTRUNCATE\b)|(\bDELETE\b)|(\bUPDATE\b)|(\bINSERT\b)|(\bEXEC\b)|(\bEXECUTE\b)/gi;
6
+
7
+ function appendSearchCondition2Count(originSql, searchCondition) {
8
+ ///如果包含了这个特定的字符,则替换
9
+ if (originSql.indexOf('#APPENDSEARCH#') >= 0)
10
+ return originSql.replace('#APPENDSEARCH#', searchCondition);
11
+ return originSql + ' ' + searchCondition;
12
+ }
13
+ /**
14
+ * 检查是否有Sql注入的风险
15
+ * @param {*} sql
16
+ */
17
+ function checkSqlInjection(sql) {
18
+ if (!sql) return sql;
19
+ //let sqlError = sql.match(FORBID_SQL_KEYWORD); //strict ? sql.match(FORBID_SQL_KEYWORD_STRICT):sql.match(FORBID_SQL_KEYWORD);
20
+ //if (sqlError && sqlError.length > 0) return '';
21
+ if (FORBID_SQL_KEYWORD.test(sql)) return '';
22
+ return sql;
23
+ }
24
+ /*
25
+ * 根据对应列表的配置(dataConfig.list.search),从请求上下文中获取用户进行搜索的参数信息
26
+ */
27
+ module.exports.getSearchCondition = (option) => {
28
+ let paraCopy = option || {};
29
+ if (!paraCopy.request || !paraCopy.refer) return '';
30
+ if (!paraCopy.valueFrom) paraCopy.valueFrom = "all";
31
+ const request = paraCopy.request;
32
+ let retSearch = [],params = [];
33
+ paraCopy.refer.forEach(element => {
34
+ //retSearch.push(this.parseTagInSql(request, element.pattern, false,true));
35
+ const paramSql=this.parseTagForParamtersSearch(request, element);
36
+ if (paramSql.sql) {
37
+ retSearch.push(paramSql.sql);
38
+ params = params.concat(paramSql.params||[]);
39
+ }
40
+ });
41
+ return {filter:retSearch.join(''),params}
42
+ //return retSearch.join('');
43
+ }
44
+
45
+ /**
46
+ * 解析sql字符串中特殊的占位符
47
+ * @param {*} req
48
+ * @param {*} sql
49
+ * @param {*} allowNull
50
+ * @returns
51
+ */
52
+ module.exports.parseTagInSql = (req, sql, allowNull = true,isSearch=false) => {
53
+ if (!sql) return '';
54
+ ///定义正则准备查找sql中的特定关键字
55
+ const matched = sql.match(/@.*?@/g);
56
+ if (!matched || matched.length <= 0) return sql;
57
+ let parseKeyWordIsNull = false;
58
+
59
+ const charReg = /\s+|:|=/gi
60
+ matched.forEach(ele => {
61
+ let matchValue = ele.substring(1, ele.length - 1);
62
+ ///不允许特殊字符出现
63
+ let notdo = matchValue.match(charReg);
64
+ if (notdo && notdo.length > 0) return;
65
+
66
+ let noQuoteProtect = matchValue[0] == '!';
67
+ if (noQuoteProtect) {
68
+ matchValue = matchValue.substring(1);
69
+ }
70
+ ///是否有格式要求
71
+ let validformat = matchValue.split('|');
72
+ matchValue = validformat[0];
73
+ let keyValue = (parseKeyValue(req, matchValue) || '')+''; //utility.ifNull(keyParse.parseKeyValue(req, matchValue), '')+'';
74
+ if (!keyValue) {
75
+ parseKeyWordIsNull = true;
76
+ } else if (typeof (keyValue) === 'string') {
77
+ keyValue = noQuoteProtect || isSearch ? checkSqlInjection(mysql.escape(keyValue)) : mysql.escape(keyValue)
78
+ keyValue = keyValue.substr(1, keyValue.length - 2);
79
+ ///验证参数的格式合法性
80
+ if (keyValue && validformat.length > 1) {
81
+ // console.log(`参数格式合法检查==>${validformat[1]}:${matchValue} = ${keyValue}`)
82
+ keyValue = validatorParamsType(keyValue, validformat[1], validformat[2])
83
+ }
84
+ if (!keyValue) {
85
+ parseKeyWordIsNull = true;
86
+ }
87
+ ///没有引号保护下发现了sql注入,则用1=0不返回任何结果
88
+ if (!keyValue && noQuoteProtect) {
89
+ keyValue = '1=0'
90
+ }
91
+ } else { ////数组形式的参数,目前框架不支持,统一认为为sql注入攻击,全部忽略
92
+ // console.log(`参数非法==>类型${typeof (keyValue)}:${matchValue} = ${keyValue}`)
93
+ parseKeyWordIsNull = true;
94
+ keyValue = ''
95
+ }
96
+ sql = sql.replace(ele, keyValue);
97
+ });
98
+ if (!allowNull && parseKeyWordIsNull) return '';
99
+ return sql;
100
+ }
101
+
102
+ /**
103
+ * 为列表查询定义参数化查询
104
+ * @param {*} req
105
+ * @param {*} sql
106
+ * @param {*} allowNull
107
+ * @param {*} isSearch
108
+ * @returns
109
+ */
110
+ module.exports.parseTagForParamtersSearch = (req, filterSetting={}) => {
111
+ let {pattern:sql,mode=''} = filterSetting;
112
+ if (!sql) return {sql:''};
113
+ ///定义正则准备查找sql中的特定关键字
114
+ const matched = sql.match(/@.*?@/g);
115
+ let params = [];
116
+ if (!matched || matched.length <= 0) return {sql};
117
+ for(const ele of matched){
118
+ let patternModel = mode;
119
+ let matchValue = ele.substring(1, ele.length - 1);
120
+ ///不允许特殊字符出现
121
+ let noQuoteProtect = matchValue[0] == '!';
122
+ if (noQuoteProtect) {
123
+ matchValue = matchValue.substring(1);
124
+ patternModel='';
125
+ }
126
+ ///是否有格式要求
127
+ let validformat = matchValue.split('|');
128
+ matchValue = validformat[0];
129
+ let keyValue = (parseKeyValue(req, matchValue) || '') + ''; //utility.ifNull(keyParse.parseKeyValue(req, matchValue), '')+'';
130
+ if (keyValue && typeof (keyValue) === 'string') {
131
+ ///验证参数的格式合法性
132
+ if (keyValue && validformat.length > 1) {
133
+ keyValue = validatorParamsType(keyValue, validformat[1], validformat[2])
134
+ }
135
+ }
136
+ ///如果解析不出这个KeyValue ,则认为当前这条SQL过滤无效
137
+ if (!keyValue) return {sql:'',params:[]};
138
+ if (keyValue) {
139
+ switch (patternModel.toLowerCase()) {
140
+ case 'like':
141
+ sql = sql.replace(ele, '?'); ///变成参数化查询
142
+ params.push('%' + keyValue + '%')
143
+ break;
144
+ case 'in':
145
+ sql = sql.replace(ele, '?'); ///变成参数化查询
146
+ params.push(keyValue.split(','))
147
+ break;
148
+ default:
149
+ sql = sql.replace(ele, '?'); ///变成参数化查询
150
+ params.push(keyValue)
151
+ break;
152
+ }
153
+ }
154
+ }
155
+ return {sql,params};
156
+ }
157
+ /*
158
+ * 列表请求上下文中获取需要的信息
159
+ * 如Page ,PageSize , Sort 等等
160
+ */
161
+ module.exports.getListInfo = (req, dataKey, cfgType = 0, dao) => {
162
+ if (!dataKey) return null;
163
+ ///确认是否是需要导出Excel
164
+ const export2Excel = (req.query.exportexcel + "").toLowerCase() === "true";
165
+ let { page = 1, rows: pageSize = 100, sort, order, clientFilter: filter } = req.query
166
+ ///防止sortSql 注入在排序的参数中
167
+ if (sort) sort = checkSqlInjection(sort);
168
+ // 防止页面数据传入非数字
169
+ if (isNaN(pageSize)) pageSize = 30;
170
+ // 防止页面数据传入非数字
171
+ if (!page || isNaN(page)) page = 1
172
+ ///最大允许获取100条数据
173
+ pageSize =Math.max(0,Math.min(pageSize, 10000));
174
+ ///拼接排序的语句
175
+ if (order && sort && ORDER_STRING.includes(order.toLowerCase())) sort = sort + ' ' + order
176
+ //req.order =req.sort? utility.ifNull((req.body.order || req.query.order), ""):'';
177
+ const dataConfig = dataconfig.getConfig(dataKey, cfgType);
178
+ if (dataConfig && dataConfig.list) {
179
+ req.dataConfig = dataConfig
180
+ let { sql, listsql, countsql, sqltype = 'sql', field, footer, search, sort: constsort } = dataConfig.list
181
+ /**解析查询条件 */
182
+ const searchCondition = this.getSearchCondition({ request: req, refer: search });
183
+ /**排序方式 *///
184
+ sort = sort || constsort;
185
+ /**来自req请求参数中的过滤条件 */
186
+ let clientfilter = checkSqlInjection(this.parseTagInSql(req, filter, false));
187
+ //如果配置文件中不是直接的sql语句,则从DAO对象中的指定方法来获取sql
188
+ if (sqltype !== 'sql' && listsql && typeof (dao[listsql]) === 'function') {
189
+ sql = dao[sql](req, { page, rows: pageSize, sort, filter: searchCondition.filter, client: clientfilter });
190
+ if (dao[countsql] && typeof (dao[countsql]) === 'function')
191
+ countsql = dao[countsql](req, { page, rows: pageSize, sort, filter: searchCondition.filter, client: clientfilter });
192
+ }
193
+
194
+ /**根据过滤条件、排序条件、分页获取的方式,和原始sql拼接成最终获取数据的sql */
195
+ if (sqltype==='sql'){
196
+ sql = this.parseTagInSql(req, sql) +
197
+ ' ' + searchCondition.filter + clientfilter +
198
+ (sort ? (' order by ' + sort) : '') +
199
+ (export2Excel ? '' : ' limit ' + Number(pageSize) + ' OFFSET ' + (Math.max(Number(page), 1) - 1) * Number(pageSize)) +
200
+ /////在Sql中再放入获取总记录数的语句
201
+ ';SELECT FOUND_ROWS() AS total;';
202
+ /*** 如果存在汇总列的sql,则把SQL放置在最末尾 */
203
+ if (countsql) {
204
+ sql += appendSearchCondition2Count(this.parseTagInSql(req, countsql), searchCondition.filter)
205
+ }
206
+ }
207
+ ///如果存在统计的SQL,则参数需要Double一次
208
+ const sqlParams = countsql ? [].concat(searchCondition.params, searchCondition.params):searchCondition.params;
209
+ return { sql, fields: field, params: sqlParams, footers: footer, page, hascounter: countsql?true:false };
210
+ }
211
+ }
212
+ /*
213
+ * 详细页面请求上下文中获取需要的信息
214
+ * 如Page ,PageSize , Sort 等等
215
+ */
216
+ module.exports.getDetailInfo = (req,dataKey) => {
217
+ if (dataKey) {
218
+ const dataConfig = dataconfig.getConfig(dataKey);
219
+ if (dataConfig && dataConfig.detail) {
220
+ req.dataConfig = dataConfig;
221
+ const { primary, field: fields, primaryIsAutoIncrease } = dataConfig.detail;
222
+ return { primary, fields, autoincrease: primaryIsAutoIncrease }
223
+ }
224
+ }
225
+ return null;
226
+ }
@@ -16,8 +16,9 @@ function appendSearchCondition2Count(originSql, searchCondition) {
16
16
  */
17
17
  function checkSqlInjection(sql) {
18
18
  if (!sql) return sql;
19
- let sqlError = sql.match(FORBID_SQL_KEYWORD); //strict ? sql.match(FORBID_SQL_KEYWORD_STRICT):sql.match(FORBID_SQL_KEYWORD);
20
- if (sqlError && sqlError.length > 0) return '';
19
+ if (FORBID_SQL_KEYWORD.test(sql)) return '';
20
+ //let sqlError = sql.match(FORBID_SQL_KEYWORD); //strict ? sql.match(FORBID_SQL_KEYWORD_STRICT):sql.match(FORBID_SQL_KEYWORD);
21
+ //if (sqlError && sqlError.length > 0) return '';
21
22
  return sql;
22
23
  }
23
24
  /*
@@ -30,7 +31,7 @@ module.exports.getSearchCondition = (option) => {
30
31
  const request = paraCopy.request;
31
32
  let retSearch = [];
32
33
  paraCopy.refer.forEach(element => {
33
- retSearch.push(this.parseTagInSql(request, element.pattern, false,true));
34
+ retSearch.push(this.parseTagInSql(request, element.pattern, false));
34
35
  });
35
36
  return retSearch.join('');
36
37
  }
@@ -42,7 +43,7 @@ module.exports.getSearchCondition = (option) => {
42
43
  * @param {*} allowNull
43
44
  * @returns
44
45
  */
45
- module.exports.parseTagInSql = (req, sql, allowNull = true,isSearch=false) => {
46
+ module.exports.parseTagInSql = (req, sql, allowNull = true) => {
46
47
  if (!sql) return '';
47
48
  ///定义正则准备查找sql中的特定关键字
48
49
  const matched = sql.match(/@.*?@/g);
@@ -67,7 +68,7 @@ module.exports.parseTagInSql = (req, sql, allowNull = true,isSearch=false) => {
67
68
  if (!keyValue) {
68
69
  parseKeyWordIsNull = true;
69
70
  } else if (typeof (keyValue) === 'string') {
70
- keyValue = noQuoteProtect || isSearch ? checkSqlInjection(mysql.escape(keyValue)) : mysql.escape(keyValue)
71
+ keyValue = noQuoteProtect ? checkSqlInjection(mysql.escape(keyValue)) : mysql.escape(keyValue)
71
72
  keyValue = keyValue.substr(1, keyValue.length - 2);
72
73
  ///验证参数的格式合法性
73
74
  if (keyValue && validformat.length > 1) {
@@ -96,7 +97,7 @@ module.exports.parseTagInSql = (req, sql, allowNull = true,isSearch=false) => {
96
97
  * 列表请求上下文中获取需要的信息
97
98
  * 如Page ,PageSize , Sort 等等
98
99
  */
99
- module.exports.getListInfo = (req, dataKey, cfgType = 0, dao) => {
100
+ module.exports.getListInfo = (req, dataKey, cfgType = 0, dao,ignorefilter=false) => {
100
101
  if (!dataKey) return null;
101
102
  ///确认是否是需要导出Excel
102
103
  const export2Excel = (req.query.exportexcel + "").toLowerCase() === "true";
@@ -117,7 +118,7 @@ module.exports.getListInfo = (req, dataKey, cfgType = 0, dao) => {
117
118
  req.dataConfig = dataConfig
118
119
  let { sql, listsql, countsql, sqltype = 'sql', field, footer, search, sort: constsort } = dataConfig.list
119
120
  /**解析查询条件 */
120
- const searchCondition = this.getSearchCondition({ request: req, refer: search });
121
+ const searchCondition = ignorefilter?'':this.getSearchCondition({ request: req, refer: search });
121
122
  /**排序方式 *///
122
123
  sort = sort || constsort;
123
124
  /**来自req请求参数中的过滤条件 */