alasql 3.1.2 → 3.1.4

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 v3.1.2 build: v3-hotfix-dfc34cf0 | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v3.1.4 build: v3-hotfix-dfc34cf0 | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 3.1.2
4
+ @version 3.1.4
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2023 Andrey Gershun & Mathias Wulff
@@ -142,7 +142,7 @@ var alasql = function(sql, params, cb, scope) {
142
142
  Current version of alasql
143
143
  @constant {string}
144
144
  */
145
- alasql.version = '3.1.2';
145
+ alasql.version = '3.1.4';
146
146
  alasql.build = 'v3-hotfix-dfc34cf0';
147
147
 
148
148
  /**
@@ -2499,7 +2499,7 @@ next:function () {
2499
2499
 
2500
2500
  // return next match that has a token
2501
2501
  lex:function lex () {
2502
- var r = this.next();
2502
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2503
2503
  if (r) {
2504
2504
  return r;
2505
2505
  } else {
@@ -4455,9 +4455,201 @@ alasql.parser.parseError = function (str, hash) {
4455
4455
  }
4456
4456
  */
4457
4457
  alasql.parse = function (sql) {
4458
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4458
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4459
+ checkSqlNames(ast);
4460
+ return ast;
4459
4461
  };
4460
4462
 
4463
+ /*
4464
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4465
+ holds a quote, a backslash or a line break would end the string and make the rest code
4466
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4467
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4468
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4469
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4470
+ of a JSON value are data, not names.
4471
+ */
4472
+ var checkSqlNames = (function () {
4473
+ var hasOwn = Object.prototype.hasOwnProperty;
4474
+ var badName = /['\\\n\r\u2028\u2029]/;
4475
+ var nameKeys = {
4476
+ as: 1,
4477
+ columnid: 1,
4478
+ tableid: 1,
4479
+ databaseid: 1,
4480
+ viewid: 1,
4481
+ indexid: 1,
4482
+ trigger: 1,
4483
+ variable: 1,
4484
+ dropcolumn: 1,
4485
+ renamecolumn: 1,
4486
+ renameto: 1,
4487
+ to: 1,
4488
+ tocolumnid: 1,
4489
+ forcolumnid: 1,
4490
+ };
4491
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4492
+
4493
+ function checkName(name) {
4494
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4495
+ }
4496
+
4497
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4498
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4499
+ function checkOrder(columns, order) {
4500
+ var aliases = {};
4501
+ (columns || []).forEach(function (col) {
4502
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4503
+ });
4504
+ order.forEach(function (ord) {
4505
+ var e = ord && ord.expression;
4506
+ var name;
4507
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4508
+ if (typeof name === 'string') {
4509
+ if (!hasOwn.call(aliases, name)) checkName(name);
4510
+ return;
4511
+ }
4512
+ checkNames(ord, false, false);
4513
+ });
4514
+ }
4515
+
4516
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4517
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4518
+ // graph item, name is data.
4519
+ function entryOf(node, key) {
4520
+ var yy = alasql.yy;
4521
+ if (
4522
+ (node instanceof yy.Select && key === 'columns') ||
4523
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4524
+ (node.srchid === 'RETURN' && key === 'args')
4525
+ )
4526
+ return 'alias';
4527
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4528
+ return '';
4529
+ }
4530
+
4531
+ // Names that never reach generated code: they are used only at run time, as the key of a
4532
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4533
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4534
+ // names are free (what they hold is still walked), and skipChild are children that are not
4535
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4536
+ var varValueFree = true;
4537
+ var intoVarFree = false;
4538
+ var freeKeys = [
4539
+ ['CreateIndex', {indexid: 1}],
4540
+ ['DropIndex', {indexid: 1}],
4541
+ ['Reindex', {indexid: 1}],
4542
+ ['CreateTrigger', {trigger: 1}],
4543
+ ['DropTrigger', {trigger: 1}],
4544
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4545
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4546
+ ['DetachDatabase', {databaseid: 1}],
4547
+ ['DropDatabase', {databaseid: 1}],
4548
+ ['ShowTables', {databaseid: 1}],
4549
+ ['ShowColumns', {databaseid: 1}],
4550
+ ['ShowIndex', {databaseid: 1}],
4551
+ ['ShowCreateTable', {databaseid: 1}],
4552
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4553
+ ];
4554
+ var freeChild = [
4555
+ ['CreateTable', {table: 1}],
4556
+ ['DropTable', {tables: 1}],
4557
+ ['TruncateTable', {table: 1}],
4558
+ ['AlterTable', {table: 1}],
4559
+ ['ShowColumns', {table: 1}],
4560
+ ['ShowIndex', {table: 1}],
4561
+ ['ShowCreateTable', {table: 1}],
4562
+ ['CreateIndex', {table: 1}],
4563
+ ['CreateTrigger', {table: 1}],
4564
+ ['Declare', {declares: 1}],
4565
+ ];
4566
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4567
+
4568
+ function freeOf(list, node) {
4569
+ var yy = alasql.yy;
4570
+ for (var i = 0; i < list.length; i++) {
4571
+ var cls = yy[list[i][0]];
4572
+ if (cls && node instanceof cls) return list[i][1];
4573
+ }
4574
+ return null;
4575
+ }
4576
+
4577
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4578
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4579
+ function freeVar(node, key, entry) {
4580
+ var yy = alasql.yy;
4581
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4582
+ if (entry === 'source') return false;
4583
+ if (entry === 'into') return intoVarFree;
4584
+ return varValueFree;
4585
+ }
4586
+
4587
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4588
+ function freeSet(node, key) {
4589
+ var yy = alasql.yy;
4590
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4591
+ }
4592
+
4593
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4594
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4595
+ // names never reach generated code.
4596
+ function checkNames(node, data, entry) {
4597
+ if (node === null || typeof node !== 'object') return;
4598
+ var i;
4599
+ if (Array.isArray(node)) {
4600
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4601
+ return;
4602
+ }
4603
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4604
+ if (data && !isNode) {
4605
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4606
+ return;
4607
+ }
4608
+ var yy = alasql.yy;
4609
+ if (node instanceof yy.Json) {
4610
+ checkNames(node.value, true, false);
4611
+ return;
4612
+ }
4613
+ var fk = freeOf(freeKeys, node);
4614
+ var fc = freeOf(freeChild, node);
4615
+ var sc = freeOf(skipChild, node);
4616
+ for (var key in node) {
4617
+ if (!hasOwn.call(node, key)) continue;
4618
+ var value = node[key];
4619
+ if (sc && sc[key] === 1) continue;
4620
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4621
+ checkOrder(node.columns, value);
4622
+ continue;
4623
+ }
4624
+ // the column list of a WITH entry renames the result at run time
4625
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4626
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4627
+ if (typeof value === 'string') {
4628
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4629
+ continue;
4630
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4631
+ checkName(value);
4632
+ } else if (value !== null && typeof value === 'object') {
4633
+ if (Array.isArray(value)) {
4634
+ for (i = 0; i < value.length; i++) {
4635
+ if (typeof value[i] === 'string') {
4636
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4637
+ } else {
4638
+ checkNames(value[i], false, child || entryOf(node, key));
4639
+ }
4640
+ }
4641
+ } else {
4642
+ checkNames(value, false, child);
4643
+ }
4644
+ }
4645
+ }
4646
+ }
4647
+
4648
+ return function (ast) {
4649
+ checkNames(ast, false, false);
4650
+ };
4651
+ })();
4652
+
4461
4653
  /**
4462
4654
  List of engines of external databases
4463
4655
  @type {object}
@@ -6538,7 +6730,7 @@ function queryfn3(query) {
6538
6730
 
6539
6731
  // Prepare variables
6540
6732
  query.data = [];
6541
- query.xgroups = {};
6733
+ query.xgroups=Object.create(null);
6542
6734
  query.groups = [];
6543
6735
 
6544
6736
  // Level of Joins
@@ -7590,7 +7782,7 @@ yy.Select = class Select {
7590
7782
  // Save data into parameters array
7591
7783
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
7592
7784
  //
7593
- query.intofns = "params['" + this.into.param + "'].push(r)";
7785
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
7594
7786
  }
7595
7787
 
7596
7788
  if (query.intofns) {
@@ -8002,7 +8194,7 @@ yy.Select.prototype.compileFrom = function (query) {
8002
8194
  return res;
8003
8195
  };
8004
8196
  } else if (tq instanceof yy.ParamValue) {
8005
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8197
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
8006
8198
 
8007
8199
  if (tq.array) ps += ',true';
8008
8200
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8251,7 +8443,7 @@ yy.Select.prototype.compileJoins = function (query) {
8251
8443
  // source.data = ;
8252
8444
  var jnparam = jn.param.param;
8253
8445
 
8254
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8446
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8255
8447
  if (jn.array) ps += ',true';
8256
8448
  ps += ');if(cb)res=cb(res, idx, query);return res';
8257
8449
 
@@ -9120,7 +9312,7 @@ function compileSelectStar(query, aliases, joinstar) {
9120
9312
  } else {
9121
9313
 
9122
9314
  // if column not exist, then copy all
9123
- 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]};';
9315
+ 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]};';
9124
9316
 
9125
9317
  query.dirtyColumns = true;
9126
9318
  }
@@ -9146,7 +9338,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9146
9338
  if (col.columnid === '*') {
9147
9339
  if (col.func) {
9148
9340
  sp +=
9149
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9341
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9150
9342
  } else if (col.tableid) {
9151
9343
  //Copy all
9152
9344
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9198,7 +9390,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9198
9390
  if (isMultisheetParam) {
9199
9391
  sp =
9200
9392
  'var r={};var w=p["' +
9201
- tbid +
9393
+ escapeq(tbid) +
9202
9394
  '"];' +
9203
9395
  'var cols=[' +
9204
9396
  self.columns
@@ -9209,7 +9401,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9209
9401
  '];var colas=[' +
9210
9402
  self.columns
9211
9403
  .map(function (col) {
9212
- return "'" + (col.as || col.columnid) + "'";
9404
+ return "'" + escapeq(col.as || col.columnid) + "'";
9213
9405
  })
9214
9406
  .join(',') +
9215
9407
  '];' +
@@ -9373,9 +9565,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9373
9565
  this.orderColumns.forEach(function (v, idx) {
9374
9566
  var key = '$$$' + idx;
9375
9567
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9376
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9568
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9377
9569
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9378
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9570
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9379
9571
  } else {
9380
9572
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9381
9573
  }
@@ -9467,7 +9659,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9467
9659
  query.groupColumns[colas] = col.nick;
9468
9660
 
9469
9661
  // if(col.as) {
9470
- s += "r['" + colas + "']=";
9662
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9471
9663
  // // } else {
9472
9664
  // // s += 'r[\''+escapeq()+'\']=';
9473
9665
  // // };
@@ -9494,7 +9686,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9494
9686
  self.columns.forEach(function (col) {
9495
9687
 
9496
9688
  if (query.ingroup.indexOf(col.nick) > -1) {
9497
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9689
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9498
9690
  }
9499
9691
  });
9500
9692
 
@@ -9504,7 +9696,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9504
9696
  var key = '$$$' + idx;
9505
9697
 
9506
9698
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9507
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9699
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9508
9700
  } else {
9509
9701
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9510
9702
  }
@@ -10598,7 +10790,7 @@ yy.Over = class Over {
10598
10790
 
10599
10791
  if (this.op === '!') {
10600
10792
  if (typeof this.right === 'string') {
10601
- s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + this.right + '"]';
10793
+ s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + escapeq(this.right) + '"]';
10602
10794
  }
10603
10795
  // TODO - add other cases
10604
10796
  }
@@ -10950,7 +11142,7 @@ yy.Over = class Over {
10950
11142
 
10951
11143
  toJS() {
10952
11144
  if (typeof this.param === 'string') {
10953
- return "params['" + this.param + "']";
11145
+ return "params['" + escapeq(this.param) + "']";
10954
11146
  }
10955
11147
 
10956
11148
  return 'params[' + this.param + ']';
@@ -12022,9 +12214,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12022
12214
  return s;
12023
12215
  });
12024
12216
 
12217
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12218
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12219
+ // are escaped, so the string cannot end early.
12220
+ function escapeJSONString(s) {
12221
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12222
+ if (c.length === 2) return c;
12223
+ if (c === '\\') return '\\\\';
12224
+ if (c === '"') return '\\"';
12225
+ if (c === '\n') return '\\n';
12226
+ if (c === '\r') return '\\r';
12227
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12228
+ });
12229
+ }
12230
+
12025
12231
  function JSONtoJS(obj, context, tableid, defcols) {
12026
12232
  var s = '';
12027
- if (typeof obj == 'string') s = '"' + obj + '"';
12233
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12028
12234
  else if (typeof obj == 'number') s = '(' + obj + ')';
12029
12235
  else if (typeof obj == 'boolean') s = obj;
12030
12236
  else if (typeof obj == 'object') {
@@ -12043,7 +12249,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12043
12249
  var ss = [];
12044
12250
  for (var k in obj) {
12045
12251
  var s1 = '';
12046
- if (typeof k == 'string') s1 += '"' + k + '"';
12252
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
12047
12253
  else if (typeof k == 'number') s1 += k;
12048
12254
  else if (typeof k == 'boolean') s1 += k;
12049
12255
  else {
@@ -15452,19 +15658,19 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
15452
15658
  }
15453
15659
  if (this.props && this.props.length > 0) {
15454
15660
  if (this.method == '@') {
15455
- var fs = "alasql.vars['" + this.variable + "']";
15661
+ var fs = "alasql.vars[k('"+this.variable+"')]";
15456
15662
  } else {
15457
- var fs = "params['" + this.variable + "']";
15663
+ var fs = "params[k('"+this.variable+"')]";
15458
15664
  }
15459
15665
  fs += this.props
15460
15666
  .map(function (prop) {
15461
15667
  if (typeof prop == 'string') {
15462
- return "['" + prop + "']";
15668
+ return "[k('"+prop+"')]";
15463
15669
  } else if (typeof prop == 'number') {
15464
15670
  return '[' + prop + ']';
15465
15671
  } else {
15466
15672
 
15467
- return '[' + prop.toJS() + ']';
15673
+ return "[k("+prop.toJS()+")]";
15468
15674
  // } else {
15469
15675
 
15470
15676
  // throw new Error('Wrong SET property');
@@ -15472,7 +15678,7 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
15472
15678
  })
15473
15679
  .join();
15474
15680
 
15475
- new Function('value,params,alasql', 'var y;' + fs + '=value')(res, params, alasql);
15681
+ new Function('value,params,alasql', 'var y;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);
15476
15682
  } else {
15477
15683
  if (this.method == '@') {
15478
15684
  alasql.vars[this.variable] = res;