alasql 4.14.1 → 4.14.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.14.1
5
+ // Version: 4.14.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.14.1 build: develop-4992fcb1 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.14.3 build: develop-4992fcb1 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.14.1
4
+ @version 4.14.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.14.1 build: develop-4992fcb1 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.14.3 build: develop-4992fcb1 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.14.1
4
+ @version 4.14.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.14.1';
144
+ alasql.version = '4.14.3';
145
145
  alasql.build = 'develop-4992fcb1';
146
146
 
147
147
  /**
@@ -2666,7 +2666,7 @@ next:function () {
2666
2666
 
2667
2667
  // return next match that has a token
2668
2668
  lex:function lex () {
2669
- var r = this.next();
2669
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2670
2670
  if (r) {
2671
2671
  return r;
2672
2672
  } else {
@@ -4700,9 +4700,202 @@ alasql.parser.parseError = function (str, hash) {
4700
4700
  }
4701
4701
  */
4702
4702
  alasql.parse = function (sql) {
4703
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4703
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4704
+ checkSqlNames(ast);
4705
+ return ast;
4704
4706
  };
4705
4707
 
4708
+ /*
4709
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4710
+ holds a quote, a backslash or a line break would end the string and make the rest code
4711
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4712
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4713
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4714
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4715
+ of a JSON value are data, not names.
4716
+ */
4717
+ var checkSqlNames = (function () {
4718
+ var hasOwn = Object.prototype.hasOwnProperty;
4719
+ var badName = /['\\\n\r\u2028\u2029]/;
4720
+ var nameKeys = {
4721
+ as: 1,
4722
+ columnid: 1,
4723
+ tableid: 1,
4724
+ databaseid: 1,
4725
+ viewid: 1,
4726
+ indexid: 1,
4727
+ trigger: 1,
4728
+ variable: 1,
4729
+ dropcolumn: 1,
4730
+ renamecolumn: 1,
4731
+ renameto: 1,
4732
+ to: 1,
4733
+ tocolumnid: 1,
4734
+ forcolumnid: 1,
4735
+ };
4736
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4737
+
4738
+ function checkName(name) {
4739
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4740
+ }
4741
+
4742
+ // ORDER BY: a quoted string is a column reference too. A name or string that equals an alias of
4743
+ // the select list is that alias and is allowed (the code escapes it), anything else is checked.
4744
+ function checkOrder(columns, order) {
4745
+ var aliases = {};
4746
+ (columns || []).forEach(function (col) {
4747
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4748
+ });
4749
+ order.forEach(function (ord) {
4750
+ var e = ord && ord.expression;
4751
+ var name;
4752
+ if (e instanceof alasql.yy.StringValue) name = e.value;
4753
+ else if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4754
+ if (typeof name === 'string') {
4755
+ if (!hasOwn.call(aliases, name)) checkName(name);
4756
+ return;
4757
+ }
4758
+ checkNames(ord, false, false);
4759
+ });
4760
+ }
4761
+
4762
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4763
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4764
+ // graph item, name is data.
4765
+ function entryOf(node, key) {
4766
+ var yy = alasql.yy;
4767
+ if (
4768
+ (node instanceof yy.Select && key === 'columns') ||
4769
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4770
+ (node.srchid === 'RETURN' && key === 'args')
4771
+ )
4772
+ return 'alias';
4773
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4774
+ return '';
4775
+ }
4776
+
4777
+ // Names that never reach generated code: they are used only at run time, as the key of a
4778
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4779
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4780
+ // names are free (what they hold is still walked), and skipChild are children that are not
4781
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4782
+ var varValueFree = true;
4783
+ var intoVarFree = true;
4784
+ var freeKeys = [
4785
+ ['CreateIndex', {indexid: 1}],
4786
+ ['DropIndex', {indexid: 1}],
4787
+ ['Reindex', {indexid: 1}],
4788
+ ['CreateTrigger', {trigger: 1}],
4789
+ ['DropTrigger', {trigger: 1}],
4790
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4791
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4792
+ ['DetachDatabase', {databaseid: 1}],
4793
+ ['DropDatabase', {databaseid: 1}],
4794
+ ['ShowTables', {databaseid: 1}],
4795
+ ['ShowColumns', {databaseid: 1}],
4796
+ ['ShowIndex', {databaseid: 1}],
4797
+ ['ShowCreateTable', {databaseid: 1}],
4798
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4799
+ ];
4800
+ var freeChild = [
4801
+ ['CreateTable', {table: 1}],
4802
+ ['DropTable', {tables: 1}],
4803
+ ['TruncateTable', {table: 1}],
4804
+ ['AlterTable', {table: 1}],
4805
+ ['ShowColumns', {table: 1}],
4806
+ ['ShowIndex', {table: 1}],
4807
+ ['ShowCreateTable', {table: 1}],
4808
+ ['CreateIndex', {table: 1}],
4809
+ ['CreateTrigger', {table: 1}],
4810
+ ['Declare', {declares: 1}],
4811
+ ];
4812
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4813
+
4814
+ function freeOf(list, node) {
4815
+ var yy = alasql.yy;
4816
+ for (var i = 0; i < list.length; i++) {
4817
+ var cls = yy[list[i][0]];
4818
+ if (cls && node instanceof cls) return list[i][1];
4819
+ }
4820
+ return null;
4821
+ }
4822
+
4823
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4824
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4825
+ function freeVar(node, key, entry) {
4826
+ var yy = alasql.yy;
4827
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4828
+ if (entry === 'source') return false;
4829
+ if (entry === 'into') return intoVarFree;
4830
+ return varValueFree;
4831
+ }
4832
+
4833
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4834
+ function freeSet(node, key) {
4835
+ var yy = alasql.yy;
4836
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4837
+ }
4838
+
4839
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4840
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4841
+ // names never reach generated code.
4842
+ function checkNames(node, data, entry) {
4843
+ if (node === null || typeof node !== 'object') return;
4844
+ var i;
4845
+ if (Array.isArray(node)) {
4846
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4847
+ return;
4848
+ }
4849
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4850
+ if (data && !isNode) {
4851
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4852
+ return;
4853
+ }
4854
+ var yy = alasql.yy;
4855
+ if (node instanceof yy.Json) {
4856
+ checkNames(node.value, true, false);
4857
+ return;
4858
+ }
4859
+ var fk = freeOf(freeKeys, node);
4860
+ var fc = freeOf(freeChild, node);
4861
+ var sc = freeOf(skipChild, node);
4862
+ for (var key in node) {
4863
+ if (!hasOwn.call(node, key)) continue;
4864
+ var value = node[key];
4865
+ if (sc && sc[key] === 1) continue;
4866
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4867
+ checkOrder(node.columns, value);
4868
+ continue;
4869
+ }
4870
+ // the column list of a WITH entry renames the result at run time
4871
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4872
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4873
+ if (typeof value === 'string') {
4874
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4875
+ continue;
4876
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4877
+ checkName(value);
4878
+ } else if (value !== null && typeof value === 'object') {
4879
+ if (Array.isArray(value)) {
4880
+ for (i = 0; i < value.length; i++) {
4881
+ if (typeof value[i] === 'string') {
4882
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4883
+ } else {
4884
+ checkNames(value[i], false, child || entryOf(node, key));
4885
+ }
4886
+ }
4887
+ } else {
4888
+ checkNames(value, false, child);
4889
+ }
4890
+ }
4891
+ }
4892
+ }
4893
+
4894
+ return function (ast) {
4895
+ checkNames(ast, false, false);
4896
+ };
4897
+ })();
4898
+
4706
4899
  /**
4707
4900
  List of engines of external databases
4708
4901
  @type {object}
@@ -6833,7 +7026,7 @@ function queryfn3(query) {
6833
7026
 
6834
7027
  // Prepare variables
6835
7028
  query.data = [];
6836
- query.xgroups = {};
7029
+ query.xgroups=Object.create(null);
6837
7030
  query.groups = [];
6838
7031
 
6839
7032
  // Level of Joins
@@ -8339,7 +8532,7 @@ yy.Select.prototype.compileFrom = function (query) {
8339
8532
  return res;
8340
8533
  };
8341
8534
  } else if (tq instanceof yy.ParamValue) {
8342
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8535
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
8343
8536
  if (tq.array) ps += ',true';
8344
8537
  ps += ');if(cb)res=cb(res,idx,query);return res';
8345
8538
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8550,7 +8743,7 @@ yy.Select.prototype.compileJoins = function (query) {
8550
8743
  srcwherefn: returnTrue,
8551
8744
  };
8552
8745
  const jnparam = jn.param.param;
8553
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
8746
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8554
8747
  if (jn.array) ps += ',true';
8555
8748
  ps += '); if(cb) res=cb(res, idx, query); return res';
8556
8749
 
@@ -9534,7 +9727,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9534
9727
  if (col.columnid === '*') {
9535
9728
  if (col.func) {
9536
9729
  sp +=
9537
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9730
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9538
9731
  } else if (col.tableid) {
9539
9732
  //Copy all
9540
9733
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9586,7 +9779,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9586
9779
  if (isMultisheetParam) {
9587
9780
  sp =
9588
9781
  'var r={};var w=p["' +
9589
- tbid +
9782
+ escapeq(tbid) +
9590
9783
  '"];' +
9591
9784
  'var cols=[' +
9592
9785
  self.columns
@@ -9597,7 +9790,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9597
9790
  '];var colas=[' +
9598
9791
  self.columns
9599
9792
  .map(function (col) {
9600
- return "'" + (col.as || col.columnid) + "'";
9793
+ return "'" + escapeq(col.as || col.columnid) + "'";
9601
9794
  })
9602
9795
  .join(',') +
9603
9796
  '];' +
@@ -9828,9 +10021,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9828
10021
  // Use Object.keys to get column names and access by index
9829
10022
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
9830
10023
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9831
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10024
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9832
10025
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9833
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
10026
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9834
10027
  } else {
9835
10028
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9836
10029
  }
@@ -9937,7 +10130,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9937
10130
  query.groupColumns[colas] = col.nick;
9938
10131
 
9939
10132
  // if(col.as) {
9940
- s += "r['" + colas + "']=";
10133
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9941
10134
  // // } else {
9942
10135
  // // s += 'r[\''+escapeq()+'\']=';
9943
10136
  // // };
@@ -9988,7 +10181,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9988
10181
  if (isInGroup) {
9989
10182
  // For columns in GROUP BY, use the GROUP BY column's nick if available
9990
10183
  var groupNick = (groupCol && groupCol.nick) || col.nick;
9991
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
10184
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
9992
10185
  }
9993
10186
  });
9994
10187
 
@@ -10010,7 +10203,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10010
10203
  // Use Object.keys to get column names and access by index
10011
10204
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10012
10205
  } else if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
10013
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10206
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10014
10207
  } else {
10015
10208
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
10016
10209
  }
@@ -11163,7 +11356,7 @@ yy.Over = class Over {
11163
11356
  }
11164
11357
  } else if (this.op === '!') {
11165
11358
  if (typeof this.right === 'string') {
11166
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
11359
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
11167
11360
  }
11168
11361
  // TODO - add other cases
11169
11362
  } else if (this.op === 'IS') {
@@ -11480,7 +11673,7 @@ yy.Over = class Over {
11480
11673
 
11481
11674
  toJS() {
11482
11675
  if (typeof this.param === 'string') {
11483
- return "params['" + this.param + "']";
11676
+ return "params['" + escapeq(this.param) + "']";
11484
11677
  }
11485
11678
 
11486
11679
  return 'params[' + this.param + ']';
@@ -12522,9 +12715,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12522
12715
  }
12523
12716
  });
12524
12717
 
12718
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12719
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12720
+ // are escaped, so the string cannot end early.
12721
+ function escapeJSONString(s) {
12722
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12723
+ if (c.length === 2) return c;
12724
+ if (c === '\\') return '\\\\';
12725
+ if (c === '"') return '\\"';
12726
+ if (c === '\n') return '\\n';
12727
+ if (c === '\r') return '\\r';
12728
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12729
+ });
12730
+ }
12731
+
12525
12732
  function JSONtoJS(obj, context, tableid, defcols) {
12526
12733
  var s = '';
12527
- if (typeof obj == 'string') s = '"' + obj + '"';
12734
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12528
12735
  else if (typeof obj == 'number') s = '(' + obj + ')';
12529
12736
  else if (typeof obj == 'boolean') s = obj;
12530
12737
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -12534,7 +12741,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12534
12741
  } else if (!obj.toJS || obj instanceof yy.Json) {
12535
12742
  let ss = [];
12536
12743
  for (const k in obj) {
12537
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
12744
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
12538
12745
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
12539
12746
  ss.push(`${keyStr}:${valueStr}`);
12540
12747
  }
@@ -16239,22 +16446,22 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
16239
16446
  if (this.props && this.props.length > 0) {
16240
16447
  let fs;
16241
16448
  if (this.method === '@') {
16242
- fs = `alasql.vars['${this.variable}']`;
16449
+ fs = `alasql.vars[k('${this.variable}')]`;
16243
16450
  } else {
16244
- fs = `params['${this.variable}']`;
16451
+ fs = `params[k('${this.variable}')]`;
16245
16452
  }
16246
16453
  this.props.forEach(prop => {
16247
16454
  if (typeof prop === 'string') {
16248
- fs += `['${prop}']`;
16455
+ fs += `[k('${prop}')]`;
16249
16456
  } else if (typeof prop === 'number') {
16250
16457
  fs += `[${prop}]`;
16251
16458
  } else {
16252
16459
  // Assuming prop.toJS() is a method that converts prop to a JavaScript expression.
16253
- fs += `[${prop.toJS()}]`;
16460
+ fs += `[k(${prop.toJS()})]`;
16254
16461
  }
16255
16462
  });
16256
16463
 
16257
- new Function('value, params, alasql', `${fs} = value`)(res, params, alasql);
16464
+ 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);
16258
16465
  } else {
16259
16466
  if (this.method === '@') {
16260
16467
  alasql.vars[this.variable] = res;