alasql 2.0.9 → 2.0.10

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.0.9 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
1
+ //! AlaSQL v2.0.10 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 2.0.9
4
+ @version 2.0.10
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.0.9';
145
+ alasql.version = '2.0.10';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -4445,9 +4445,201 @@ alasql.parser.parseError = function (str, hash) {
4445
4445
  }
4446
4446
  */
4447
4447
  alasql.parse = function (sql) {
4448
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4448
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4449
+ checkSqlNames(ast);
4450
+ return ast;
4449
4451
  };
4450
4452
 
4453
+ /*
4454
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4455
+ holds a quote, a backslash or a line break would end the string and make the rest code
4456
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4457
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4458
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4459
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4460
+ of a JSON value are data, not names.
4461
+ */
4462
+ var checkSqlNames = (function () {
4463
+ var hasOwn = Object.prototype.hasOwnProperty;
4464
+ var badName = /['\\\n\r\u2028\u2029]/;
4465
+ var nameKeys = {
4466
+ as: 1,
4467
+ columnid: 1,
4468
+ tableid: 1,
4469
+ databaseid: 1,
4470
+ viewid: 1,
4471
+ indexid: 1,
4472
+ trigger: 1,
4473
+ variable: 1,
4474
+ dropcolumn: 1,
4475
+ renamecolumn: 1,
4476
+ renameto: 1,
4477
+ to: 1,
4478
+ tocolumnid: 1,
4479
+ forcolumnid: 1,
4480
+ };
4481
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4482
+
4483
+ function checkName(name) {
4484
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4485
+ }
4486
+
4487
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4488
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4489
+ function checkOrder(columns, order) {
4490
+ var aliases = {};
4491
+ (columns || []).forEach(function (col) {
4492
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4493
+ });
4494
+ order.forEach(function (ord) {
4495
+ var e = ord && ord.expression;
4496
+ var name;
4497
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4498
+ if (typeof name === 'string') {
4499
+ if (!hasOwn.call(aliases, name)) checkName(name);
4500
+ return;
4501
+ }
4502
+ checkNames(ord, false, false);
4503
+ });
4504
+ }
4505
+
4506
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4507
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4508
+ // graph item, name is data.
4509
+ function entryOf(node, key) {
4510
+ var yy = alasql.yy;
4511
+ if (
4512
+ (node instanceof yy.Select && key === 'columns') ||
4513
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4514
+ (node.srchid === 'RETURN' && key === 'args')
4515
+ )
4516
+ return 'alias';
4517
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4518
+ return '';
4519
+ }
4520
+
4521
+ // Names that never reach generated code: they are used only at run time, as the key of a
4522
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4523
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4524
+ // names are free (what they hold is still walked), and skipChild are children that are not
4525
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4526
+ var varValueFree = false;
4527
+ var intoVarFree = false;
4528
+ var freeKeys = [
4529
+ ['CreateIndex', {indexid: 1}],
4530
+ ['DropIndex', {indexid: 1}],
4531
+ ['Reindex', {indexid: 1}],
4532
+ ['CreateTrigger', {trigger: 1}],
4533
+ ['DropTrigger', {trigger: 1}],
4534
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4535
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4536
+ ['DetachDatabase', {databaseid: 1}],
4537
+ ['DropDatabase', {databaseid: 1}],
4538
+ ['ShowTables', {databaseid: 1}],
4539
+ ['ShowColumns', {databaseid: 1}],
4540
+ ['ShowIndex', {databaseid: 1}],
4541
+ ['ShowCreateTable', {databaseid: 1}],
4542
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4543
+ ];
4544
+ var freeChild = [
4545
+ ['CreateTable', {table: 1}],
4546
+ ['DropTable', {tables: 1}],
4547
+ ['TruncateTable', {table: 1}],
4548
+ ['AlterTable', {table: 1}],
4549
+ ['ShowColumns', {table: 1}],
4550
+ ['ShowIndex', {table: 1}],
4551
+ ['ShowCreateTable', {table: 1}],
4552
+ ['CreateIndex', {table: 1}],
4553
+ ['CreateTrigger', {table: 1}],
4554
+ ['Declare', {declares: 1}],
4555
+ ];
4556
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4557
+
4558
+ function freeOf(list, node) {
4559
+ var yy = alasql.yy;
4560
+ for (var i = 0; i < list.length; i++) {
4561
+ var cls = yy[list[i][0]];
4562
+ if (cls && node instanceof cls) return list[i][1];
4563
+ }
4564
+ return null;
4565
+ }
4566
+
4567
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4568
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4569
+ function freeVar(node, key, entry) {
4570
+ var yy = alasql.yy;
4571
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4572
+ if (entry === 'source') return false;
4573
+ if (entry === 'into') return intoVarFree;
4574
+ return varValueFree;
4575
+ }
4576
+
4577
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4578
+ function freeSet(node, key) {
4579
+ var yy = alasql.yy;
4580
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4581
+ }
4582
+
4583
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4584
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4585
+ // names never reach generated code.
4586
+ function checkNames(node, data, entry) {
4587
+ if (node === null || typeof node !== 'object') return;
4588
+ var i;
4589
+ if (Array.isArray(node)) {
4590
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4591
+ return;
4592
+ }
4593
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4594
+ if (data && !isNode) {
4595
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4596
+ return;
4597
+ }
4598
+ var yy = alasql.yy;
4599
+ if (node instanceof yy.Json) {
4600
+ checkNames(node.value, true, false);
4601
+ return;
4602
+ }
4603
+ var fk = freeOf(freeKeys, node);
4604
+ var fc = freeOf(freeChild, node);
4605
+ var sc = freeOf(skipChild, node);
4606
+ for (var key in node) {
4607
+ if (!hasOwn.call(node, key)) continue;
4608
+ var value = node[key];
4609
+ if (sc && sc[key] === 1) continue;
4610
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4611
+ checkOrder(node.columns, value);
4612
+ continue;
4613
+ }
4614
+ // the column list of a WITH entry renames the result at run time
4615
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4616
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4617
+ if (typeof value === 'string') {
4618
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4619
+ continue;
4620
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4621
+ checkName(value);
4622
+ } else if (value !== null && typeof value === 'object') {
4623
+ if (Array.isArray(value)) {
4624
+ for (i = 0; i < value.length; i++) {
4625
+ if (typeof value[i] === 'string') {
4626
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4627
+ } else {
4628
+ checkNames(value[i], false, child || entryOf(node, key));
4629
+ }
4630
+ }
4631
+ } else {
4632
+ checkNames(value, false, child);
4633
+ }
4634
+ }
4635
+ }
4636
+ }
4637
+
4638
+ return function (ast) {
4639
+ checkNames(ast, false, false);
4640
+ };
4641
+ })();
4642
+
4451
4643
  /**
4452
4644
  List of engines of external databases
4453
4645
  @type {object}
@@ -7983,7 +8175,7 @@ yy.Select.prototype.compile = function (databaseid, params) {
7983
8175
  // Save data into parameters array
7984
8176
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
7985
8177
  //
7986
- query.intofns = "params['" + this.into.param + "'].push(r)";
8178
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
7987
8179
  }
7988
8180
 
7989
8181
  if (query.intofns) {
@@ -8388,7 +8580,7 @@ yy.Select.prototype.compileFrom = function (query) {
8388
8580
  return res;
8389
8581
  };
8390
8582
  } else if (tq instanceof yy.ParamValue) {
8391
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8583
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
8392
8584
 
8393
8585
  if (tq.array) ps += ',true';
8394
8586
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8637,7 +8829,7 @@ yy.Select.prototype.compileJoins = function (query) {
8637
8829
  // source.data = ;
8638
8830
  var jnparam = jn.param.param;
8639
8831
 
8640
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8832
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8641
8833
  if (jn.array) ps += ',true';
8642
8834
  ps += ');if(cb)res=cb(res, idx, query);return res';
8643
8835
 
@@ -9336,9 +9528,10 @@ function compileSelectStar(query, aliases, joinstar) {
9336
9528
 
9337
9529
  if (columns && columns.length > 0) {
9338
9530
  columns.forEach(function (tcol) {
9531
+ var escapedColumnId = escapeq(tcol.columnid);
9339
9532
  if (joinstar && alasql.options.joinstar == 'underscore') {
9340
9533
  ss.push(
9341
- "'" + alias + '_' + tcol.columnid + "':p['" + alias + "']['" + tcol.columnid + "']"
9534
+ "'" + alias + '_' + escapedColumnId + "':p['" + alias + "']['" + escapedColumnId + "']"
9342
9535
  );
9343
9536
  } else if (joinstar && alasql.options.joinstar == 'json') {
9344
9537
  // ss.push('\''+alias+'_'+tcol.columnid+'\':p[\''+alias+'\'][\''+tcol.columnid+'\']');
@@ -9346,16 +9539,16 @@ function compileSelectStar(query, aliases, joinstar) {
9346
9539
  "r['" +
9347
9540
  alias +
9348
9541
  "']['" +
9349
- tcol.columnid +
9542
+ escapedColumnId +
9350
9543
  "']=p['" +
9351
9544
  alias +
9352
9545
  "']['" +
9353
- tcol.columnid +
9546
+ escapedColumnId +
9354
9547
  "'];";
9355
9548
  } else {
9356
- var value = "p['" + alias + "']['" + tcol.columnid + "']";
9549
+ var value = "p['" + alias + "']['" + escapedColumnId + "']";
9357
9550
  if (!columnIds[tcol.columnid]) {
9358
- var key = "'" + tcol.columnid + "':";
9551
+ var key = "'" + escapedColumnId + "':";
9359
9552
  ss.push(key + value);
9360
9553
  columnIds[tcol.columnid] = {
9361
9554
  id: ss.length - 1,
@@ -9386,7 +9579,7 @@ function compileSelectStar(query, aliases, joinstar) {
9386
9579
  } else {
9387
9580
 
9388
9581
  // if column not exist, then copy all
9389
- 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]};';
9582
+ 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]};';
9390
9583
 
9391
9584
  query.dirtyColumns = true;
9392
9585
  }
@@ -9413,7 +9606,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9413
9606
  if (col.columnid === '*') {
9414
9607
  if (col.func) {
9415
9608
  sp +=
9416
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9609
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9417
9610
  } else if (col.tableid) {
9418
9611
  //Copy all
9419
9612
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9465,7 +9658,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9465
9658
  if (isMultisheetParam) {
9466
9659
  sp =
9467
9660
  'var r={};var w=p["' +
9468
- tbid +
9661
+ escapeq(tbid) +
9469
9662
  '"];' +
9470
9663
  'var cols=[' +
9471
9664
  self.columns
@@ -9476,7 +9669,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9476
9669
  '];var colas=[' +
9477
9670
  self.columns
9478
9671
  .map(function (col) {
9479
- return "'" + (col.as || col.columnid) + "'";
9672
+ return "'" + escapeq(col.as || col.columnid) + "'";
9480
9673
  })
9481
9674
  .join(',') +
9482
9675
  '];' +
@@ -9639,9 +9832,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9639
9832
  this.orderColumns.forEach(function (v, idx) {
9640
9833
  var key = '$$$' + idx;
9641
9834
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9642
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9835
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9643
9836
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9644
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9837
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9645
9838
  } else {
9646
9839
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9647
9840
  }
@@ -9734,7 +9927,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9734
9927
  query.groupColumns[colas] = col.nick;
9735
9928
 
9736
9929
  // if(col.as) {
9737
- s += "r['" + colas + "']=";
9930
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9738
9931
  // // } else {
9739
9932
  // // s += 'r[\''+escapeq()+'\']=';
9740
9933
  // // };
@@ -9761,7 +9954,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9761
9954
  self.columns.forEach(function (col) {
9762
9955
 
9763
9956
  if (query.ingroup.indexOf(col.nick) > -1) {
9764
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9957
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9765
9958
  }
9766
9959
  });
9767
9960
 
@@ -9771,7 +9964,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9771
9964
  var key = '$$$' + idx;
9772
9965
 
9773
9966
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9774
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9967
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9775
9968
  } else {
9776
9969
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9777
9970
  }
@@ -10839,7 +11032,7 @@ yy.Op.prototype.toJS = function (context, tableid, defcols) {
10839
11032
  var ljs = '(' + leftJS() + '||{})';
10840
11033
 
10841
11034
  if (typeof this.right === 'string') {
10842
- s = ljs + '["' + this.right + '"]';
11035
+ s = ljs + '["' + escapeq(this.right) + '"]';
10843
11036
  } else if (typeof this.right === 'number') {
10844
11037
  s = ljs + '[' + this.right + ']';
10845
11038
  } else if (this.right instanceof yy.FuncValue) {
@@ -10855,7 +11048,7 @@ yy.Op.prototype.toJS = function (context, tableid, defcols) {
10855
11048
 
10856
11049
  if (this.op === '!') {
10857
11050
  if (typeof this.right === 'string') {
10858
- s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + this.right + '"]';
11051
+ s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + escapeq(this.right) + '"]';
10859
11052
  }
10860
11053
  // TODO - add other cases
10861
11054
  }
@@ -11181,7 +11374,7 @@ yy.ParamValue.prototype.toString = function () {
11181
11374
  };
11182
11375
  yy.ParamValue.prototype.toJS = function () {
11183
11376
  if (typeof this.param === 'string') {
11184
- return "params['" + this.param + "']";
11377
+ return "params['" + escapeq(this.param) + "']";
11185
11378
  }
11186
11379
 
11187
11380
  return 'params[' + this.param + ']';
@@ -12243,9 +12436,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12243
12436
  return s;
12244
12437
  });
12245
12438
 
12439
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12440
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12441
+ // are escaped, so the string cannot end early.
12442
+ function escapeJSONString(s) {
12443
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12444
+ if (c.length === 2) return c;
12445
+ if (c === '\\') return '\\\\';
12446
+ if (c === '"') return '\\"';
12447
+ if (c === '\n') return '\\n';
12448
+ if (c === '\r') return '\\r';
12449
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12450
+ });
12451
+ }
12452
+
12246
12453
  function JSONtoJS(obj, context, tableid, defcols) {
12247
12454
  var s = '';
12248
- if (typeof obj == 'string') s = '"' + obj + '"';
12455
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12249
12456
  else if (typeof obj == 'number') s = '(' + obj + ')';
12250
12457
  else if (typeof obj == 'boolean') s = obj;
12251
12458
  else if (typeof obj == 'object') {
@@ -12264,7 +12471,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12264
12471
  var ss = [];
12265
12472
  for (var k in obj) {
12266
12473
  var s1 = '';
12267
- if (typeof k == 'string') s1 += '"' + k + '"';
12474
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
12268
12475
  else if (typeof k == 'number') s1 += k;
12269
12476
  else if (typeof k == 'boolean') s1 += k;
12270
12477
  else {