alasql 4.1.12 → 4.1.14

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.1.12 build: develop-1493d57a | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.1.14 build: develop-1493d57a | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.1.12
4
+ @version 4.1.14
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2023 Andrey Gershun & Mathias Wulff
@@ -141,7 +141,7 @@ var alasql = function(sql, params, cb, scope) {
141
141
  Current version of alasql
142
142
  @constant {string}
143
143
  */
144
- alasql.version = '4.1.12';
144
+ alasql.version = '4.1.14';
145
145
  alasql.build = 'develop-1493d57a';
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 {
@@ -4449,9 +4449,201 @@ alasql.parser.parseError = function (str, hash) {
4449
4449
  }
4450
4450
  */
4451
4451
  alasql.parse = function (sql) {
4452
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4452
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4453
+ checkSqlNames(ast);
4454
+ return ast;
4453
4455
  };
4454
4456
 
4457
+ /*
4458
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4459
+ holds a quote, a backslash or a line break would end the string and make the rest code
4460
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4461
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4462
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4463
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4464
+ of a JSON value are data, not names.
4465
+ */
4466
+ var checkSqlNames = (function () {
4467
+ var hasOwn = Object.prototype.hasOwnProperty;
4468
+ var badName = /['\\\n\r\u2028\u2029]/;
4469
+ var nameKeys = {
4470
+ as: 1,
4471
+ columnid: 1,
4472
+ tableid: 1,
4473
+ databaseid: 1,
4474
+ viewid: 1,
4475
+ indexid: 1,
4476
+ trigger: 1,
4477
+ variable: 1,
4478
+ dropcolumn: 1,
4479
+ renamecolumn: 1,
4480
+ renameto: 1,
4481
+ to: 1,
4482
+ tocolumnid: 1,
4483
+ forcolumnid: 1,
4484
+ };
4485
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4486
+
4487
+ function checkName(name) {
4488
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4489
+ }
4490
+
4491
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4492
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4493
+ function checkOrder(columns, order) {
4494
+ var aliases = {};
4495
+ (columns || []).forEach(function (col) {
4496
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4497
+ });
4498
+ order.forEach(function (ord) {
4499
+ var e = ord && ord.expression;
4500
+ var name;
4501
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4502
+ if (typeof name === 'string') {
4503
+ if (!hasOwn.call(aliases, name)) checkName(name);
4504
+ return;
4505
+ }
4506
+ checkNames(ord, false, false);
4507
+ });
4508
+ }
4509
+
4510
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4511
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4512
+ // graph item, name is data.
4513
+ function entryOf(node, key) {
4514
+ var yy = alasql.yy;
4515
+ if (
4516
+ (node instanceof yy.Select && key === 'columns') ||
4517
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4518
+ (node.srchid === 'RETURN' && key === 'args')
4519
+ )
4520
+ return 'alias';
4521
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4522
+ return '';
4523
+ }
4524
+
4525
+ // Names that never reach generated code: they are used only at run time, as the key of a
4526
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4527
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4528
+ // names are free (what they hold is still walked), and skipChild are children that are not
4529
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4530
+ var varValueFree = true;
4531
+ var intoVarFree = true;
4532
+ var freeKeys = [
4533
+ ['CreateIndex', {indexid: 1}],
4534
+ ['DropIndex', {indexid: 1}],
4535
+ ['Reindex', {indexid: 1}],
4536
+ ['CreateTrigger', {trigger: 1}],
4537
+ ['DropTrigger', {trigger: 1}],
4538
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4539
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4540
+ ['DetachDatabase', {databaseid: 1}],
4541
+ ['DropDatabase', {databaseid: 1}],
4542
+ ['ShowTables', {databaseid: 1}],
4543
+ ['ShowColumns', {databaseid: 1}],
4544
+ ['ShowIndex', {databaseid: 1}],
4545
+ ['ShowCreateTable', {databaseid: 1}],
4546
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4547
+ ];
4548
+ var freeChild = [
4549
+ ['CreateTable', {table: 1}],
4550
+ ['DropTable', {tables: 1}],
4551
+ ['TruncateTable', {table: 1}],
4552
+ ['AlterTable', {table: 1}],
4553
+ ['ShowColumns', {table: 1}],
4554
+ ['ShowIndex', {table: 1}],
4555
+ ['ShowCreateTable', {table: 1}],
4556
+ ['CreateIndex', {table: 1}],
4557
+ ['CreateTrigger', {table: 1}],
4558
+ ['Declare', {declares: 1}],
4559
+ ];
4560
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4561
+
4562
+ function freeOf(list, node) {
4563
+ var yy = alasql.yy;
4564
+ for (var i = 0; i < list.length; i++) {
4565
+ var cls = yy[list[i][0]];
4566
+ if (cls && node instanceof cls) return list[i][1];
4567
+ }
4568
+ return null;
4569
+ }
4570
+
4571
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4572
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4573
+ function freeVar(node, key, entry) {
4574
+ var yy = alasql.yy;
4575
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4576
+ if (entry === 'source') return false;
4577
+ if (entry === 'into') return intoVarFree;
4578
+ return varValueFree;
4579
+ }
4580
+
4581
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4582
+ function freeSet(node, key) {
4583
+ var yy = alasql.yy;
4584
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4585
+ }
4586
+
4587
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4588
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4589
+ // names never reach generated code.
4590
+ function checkNames(node, data, entry) {
4591
+ if (node === null || typeof node !== 'object') return;
4592
+ var i;
4593
+ if (Array.isArray(node)) {
4594
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4595
+ return;
4596
+ }
4597
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4598
+ if (data && !isNode) {
4599
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4600
+ return;
4601
+ }
4602
+ var yy = alasql.yy;
4603
+ if (node instanceof yy.Json) {
4604
+ checkNames(node.value, true, false);
4605
+ return;
4606
+ }
4607
+ var fk = freeOf(freeKeys, node);
4608
+ var fc = freeOf(freeChild, node);
4609
+ var sc = freeOf(skipChild, node);
4610
+ for (var key in node) {
4611
+ if (!hasOwn.call(node, key)) continue;
4612
+ var value = node[key];
4613
+ if (sc && sc[key] === 1) continue;
4614
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4615
+ checkOrder(node.columns, value);
4616
+ continue;
4617
+ }
4618
+ // the column list of a WITH entry renames the result at run time
4619
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4620
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4621
+ if (typeof value === 'string') {
4622
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4623
+ continue;
4624
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4625
+ checkName(value);
4626
+ } else if (value !== null && typeof value === 'object') {
4627
+ if (Array.isArray(value)) {
4628
+ for (i = 0; i < value.length; i++) {
4629
+ if (typeof value[i] === 'string') {
4630
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4631
+ } else {
4632
+ checkNames(value[i], false, child || entryOf(node, key));
4633
+ }
4634
+ }
4635
+ } else {
4636
+ checkNames(value, false, child);
4637
+ }
4638
+ }
4639
+ }
4640
+ }
4641
+
4642
+ return function (ast) {
4643
+ checkNames(ast, false, false);
4644
+ };
4645
+ })();
4646
+
4455
4647
  /**
4456
4648
  List of engines of external databases
4457
4649
  @type {object}
@@ -6548,7 +6740,7 @@ function queryfn3(query) {
6548
6740
 
6549
6741
  // Prepare variables
6550
6742
  query.data = [];
6551
- query.xgroups = {};
6743
+ query.xgroups=Object.create(null);
6552
6744
  query.groups = [];
6553
6745
 
6554
6746
  // Level of Joins
@@ -8006,7 +8198,7 @@ yy.Select.prototype.compileFrom = function (query) {
8006
8198
  return res;
8007
8199
  };
8008
8200
  } else if (tq instanceof yy.ParamValue) {
8009
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8201
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
8010
8202
 
8011
8203
  if (tq.array) ps += ',true';
8012
8204
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8259,7 +8451,7 @@ yy.Select.prototype.compileJoins = function (query) {
8259
8451
  // source.data = ;
8260
8452
  var jnparam = jn.param.param;
8261
8453
 
8262
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8454
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8263
8455
  if (jn.array) ps += ',true';
8264
8456
  ps += ');if(cb)res=cb(res, idx, query);return res';
8265
8457
 
@@ -9123,7 +9315,7 @@ function compileSelectStar(query, aliases, joinstar) {
9123
9315
  } else {
9124
9316
 
9125
9317
  // if column not exist, then copy all
9126
- 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]};';
9318
+ 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]};';
9127
9319
 
9128
9320
  query.dirtyColumns = true;
9129
9321
  }
@@ -9149,7 +9341,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9149
9341
  if (col.columnid === '*') {
9150
9342
  if (col.func) {
9151
9343
  sp +=
9152
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9344
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9153
9345
  } else if (col.tableid) {
9154
9346
  //Copy all
9155
9347
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9201,7 +9393,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9201
9393
  if (isMultisheetParam) {
9202
9394
  sp =
9203
9395
  'var r={};var w=p["' +
9204
- tbid +
9396
+ escapeq(tbid) +
9205
9397
  '"];' +
9206
9398
  'var cols=[' +
9207
9399
  self.columns
@@ -9212,7 +9404,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9212
9404
  '];var colas=[' +
9213
9405
  self.columns
9214
9406
  .map(function (col) {
9215
- return "'" + (col.as || col.columnid) + "'";
9407
+ return "'" + escapeq(col.as || col.columnid) + "'";
9216
9408
  })
9217
9409
  .join(',') +
9218
9410
  '];' +
@@ -9376,9 +9568,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9376
9568
  this.orderColumns.forEach(function (v, idx) {
9377
9569
  var key = '$$$' + idx;
9378
9570
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9379
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9571
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9380
9572
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9381
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9573
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9382
9574
  } else {
9383
9575
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9384
9576
  }
@@ -9470,7 +9662,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9470
9662
  query.groupColumns[colas] = col.nick;
9471
9663
 
9472
9664
  // if(col.as) {
9473
- s += "r['" + colas + "']=";
9665
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9474
9666
  // // } else {
9475
9667
  // // s += 'r[\''+escapeq()+'\']=';
9476
9668
  // // };
@@ -9497,7 +9689,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9497
9689
  self.columns.forEach(function (col) {
9498
9690
 
9499
9691
  if (query.ingroup.indexOf(col.nick) > -1) {
9500
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9692
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9501
9693
  }
9502
9694
  });
9503
9695
 
@@ -9507,7 +9699,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9507
9699
  var key = '$$$' + idx;
9508
9700
 
9509
9701
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9510
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9702
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9511
9703
  } else {
9512
9704
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9513
9705
  }
@@ -10590,7 +10782,7 @@ yy.Over = class Over {
10590
10782
 
10591
10783
  if (this.op === '!') {
10592
10784
  if (typeof this.right === 'string') {
10593
- s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + this.right + '"]';
10785
+ s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + escapeq(this.right) + '"]';
10594
10786
  }
10595
10787
  // TODO - add other cases
10596
10788
  }
@@ -10942,7 +11134,7 @@ yy.Over = class Over {
10942
11134
 
10943
11135
  toJS() {
10944
11136
  if (typeof this.param === 'string') {
10945
- return "params['" + this.param + "']";
11137
+ return "params['" + escapeq(this.param) + "']";
10946
11138
  }
10947
11139
 
10948
11140
  return 'params[' + this.param + ']';
@@ -12010,9 +12202,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12010
12202
  return s;
12011
12203
  });
12012
12204
 
12205
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12206
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12207
+ // are escaped, so the string cannot end early.
12208
+ function escapeJSONString(s) {
12209
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12210
+ if (c.length === 2) return c;
12211
+ if (c === '\\') return '\\\\';
12212
+ if (c === '"') return '\\"';
12213
+ if (c === '\n') return '\\n';
12214
+ if (c === '\r') return '\\r';
12215
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12216
+ });
12217
+ }
12218
+
12013
12219
  function JSONtoJS(obj, context, tableid, defcols) {
12014
12220
  var s = '';
12015
- if (typeof obj == 'string') s = '"' + obj + '"';
12221
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12016
12222
  else if (typeof obj == 'number') s = '(' + obj + ')';
12017
12223
  else if (typeof obj == 'boolean') s = obj;
12018
12224
  else if (typeof obj == 'object') {
@@ -12031,7 +12237,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12031
12237
  var ss = [];
12032
12238
  for (var k in obj) {
12033
12239
  var s1 = '';
12034
- if (typeof k == 'string') s1 += '"' + k + '"';
12240
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
12035
12241
  else if (typeof k == 'number') s1 += k;
12036
12242
  else if (typeof k == 'boolean') s1 += k;
12037
12243
  else {
@@ -15337,19 +15543,19 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
15337
15543
  }
15338
15544
  if (this.props && this.props.length > 0) {
15339
15545
  if (this.method == '@') {
15340
- var fs = "alasql.vars['" + this.variable + "']";
15546
+ var fs = "alasql.vars[k('"+this.variable+"')]";
15341
15547
  } else {
15342
- var fs = "params['" + this.variable + "']";
15548
+ var fs = "params[k('"+this.variable+"')]";
15343
15549
  }
15344
15550
  fs += this.props
15345
15551
  .map(function (prop) {
15346
15552
  if (typeof prop == 'string') {
15347
- return "['" + prop + "']";
15553
+ return "[k('"+prop+"')]";
15348
15554
  } else if (typeof prop == 'number') {
15349
15555
  return '[' + prop + ']';
15350
15556
  } else {
15351
15557
 
15352
- return '[' + prop.toJS() + ']';
15558
+ return "[k("+prop.toJS()+")]";
15353
15559
  // } else {
15354
15560
 
15355
15561
  // throw new Error('Wrong SET property');
@@ -15357,7 +15563,7 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
15357
15563
  })
15358
15564
  .join();
15359
15565
 
15360
- new Function('value,params,alasql', 'var y;' + fs + '=value')(res, params, alasql);
15566
+ 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);
15361
15567
  } else {
15362
15568
  if (this.method == '@') {
15363
15569
  alasql.vars[this.variable] = res;