alasql 4.5.3 → 4.5.5

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.5.3 build: develop-a7daf2ff | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.5.5 build: develop-a7daf2ff | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.5.3
4
+ @version 4.5.5
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.5.3';
144
+ alasql.version = '4.5.5';
145
145
  alasql.build = 'develop-a7daf2ff';
146
146
 
147
147
  /**
@@ -2502,7 +2502,7 @@ next:function () {
2502
2502
 
2503
2503
  // return next match that has a token
2504
2504
  lex:function lex () {
2505
- var r = this.next();
2505
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2506
2506
  if (r) {
2507
2507
  return r;
2508
2508
  } else {
@@ -4478,9 +4478,201 @@ alasql.parser.parseError = function (str, hash) {
4478
4478
  }
4479
4479
  */
4480
4480
  alasql.parse = function (sql) {
4481
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4481
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4482
+ checkSqlNames(ast);
4483
+ return ast;
4482
4484
  };
4483
4485
 
4486
+ /*
4487
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4488
+ holds a quote, a backslash or a line break would end the string and make the rest code
4489
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4490
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4491
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4492
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4493
+ of a JSON value are data, not names.
4494
+ */
4495
+ var checkSqlNames = (function () {
4496
+ var hasOwn = Object.prototype.hasOwnProperty;
4497
+ var badName = /['\\\n\r\u2028\u2029]/;
4498
+ var nameKeys = {
4499
+ as: 1,
4500
+ columnid: 1,
4501
+ tableid: 1,
4502
+ databaseid: 1,
4503
+ viewid: 1,
4504
+ indexid: 1,
4505
+ trigger: 1,
4506
+ variable: 1,
4507
+ dropcolumn: 1,
4508
+ renamecolumn: 1,
4509
+ renameto: 1,
4510
+ to: 1,
4511
+ tocolumnid: 1,
4512
+ forcolumnid: 1,
4513
+ };
4514
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4515
+
4516
+ function checkName(name) {
4517
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4518
+ }
4519
+
4520
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4521
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4522
+ function checkOrder(columns, order) {
4523
+ var aliases = {};
4524
+ (columns || []).forEach(function (col) {
4525
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4526
+ });
4527
+ order.forEach(function (ord) {
4528
+ var e = ord && ord.expression;
4529
+ var name;
4530
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4531
+ if (typeof name === 'string') {
4532
+ if (!hasOwn.call(aliases, name)) checkName(name);
4533
+ return;
4534
+ }
4535
+ checkNames(ord, false, false);
4536
+ });
4537
+ }
4538
+
4539
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4540
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4541
+ // graph item, name is data.
4542
+ function entryOf(node, key) {
4543
+ var yy = alasql.yy;
4544
+ if (
4545
+ (node instanceof yy.Select && key === 'columns') ||
4546
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4547
+ (node.srchid === 'RETURN' && key === 'args')
4548
+ )
4549
+ return 'alias';
4550
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4551
+ return '';
4552
+ }
4553
+
4554
+ // Names that never reach generated code: they are used only at run time, as the key of a
4555
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4556
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4557
+ // names are free (what they hold is still walked), and skipChild are children that are not
4558
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4559
+ var varValueFree = true;
4560
+ var intoVarFree = true;
4561
+ var freeKeys = [
4562
+ ['CreateIndex', {indexid: 1}],
4563
+ ['DropIndex', {indexid: 1}],
4564
+ ['Reindex', {indexid: 1}],
4565
+ ['CreateTrigger', {trigger: 1}],
4566
+ ['DropTrigger', {trigger: 1}],
4567
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4568
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4569
+ ['DetachDatabase', {databaseid: 1}],
4570
+ ['DropDatabase', {databaseid: 1}],
4571
+ ['ShowTables', {databaseid: 1}],
4572
+ ['ShowColumns', {databaseid: 1}],
4573
+ ['ShowIndex', {databaseid: 1}],
4574
+ ['ShowCreateTable', {databaseid: 1}],
4575
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4576
+ ];
4577
+ var freeChild = [
4578
+ ['CreateTable', {table: 1}],
4579
+ ['DropTable', {tables: 1}],
4580
+ ['TruncateTable', {table: 1}],
4581
+ ['AlterTable', {table: 1}],
4582
+ ['ShowColumns', {table: 1}],
4583
+ ['ShowIndex', {table: 1}],
4584
+ ['ShowCreateTable', {table: 1}],
4585
+ ['CreateIndex', {table: 1}],
4586
+ ['CreateTrigger', {table: 1}],
4587
+ ['Declare', {declares: 1}],
4588
+ ];
4589
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4590
+
4591
+ function freeOf(list, node) {
4592
+ var yy = alasql.yy;
4593
+ for (var i = 0; i < list.length; i++) {
4594
+ var cls = yy[list[i][0]];
4595
+ if (cls && node instanceof cls) return list[i][1];
4596
+ }
4597
+ return null;
4598
+ }
4599
+
4600
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4601
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4602
+ function freeVar(node, key, entry) {
4603
+ var yy = alasql.yy;
4604
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4605
+ if (entry === 'source') return false;
4606
+ if (entry === 'into') return intoVarFree;
4607
+ return varValueFree;
4608
+ }
4609
+
4610
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4611
+ function freeSet(node, key) {
4612
+ var yy = alasql.yy;
4613
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4614
+ }
4615
+
4616
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4617
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4618
+ // names never reach generated code.
4619
+ function checkNames(node, data, entry) {
4620
+ if (node === null || typeof node !== 'object') return;
4621
+ var i;
4622
+ if (Array.isArray(node)) {
4623
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4624
+ return;
4625
+ }
4626
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4627
+ if (data && !isNode) {
4628
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4629
+ return;
4630
+ }
4631
+ var yy = alasql.yy;
4632
+ if (node instanceof yy.Json) {
4633
+ checkNames(node.value, true, false);
4634
+ return;
4635
+ }
4636
+ var fk = freeOf(freeKeys, node);
4637
+ var fc = freeOf(freeChild, node);
4638
+ var sc = freeOf(skipChild, node);
4639
+ for (var key in node) {
4640
+ if (!hasOwn.call(node, key)) continue;
4641
+ var value = node[key];
4642
+ if (sc && sc[key] === 1) continue;
4643
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4644
+ checkOrder(node.columns, value);
4645
+ continue;
4646
+ }
4647
+ // the column list of a WITH entry renames the result at run time
4648
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4649
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4650
+ if (typeof value === 'string') {
4651
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4652
+ continue;
4653
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4654
+ checkName(value);
4655
+ } else if (value !== null && typeof value === 'object') {
4656
+ if (Array.isArray(value)) {
4657
+ for (i = 0; i < value.length; i++) {
4658
+ if (typeof value[i] === 'string') {
4659
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4660
+ } else {
4661
+ checkNames(value[i], false, child || entryOf(node, key));
4662
+ }
4663
+ }
4664
+ } else {
4665
+ checkNames(value, false, child);
4666
+ }
4667
+ }
4668
+ }
4669
+ }
4670
+
4671
+ return function (ast) {
4672
+ checkNames(ast, false, false);
4673
+ };
4674
+ })();
4675
+
4484
4676
  /**
4485
4677
  List of engines of external databases
4486
4678
  @type {object}
@@ -6542,7 +6734,7 @@ function queryfn3(query) {
6542
6734
 
6543
6735
  // Prepare variables
6544
6736
  query.data = [];
6545
- query.xgroups = {};
6737
+ query.xgroups=Object.create(null);
6546
6738
  query.groups = [];
6547
6739
 
6548
6740
  // Level of Joins
@@ -7829,7 +8021,7 @@ yy.Select.prototype.compileFrom = function (query) {
7829
8021
  return res;
7830
8022
  };
7831
8023
  } else if (tq instanceof yy.ParamValue) {
7832
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8024
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
7833
8025
  if (tq.array) ps += ',true';
7834
8026
  ps += ');if(cb)res=cb(res,idx,query);return res';
7835
8027
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8039,7 +8231,7 @@ yy.Select.prototype.compileJoins = function (query) {
8039
8231
  srcwherefn: returnTrue,
8040
8232
  };
8041
8233
  const jnparam = jn.param.param;
8042
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
8234
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8043
8235
  if (jn.array) ps += ',true';
8044
8236
  ps += '); if(cb) res=cb(res, idx, query); return res';
8045
8237
 
@@ -8777,7 +8969,7 @@ function compileSelectStar(query, aliases, joinstar) {
8777
8969
  } else {
8778
8970
 
8779
8971
  // if column not exist, then copy all
8780
- 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]};';
8972
+ 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]};';
8781
8973
 
8782
8974
  query.dirtyColumns = true;
8783
8975
  }
@@ -8803,7 +8995,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8803
8995
  if (col.columnid === '*') {
8804
8996
  if (col.func) {
8805
8997
  sp +=
8806
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
8998
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
8807
8999
  } else if (col.tableid) {
8808
9000
  //Copy all
8809
9001
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -8855,7 +9047,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8855
9047
  if (isMultisheetParam) {
8856
9048
  sp =
8857
9049
  'var r={};var w=p["' +
8858
- tbid +
9050
+ escapeq(tbid) +
8859
9051
  '"];' +
8860
9052
  'var cols=[' +
8861
9053
  self.columns
@@ -8866,7 +9058,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8866
9058
  '];var colas=[' +
8867
9059
  self.columns
8868
9060
  .map(function (col) {
8869
- return "'" + (col.as || col.columnid) + "'";
9061
+ return "'" + escapeq(col.as || col.columnid) + "'";
8870
9062
  })
8871
9063
  .join(',') +
8872
9064
  '];' +
@@ -9030,9 +9222,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9030
9222
  this.orderColumns.forEach(function (v, idx) {
9031
9223
  var key = '$$$' + idx;
9032
9224
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9033
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9225
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9034
9226
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9035
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9227
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9036
9228
  } else {
9037
9229
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9038
9230
  }
@@ -9124,7 +9316,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9124
9316
  query.groupColumns[colas] = col.nick;
9125
9317
 
9126
9318
  // if(col.as) {
9127
- s += "r['" + colas + "']=";
9319
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9128
9320
  // // } else {
9129
9321
  // // s += 'r[\''+escapeq()+'\']=';
9130
9322
  // // };
@@ -9151,7 +9343,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9151
9343
  self.columns.forEach(function (col) {
9152
9344
 
9153
9345
  if (query.ingroup.indexOf(col.nick) > -1) {
9154
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9346
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9155
9347
  }
9156
9348
  });
9157
9349
 
@@ -9161,7 +9353,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9161
9353
  var key = '$$$' + idx;
9162
9354
 
9163
9355
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9164
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9356
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9165
9357
  } else {
9166
9358
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9167
9359
  }
@@ -10171,7 +10363,7 @@ yy.Over = class Over {
10171
10363
  }
10172
10364
  } else if (this.op === '!') {
10173
10365
  if (typeof this.right === 'string') {
10174
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
10366
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
10175
10367
  }
10176
10368
  // TODO - add other cases
10177
10369
  } else if (this.op === 'IS') {
@@ -10449,7 +10641,7 @@ yy.Over = class Over {
10449
10641
 
10450
10642
  toJS() {
10451
10643
  if (typeof this.param === 'string') {
10452
- return "params['" + this.param + "']";
10644
+ return "params['" + escapeq(this.param) + "']";
10453
10645
  }
10454
10646
 
10455
10647
  return 'params[' + this.param + ']';
@@ -11443,9 +11635,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
11443
11635
  }
11444
11636
  });
11445
11637
 
11638
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
11639
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
11640
+ // are escaped, so the string cannot end early.
11641
+ function escapeJSONString(s) {
11642
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
11643
+ if (c.length === 2) return c;
11644
+ if (c === '\\') return '\\\\';
11645
+ if (c === '"') return '\\"';
11646
+ if (c === '\n') return '\\n';
11647
+ if (c === '\r') return '\\r';
11648
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
11649
+ });
11650
+ }
11651
+
11446
11652
  function JSONtoJS(obj, context, tableid, defcols) {
11447
11653
  var s = '';
11448
- if (typeof obj == 'string') s = '"' + obj + '"';
11654
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
11449
11655
  else if (typeof obj == 'number') s = '(' + obj + ')';
11450
11656
  else if (typeof obj == 'boolean') s = obj;
11451
11657
  else if (typeof obj === 'object') {
@@ -11454,7 +11660,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
11454
11660
  } else if (!obj.toJS || obj instanceof yy.Json) {
11455
11661
  let ss = [];
11456
11662
  for (const k in obj) {
11457
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
11663
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
11458
11664
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
11459
11665
  ss.push(`${keyStr}:${valueStr}`);
11460
11666
  }
@@ -14633,22 +14839,22 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
14633
14839
  if (this.props && this.props.length > 0) {
14634
14840
  let fs;
14635
14841
  if (this.method === '@') {
14636
- fs = `alasql.vars['${this.variable}']`;
14842
+ fs = `alasql.vars[k('${this.variable}')]`;
14637
14843
  } else {
14638
- fs = `params['${this.variable}']`;
14844
+ fs = `params[k('${this.variable}')]`;
14639
14845
  }
14640
14846
  this.props.forEach(prop => {
14641
14847
  if (typeof prop === 'string') {
14642
- fs += `['${prop}']`;
14848
+ fs += `[k('${prop}')]`;
14643
14849
  } else if (typeof prop === 'number') {
14644
14850
  fs += `[${prop}]`;
14645
14851
  } else {
14646
14852
  // Assuming prop.toJS() is a method that converts prop to a JavaScript expression.
14647
- fs += `[${prop.toJS()}]`;
14853
+ fs += `[k(${prop.toJS()})]`;
14648
14854
  }
14649
14855
  });
14650
14856
 
14651
- new Function('value, params, alasql', `${fs} = value`)(res, params, alasql);
14857
+ 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);
14652
14858
  } else {
14653
14859
  if (this.method === '@') {
14654
14860
  alasql.vars[this.variable] = res;