alasql 4.15.1 → 4.15.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.
package/bin/alasql-cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  //
4
4
  // Command line interface for Alasql
5
- // Version: 4.15.1
5
+ // Version: 4.15.3
6
6
  // Date: 28.07.2015
7
7
  // (c) 2014-2022, Andrey Gershun & Mathias Wulff
8
8
  //
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.15.1 build: develop-3b8d5542 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.15.3 build: develop-3b8d5542 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.15.1
4
+ @version 4.15.3
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2025 Andrey Gershun & Mathias Wulff
package/dist/alasql.fs.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.15.1 build: develop-3b8d5542 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.15.3 build: develop-3b8d5542 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.15.1
4
+ @version 4.15.3
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2025 Andrey Gershun & Mathias Wulff
@@ -141,7 +141,7 @@ SOFTWARE.
141
141
  Current version of alasql
142
142
  @constant {string}
143
143
  */
144
- alasql.version = '4.15.1';
144
+ alasql.version = '4.15.3';
145
145
  alasql.build = 'develop-3b8d5542';
146
146
 
147
147
  /**
@@ -2948,7 +2948,7 @@ next:function () {
2948
2948
 
2949
2949
  // return next match that has a token
2950
2950
  lex:function lex () {
2951
- var r = this.next();
2951
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2952
2952
  if (r) {
2953
2953
  return r;
2954
2954
  } else {
@@ -4982,9 +4982,202 @@ alasql.parser.parseError = function (str, hash) {
4982
4982
  }
4983
4983
  */
4984
4984
  alasql.parse = function (sql) {
4985
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4985
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4986
+ checkSqlNames(ast);
4987
+ return ast;
4986
4988
  };
4987
4989
 
4990
+ /*
4991
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4992
+ holds a quote, a backslash or a line break would end the string and make the rest code
4993
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4994
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4995
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4996
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4997
+ of a JSON value are data, not names.
4998
+ */
4999
+ var checkSqlNames = (function () {
5000
+ var hasOwn = Object.prototype.hasOwnProperty;
5001
+ var badName = /['\\\n\r\u2028\u2029]/;
5002
+ var nameKeys = {
5003
+ as: 1,
5004
+ columnid: 1,
5005
+ tableid: 1,
5006
+ databaseid: 1,
5007
+ viewid: 1,
5008
+ indexid: 1,
5009
+ trigger: 1,
5010
+ variable: 1,
5011
+ dropcolumn: 1,
5012
+ renamecolumn: 1,
5013
+ renameto: 1,
5014
+ to: 1,
5015
+ tocolumnid: 1,
5016
+ forcolumnid: 1,
5017
+ };
5018
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
5019
+
5020
+ function checkName(name) {
5021
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
5022
+ }
5023
+
5024
+ // ORDER BY: a quoted string is a column reference too. A name or string that equals an alias of
5025
+ // the select list is that alias and is allowed (the code escapes it), anything else is checked.
5026
+ function checkOrder(columns, order) {
5027
+ var aliases = {};
5028
+ (columns || []).forEach(function (col) {
5029
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
5030
+ });
5031
+ order.forEach(function (ord) {
5032
+ var e = ord && ord.expression;
5033
+ var name;
5034
+ if (e instanceof alasql.yy.StringValue) name = e.value;
5035
+ else if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
5036
+ if (typeof name === 'string') {
5037
+ if (!hasOwn.call(aliases, name)) checkName(name);
5038
+ return;
5039
+ }
5040
+ checkNames(ord, false, false);
5041
+ });
5042
+ }
5043
+
5044
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
5045
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
5046
+ // graph item, name is data.
5047
+ function entryOf(node, key) {
5048
+ var yy = alasql.yy;
5049
+ if (
5050
+ (node instanceof yy.Select && key === 'columns') ||
5051
+ (node instanceof yy.CreateGraph && key === 'graph') ||
5052
+ (node.srchid === 'RETURN' && key === 'args')
5053
+ )
5054
+ return 'alias';
5055
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
5056
+ return '';
5057
+ }
5058
+
5059
+ // Names that never reach generated code: they are used only at run time, as the key of a
5060
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
5061
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
5062
+ // names are free (what they hold is still walked), and skipChild are children that are not
5063
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
5064
+ var varValueFree = true;
5065
+ var intoVarFree = true;
5066
+ var freeKeys = [
5067
+ ['CreateIndex', {indexid: 1}],
5068
+ ['DropIndex', {indexid: 1}],
5069
+ ['Reindex', {indexid: 1}],
5070
+ ['CreateTrigger', {trigger: 1}],
5071
+ ['DropTrigger', {trigger: 1}],
5072
+ ['CreateDatabase', {databaseid: 1, as: 1}],
5073
+ ['AttachDatabase', {databaseid: 1, as: 1}],
5074
+ ['DetachDatabase', {databaseid: 1}],
5075
+ ['DropDatabase', {databaseid: 1}],
5076
+ ['ShowTables', {databaseid: 1}],
5077
+ ['ShowColumns', {databaseid: 1}],
5078
+ ['ShowIndex', {databaseid: 1}],
5079
+ ['ShowCreateTable', {databaseid: 1}],
5080
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
5081
+ ];
5082
+ var freeChild = [
5083
+ ['CreateTable', {table: 1}],
5084
+ ['DropTable', {tables: 1}],
5085
+ ['TruncateTable', {table: 1}],
5086
+ ['AlterTable', {table: 1}],
5087
+ ['ShowColumns', {table: 1}],
5088
+ ['ShowIndex', {table: 1}],
5089
+ ['ShowCreateTable', {table: 1}],
5090
+ ['CreateIndex', {table: 1}],
5091
+ ['CreateTrigger', {table: 1}],
5092
+ ['Declare', {declares: 1}],
5093
+ ];
5094
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
5095
+
5096
+ function freeOf(list, node) {
5097
+ var yy = alasql.yy;
5098
+ for (var i = 0; i < list.length; i++) {
5099
+ var cls = yy[list[i][0]];
5100
+ if (cls && node instanceof cls) return list[i][1];
5101
+ }
5102
+ return null;
5103
+ }
5104
+
5105
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
5106
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
5107
+ function freeVar(node, key, entry) {
5108
+ var yy = alasql.yy;
5109
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
5110
+ if (entry === 'source') return false;
5111
+ if (entry === 'into') return intoVarFree;
5112
+ return varValueFree;
5113
+ }
5114
+
5115
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
5116
+ function freeSet(node, key) {
5117
+ var yy = alasql.yy;
5118
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
5119
+ }
5120
+
5121
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
5122
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
5123
+ // names never reach generated code.
5124
+ function checkNames(node, data, entry) {
5125
+ if (node === null || typeof node !== 'object') return;
5126
+ var i;
5127
+ if (Array.isArray(node)) {
5128
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
5129
+ return;
5130
+ }
5131
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
5132
+ if (data && !isNode) {
5133
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
5134
+ return;
5135
+ }
5136
+ var yy = alasql.yy;
5137
+ if (node instanceof yy.Json) {
5138
+ checkNames(node.value, true, false);
5139
+ return;
5140
+ }
5141
+ var fk = freeOf(freeKeys, node);
5142
+ var fc = freeOf(freeChild, node);
5143
+ var sc = freeOf(skipChild, node);
5144
+ for (var key in node) {
5145
+ if (!hasOwn.call(node, key)) continue;
5146
+ var value = node[key];
5147
+ if (sc && sc[key] === 1) continue;
5148
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
5149
+ checkOrder(node.columns, value);
5150
+ continue;
5151
+ }
5152
+ // the column list of a WITH entry renames the result at run time
5153
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
5154
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
5155
+ if (typeof value === 'string') {
5156
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
5157
+ continue;
5158
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
5159
+ checkName(value);
5160
+ } else if (value !== null && typeof value === 'object') {
5161
+ if (Array.isArray(value)) {
5162
+ for (i = 0; i < value.length; i++) {
5163
+ if (typeof value[i] === 'string') {
5164
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
5165
+ } else {
5166
+ checkNames(value[i], false, child || entryOf(node, key));
5167
+ }
5168
+ }
5169
+ } else {
5170
+ checkNames(value, false, child);
5171
+ }
5172
+ }
5173
+ }
5174
+ }
5175
+
5176
+ return function (ast) {
5177
+ checkNames(ast, false, false);
5178
+ };
5179
+ })();
5180
+
4988
5181
  /**
4989
5182
  List of engines of external databases
4990
5183
  @type {object}
@@ -7133,7 +7326,7 @@ function queryfn3(query) {
7133
7326
 
7134
7327
  // Prepare variables
7135
7328
  query.data = [];
7136
- query.xgroups = {};
7329
+ query.xgroups=Object.create(null);
7137
7330
  query.groups = [];
7138
7331
 
7139
7332
  // Level of Joins
@@ -8653,7 +8846,7 @@ yy.Select.prototype.compileFrom = function (query) {
8653
8846
  return res;
8654
8847
  };
8655
8848
  } else if (tq instanceof yy.ParamValue) {
8656
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8849
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
8657
8850
  if (tq.array) ps += ',true';
8658
8851
  ps += ');if(cb)res=cb(res,idx,query);return res';
8659
8852
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8864,7 +9057,7 @@ yy.Select.prototype.compileJoins = function (query) {
8864
9057
  srcwherefn: returnTrue,
8865
9058
  };
8866
9059
  const jnparam = jn.param.param;
8867
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
9060
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8868
9061
  if (jn.array) ps += ',true';
8869
9062
  ps += '); if(cb) res=cb(res, idx, query); return res';
8870
9063
 
@@ -9848,7 +10041,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9848
10041
  if (col.columnid === '*') {
9849
10042
  if (col.func) {
9850
10043
  sp +=
9851
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10044
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9852
10045
  } else if (col.tableid) {
9853
10046
  //Copy all
9854
10047
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9900,7 +10093,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9900
10093
  if (isMultisheetParam) {
9901
10094
  sp =
9902
10095
  'var r={};var w=p["' +
9903
- tbid +
10096
+ escapeq(tbid) +
9904
10097
  '"];' +
9905
10098
  'var cols=[' +
9906
10099
  self.columns
@@ -9911,7 +10104,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9911
10104
  '];var colas=[' +
9912
10105
  self.columns
9913
10106
  .map(function (col) {
9914
- return "'" + (col.as || col.columnid) + "'";
10107
+ return "'" + escapeq(col.as || col.columnid) + "'";
9915
10108
  })
9916
10109
  .join(',') +
9917
10110
  '];' +
@@ -10142,9 +10335,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
10142
10335
  // Use Object.keys to get column names and access by index
10143
10336
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10144
10337
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
10145
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10338
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10146
10339
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
10147
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
10340
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
10148
10341
  } else {
10149
10342
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
10150
10343
  }
@@ -10251,7 +10444,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
10251
10444
  query.groupColumns[colas] = col.nick;
10252
10445
 
10253
10446
  // if(col.as) {
10254
- s += "r['" + colas + "']=";
10447
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
10255
10448
  // // } else {
10256
10449
  // // s += 'r[\''+escapeq()+'\']=';
10257
10450
  // // };
@@ -10302,7 +10495,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10302
10495
  if (isInGroup) {
10303
10496
  // For columns in GROUP BY, use the GROUP BY column's nick if available
10304
10497
  var groupNick = (groupCol && groupCol.nick) || col.nick;
10305
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
10498
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
10306
10499
  }
10307
10500
  });
10308
10501
 
@@ -10324,7 +10517,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10324
10517
  // Use Object.keys to get column names and access by index
10325
10518
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10326
10519
  } else if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
10327
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10520
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10328
10521
  } else {
10329
10522
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
10330
10523
  }
@@ -11477,7 +11670,7 @@ yy.Over = class Over {
11477
11670
  }
11478
11671
  } else if (this.op === '!') {
11479
11672
  if (typeof this.right === 'string') {
11480
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
11673
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
11481
11674
  }
11482
11675
  // TODO - add other cases
11483
11676
  } else if (this.op === 'IS') {
@@ -11794,7 +11987,7 @@ yy.Over = class Over {
11794
11987
 
11795
11988
  toJS() {
11796
11989
  if (typeof this.param === 'string') {
11797
- return "params['" + this.param + "']";
11990
+ return "params['" + escapeq(this.param) + "']";
11798
11991
  }
11799
11992
 
11800
11993
  return 'params[' + this.param + ']';
@@ -12836,9 +13029,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12836
13029
  }
12837
13030
  });
12838
13031
 
13032
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
13033
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
13034
+ // are escaped, so the string cannot end early.
13035
+ function escapeJSONString(s) {
13036
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
13037
+ if (c.length === 2) return c;
13038
+ if (c === '\\') return '\\\\';
13039
+ if (c === '"') return '\\"';
13040
+ if (c === '\n') return '\\n';
13041
+ if (c === '\r') return '\\r';
13042
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
13043
+ });
13044
+ }
13045
+
12839
13046
  function JSONtoJS(obj, context, tableid, defcols) {
12840
13047
  var s = '';
12841
- if (typeof obj == 'string') s = '"' + obj + '"';
13048
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12842
13049
  else if (typeof obj == 'number') s = '(' + obj + ')';
12843
13050
  else if (typeof obj == 'boolean') s = obj;
12844
13051
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -12848,7 +13055,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12848
13055
  } else if (!obj.toJS || obj instanceof yy.Json) {
12849
13056
  let ss = [];
12850
13057
  for (const k in obj) {
12851
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
13058
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
12852
13059
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
12853
13060
  ss.push(`${keyStr}:${valueStr}`);
12854
13061
  }
@@ -16580,22 +16787,22 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
16580
16787
  if (this.props && this.props.length > 0) {
16581
16788
  let fs;
16582
16789
  if (this.method === '@') {
16583
- fs = `alasql.vars['${this.variable}']`;
16790
+ fs = `alasql.vars[k('${this.variable}')]`;
16584
16791
  } else {
16585
- fs = `params['${this.variable}']`;
16792
+ fs = `params[k('${this.variable}')]`;
16586
16793
  }
16587
16794
  this.props.forEach(prop => {
16588
16795
  if (typeof prop === 'string') {
16589
- fs += `['${prop}']`;
16796
+ fs += `[k('${prop}')]`;
16590
16797
  } else if (typeof prop === 'number') {
16591
16798
  fs += `[${prop}]`;
16592
16799
  } else {
16593
16800
  // Assuming prop.toJS() is a method that converts prop to a JavaScript expression.
16594
- fs += `[${prop.toJS()}]`;
16801
+ fs += `[k(${prop.toJS()})]`;
16595
16802
  }
16596
16803
  });
16597
16804
 
16598
- new Function('value, params, alasql', `${fs} = value`)(res, params, alasql);
16805
+ new Function('value, params, alasql', `var k=function(x){if(x==="__proto__"||x==="constructor"||x==="prototype")throw new Error(x+" is not allowed");return x};${fs} = value`)(res, params, alasql);
16599
16806
  } else {
16600
16807
  if (this.method === '@') {
16601
16808
  alasql.vars[this.variable] = res;