alasql 4.9.1 → 4.9.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/dist/alasql.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.9.1 build: develop-af967432 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.9.3 build: develop-af967432 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.9.1
4
+ @version 4.9.3
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.9.1';
144
+ alasql.version = '4.9.3';
145
145
  alasql.build = 'develop-af967432';
146
146
 
147
147
  /**
@@ -2505,7 +2505,7 @@ next:function () {
2505
2505
 
2506
2506
  // return next match that has a token
2507
2507
  lex:function lex () {
2508
- var r = this.next();
2508
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2509
2509
  if (r) {
2510
2510
  return r;
2511
2511
  } else {
@@ -4491,9 +4491,201 @@ alasql.parser.parseError = function (str, hash) {
4491
4491
  }
4492
4492
  */
4493
4493
  alasql.parse = function (sql) {
4494
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4494
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4495
+ checkSqlNames(ast);
4496
+ return ast;
4495
4497
  };
4496
4498
 
4499
+ /*
4500
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4501
+ holds a quote, a backslash or a line break would end the string and make the rest code
4502
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4503
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4504
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4505
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4506
+ of a JSON value are data, not names.
4507
+ */
4508
+ var checkSqlNames = (function () {
4509
+ var hasOwn = Object.prototype.hasOwnProperty;
4510
+ var badName = /['\\\n\r\u2028\u2029]/;
4511
+ var nameKeys = {
4512
+ as: 1,
4513
+ columnid: 1,
4514
+ tableid: 1,
4515
+ databaseid: 1,
4516
+ viewid: 1,
4517
+ indexid: 1,
4518
+ trigger: 1,
4519
+ variable: 1,
4520
+ dropcolumn: 1,
4521
+ renamecolumn: 1,
4522
+ renameto: 1,
4523
+ to: 1,
4524
+ tocolumnid: 1,
4525
+ forcolumnid: 1,
4526
+ };
4527
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4528
+
4529
+ function checkName(name) {
4530
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4531
+ }
4532
+
4533
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4534
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4535
+ function checkOrder(columns, order) {
4536
+ var aliases = {};
4537
+ (columns || []).forEach(function (col) {
4538
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4539
+ });
4540
+ order.forEach(function (ord) {
4541
+ var e = ord && ord.expression;
4542
+ var name;
4543
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4544
+ if (typeof name === 'string') {
4545
+ if (!hasOwn.call(aliases, name)) checkName(name);
4546
+ return;
4547
+ }
4548
+ checkNames(ord, false, false);
4549
+ });
4550
+ }
4551
+
4552
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4553
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4554
+ // graph item, name is data.
4555
+ function entryOf(node, key) {
4556
+ var yy = alasql.yy;
4557
+ if (
4558
+ (node instanceof yy.Select && key === 'columns') ||
4559
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4560
+ (node.srchid === 'RETURN' && key === 'args')
4561
+ )
4562
+ return 'alias';
4563
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4564
+ return '';
4565
+ }
4566
+
4567
+ // Names that never reach generated code: they are used only at run time, as the key of a
4568
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4569
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4570
+ // names are free (what they hold is still walked), and skipChild are children that are not
4571
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4572
+ var varValueFree = true;
4573
+ var intoVarFree = true;
4574
+ var freeKeys = [
4575
+ ['CreateIndex', {indexid: 1}],
4576
+ ['DropIndex', {indexid: 1}],
4577
+ ['Reindex', {indexid: 1}],
4578
+ ['CreateTrigger', {trigger: 1}],
4579
+ ['DropTrigger', {trigger: 1}],
4580
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4581
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4582
+ ['DetachDatabase', {databaseid: 1}],
4583
+ ['DropDatabase', {databaseid: 1}],
4584
+ ['ShowTables', {databaseid: 1}],
4585
+ ['ShowColumns', {databaseid: 1}],
4586
+ ['ShowIndex', {databaseid: 1}],
4587
+ ['ShowCreateTable', {databaseid: 1}],
4588
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4589
+ ];
4590
+ var freeChild = [
4591
+ ['CreateTable', {table: 1}],
4592
+ ['DropTable', {tables: 1}],
4593
+ ['TruncateTable', {table: 1}],
4594
+ ['AlterTable', {table: 1}],
4595
+ ['ShowColumns', {table: 1}],
4596
+ ['ShowIndex', {table: 1}],
4597
+ ['ShowCreateTable', {table: 1}],
4598
+ ['CreateIndex', {table: 1}],
4599
+ ['CreateTrigger', {table: 1}],
4600
+ ['Declare', {declares: 1}],
4601
+ ];
4602
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4603
+
4604
+ function freeOf(list, node) {
4605
+ var yy = alasql.yy;
4606
+ for (var i = 0; i < list.length; i++) {
4607
+ var cls = yy[list[i][0]];
4608
+ if (cls && node instanceof cls) return list[i][1];
4609
+ }
4610
+ return null;
4611
+ }
4612
+
4613
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4614
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4615
+ function freeVar(node, key, entry) {
4616
+ var yy = alasql.yy;
4617
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4618
+ if (entry === 'source') return false;
4619
+ if (entry === 'into') return intoVarFree;
4620
+ return varValueFree;
4621
+ }
4622
+
4623
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4624
+ function freeSet(node, key) {
4625
+ var yy = alasql.yy;
4626
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4627
+ }
4628
+
4629
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4630
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4631
+ // names never reach generated code.
4632
+ function checkNames(node, data, entry) {
4633
+ if (node === null || typeof node !== 'object') return;
4634
+ var i;
4635
+ if (Array.isArray(node)) {
4636
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4637
+ return;
4638
+ }
4639
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4640
+ if (data && !isNode) {
4641
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4642
+ return;
4643
+ }
4644
+ var yy = alasql.yy;
4645
+ if (node instanceof yy.Json) {
4646
+ checkNames(node.value, true, false);
4647
+ return;
4648
+ }
4649
+ var fk = freeOf(freeKeys, node);
4650
+ var fc = freeOf(freeChild, node);
4651
+ var sc = freeOf(skipChild, node);
4652
+ for (var key in node) {
4653
+ if (!hasOwn.call(node, key)) continue;
4654
+ var value = node[key];
4655
+ if (sc && sc[key] === 1) continue;
4656
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4657
+ checkOrder(node.columns, value);
4658
+ continue;
4659
+ }
4660
+ // the column list of a WITH entry renames the result at run time
4661
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4662
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4663
+ if (typeof value === 'string') {
4664
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4665
+ continue;
4666
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4667
+ checkName(value);
4668
+ } else if (value !== null && typeof value === 'object') {
4669
+ if (Array.isArray(value)) {
4670
+ for (i = 0; i < value.length; i++) {
4671
+ if (typeof value[i] === 'string') {
4672
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4673
+ } else {
4674
+ checkNames(value[i], false, child || entryOf(node, key));
4675
+ }
4676
+ }
4677
+ } else {
4678
+ checkNames(value, false, child);
4679
+ }
4680
+ }
4681
+ }
4682
+ }
4683
+
4684
+ return function (ast) {
4685
+ checkNames(ast, false, false);
4686
+ };
4687
+ })();
4688
+
4497
4689
  /**
4498
4690
  List of engines of external databases
4499
4691
  @type {object}
@@ -6575,7 +6767,7 @@ function queryfn3(query) {
6575
6767
 
6576
6768
  // Prepare variables
6577
6769
  query.data = [];
6578
- query.xgroups = {};
6770
+ query.xgroups=Object.create(null);
6579
6771
  query.groups = [];
6580
6772
 
6581
6773
  // Level of Joins
@@ -7865,7 +8057,7 @@ yy.Select.prototype.compileFrom = function (query) {
7865
8057
  return res;
7866
8058
  };
7867
8059
  } else if (tq instanceof yy.ParamValue) {
7868
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8060
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
7869
8061
  if (tq.array) ps += ',true';
7870
8062
  ps += ');if(cb)res=cb(res,idx,query);return res';
7871
8063
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8075,7 +8267,7 @@ yy.Select.prototype.compileJoins = function (query) {
8075
8267
  srcwherefn: returnTrue,
8076
8268
  };
8077
8269
  const jnparam = jn.param.param;
8078
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
8270
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8079
8271
  if (jn.array) ps += ',true';
8080
8272
  ps += '); if(cb) res=cb(res, idx, query); return res';
8081
8273
 
@@ -8873,7 +9065,7 @@ function compileSelectStar(query, aliases, joinstar) {
8873
9065
  } else {
8874
9066
 
8875
9067
  // if column not exist, then copy all
8876
- 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]};';
9068
+ 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]};';
8877
9069
 
8878
9070
  query.dirtyColumns = true;
8879
9071
  }
@@ -8899,7 +9091,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8899
9091
  if (col.columnid === '*') {
8900
9092
  if (col.func) {
8901
9093
  sp +=
8902
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9094
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
8903
9095
  } else if (col.tableid) {
8904
9096
  //Copy all
8905
9097
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -8951,7 +9143,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8951
9143
  if (isMultisheetParam) {
8952
9144
  sp =
8953
9145
  'var r={};var w=p["' +
8954
- tbid +
9146
+ escapeq(tbid) +
8955
9147
  '"];' +
8956
9148
  'var cols=[' +
8957
9149
  self.columns
@@ -8962,7 +9154,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8962
9154
  '];var colas=[' +
8963
9155
  self.columns
8964
9156
  .map(function (col) {
8965
- return "'" + (col.as || col.columnid) + "'";
9157
+ return "'" + escapeq(col.as || col.columnid) + "'";
8966
9158
  })
8967
9159
  .join(',') +
8968
9160
  '];' +
@@ -9126,9 +9318,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9126
9318
  this.orderColumns.forEach(function (v, idx) {
9127
9319
  var key = '$$$' + idx;
9128
9320
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9129
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9321
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9130
9322
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9131
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9323
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9132
9324
  } else {
9133
9325
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9134
9326
  }
@@ -9220,7 +9412,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9220
9412
  query.groupColumns[colas] = col.nick;
9221
9413
 
9222
9414
  // if(col.as) {
9223
- s += "r['" + colas + "']=";
9415
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9224
9416
  // // } else {
9225
9417
  // // s += 'r[\''+escapeq()+'\']=';
9226
9418
  // // };
@@ -9247,7 +9439,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9247
9439
  self.columns.forEach(function (col) {
9248
9440
 
9249
9441
  if (query.ingroup.indexOf(col.nick) > -1) {
9250
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9442
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9251
9443
  }
9252
9444
  });
9253
9445
 
@@ -9257,7 +9449,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9257
9449
  var key = '$$$' + idx;
9258
9450
 
9259
9451
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9260
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9452
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9261
9453
  } else {
9262
9454
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9263
9455
  }
@@ -10383,7 +10575,7 @@ yy.Over = class Over {
10383
10575
  }
10384
10576
  } else if (this.op === '!') {
10385
10577
  if (typeof this.right === 'string') {
10386
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
10578
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
10387
10579
  }
10388
10580
  // TODO - add other cases
10389
10581
  } else if (this.op === 'IS') {
@@ -10661,7 +10853,7 @@ yy.Over = class Over {
10661
10853
 
10662
10854
  toJS() {
10663
10855
  if (typeof this.param === 'string') {
10664
- return "params['" + this.param + "']";
10856
+ return "params['" + escapeq(this.param) + "']";
10665
10857
  }
10666
10858
 
10667
10859
  return 'params[' + this.param + ']';
@@ -11668,9 +11860,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
11668
11860
  }
11669
11861
  });
11670
11862
 
11863
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
11864
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
11865
+ // are escaped, so the string cannot end early.
11866
+ function escapeJSONString(s) {
11867
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
11868
+ if (c.length === 2) return c;
11869
+ if (c === '\\') return '\\\\';
11870
+ if (c === '"') return '\\"';
11871
+ if (c === '\n') return '\\n';
11872
+ if (c === '\r') return '\\r';
11873
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
11874
+ });
11875
+ }
11876
+
11671
11877
  function JSONtoJS(obj, context, tableid, defcols) {
11672
11878
  var s = '';
11673
- if (typeof obj == 'string') s = '"' + obj + '"';
11879
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
11674
11880
  else if (typeof obj == 'number') s = '(' + obj + ')';
11675
11881
  else if (typeof obj == 'boolean') s = obj;
11676
11882
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -11680,7 +11886,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
11680
11886
  } else if (!obj.toJS || obj instanceof yy.Json) {
11681
11887
  let ss = [];
11682
11888
  for (const k in obj) {
11683
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
11889
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
11684
11890
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
11685
11891
  ss.push(`${keyStr}:${valueStr}`);
11686
11892
  }
@@ -14859,22 +15065,22 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
14859
15065
  if (this.props && this.props.length > 0) {
14860
15066
  let fs;
14861
15067
  if (this.method === '@') {
14862
- fs = `alasql.vars['${this.variable}']`;
15068
+ fs = `alasql.vars[k('${this.variable}')]`;
14863
15069
  } else {
14864
- fs = `params['${this.variable}']`;
15070
+ fs = `params[k('${this.variable}')]`;
14865
15071
  }
14866
15072
  this.props.forEach(prop => {
14867
15073
  if (typeof prop === 'string') {
14868
- fs += `['${prop}']`;
15074
+ fs += `[k('${prop}')]`;
14869
15075
  } else if (typeof prop === 'number') {
14870
15076
  fs += `[${prop}]`;
14871
15077
  } else {
14872
15078
  // Assuming prop.toJS() is a method that converts prop to a JavaScript expression.
14873
- fs += `[${prop.toJS()}]`;
15079
+ fs += `[k(${prop.toJS()})]`;
14874
15080
  }
14875
15081
  });
14876
15082
 
14877
- new Function('value, params, alasql', `${fs} = value`)(res, params, alasql);
15083
+ 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);
14878
15084
  } else {
14879
15085
  if (this.method === '@') {
14880
15086
  alasql.vars[this.variable] = res;