alasql 4.12.1 → 4.12.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.12.1
5
+ // Version: 4.12.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.12.1 build: develop-9b30298b | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.12.3 build: develop-9b30298b | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.12.1
4
+ @version 4.12.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.12.1 build: develop-9b30298b | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.12.3 build: develop-9b30298b | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.12.1
4
+ @version 4.12.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.12.1';
144
+ alasql.version = '4.12.3';
145
145
  alasql.build = 'develop-9b30298b';
146
146
 
147
147
  /**
@@ -2660,7 +2660,7 @@ next:function () {
2660
2660
 
2661
2661
  // return next match that has a token
2662
2662
  lex:function lex () {
2663
- var r = this.next();
2663
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2664
2664
  if (r) {
2665
2665
  return r;
2666
2666
  } else {
@@ -4694,9 +4694,202 @@ alasql.parser.parseError = function (str, hash) {
4694
4694
  }
4695
4695
  */
4696
4696
  alasql.parse = function (sql) {
4697
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4697
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4698
+ checkSqlNames(ast);
4699
+ return ast;
4698
4700
  };
4699
4701
 
4702
+ /*
4703
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4704
+ holds a quote, a backslash or a line break would end the string and make the rest code
4705
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4706
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4707
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4708
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4709
+ of a JSON value are data, not names.
4710
+ */
4711
+ var checkSqlNames = (function () {
4712
+ var hasOwn = Object.prototype.hasOwnProperty;
4713
+ var badName = /['\\\n\r\u2028\u2029]/;
4714
+ var nameKeys = {
4715
+ as: 1,
4716
+ columnid: 1,
4717
+ tableid: 1,
4718
+ databaseid: 1,
4719
+ viewid: 1,
4720
+ indexid: 1,
4721
+ trigger: 1,
4722
+ variable: 1,
4723
+ dropcolumn: 1,
4724
+ renamecolumn: 1,
4725
+ renameto: 1,
4726
+ to: 1,
4727
+ tocolumnid: 1,
4728
+ forcolumnid: 1,
4729
+ };
4730
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4731
+
4732
+ function checkName(name) {
4733
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4734
+ }
4735
+
4736
+ // ORDER BY: a quoted string is a column reference too. A name or string that equals an alias of
4737
+ // the select list is that alias and is allowed (the code escapes it), anything else is checked.
4738
+ function checkOrder(columns, order) {
4739
+ var aliases = {};
4740
+ (columns || []).forEach(function (col) {
4741
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4742
+ });
4743
+ order.forEach(function (ord) {
4744
+ var e = ord && ord.expression;
4745
+ var name;
4746
+ if (e instanceof alasql.yy.StringValue) name = e.value;
4747
+ else if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4748
+ if (typeof name === 'string') {
4749
+ if (!hasOwn.call(aliases, name)) checkName(name);
4750
+ return;
4751
+ }
4752
+ checkNames(ord, false, false);
4753
+ });
4754
+ }
4755
+
4756
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4757
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4758
+ // graph item, name is data.
4759
+ function entryOf(node, key) {
4760
+ var yy = alasql.yy;
4761
+ if (
4762
+ (node instanceof yy.Select && key === 'columns') ||
4763
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4764
+ (node.srchid === 'RETURN' && key === 'args')
4765
+ )
4766
+ return 'alias';
4767
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4768
+ return '';
4769
+ }
4770
+
4771
+ // Names that never reach generated code: they are used only at run time, as the key of a
4772
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4773
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4774
+ // names are free (what they hold is still walked), and skipChild are children that are not
4775
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4776
+ var varValueFree = true;
4777
+ var intoVarFree = true;
4778
+ var freeKeys = [
4779
+ ['CreateIndex', {indexid: 1}],
4780
+ ['DropIndex', {indexid: 1}],
4781
+ ['Reindex', {indexid: 1}],
4782
+ ['CreateTrigger', {trigger: 1}],
4783
+ ['DropTrigger', {trigger: 1}],
4784
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4785
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4786
+ ['DetachDatabase', {databaseid: 1}],
4787
+ ['DropDatabase', {databaseid: 1}],
4788
+ ['ShowTables', {databaseid: 1}],
4789
+ ['ShowColumns', {databaseid: 1}],
4790
+ ['ShowIndex', {databaseid: 1}],
4791
+ ['ShowCreateTable', {databaseid: 1}],
4792
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4793
+ ];
4794
+ var freeChild = [
4795
+ ['CreateTable', {table: 1}],
4796
+ ['DropTable', {tables: 1}],
4797
+ ['TruncateTable', {table: 1}],
4798
+ ['AlterTable', {table: 1}],
4799
+ ['ShowColumns', {table: 1}],
4800
+ ['ShowIndex', {table: 1}],
4801
+ ['ShowCreateTable', {table: 1}],
4802
+ ['CreateIndex', {table: 1}],
4803
+ ['CreateTrigger', {table: 1}],
4804
+ ['Declare', {declares: 1}],
4805
+ ];
4806
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4807
+
4808
+ function freeOf(list, node) {
4809
+ var yy = alasql.yy;
4810
+ for (var i = 0; i < list.length; i++) {
4811
+ var cls = yy[list[i][0]];
4812
+ if (cls && node instanceof cls) return list[i][1];
4813
+ }
4814
+ return null;
4815
+ }
4816
+
4817
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4818
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4819
+ function freeVar(node, key, entry) {
4820
+ var yy = alasql.yy;
4821
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4822
+ if (entry === 'source') return false;
4823
+ if (entry === 'into') return intoVarFree;
4824
+ return varValueFree;
4825
+ }
4826
+
4827
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4828
+ function freeSet(node, key) {
4829
+ var yy = alasql.yy;
4830
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4831
+ }
4832
+
4833
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4834
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4835
+ // names never reach generated code.
4836
+ function checkNames(node, data, entry) {
4837
+ if (node === null || typeof node !== 'object') return;
4838
+ var i;
4839
+ if (Array.isArray(node)) {
4840
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4841
+ return;
4842
+ }
4843
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4844
+ if (data && !isNode) {
4845
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4846
+ return;
4847
+ }
4848
+ var yy = alasql.yy;
4849
+ if (node instanceof yy.Json) {
4850
+ checkNames(node.value, true, false);
4851
+ return;
4852
+ }
4853
+ var fk = freeOf(freeKeys, node);
4854
+ var fc = freeOf(freeChild, node);
4855
+ var sc = freeOf(skipChild, node);
4856
+ for (var key in node) {
4857
+ if (!hasOwn.call(node, key)) continue;
4858
+ var value = node[key];
4859
+ if (sc && sc[key] === 1) continue;
4860
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4861
+ checkOrder(node.columns, value);
4862
+ continue;
4863
+ }
4864
+ // the column list of a WITH entry renames the result at run time
4865
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4866
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4867
+ if (typeof value === 'string') {
4868
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4869
+ continue;
4870
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4871
+ checkName(value);
4872
+ } else if (value !== null && typeof value === 'object') {
4873
+ if (Array.isArray(value)) {
4874
+ for (i = 0; i < value.length; i++) {
4875
+ if (typeof value[i] === 'string') {
4876
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4877
+ } else {
4878
+ checkNames(value[i], false, child || entryOf(node, key));
4879
+ }
4880
+ }
4881
+ } else {
4882
+ checkNames(value, false, child);
4883
+ }
4884
+ }
4885
+ }
4886
+ }
4887
+
4888
+ return function (ast) {
4889
+ checkNames(ast, false, false);
4890
+ };
4891
+ })();
4892
+
4700
4893
  /**
4701
4894
  List of engines of external databases
4702
4895
  @type {object}
@@ -6786,7 +6979,7 @@ function queryfn3(query) {
6786
6979
 
6787
6980
  // Prepare variables
6788
6981
  query.data = [];
6789
- query.xgroups = {};
6982
+ query.xgroups=Object.create(null);
6790
6983
  query.groups = [];
6791
6984
 
6792
6985
  // Level of Joins
@@ -8292,7 +8485,7 @@ yy.Select.prototype.compileFrom = function (query) {
8292
8485
  return res;
8293
8486
  };
8294
8487
  } else if (tq instanceof yy.ParamValue) {
8295
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8488
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
8296
8489
  if (tq.array) ps += ',true';
8297
8490
  ps += ');if(cb)res=cb(res,idx,query);return res';
8298
8491
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8503,7 +8696,7 @@ yy.Select.prototype.compileJoins = function (query) {
8503
8696
  srcwherefn: returnTrue,
8504
8697
  };
8505
8698
  const jnparam = jn.param.param;
8506
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
8699
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8507
8700
  if (jn.array) ps += ',true';
8508
8701
  ps += '); if(cb) res=cb(res, idx, query); return res';
8509
8702
 
@@ -9487,7 +9680,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9487
9680
  if (col.columnid === '*') {
9488
9681
  if (col.func) {
9489
9682
  sp +=
9490
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9683
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9491
9684
  } else if (col.tableid) {
9492
9685
  //Copy all
9493
9686
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9539,7 +9732,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9539
9732
  if (isMultisheetParam) {
9540
9733
  sp =
9541
9734
  'var r={};var w=p["' +
9542
- tbid +
9735
+ escapeq(tbid) +
9543
9736
  '"];' +
9544
9737
  'var cols=[' +
9545
9738
  self.columns
@@ -9550,7 +9743,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9550
9743
  '];var colas=[' +
9551
9744
  self.columns
9552
9745
  .map(function (col) {
9553
- return "'" + (col.as || col.columnid) + "'";
9746
+ return "'" + escapeq(col.as || col.columnid) + "'";
9554
9747
  })
9555
9748
  .join(',') +
9556
9749
  '];' +
@@ -9781,9 +9974,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9781
9974
  // Use Object.keys to get column names and access by index
9782
9975
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
9783
9976
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9784
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9977
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9785
9978
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9786
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9979
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9787
9980
  } else {
9788
9981
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9789
9982
  }
@@ -9890,7 +10083,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9890
10083
  query.groupColumns[colas] = col.nick;
9891
10084
 
9892
10085
  // if(col.as) {
9893
- s += "r['" + colas + "']=";
10086
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9894
10087
  // // } else {
9895
10088
  // // s += 'r[\''+escapeq()+'\']=';
9896
10089
  // // };
@@ -9941,7 +10134,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9941
10134
  if (isInGroup) {
9942
10135
  // For columns in GROUP BY, use the GROUP BY column's nick if available
9943
10136
  var groupNick = (groupCol && groupCol.nick) || col.nick;
9944
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
10137
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
9945
10138
  }
9946
10139
  });
9947
10140
 
@@ -9963,7 +10156,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9963
10156
  // Use Object.keys to get column names and access by index
9964
10157
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
9965
10158
  } else if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9966
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10159
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9967
10160
  } else {
9968
10161
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9969
10162
  }
@@ -11116,7 +11309,7 @@ yy.Over = class Over {
11116
11309
  }
11117
11310
  } else if (this.op === '!') {
11118
11311
  if (typeof this.right === 'string') {
11119
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
11312
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
11120
11313
  }
11121
11314
  // TODO - add other cases
11122
11315
  } else if (this.op === 'IS') {
@@ -11433,7 +11626,7 @@ yy.Over = class Over {
11433
11626
 
11434
11627
  toJS() {
11435
11628
  if (typeof this.param === 'string') {
11436
- return "params['" + this.param + "']";
11629
+ return "params['" + escapeq(this.param) + "']";
11437
11630
  }
11438
11631
 
11439
11632
  return 'params[' + this.param + ']';
@@ -12475,9 +12668,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12475
12668
  }
12476
12669
  });
12477
12670
 
12671
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12672
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12673
+ // are escaped, so the string cannot end early.
12674
+ function escapeJSONString(s) {
12675
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12676
+ if (c.length === 2) return c;
12677
+ if (c === '\\') return '\\\\';
12678
+ if (c === '"') return '\\"';
12679
+ if (c === '\n') return '\\n';
12680
+ if (c === '\r') return '\\r';
12681
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12682
+ });
12683
+ }
12684
+
12478
12685
  function JSONtoJS(obj, context, tableid, defcols) {
12479
12686
  var s = '';
12480
- if (typeof obj == 'string') s = '"' + obj + '"';
12687
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12481
12688
  else if (typeof obj == 'number') s = '(' + obj + ')';
12482
12689
  else if (typeof obj == 'boolean') s = obj;
12483
12690
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -12487,7 +12694,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12487
12694
  } else if (!obj.toJS || obj instanceof yy.Json) {
12488
12695
  let ss = [];
12489
12696
  for (const k in obj) {
12490
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
12697
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
12491
12698
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
12492
12699
  ss.push(`${keyStr}:${valueStr}`);
12493
12700
  }
@@ -16175,22 +16382,22 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
16175
16382
  if (this.props && this.props.length > 0) {
16176
16383
  let fs;
16177
16384
  if (this.method === '@') {
16178
- fs = `alasql.vars['${this.variable}']`;
16385
+ fs = `alasql.vars[k('${this.variable}')]`;
16179
16386
  } else {
16180
- fs = `params['${this.variable}']`;
16387
+ fs = `params[k('${this.variable}')]`;
16181
16388
  }
16182
16389
  this.props.forEach(prop => {
16183
16390
  if (typeof prop === 'string') {
16184
- fs += `['${prop}']`;
16391
+ fs += `[k('${prop}')]`;
16185
16392
  } else if (typeof prop === 'number') {
16186
16393
  fs += `[${prop}]`;
16187
16394
  } else {
16188
16395
  // Assuming prop.toJS() is a method that converts prop to a JavaScript expression.
16189
- fs += `[${prop.toJS()}]`;
16396
+ fs += `[k(${prop.toJS()})]`;
16190
16397
  }
16191
16398
  });
16192
16399
 
16193
- new Function('value, params, alasql', `${fs} = value`)(res, params, alasql);
16400
+ 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);
16194
16401
  } else {
16195
16402
  if (this.method === '@') {
16196
16403
  alasql.vars[this.variable] = res;