alasql 4.3.4 → 4.3.6

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/dist/alasql.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.3.4 build: develop-6ecba2e1 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.3.6 build: develop-6ecba2e1 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.3.4
4
+ @version 4.3.6
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2024 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.3.4';
144
+ alasql.version = '4.3.6';
145
145
  alasql.build = 'develop-6ecba2e1';
146
146
 
147
147
  /**
@@ -2497,7 +2497,7 @@ next:function () {
2497
2497
 
2498
2498
  // return next match that has a token
2499
2499
  lex:function lex () {
2500
- var r = this.next();
2500
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2501
2501
  if (r) {
2502
2502
  return r;
2503
2503
  } else {
@@ -4469,9 +4469,201 @@ alasql.parser.parseError = function (str, hash) {
4469
4469
  }
4470
4470
  */
4471
4471
  alasql.parse = function (sql) {
4472
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4472
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4473
+ checkSqlNames(ast);
4474
+ return ast;
4473
4475
  };
4474
4476
 
4477
+ /*
4478
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4479
+ holds a quote, a backslash or a line break would end the string and make the rest code
4480
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4481
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4482
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4483
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4484
+ of a JSON value are data, not names.
4485
+ */
4486
+ var checkSqlNames = (function () {
4487
+ var hasOwn = Object.prototype.hasOwnProperty;
4488
+ var badName = /['\\\n\r\u2028\u2029]/;
4489
+ var nameKeys = {
4490
+ as: 1,
4491
+ columnid: 1,
4492
+ tableid: 1,
4493
+ databaseid: 1,
4494
+ viewid: 1,
4495
+ indexid: 1,
4496
+ trigger: 1,
4497
+ variable: 1,
4498
+ dropcolumn: 1,
4499
+ renamecolumn: 1,
4500
+ renameto: 1,
4501
+ to: 1,
4502
+ tocolumnid: 1,
4503
+ forcolumnid: 1,
4504
+ };
4505
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4506
+
4507
+ function checkName(name) {
4508
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4509
+ }
4510
+
4511
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4512
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4513
+ function checkOrder(columns, order) {
4514
+ var aliases = {};
4515
+ (columns || []).forEach(function (col) {
4516
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4517
+ });
4518
+ order.forEach(function (ord) {
4519
+ var e = ord && ord.expression;
4520
+ var name;
4521
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4522
+ if (typeof name === 'string') {
4523
+ if (!hasOwn.call(aliases, name)) checkName(name);
4524
+ return;
4525
+ }
4526
+ checkNames(ord, false, false);
4527
+ });
4528
+ }
4529
+
4530
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4531
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4532
+ // graph item, name is data.
4533
+ function entryOf(node, key) {
4534
+ var yy = alasql.yy;
4535
+ if (
4536
+ (node instanceof yy.Select && key === 'columns') ||
4537
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4538
+ (node.srchid === 'RETURN' && key === 'args')
4539
+ )
4540
+ return 'alias';
4541
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4542
+ return '';
4543
+ }
4544
+
4545
+ // Names that never reach generated code: they are used only at run time, as the key of a
4546
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4547
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4548
+ // names are free (what they hold is still walked), and skipChild are children that are not
4549
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4550
+ var varValueFree = true;
4551
+ var intoVarFree = true;
4552
+ var freeKeys = [
4553
+ ['CreateIndex', {indexid: 1}],
4554
+ ['DropIndex', {indexid: 1}],
4555
+ ['Reindex', {indexid: 1}],
4556
+ ['CreateTrigger', {trigger: 1}],
4557
+ ['DropTrigger', {trigger: 1}],
4558
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4559
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4560
+ ['DetachDatabase', {databaseid: 1}],
4561
+ ['DropDatabase', {databaseid: 1}],
4562
+ ['ShowTables', {databaseid: 1}],
4563
+ ['ShowColumns', {databaseid: 1}],
4564
+ ['ShowIndex', {databaseid: 1}],
4565
+ ['ShowCreateTable', {databaseid: 1}],
4566
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4567
+ ];
4568
+ var freeChild = [
4569
+ ['CreateTable', {table: 1}],
4570
+ ['DropTable', {tables: 1}],
4571
+ ['TruncateTable', {table: 1}],
4572
+ ['AlterTable', {table: 1}],
4573
+ ['ShowColumns', {table: 1}],
4574
+ ['ShowIndex', {table: 1}],
4575
+ ['ShowCreateTable', {table: 1}],
4576
+ ['CreateIndex', {table: 1}],
4577
+ ['CreateTrigger', {table: 1}],
4578
+ ['Declare', {declares: 1}],
4579
+ ];
4580
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4581
+
4582
+ function freeOf(list, node) {
4583
+ var yy = alasql.yy;
4584
+ for (var i = 0; i < list.length; i++) {
4585
+ var cls = yy[list[i][0]];
4586
+ if (cls && node instanceof cls) return list[i][1];
4587
+ }
4588
+ return null;
4589
+ }
4590
+
4591
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4592
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4593
+ function freeVar(node, key, entry) {
4594
+ var yy = alasql.yy;
4595
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4596
+ if (entry === 'source') return false;
4597
+ if (entry === 'into') return intoVarFree;
4598
+ return varValueFree;
4599
+ }
4600
+
4601
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4602
+ function freeSet(node, key) {
4603
+ var yy = alasql.yy;
4604
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4605
+ }
4606
+
4607
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4608
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4609
+ // names never reach generated code.
4610
+ function checkNames(node, data, entry) {
4611
+ if (node === null || typeof node !== 'object') return;
4612
+ var i;
4613
+ if (Array.isArray(node)) {
4614
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4615
+ return;
4616
+ }
4617
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4618
+ if (data && !isNode) {
4619
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4620
+ return;
4621
+ }
4622
+ var yy = alasql.yy;
4623
+ if (node instanceof yy.Json) {
4624
+ checkNames(node.value, true, false);
4625
+ return;
4626
+ }
4627
+ var fk = freeOf(freeKeys, node);
4628
+ var fc = freeOf(freeChild, node);
4629
+ var sc = freeOf(skipChild, node);
4630
+ for (var key in node) {
4631
+ if (!hasOwn.call(node, key)) continue;
4632
+ var value = node[key];
4633
+ if (sc && sc[key] === 1) continue;
4634
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4635
+ checkOrder(node.columns, value);
4636
+ continue;
4637
+ }
4638
+ // the column list of a WITH entry renames the result at run time
4639
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4640
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4641
+ if (typeof value === 'string') {
4642
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4643
+ continue;
4644
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4645
+ checkName(value);
4646
+ } else if (value !== null && typeof value === 'object') {
4647
+ if (Array.isArray(value)) {
4648
+ for (i = 0; i < value.length; i++) {
4649
+ if (typeof value[i] === 'string') {
4650
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4651
+ } else {
4652
+ checkNames(value[i], false, child || entryOf(node, key));
4653
+ }
4654
+ }
4655
+ } else {
4656
+ checkNames(value, false, child);
4657
+ }
4658
+ }
4659
+ }
4660
+ }
4661
+
4662
+ return function (ast) {
4663
+ checkNames(ast, false, false);
4664
+ };
4665
+ })();
4666
+
4475
4667
  /**
4476
4668
  List of engines of external databases
4477
4669
  @type {object}
@@ -6533,7 +6725,7 @@ function queryfn3(query) {
6533
6725
 
6534
6726
  // Prepare variables
6535
6727
  query.data = [];
6536
- query.xgroups = {};
6728
+ query.xgroups=Object.create(null);
6537
6729
  query.groups = [];
6538
6730
 
6539
6731
  // Level of Joins
@@ -7810,7 +8002,7 @@ yy.Select.prototype.compileFrom = function (query) {
7810
8002
  return res;
7811
8003
  };
7812
8004
  } else if (tq instanceof yy.ParamValue) {
7813
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8005
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
7814
8006
  if (tq.array) ps += ',true';
7815
8007
  ps += ');if(cb)res=cb(res,idx,query);return res';
7816
8008
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8020,7 +8212,7 @@ yy.Select.prototype.compileJoins = function (query) {
8020
8212
  srcwherefn: returnTrue,
8021
8213
  };
8022
8214
  const jnparam = jn.param.param;
8023
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
8215
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8024
8216
  if (jn.array) ps += ',true';
8025
8217
  ps += '); if(cb) res=cb(res, idx, query); return res';
8026
8218
 
@@ -8705,7 +8897,7 @@ function compileSelectStar(query, aliases, joinstar) {
8705
8897
  } else {
8706
8898
 
8707
8899
  // if column not exist, then copy all
8708
- sp += 'var w=p["' + alias + '"];for(var k in w){if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=w[k]};';
8900
+ sp += 'var w=p["' + escapeq(alias) + '"];for(var k in w){if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=w[k]};';
8709
8901
 
8710
8902
  query.dirtyColumns = true;
8711
8903
  }
@@ -8731,7 +8923,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8731
8923
  if (col.columnid === '*') {
8732
8924
  if (col.func) {
8733
8925
  sp +=
8734
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
8926
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
8735
8927
  } else if (col.tableid) {
8736
8928
  //Copy all
8737
8929
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -8783,7 +8975,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8783
8975
  if (isMultisheetParam) {
8784
8976
  sp =
8785
8977
  'var r={};var w=p["' +
8786
- tbid +
8978
+ escapeq(tbid) +
8787
8979
  '"];' +
8788
8980
  'var cols=[' +
8789
8981
  self.columns
@@ -8794,7 +8986,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8794
8986
  '];var colas=[' +
8795
8987
  self.columns
8796
8988
  .map(function (col) {
8797
- return "'" + (col.as || col.columnid) + "'";
8989
+ return "'" + escapeq(col.as || col.columnid) + "'";
8798
8990
  })
8799
8991
  .join(',') +
8800
8992
  '];' +
@@ -8958,9 +9150,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
8958
9150
  this.orderColumns.forEach(function (v, idx) {
8959
9151
  var key = '$$$' + idx;
8960
9152
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
8961
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9153
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
8962
9154
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
8963
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9155
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
8964
9156
  } else {
8965
9157
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
8966
9158
  }
@@ -9052,7 +9244,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9052
9244
  query.groupColumns[colas] = col.nick;
9053
9245
 
9054
9246
  // if(col.as) {
9055
- s += "r['" + colas + "']=";
9247
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9056
9248
  // // } else {
9057
9249
  // // s += 'r[\''+escapeq()+'\']=';
9058
9250
  // // };
@@ -9079,7 +9271,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9079
9271
  self.columns.forEach(function (col) {
9080
9272
 
9081
9273
  if (query.ingroup.indexOf(col.nick) > -1) {
9082
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9274
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9083
9275
  }
9084
9276
  });
9085
9277
 
@@ -9089,7 +9281,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9089
9281
  var key = '$$$' + idx;
9090
9282
 
9091
9283
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9092
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9284
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9093
9285
  } else {
9094
9286
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9095
9287
  }
@@ -10099,7 +10291,7 @@ yy.Over = class Over {
10099
10291
  }
10100
10292
  } else if (this.op === '!') {
10101
10293
  if (typeof this.right === 'string') {
10102
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
10294
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
10103
10295
  }
10104
10296
  // TODO - add other cases
10105
10297
  } else if (this.op === 'IS') {
@@ -10377,7 +10569,7 @@ yy.Over = class Over {
10377
10569
 
10378
10570
  toJS() {
10379
10571
  if (typeof this.param === 'string') {
10380
- return "params['" + this.param + "']";
10572
+ return "params['" + escapeq(this.param) + "']";
10381
10573
  }
10382
10574
 
10383
10575
  return 'params[' + this.param + ']';
@@ -11371,9 +11563,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
11371
11563
  }
11372
11564
  });
11373
11565
 
11566
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
11567
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
11568
+ // are escaped, so the string cannot end early.
11569
+ function escapeJSONString(s) {
11570
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
11571
+ if (c.length === 2) return c;
11572
+ if (c === '\\') return '\\\\';
11573
+ if (c === '"') return '\\"';
11574
+ if (c === '\n') return '\\n';
11575
+ if (c === '\r') return '\\r';
11576
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
11577
+ });
11578
+ }
11579
+
11374
11580
  function JSONtoJS(obj, context, tableid, defcols) {
11375
11581
  var s = '';
11376
- if (typeof obj == 'string') s = '"' + obj + '"';
11582
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
11377
11583
  else if (typeof obj == 'number') s = '(' + obj + ')';
11378
11584
  else if (typeof obj == 'boolean') s = obj;
11379
11585
  else if (typeof obj === 'object') {
@@ -11382,7 +11588,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
11382
11588
  } else if (!obj.toJS || obj instanceof yy.Json) {
11383
11589
  let ss = [];
11384
11590
  for (const k in obj) {
11385
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
11591
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
11386
11592
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
11387
11593
  ss.push(`${keyStr}:${valueStr}`);
11388
11594
  }
@@ -14542,22 +14748,22 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
14542
14748
  if (this.props && this.props.length > 0) {
14543
14749
  let fs;
14544
14750
  if (this.method === '@') {
14545
- fs = `alasql.vars['${this.variable}']`;
14751
+ fs = `alasql.vars[k('${this.variable}')]`;
14546
14752
  } else {
14547
- fs = `params['${this.variable}']`;
14753
+ fs = `params[k('${this.variable}')]`;
14548
14754
  }
14549
14755
  this.props.forEach(prop => {
14550
14756
  if (typeof prop === 'string') {
14551
- fs += `['${prop}']`;
14757
+ fs += `[k('${prop}')]`;
14552
14758
  } else if (typeof prop === 'number') {
14553
14759
  fs += `[${prop}]`;
14554
14760
  } else {
14555
14761
  // Assuming prop.toJS() is a method that converts prop to a JavaScript expression.
14556
- fs += `[${prop.toJS()}]`;
14762
+ fs += `[k(${prop.toJS()})]`;
14557
14763
  }
14558
14764
  });
14559
14765
 
14560
- new Function('value, params, alasql', `${fs} = value`)(res, params, alasql);
14766
+ 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);
14561
14767
  } else {
14562
14768
  if (this.method === '@') {
14563
14769
  alasql.vars[this.variable] = res;