alasql 0.5.11 → 0.5.13

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 v0.5.11 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
1
+ //! AlaSQL v0.5.13 | © 2014-2018 Andrey Gershun & Mathias Rangel Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 0.5.11
4
+ @version 0.5.13
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 = '0.5.11';
145
+ alasql.version = '0.5.13';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -2483,7 +2483,7 @@ next:function () {
2483
2483
 
2484
2484
  // return next match that has a token
2485
2485
  lex:function lex() {
2486
- var r = this.next();
2486
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2487
2487
  if (r) {
2488
2488
  return r;
2489
2489
  } else {
@@ -4422,9 +4422,201 @@ alasql.parser.parseError = function(str, hash) {
4422
4422
  }
4423
4423
  */
4424
4424
  alasql.parse = function(sql) {
4425
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4425
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4426
+ checkSqlNames(ast);
4427
+ return ast;
4426
4428
  };
4427
4429
 
4430
+ /*
4431
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4432
+ holds a quote, a backslash or a line break would end the string and make the rest code
4433
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4434
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4435
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4436
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4437
+ of a JSON value are data, not names.
4438
+ */
4439
+ var checkSqlNames = (function () {
4440
+ var hasOwn = Object.prototype.hasOwnProperty;
4441
+ var badName = /['\\\n\r\u2028\u2029]/;
4442
+ var nameKeys = {
4443
+ as: 1,
4444
+ columnid: 1,
4445
+ tableid: 1,
4446
+ databaseid: 1,
4447
+ viewid: 1,
4448
+ indexid: 1,
4449
+ trigger: 1,
4450
+ variable: 1,
4451
+ dropcolumn: 1,
4452
+ renamecolumn: 1,
4453
+ renameto: 1,
4454
+ to: 1,
4455
+ tocolumnid: 1,
4456
+ forcolumnid: 1,
4457
+ };
4458
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4459
+
4460
+ function checkName(name) {
4461
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4462
+ }
4463
+
4464
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4465
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4466
+ function checkOrder(columns, order) {
4467
+ var aliases = {};
4468
+ (columns || []).forEach(function (col) {
4469
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4470
+ });
4471
+ order.forEach(function (ord) {
4472
+ var e = ord && ord.expression;
4473
+ var name;
4474
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4475
+ if (typeof name === 'string') {
4476
+ if (!hasOwn.call(aliases, name)) checkName(name);
4477
+ return;
4478
+ }
4479
+ checkNames(ord, false, false);
4480
+ });
4481
+ }
4482
+
4483
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4484
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4485
+ // graph item, name is data.
4486
+ function entryOf(node, key) {
4487
+ var yy = alasql.yy;
4488
+ if (
4489
+ (node instanceof yy.Select && key === 'columns') ||
4490
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4491
+ (node.srchid === 'RETURN' && key === 'args')
4492
+ )
4493
+ return 'alias';
4494
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4495
+ return '';
4496
+ }
4497
+
4498
+ // Names that never reach generated code: they are used only at run time, as the key of a
4499
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4500
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4501
+ // names are free (what they hold is still walked), and skipChild are children that are not
4502
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4503
+ var varValueFree = false;
4504
+ var intoVarFree = false;
4505
+ var freeKeys = [
4506
+ ['CreateIndex', {indexid: 1}],
4507
+ ['DropIndex', {indexid: 1}],
4508
+ ['Reindex', {indexid: 1}],
4509
+ ['CreateTrigger', {trigger: 1}],
4510
+ ['DropTrigger', {trigger: 1}],
4511
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4512
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4513
+ ['DetachDatabase', {databaseid: 1}],
4514
+ ['DropDatabase', {databaseid: 1}],
4515
+ ['ShowTables', {databaseid: 1}],
4516
+ ['ShowColumns', {databaseid: 1}],
4517
+ ['ShowIndex', {databaseid: 1}],
4518
+ ['ShowCreateTable', {databaseid: 1}],
4519
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4520
+ ];
4521
+ var freeChild = [
4522
+ ['CreateTable', {table: 1}],
4523
+ ['DropTable', {tables: 1}],
4524
+ ['TruncateTable', {table: 1}],
4525
+ ['AlterTable', {table: 1}],
4526
+ ['ShowColumns', {table: 1}],
4527
+ ['ShowIndex', {table: 1}],
4528
+ ['ShowCreateTable', {table: 1}],
4529
+ ['CreateIndex', {table: 1}],
4530
+ ['CreateTrigger', {table: 1}],
4531
+ ['Declare', {declares: 1}],
4532
+ ];
4533
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4534
+
4535
+ function freeOf(list, node) {
4536
+ var yy = alasql.yy;
4537
+ for (var i = 0; i < list.length; i++) {
4538
+ var cls = yy[list[i][0]];
4539
+ if (cls && node instanceof cls) return list[i][1];
4540
+ }
4541
+ return null;
4542
+ }
4543
+
4544
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4545
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4546
+ function freeVar(node, key, entry) {
4547
+ var yy = alasql.yy;
4548
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4549
+ if (entry === 'source') return false;
4550
+ if (entry === 'into') return intoVarFree;
4551
+ return varValueFree;
4552
+ }
4553
+
4554
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4555
+ function freeSet(node, key) {
4556
+ var yy = alasql.yy;
4557
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4558
+ }
4559
+
4560
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4561
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4562
+ // names never reach generated code.
4563
+ function checkNames(node, data, entry) {
4564
+ if (node === null || typeof node !== 'object') return;
4565
+ var i;
4566
+ if (Array.isArray(node)) {
4567
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4568
+ return;
4569
+ }
4570
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4571
+ if (data && !isNode) {
4572
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4573
+ return;
4574
+ }
4575
+ var yy = alasql.yy;
4576
+ if (node instanceof yy.Json) {
4577
+ checkNames(node.value, true, false);
4578
+ return;
4579
+ }
4580
+ var fk = freeOf(freeKeys, node);
4581
+ var fc = freeOf(freeChild, node);
4582
+ var sc = freeOf(skipChild, node);
4583
+ for (var key in node) {
4584
+ if (!hasOwn.call(node, key)) continue;
4585
+ var value = node[key];
4586
+ if (sc && sc[key] === 1) continue;
4587
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4588
+ checkOrder(node.columns, value);
4589
+ continue;
4590
+ }
4591
+ // the column list of a WITH entry renames the result at run time
4592
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4593
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4594
+ if (typeof value === 'string') {
4595
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4596
+ continue;
4597
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4598
+ checkName(value);
4599
+ } else if (value !== null && typeof value === 'object') {
4600
+ if (Array.isArray(value)) {
4601
+ for (i = 0; i < value.length; i++) {
4602
+ if (typeof value[i] === 'string') {
4603
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4604
+ } else {
4605
+ checkNames(value[i], false, child || entryOf(node, key));
4606
+ }
4607
+ }
4608
+ } else {
4609
+ checkNames(value, false, child);
4610
+ }
4611
+ }
4612
+ }
4613
+ }
4614
+
4615
+ return function (ast) {
4616
+ checkNames(ast, false, false);
4617
+ };
4618
+ })();
4619
+
4428
4620
  /**
4429
4621
  List of engines of external databases
4430
4622
  @type {object}
@@ -6958,7 +7150,7 @@ function queryfn3(query) {
6958
7150
 
6959
7151
  // Prepare variables
6960
7152
  query.data = [];
6961
- query.xgroups = {};
7153
+ query.xgroups=Object.create(null);
6962
7154
  query.groups = [];
6963
7155
 
6964
7156
  // Level of Joins
@@ -8006,7 +8198,7 @@ yy.Select.prototype.compile = function(databaseid, params) {
8006
8198
  // Save data into parameters array
8007
8199
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
8008
8200
  //
8009
- query.intofns = "params['" + this.into.param + "'].push(r)";
8201
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
8010
8202
  }
8011
8203
 
8012
8204
  if (query.intofns) {
@@ -8413,7 +8605,7 @@ yy.Select.prototype.compileFrom = function(query) {
8413
8605
  return res;
8414
8606
  };
8415
8607
  } else if (tq instanceof yy.ParamValue) {
8416
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8608
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
8417
8609
 
8418
8610
  if (tq.array) ps += ',true';
8419
8611
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8668,7 +8860,7 @@ yy.Select.prototype.compileJoins = function(query) {
8668
8860
  // source.data = ;
8669
8861
  var jnparam = jn.param.param;
8670
8862
 
8671
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8863
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8672
8864
  if (jn.array) ps += ',true';
8673
8865
  ps += ');if(cb)res=cb(res, idx, query);return res';
8674
8866
 
@@ -9372,16 +9564,17 @@ function compileSelectStar(query, aliases, joinstar) {
9372
9564
 
9373
9565
  if (columns && columns.length > 0) {
9374
9566
  columns.forEach(function(tcol) {
9567
+ var escapedColumnId = escapeq(tcol.columnid);
9375
9568
  if (joinstar && alasql.options.joinstar == 'underscore') {
9376
9569
  ss.push(
9377
9570
  "'" +
9378
9571
  alias +
9379
9572
  '_' +
9380
- tcol.columnid +
9573
+ escapedColumnId +
9381
9574
  "':p['" +
9382
9575
  alias +
9383
9576
  "']['" +
9384
- tcol.columnid +
9577
+ escapedColumnId +
9385
9578
  "']"
9386
9579
  );
9387
9580
  } else if (joinstar && alasql.options.joinstar == 'json') {
@@ -9390,16 +9583,16 @@ function compileSelectStar(query, aliases, joinstar) {
9390
9583
  "r['" +
9391
9584
  alias +
9392
9585
  "']['" +
9393
- tcol.columnid +
9586
+ escapedColumnId +
9394
9587
  "']=p['" +
9395
9588
  alias +
9396
9589
  "']['" +
9397
- tcol.columnid +
9590
+ escapedColumnId +
9398
9591
  "'];";
9399
9592
  } else {
9400
- var value = "p['" + alias + "']['" + tcol.columnid + "']";
9593
+ var value = "p['" + alias + "']['" + escapedColumnId + "']";
9401
9594
  if (!columnIds[tcol.columnid]) {
9402
- var key = "'" + tcol.columnid + "':";
9595
+ var key = "'" + escapedColumnId + "':";
9403
9596
  ss.push(key + value);
9404
9597
  columnIds[tcol.columnid] = {
9405
9598
  id: ss.length - 1,
@@ -9434,7 +9627,7 @@ function compileSelectStar(query, aliases, joinstar) {
9434
9627
  } else {
9435
9628
 
9436
9629
  // if column not exist, then copy all
9437
- 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]};';
9630
+ 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]};';
9438
9631
 
9439
9632
  query.dirtyColumns = true;
9440
9633
  }
@@ -9462,7 +9655,7 @@ yy.Select.prototype.compileSelect1 = function(query, params) {
9462
9655
  if (col.func) {
9463
9656
  sp +=
9464
9657
  "r=params['" +
9465
- col.param +
9658
+ escapeq(col.param) +
9466
9659
  "'](p['" +
9467
9660
  query.sources[0].alias +
9468
9661
  "'],p,params,alasql);";
@@ -9523,7 +9716,7 @@ yy.Select.prototype.compileSelect1 = function(query, params) {
9523
9716
  if (isMultisheetParam) {
9524
9717
  sp =
9525
9718
  'var r={};var w=p["' +
9526
- tbid +
9719
+ escapeq(tbid) +
9527
9720
  '"];' +
9528
9721
  'var cols=[' +
9529
9722
  self.columns
@@ -9534,7 +9727,7 @@ yy.Select.prototype.compileSelect1 = function(query, params) {
9534
9727
  '];var colas=[' +
9535
9728
  self.columns
9536
9729
  .map(function(col) {
9537
- return "'" + (col.as || col.columnid) + "'";
9730
+ return "'" + escapeq(col.as || col.columnid) + "'";
9538
9731
  })
9539
9732
  .join(',') +
9540
9733
  '];' +
@@ -9699,9 +9892,9 @@ yy.Select.prototype.compileSelect2 = function(query, params) {
9699
9892
  this.orderColumns.forEach(function(v, idx) {
9700
9893
  var key = '$$$' + idx;
9701
9894
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9702
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9895
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9703
9896
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9704
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9897
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9705
9898
  } else {
9706
9899
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9707
9900
  }
@@ -9794,7 +9987,7 @@ yy.Select.prototype.compileSelectGroup1 = function(query) {
9794
9987
  query.groupColumns[colas] = col.nick;
9795
9988
 
9796
9989
  // if(col.as) {
9797
- s += "r['" + colas + "']=";
9990
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9798
9991
  // // } else {
9799
9992
  // // s += 'r[\''+escapeq()+'\']=';
9800
9993
  // // };
@@ -9821,7 +10014,7 @@ yy.Select.prototype.compileSelectGroup2 = function(query) {
9821
10014
  self.columns.forEach(function(col) {
9822
10015
 
9823
10016
  if (query.ingroup.indexOf(col.nick) > -1) {
9824
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
10017
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9825
10018
  }
9826
10019
  });
9827
10020
 
@@ -9831,7 +10024,7 @@ yy.Select.prototype.compileSelectGroup2 = function(query) {
9831
10024
  var key = '$$$' + idx;
9832
10025
 
9833
10026
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9834
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10027
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9835
10028
  } else {
9836
10029
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9837
10030
  }
@@ -10884,7 +11077,7 @@ yy.Op.prototype.toJS = function(context, tableid, defcols) {
10884
11077
  var ljs = '(' + leftJS() + '||{})';
10885
11078
 
10886
11079
  if (typeof this.right === 'string') {
10887
- s = ljs + '["' + this.right + '"]';
11080
+ s = ljs + '["' + escapeq(this.right) + '"]';
10888
11081
  } else if (typeof this.right === 'number') {
10889
11082
  s = ljs + '[' + this.right + ']';
10890
11083
  } else if (this.right instanceof yy.FuncValue) {
@@ -10905,7 +11098,7 @@ yy.Op.prototype.toJS = function(context, tableid, defcols) {
10905
11098
  'alasql.databases[alasql.useid].objects[' +
10906
11099
  leftJS() +
10907
11100
  ']["' +
10908
- this.right +
11101
+ escapeq(this.right) +
10909
11102
  '"]';
10910
11103
  }
10911
11104
  // TODO - add other cases
@@ -11247,7 +11440,7 @@ yy.ParamValue.prototype.toString = function() {
11247
11440
  };
11248
11441
  yy.ParamValue.prototype.toJS = function() {
11249
11442
  if (typeof this.param === 'string') {
11250
- return "params['" + this.param + "']";
11443
+ return "params['" + escapeq(this.param) + "']";
11251
11444
  }
11252
11445
 
11253
11446
  return 'params[' + this.param + ']';
@@ -12307,9 +12500,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function(obj) {
12307
12500
  return s;
12308
12501
  });
12309
12502
 
12503
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12504
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12505
+ // are escaped, so the string cannot end early.
12506
+ function escapeJSONString(s) {
12507
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12508
+ if (c.length === 2) return c;
12509
+ if (c === '\\') return '\\\\';
12510
+ if (c === '"') return '\\"';
12511
+ if (c === '\n') return '\\n';
12512
+ if (c === '\r') return '\\r';
12513
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12514
+ });
12515
+ }
12516
+
12310
12517
  function JSONtoJS(obj, context, tableid, defcols) {
12311
12518
  var s = '';
12312
- if (typeof obj == 'string') s = '"' + obj + '"';
12519
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12313
12520
  else if (typeof obj == 'number') s = '(' + obj + ')';
12314
12521
  else if (typeof obj == 'boolean') s = obj;
12315
12522
  else if (typeof obj == 'object') {
@@ -12328,7 +12535,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12328
12535
  var ss = [];
12329
12536
  for (var k in obj) {
12330
12537
  var s1 = '';
12331
- if (typeof k == 'string') s1 += '"' + k + '"';
12538
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
12332
12539
  else if (typeof k == 'number') s1 += k;
12333
12540
  else if (typeof k == 'boolean') s1 += k;
12334
12541
  else {
@@ -15741,19 +15948,19 @@ yy.SetVariable.prototype.execute = function(databaseid, params, cb) {
15741
15948
  }
15742
15949
  if (this.props && this.props.length > 0) {
15743
15950
  if (this.method == '@') {
15744
- var fs = "alasql.vars['" + this.variable + "']";
15951
+ var fs = "alasql.vars[k('"+this.variable+"')]";
15745
15952
  } else {
15746
- var fs = "params['" + this.variable + "']";
15953
+ var fs = "params[k('"+this.variable+"')]";
15747
15954
  }
15748
15955
  fs += this.props
15749
15956
  .map(function(prop) {
15750
15957
  if (typeof prop == 'string') {
15751
- return "['" + prop + "']";
15958
+ return "[k('"+prop+"')]";
15752
15959
  } else if (typeof prop == 'number') {
15753
15960
  return '[' + prop + ']';
15754
15961
  } else {
15755
15962
 
15756
- return '[' + prop.toJS() + ']';
15963
+ return "[k("+prop.toJS()+")]";
15757
15964
  // } else {
15758
15965
 
15759
15966
  // throw new Error('Wrong SET property');
@@ -15761,7 +15968,7 @@ yy.SetVariable.prototype.execute = function(databaseid, params, cb) {
15761
15968
  })
15762
15969
  .join();
15763
15970
 
15764
- new Function('value,params,alasql', 'var y;' + fs + '=value')(res, params, alasql);
15971
+ 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);
15765
15972
  } else {
15766
15973
  if (this.method == '@') {
15767
15974
  alasql.vars[this.variable] = res;