alasql 1.7.7 → 1.7.8

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 v1.7.7 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
1
+ //! AlaSQL v1.7.8 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 1.7.7
4
+ @version 1.7.8
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 = '1.7.7';
145
+ alasql.version = '1.7.8';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -4446,9 +4446,201 @@ alasql.parser.parseError = function (str, hash) {
4446
4446
  }
4447
4447
  */
4448
4448
  alasql.parse = function (sql) {
4449
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4449
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4450
+ checkSqlNames(ast);
4451
+ return ast;
4450
4452
  };
4451
4453
 
4454
+ /*
4455
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4456
+ holds a quote, a backslash or a line break would end the string and make the rest code
4457
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4458
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4459
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4460
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4461
+ of a JSON value are data, not names.
4462
+ */
4463
+ var checkSqlNames = (function () {
4464
+ var hasOwn = Object.prototype.hasOwnProperty;
4465
+ var badName = /['\\\n\r\u2028\u2029]/;
4466
+ var nameKeys = {
4467
+ as: 1,
4468
+ columnid: 1,
4469
+ tableid: 1,
4470
+ databaseid: 1,
4471
+ viewid: 1,
4472
+ indexid: 1,
4473
+ trigger: 1,
4474
+ variable: 1,
4475
+ dropcolumn: 1,
4476
+ renamecolumn: 1,
4477
+ renameto: 1,
4478
+ to: 1,
4479
+ tocolumnid: 1,
4480
+ forcolumnid: 1,
4481
+ };
4482
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4483
+
4484
+ function checkName(name) {
4485
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4486
+ }
4487
+
4488
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4489
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4490
+ function checkOrder(columns, order) {
4491
+ var aliases = {};
4492
+ (columns || []).forEach(function (col) {
4493
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4494
+ });
4495
+ order.forEach(function (ord) {
4496
+ var e = ord && ord.expression;
4497
+ var name;
4498
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4499
+ if (typeof name === 'string') {
4500
+ if (!hasOwn.call(aliases, name)) checkName(name);
4501
+ return;
4502
+ }
4503
+ checkNames(ord, false, false);
4504
+ });
4505
+ }
4506
+
4507
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4508
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4509
+ // graph item, name is data.
4510
+ function entryOf(node, key) {
4511
+ var yy = alasql.yy;
4512
+ if (
4513
+ (node instanceof yy.Select && key === 'columns') ||
4514
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4515
+ (node.srchid === 'RETURN' && key === 'args')
4516
+ )
4517
+ return 'alias';
4518
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4519
+ return '';
4520
+ }
4521
+
4522
+ // Names that never reach generated code: they are used only at run time, as the key of a
4523
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4524
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4525
+ // names are free (what they hold is still walked), and skipChild are children that are not
4526
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4527
+ var varValueFree = true;
4528
+ var intoVarFree = false;
4529
+ var freeKeys = [
4530
+ ['CreateIndex', {indexid: 1}],
4531
+ ['DropIndex', {indexid: 1}],
4532
+ ['Reindex', {indexid: 1}],
4533
+ ['CreateTrigger', {trigger: 1}],
4534
+ ['DropTrigger', {trigger: 1}],
4535
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4536
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4537
+ ['DetachDatabase', {databaseid: 1}],
4538
+ ['DropDatabase', {databaseid: 1}],
4539
+ ['ShowTables', {databaseid: 1}],
4540
+ ['ShowColumns', {databaseid: 1}],
4541
+ ['ShowIndex', {databaseid: 1}],
4542
+ ['ShowCreateTable', {databaseid: 1}],
4543
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4544
+ ];
4545
+ var freeChild = [
4546
+ ['CreateTable', {table: 1}],
4547
+ ['DropTable', {tables: 1}],
4548
+ ['TruncateTable', {table: 1}],
4549
+ ['AlterTable', {table: 1}],
4550
+ ['ShowColumns', {table: 1}],
4551
+ ['ShowIndex', {table: 1}],
4552
+ ['ShowCreateTable', {table: 1}],
4553
+ ['CreateIndex', {table: 1}],
4554
+ ['CreateTrigger', {table: 1}],
4555
+ ['Declare', {declares: 1}],
4556
+ ];
4557
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4558
+
4559
+ function freeOf(list, node) {
4560
+ var yy = alasql.yy;
4561
+ for (var i = 0; i < list.length; i++) {
4562
+ var cls = yy[list[i][0]];
4563
+ if (cls && node instanceof cls) return list[i][1];
4564
+ }
4565
+ return null;
4566
+ }
4567
+
4568
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4569
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4570
+ function freeVar(node, key, entry) {
4571
+ var yy = alasql.yy;
4572
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4573
+ if (entry === 'source') return false;
4574
+ if (entry === 'into') return intoVarFree;
4575
+ return varValueFree;
4576
+ }
4577
+
4578
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4579
+ function freeSet(node, key) {
4580
+ var yy = alasql.yy;
4581
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4582
+ }
4583
+
4584
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4585
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4586
+ // names never reach generated code.
4587
+ function checkNames(node, data, entry) {
4588
+ if (node === null || typeof node !== 'object') return;
4589
+ var i;
4590
+ if (Array.isArray(node)) {
4591
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4592
+ return;
4593
+ }
4594
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4595
+ if (data && !isNode) {
4596
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4597
+ return;
4598
+ }
4599
+ var yy = alasql.yy;
4600
+ if (node instanceof yy.Json) {
4601
+ checkNames(node.value, true, false);
4602
+ return;
4603
+ }
4604
+ var fk = freeOf(freeKeys, node);
4605
+ var fc = freeOf(freeChild, node);
4606
+ var sc = freeOf(skipChild, node);
4607
+ for (var key in node) {
4608
+ if (!hasOwn.call(node, key)) continue;
4609
+ var value = node[key];
4610
+ if (sc && sc[key] === 1) continue;
4611
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4612
+ checkOrder(node.columns, value);
4613
+ continue;
4614
+ }
4615
+ // the column list of a WITH entry renames the result at run time
4616
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4617
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4618
+ if (typeof value === 'string') {
4619
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4620
+ continue;
4621
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4622
+ checkName(value);
4623
+ } else if (value !== null && typeof value === 'object') {
4624
+ if (Array.isArray(value)) {
4625
+ for (i = 0; i < value.length; i++) {
4626
+ if (typeof value[i] === 'string') {
4627
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4628
+ } else {
4629
+ checkNames(value[i], false, child || entryOf(node, key));
4630
+ }
4631
+ }
4632
+ } else {
4633
+ checkNames(value, false, child);
4634
+ }
4635
+ }
4636
+ }
4637
+ }
4638
+
4639
+ return function (ast) {
4640
+ checkNames(ast, false, false);
4641
+ };
4642
+ })();
4643
+
4452
4644
  /**
4453
4645
  List of engines of external databases
4454
4646
  @type {object}
@@ -7991,7 +8183,7 @@ yy.Select.prototype.compile = function (databaseid, params) {
7991
8183
  // Save data into parameters array
7992
8184
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
7993
8185
  //
7994
- query.intofns = "params['" + this.into.param + "'].push(r)";
8186
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
7995
8187
  }
7996
8188
 
7997
8189
  if (query.intofns) {
@@ -8396,7 +8588,7 @@ yy.Select.prototype.compileFrom = function (query) {
8396
8588
  return res;
8397
8589
  };
8398
8590
  } else if (tq instanceof yy.ParamValue) {
8399
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8591
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
8400
8592
 
8401
8593
  if (tq.array) ps += ',true';
8402
8594
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8645,7 +8837,7 @@ yy.Select.prototype.compileJoins = function (query) {
8645
8837
  // source.data = ;
8646
8838
  var jnparam = jn.param.param;
8647
8839
 
8648
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8840
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8649
8841
  if (jn.array) ps += ',true';
8650
8842
  ps += ');if(cb)res=cb(res, idx, query);return res';
8651
8843
 
@@ -9395,7 +9587,7 @@ function compileSelectStar(query, aliases, joinstar) {
9395
9587
  } else {
9396
9588
 
9397
9589
  // if column not exist, then copy all
9398
- 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]};';
9590
+ 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]};';
9399
9591
 
9400
9592
  query.dirtyColumns = true;
9401
9593
  }
@@ -9422,7 +9614,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9422
9614
  if (col.columnid === '*') {
9423
9615
  if (col.func) {
9424
9616
  sp +=
9425
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9617
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9426
9618
  } else if (col.tableid) {
9427
9619
  //Copy all
9428
9620
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9474,7 +9666,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9474
9666
  if (isMultisheetParam) {
9475
9667
  sp =
9476
9668
  'var r={};var w=p["' +
9477
- tbid +
9669
+ escapeq(tbid) +
9478
9670
  '"];' +
9479
9671
  'var cols=[' +
9480
9672
  self.columns
@@ -9485,7 +9677,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9485
9677
  '];var colas=[' +
9486
9678
  self.columns
9487
9679
  .map(function (col) {
9488
- return "'" + (col.as || col.columnid) + "'";
9680
+ return "'" + escapeq(col.as || col.columnid) + "'";
9489
9681
  })
9490
9682
  .join(',') +
9491
9683
  '];' +
@@ -9648,9 +9840,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9648
9840
  this.orderColumns.forEach(function (v, idx) {
9649
9841
  var key = '$$$' + idx;
9650
9842
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9651
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9843
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9652
9844
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9653
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9845
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9654
9846
  } else {
9655
9847
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9656
9848
  }
@@ -9743,7 +9935,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9743
9935
  query.groupColumns[colas] = col.nick;
9744
9936
 
9745
9937
  // if(col.as) {
9746
- s += "r['" + colas + "']=";
9938
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9747
9939
  // // } else {
9748
9940
  // // s += 'r[\''+escapeq()+'\']=';
9749
9941
  // // };
@@ -9770,7 +9962,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9770
9962
  self.columns.forEach(function (col) {
9771
9963
 
9772
9964
  if (query.ingroup.indexOf(col.nick) > -1) {
9773
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9965
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9774
9966
  }
9775
9967
  });
9776
9968
 
@@ -9780,7 +9972,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9780
9972
  var key = '$$$' + idx;
9781
9973
 
9782
9974
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9783
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9975
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9784
9976
  } else {
9785
9977
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9786
9978
  }
@@ -10865,7 +11057,7 @@ yy.Op.prototype.toJS = function (context, tableid, defcols) {
10865
11057
 
10866
11058
  if (this.op === '!') {
10867
11059
  if (typeof this.right === 'string') {
10868
- s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + this.right + '"]';
11060
+ s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + escapeq(this.right) + '"]';
10869
11061
  }
10870
11062
  // TODO - add other cases
10871
11063
  }
@@ -11191,7 +11383,7 @@ yy.ParamValue.prototype.toString = function () {
11191
11383
  };
11192
11384
  yy.ParamValue.prototype.toJS = function () {
11193
11385
  if (typeof this.param === 'string') {
11194
- return "params['" + this.param + "']";
11386
+ return "params['" + escapeq(this.param) + "']";
11195
11387
  }
11196
11388
 
11197
11389
  return 'params[' + this.param + ']';
@@ -12260,9 +12452,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12260
12452
  return s;
12261
12453
  });
12262
12454
 
12455
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12456
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12457
+ // are escaped, so the string cannot end early.
12458
+ function escapeJSONString(s) {
12459
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12460
+ if (c.length === 2) return c;
12461
+ if (c === '\\') return '\\\\';
12462
+ if (c === '"') return '\\"';
12463
+ if (c === '\n') return '\\n';
12464
+ if (c === '\r') return '\\r';
12465
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12466
+ });
12467
+ }
12468
+
12263
12469
  function JSONtoJS(obj, context, tableid, defcols) {
12264
12470
  var s = '';
12265
- if (typeof obj == 'string') s = '"' + obj + '"';
12471
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12266
12472
  else if (typeof obj == 'number') s = '(' + obj + ')';
12267
12473
  else if (typeof obj == 'boolean') s = obj;
12268
12474
  else if (typeof obj == 'object') {
@@ -12281,7 +12487,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12281
12487
  var ss = [];
12282
12488
  for (var k in obj) {
12283
12489
  var s1 = '';
12284
- if (typeof k == 'string') s1 += '"' + k + '"';
12490
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
12285
12491
  else if (typeof k == 'number') s1 += k;
12286
12492
  else if (typeof k == 'boolean') s1 += k;
12287
12493
  else {