alasql 2.1.10 → 2.1.11

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 v2.1.10 | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v2.1.11 | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 2.1.10
4
+ @version 2.1.11
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2016 Andrey Gershun & Mathias Rangel 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 = '2.1.10';
145
+ alasql.version = '2.1.11';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -4480,9 +4480,201 @@ alasql.parser.parseError = function (str, hash) {
4480
4480
  }
4481
4481
  */
4482
4482
  alasql.parse = function (sql) {
4483
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4483
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4484
+ checkSqlNames(ast);
4485
+ return ast;
4484
4486
  };
4485
4487
 
4488
+ /*
4489
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4490
+ holds a quote, a backslash or a line break would end the string and make the rest code
4491
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4492
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4493
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4494
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4495
+ of a JSON value are data, not names.
4496
+ */
4497
+ var checkSqlNames = (function () {
4498
+ var hasOwn = Object.prototype.hasOwnProperty;
4499
+ var badName = /['\\\n\r\u2028\u2029]/;
4500
+ var nameKeys = {
4501
+ as: 1,
4502
+ columnid: 1,
4503
+ tableid: 1,
4504
+ databaseid: 1,
4505
+ viewid: 1,
4506
+ indexid: 1,
4507
+ trigger: 1,
4508
+ variable: 1,
4509
+ dropcolumn: 1,
4510
+ renamecolumn: 1,
4511
+ renameto: 1,
4512
+ to: 1,
4513
+ tocolumnid: 1,
4514
+ forcolumnid: 1,
4515
+ };
4516
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4517
+
4518
+ function checkName(name) {
4519
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4520
+ }
4521
+
4522
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4523
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4524
+ function checkOrder(columns, order) {
4525
+ var aliases = {};
4526
+ (columns || []).forEach(function (col) {
4527
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4528
+ });
4529
+ order.forEach(function (ord) {
4530
+ var e = ord && ord.expression;
4531
+ var name;
4532
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4533
+ if (typeof name === 'string') {
4534
+ if (!hasOwn.call(aliases, name)) checkName(name);
4535
+ return;
4536
+ }
4537
+ checkNames(ord, false, false);
4538
+ });
4539
+ }
4540
+
4541
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4542
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4543
+ // graph item, name is data.
4544
+ function entryOf(node, key) {
4545
+ var yy = alasql.yy;
4546
+ if (
4547
+ (node instanceof yy.Select && key === 'columns') ||
4548
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4549
+ (node.srchid === 'RETURN' && key === 'args')
4550
+ )
4551
+ return 'alias';
4552
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4553
+ return '';
4554
+ }
4555
+
4556
+ // Names that never reach generated code: they are used only at run time, as the key of a
4557
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4558
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4559
+ // names are free (what they hold is still walked), and skipChild are children that are not
4560
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4561
+ var varValueFree = false;
4562
+ var intoVarFree = false;
4563
+ var freeKeys = [
4564
+ ['CreateIndex', {indexid: 1}],
4565
+ ['DropIndex', {indexid: 1}],
4566
+ ['Reindex', {indexid: 1}],
4567
+ ['CreateTrigger', {trigger: 1}],
4568
+ ['DropTrigger', {trigger: 1}],
4569
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4570
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4571
+ ['DetachDatabase', {databaseid: 1}],
4572
+ ['DropDatabase', {databaseid: 1}],
4573
+ ['ShowTables', {databaseid: 1}],
4574
+ ['ShowColumns', {databaseid: 1}],
4575
+ ['ShowIndex', {databaseid: 1}],
4576
+ ['ShowCreateTable', {databaseid: 1}],
4577
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4578
+ ];
4579
+ var freeChild = [
4580
+ ['CreateTable', {table: 1}],
4581
+ ['DropTable', {tables: 1}],
4582
+ ['TruncateTable', {table: 1}],
4583
+ ['AlterTable', {table: 1}],
4584
+ ['ShowColumns', {table: 1}],
4585
+ ['ShowIndex', {table: 1}],
4586
+ ['ShowCreateTable', {table: 1}],
4587
+ ['CreateIndex', {table: 1}],
4588
+ ['CreateTrigger', {table: 1}],
4589
+ ['Declare', {declares: 1}],
4590
+ ];
4591
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4592
+
4593
+ function freeOf(list, node) {
4594
+ var yy = alasql.yy;
4595
+ for (var i = 0; i < list.length; i++) {
4596
+ var cls = yy[list[i][0]];
4597
+ if (cls && node instanceof cls) return list[i][1];
4598
+ }
4599
+ return null;
4600
+ }
4601
+
4602
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4603
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4604
+ function freeVar(node, key, entry) {
4605
+ var yy = alasql.yy;
4606
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4607
+ if (entry === 'source') return false;
4608
+ if (entry === 'into') return intoVarFree;
4609
+ return varValueFree;
4610
+ }
4611
+
4612
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4613
+ function freeSet(node, key) {
4614
+ var yy = alasql.yy;
4615
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4616
+ }
4617
+
4618
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4619
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4620
+ // names never reach generated code.
4621
+ function checkNames(node, data, entry) {
4622
+ if (node === null || typeof node !== 'object') return;
4623
+ var i;
4624
+ if (Array.isArray(node)) {
4625
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4626
+ return;
4627
+ }
4628
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4629
+ if (data && !isNode) {
4630
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4631
+ return;
4632
+ }
4633
+ var yy = alasql.yy;
4634
+ if (node instanceof yy.Json) {
4635
+ checkNames(node.value, true, false);
4636
+ return;
4637
+ }
4638
+ var fk = freeOf(freeKeys, node);
4639
+ var fc = freeOf(freeChild, node);
4640
+ var sc = freeOf(skipChild, node);
4641
+ for (var key in node) {
4642
+ if (!hasOwn.call(node, key)) continue;
4643
+ var value = node[key];
4644
+ if (sc && sc[key] === 1) continue;
4645
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4646
+ checkOrder(node.columns, value);
4647
+ continue;
4648
+ }
4649
+ // the column list of a WITH entry renames the result at run time
4650
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4651
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4652
+ if (typeof value === 'string') {
4653
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4654
+ continue;
4655
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4656
+ checkName(value);
4657
+ } else if (value !== null && typeof value === 'object') {
4658
+ if (Array.isArray(value)) {
4659
+ for (i = 0; i < value.length; i++) {
4660
+ if (typeof value[i] === 'string') {
4661
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4662
+ } else {
4663
+ checkNames(value[i], false, child || entryOf(node, key));
4664
+ }
4665
+ }
4666
+ } else {
4667
+ checkNames(value, false, child);
4668
+ }
4669
+ }
4670
+ }
4671
+ }
4672
+
4673
+ return function (ast) {
4674
+ checkNames(ast, false, false);
4675
+ };
4676
+ })();
4677
+
4486
4678
  /**
4487
4679
  List of engines of external databases
4488
4680
  @type {object}
@@ -7607,7 +7799,7 @@ yy.Select.prototype.compile = function (databaseid, params) {
7607
7799
  // Save data into parameters array
7608
7800
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
7609
7801
  //
7610
- query.intofns = "params['" + this.into.param + "'].push(r)";
7802
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
7611
7803
  }
7612
7804
 
7613
7805
  if (query.intofns) {
@@ -8012,7 +8204,7 @@ yy.Select.prototype.compileFrom = function (query) {
8012
8204
  return res;
8013
8205
  };
8014
8206
  } else if (tq instanceof yy.ParamValue) {
8015
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8207
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
8016
8208
 
8017
8209
  if (tq.array) ps += ',true';
8018
8210
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8261,7 +8453,7 @@ yy.Select.prototype.compileJoins = function (query) {
8261
8453
  // source.data = ;
8262
8454
  var jnparam = jn.param.param;
8263
8455
 
8264
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8456
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8265
8457
  if (jn.array) ps += ',true';
8266
8458
  ps += ');if(cb)res=cb(res, idx, query);return res';
8267
8459
 
@@ -8959,9 +9151,10 @@ function compileSelectStar(query, aliases, joinstar) {
8959
9151
 
8960
9152
  if (columns && columns.length > 0) {
8961
9153
  columns.forEach(function (tcol) {
9154
+ var escapedColumnId = escapeq(tcol.columnid);
8962
9155
  if (joinstar && alasql.options.joinstar == 'underscore') {
8963
9156
  ss.push(
8964
- "'" + alias + '_' + tcol.columnid + "':p['" + alias + "']['" + tcol.columnid + "']"
9157
+ "'" + alias + '_' + escapedColumnId + "':p['" + alias + "']['" + escapedColumnId + "']"
8965
9158
  );
8966
9159
  } else if (joinstar && alasql.options.joinstar == 'json') {
8967
9160
  // ss.push('\''+alias+'_'+tcol.columnid+'\':p[\''+alias+'\'][\''+tcol.columnid+'\']');
@@ -8969,16 +9162,16 @@ function compileSelectStar(query, aliases, joinstar) {
8969
9162
  "r['" +
8970
9163
  alias +
8971
9164
  "']['" +
8972
- tcol.columnid +
9165
+ escapedColumnId +
8973
9166
  "']=p['" +
8974
9167
  alias +
8975
9168
  "']['" +
8976
- tcol.columnid +
9169
+ escapedColumnId +
8977
9170
  "'];";
8978
9171
  } else {
8979
- var value = "p['" + alias + "']['" + tcol.columnid + "']";
9172
+ var value = "p['" + alias + "']['" + escapedColumnId + "']";
8980
9173
  if (!columnIds[tcol.columnid]) {
8981
- var key = "'" + tcol.columnid + "':";
9174
+ var key = "'" + escapedColumnId + "':";
8982
9175
  ss.push(key + value);
8983
9176
  columnIds[tcol.columnid] = {
8984
9177
  id: ss.length - 1,
@@ -9009,7 +9202,7 @@ function compileSelectStar(query, aliases, joinstar) {
9009
9202
  } else {
9010
9203
 
9011
9204
  // if column not exist, then copy all
9012
- 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]};';
9205
+ 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]};';
9013
9206
 
9014
9207
  query.dirtyColumns = true;
9015
9208
  }
@@ -9036,7 +9229,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9036
9229
  if (col.columnid === '*') {
9037
9230
  if (col.func) {
9038
9231
  sp +=
9039
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9232
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9040
9233
  } else if (col.tableid) {
9041
9234
  //Copy all
9042
9235
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9088,7 +9281,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9088
9281
  if (isMultisheetParam) {
9089
9282
  sp =
9090
9283
  'var r={};var w=p["' +
9091
- tbid +
9284
+ escapeq(tbid) +
9092
9285
  '"];' +
9093
9286
  'var cols=[' +
9094
9287
  self.columns
@@ -9099,7 +9292,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9099
9292
  '];var colas=[' +
9100
9293
  self.columns
9101
9294
  .map(function (col) {
9102
- return "'" + (col.as || col.columnid) + "'";
9295
+ return "'" + escapeq(col.as || col.columnid) + "'";
9103
9296
  })
9104
9297
  .join(',') +
9105
9298
  '];' +
@@ -9262,9 +9455,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9262
9455
  this.orderColumns.forEach(function (v, idx) {
9263
9456
  var key = '$$$' + idx;
9264
9457
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9265
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9458
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9266
9459
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9267
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9460
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9268
9461
  } else {
9269
9462
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9270
9463
  }
@@ -9357,7 +9550,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9357
9550
  query.groupColumns[colas] = col.nick;
9358
9551
 
9359
9552
  // if(col.as) {
9360
- s += "r['" + colas + "']=";
9553
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9361
9554
  // // } else {
9362
9555
  // // s += 'r[\''+escapeq()+'\']=';
9363
9556
  // // };
@@ -9384,7 +9577,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9384
9577
  self.columns.forEach(function (col) {
9385
9578
 
9386
9579
  if (query.ingroup.indexOf(col.nick) > -1) {
9387
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9580
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9388
9581
  }
9389
9582
  });
9390
9583
 
@@ -9394,7 +9587,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9394
9587
  var key = '$$$' + idx;
9395
9588
 
9396
9589
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9397
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9590
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9398
9591
  } else {
9399
9592
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9400
9593
  }
@@ -10471,7 +10664,7 @@ yy.Op.prototype.toJS = function (context, tableid, defcols) {
10471
10664
  var ljs = '(' + leftJS() + '||{})';
10472
10665
 
10473
10666
  if (typeof this.right === 'string') {
10474
- s = ljs + '["' + this.right + '"]';
10667
+ s = ljs + '["' + escapeq(this.right) + '"]';
10475
10668
  } else if (typeof this.right === 'number') {
10476
10669
  s = ljs + '[' + this.right + ']';
10477
10670
  } else if (this.right instanceof yy.FuncValue) {
@@ -10487,7 +10680,7 @@ yy.Op.prototype.toJS = function (context, tableid, defcols) {
10487
10680
 
10488
10681
  if (this.op === '!') {
10489
10682
  if (typeof this.right === 'string') {
10490
- s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + this.right + '"]';
10683
+ s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + escapeq(this.right) + '"]';
10491
10684
  }
10492
10685
  // TODO - add other cases
10493
10686
  }
@@ -10816,7 +11009,7 @@ yy.ParamValue.prototype.toString = function () {
10816
11009
  };
10817
11010
  yy.ParamValue.prototype.toJS = function () {
10818
11011
  if (typeof this.param === 'string') {
10819
- return "params['" + this.param + "']";
11012
+ return "params['" + escapeq(this.param) + "']";
10820
11013
  }
10821
11014
 
10822
11015
  return 'params[' + this.param + ']';
@@ -11844,9 +12037,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function (obj) {
11844
12037
  return s;
11845
12038
  });
11846
12039
 
12040
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12041
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12042
+ // are escaped, so the string cannot end early.
12043
+ function escapeJSONString(s) {
12044
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12045
+ if (c.length === 2) return c;
12046
+ if (c === '\\') return '\\\\';
12047
+ if (c === '"') return '\\"';
12048
+ if (c === '\n') return '\\n';
12049
+ if (c === '\r') return '\\r';
12050
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12051
+ });
12052
+ }
12053
+
11847
12054
  function JSONtoJS(obj, context, tableid, defcols) {
11848
12055
  var s = '';
11849
- if (typeof obj == 'string') s = '"' + obj + '"';
12056
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
11850
12057
  else if (typeof obj == 'number') s = '(' + obj + ')';
11851
12058
  else if (typeof obj == 'boolean') s = obj;
11852
12059
  else if (typeof obj == 'object') {
@@ -11865,7 +12072,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
11865
12072
  var ss = [];
11866
12073
  for (var k in obj) {
11867
12074
  var s1 = '';
11868
- if (typeof k == 'string') s1 += '"' + k + '"';
12075
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
11869
12076
  else if (typeof k == 'number') s1 += k;
11870
12077
  else if (typeof k == 'boolean') s1 += k;
11871
12078
  else {