alasql 1.7.6 → 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.
@@ -1,7 +1,7 @@
1
- //! AlaSQL v1.7.6 | © 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.6
4
+ @version 1.7.8
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2016 Andrey Gershun & Mathias Rangel Wulff
@@ -1,2 +1,2 @@
1
- //! AlaSQL v1.7.6 | © 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
  !function(r,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():r.alasql=e()}(this,function(){function i(r,e,o){"function"==typeof(e=e||[])&&(scope=o,o=e,e=[]),"object"!=typeof e&&(e=[e]);var t=i.lastid++;i.buffer[t]=o,i.webworker.postMessage({id:t,sql:r,params:e})}var a;if(i.options={},i.options.progress=function(){},isArray=function(r){return"[object Array]"===Object.prototype.toString.call(r)},i.promise=function(){throw new Error("Please include a Promise/A+ library")},"undefined"!=typeof Promise&&(a=function(r,e,n,s){return new Promise(function(o,t){i(r,e,function(r,e){e?t(e):(n&&s&&!1!==i.options.progress&&i.options.progress(n,s),o(r))})})},i.promise=function(r,e){if("undefined"==typeof Promise)throw new Error("Please include a Promise/A+ library");if("string"==typeof r)return a(r,e);if(!isArray(r)||r.length<1||void 0!==e)throw new Error("Error in .promise parameters");var o=r;if(!(o.length<1)){for(var t,n,s=[],i=0;i<o.length;i++){if("string"==typeof(n=o[i])&&(n=[n]),!isArray(n)||n.length<1||2<n.length)throw new Error("Error in .promise parameter");t=n[0],n=n[1]||void 0,s.push(a(t,n,i,o.length))}return Promise.all(s)}}),i=i||!1)return i.worker=function(){throw new Error("Can find webworker in this enviroment")},"undefined"!=typeof Worker&&(i.worker=function(r,e,o){if(void 0===(r=!0===r?void 0:r))for(var t=document.getElementsByTagName("script"),n=0;n<t.length;n++){if("alasql-worker.js"===t[n].src.substr(-16).toLowerCase()){r=t[n].src.substr(0,t[n].src.length-16)+"alasql.js";break}if("alasql-worker.min.js"===t[n].src.substr(-20).toLowerCase()){r=t[n].src.substr(0,t[n].src.length-20)+"alasql.min.js";break}if("alasql.js"===t[n].src.substr(-9).toLowerCase()){r=t[n].src;break}if("alasql.min.js"===t[n].src.substr(-13).toLowerCase()){r=t[n].src.substr(0,t[n].src.length-13)+"alasql.min.js";break}}if(void 0===r)throw new Error("Path to alasql.js is not specified");var s;!1!==r?(s=(s="importScripts('")+r+"');self.onmessage = function(event) {alasql(event.data.sql,event.data.params, function(data){postMessage({id:event.data.id, data:data});});}",s=new Blob([s],{type:"text/plain"}),i.webworker=new Worker(URL.createObjectURL(s)),i.webworker.onmessage=function(r){var e=r.data.id;i.buffer[e](r.data.data),delete i.buffer[e]},i.webworker.onerror=function(r){throw r},1<arguments.length&&i("REQUIRE "+e.map(function(r){return'"'+r+'"'}).join(","),[],o)):!1===r&&delete i.webworker}),i.lastid=0,i.buffer={},i.worker(),i;throw new Error("alasql was not found")});
@@ -1,7 +1,7 @@
1
- //! AlaSQL v1.7.6 | © 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.6
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.6';
145
+ alasql.version = '1.7.8';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -2492,7 +2492,7 @@ next:function () {
2492
2492
 
2493
2493
  // return next match that has a token
2494
2494
  lex:function lex () {
2495
- var r = this.next();
2495
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2496
2496
  if (r) {
2497
2497
  return r;
2498
2498
  } else {
@@ -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}
@@ -6940,7 +7132,7 @@ function queryfn3(query) {
6940
7132
 
6941
7133
  // Prepare variables
6942
7134
  query.data = [];
6943
- query.xgroups = {};
7135
+ query.xgroups=Object.create(null);
6944
7136
  query.groups = [];
6945
7137
 
6946
7138
  // Level of Joins
@@ -7981,7 +8173,7 @@ yy.Select.prototype.compile = function (databaseid, params) {
7981
8173
  // Save data into parameters array
7982
8174
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
7983
8175
  //
7984
- query.intofns = "params['" + this.into.param + "'].push(r)";
8176
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
7985
8177
  }
7986
8178
 
7987
8179
  if (query.intofns) {
@@ -8386,7 +8578,7 @@ yy.Select.prototype.compileFrom = function (query) {
8386
8578
  return res;
8387
8579
  };
8388
8580
  } else if (tq instanceof yy.ParamValue) {
8389
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8581
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
8390
8582
 
8391
8583
  if (tq.array) ps += ',true';
8392
8584
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8635,7 +8827,7 @@ yy.Select.prototype.compileJoins = function (query) {
8635
8827
  // source.data = ;
8636
8828
  var jnparam = jn.param.param;
8637
8829
 
8638
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8830
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8639
8831
  if (jn.array) ps += ',true';
8640
8832
  ps += ');if(cb)res=cb(res, idx, query);return res';
8641
8833
 
@@ -9334,9 +9526,10 @@ function compileSelectStar(query, aliases, joinstar) {
9334
9526
 
9335
9527
  if (columns && columns.length > 0) {
9336
9528
  columns.forEach(function (tcol) {
9529
+ var escapedColumnId = escapeq(tcol.columnid);
9337
9530
  if (joinstar && alasql.options.joinstar == 'underscore') {
9338
9531
  ss.push(
9339
- "'" + alias + '_' + tcol.columnid + "':p['" + alias + "']['" + tcol.columnid + "']"
9532
+ "'" + alias + '_' + escapedColumnId + "':p['" + alias + "']['" + escapedColumnId + "']"
9340
9533
  );
9341
9534
  } else if (joinstar && alasql.options.joinstar == 'json') {
9342
9535
  // ss.push('\''+alias+'_'+tcol.columnid+'\':p[\''+alias+'\'][\''+tcol.columnid+'\']');
@@ -9344,16 +9537,16 @@ function compileSelectStar(query, aliases, joinstar) {
9344
9537
  "r['" +
9345
9538
  alias +
9346
9539
  "']['" +
9347
- tcol.columnid +
9540
+ escapedColumnId +
9348
9541
  "']=p['" +
9349
9542
  alias +
9350
9543
  "']['" +
9351
- tcol.columnid +
9544
+ escapedColumnId +
9352
9545
  "'];";
9353
9546
  } else {
9354
- var value = "p['" + alias + "']['" + tcol.columnid + "']";
9547
+ var value = "p['" + alias + "']['" + escapedColumnId + "']";
9355
9548
  if (!columnIds[tcol.columnid]) {
9356
- var key = "'" + tcol.columnid + "':";
9549
+ var key = "'" + escapedColumnId + "':";
9357
9550
  ss.push(key + value);
9358
9551
  columnIds[tcol.columnid] = {
9359
9552
  id: ss.length - 1,
@@ -9384,7 +9577,7 @@ function compileSelectStar(query, aliases, joinstar) {
9384
9577
  } else {
9385
9578
 
9386
9579
  // if column not exist, then copy all
9387
- 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]};';
9580
+ 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]};';
9388
9581
 
9389
9582
  query.dirtyColumns = true;
9390
9583
  }
@@ -9411,7 +9604,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9411
9604
  if (col.columnid === '*') {
9412
9605
  if (col.func) {
9413
9606
  sp +=
9414
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9607
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9415
9608
  } else if (col.tableid) {
9416
9609
  //Copy all
9417
9610
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9463,7 +9656,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9463
9656
  if (isMultisheetParam) {
9464
9657
  sp =
9465
9658
  'var r={};var w=p["' +
9466
- tbid +
9659
+ escapeq(tbid) +
9467
9660
  '"];' +
9468
9661
  'var cols=[' +
9469
9662
  self.columns
@@ -9474,7 +9667,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9474
9667
  '];var colas=[' +
9475
9668
  self.columns
9476
9669
  .map(function (col) {
9477
- return "'" + (col.as || col.columnid) + "'";
9670
+ return "'" + escapeq(col.as || col.columnid) + "'";
9478
9671
  })
9479
9672
  .join(',') +
9480
9673
  '];' +
@@ -9637,9 +9830,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9637
9830
  this.orderColumns.forEach(function (v, idx) {
9638
9831
  var key = '$$$' + idx;
9639
9832
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9640
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9833
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9641
9834
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9642
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9835
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9643
9836
  } else {
9644
9837
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9645
9838
  }
@@ -9732,7 +9925,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9732
9925
  query.groupColumns[colas] = col.nick;
9733
9926
 
9734
9927
  // if(col.as) {
9735
- s += "r['" + colas + "']=";
9928
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9736
9929
  // // } else {
9737
9930
  // // s += 'r[\''+escapeq()+'\']=';
9738
9931
  // // };
@@ -9759,7 +9952,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9759
9952
  self.columns.forEach(function (col) {
9760
9953
 
9761
9954
  if (query.ingroup.indexOf(col.nick) > -1) {
9762
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9955
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9763
9956
  }
9764
9957
  });
9765
9958
 
@@ -9769,7 +9962,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9769
9962
  var key = '$$$' + idx;
9770
9963
 
9771
9964
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9772
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9965
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9773
9966
  } else {
9774
9967
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9775
9968
  }
@@ -10838,7 +11031,7 @@ yy.Op.prototype.toJS = function (context, tableid, defcols) {
10838
11031
  var ljs = '(' + leftJS() + '||{})';
10839
11032
 
10840
11033
  if (typeof this.right === 'string') {
10841
- s = ljs + '["' + this.right + '"]';
11034
+ s = ljs + '["' + escapeq(this.right) + '"]';
10842
11035
  } else if (typeof this.right === 'number') {
10843
11036
  s = ljs + '[' + this.right + ']';
10844
11037
  } else if (this.right instanceof yy.FuncValue) {
@@ -10854,7 +11047,7 @@ yy.Op.prototype.toJS = function (context, tableid, defcols) {
10854
11047
 
10855
11048
  if (this.op === '!') {
10856
11049
  if (typeof this.right === 'string') {
10857
- s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + this.right + '"]';
11050
+ s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + escapeq(this.right) + '"]';
10858
11051
  }
10859
11052
  // TODO - add other cases
10860
11053
  }
@@ -11180,7 +11373,7 @@ yy.ParamValue.prototype.toString = function () {
11180
11373
  };
11181
11374
  yy.ParamValue.prototype.toJS = function () {
11182
11375
  if (typeof this.param === 'string') {
11183
- return "params['" + this.param + "']";
11376
+ return "params['" + escapeq(this.param) + "']";
11184
11377
  }
11185
11378
 
11186
11379
  return 'params[' + this.param + ']';
@@ -12249,9 +12442,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12249
12442
  return s;
12250
12443
  });
12251
12444
 
12445
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12446
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12447
+ // are escaped, so the string cannot end early.
12448
+ function escapeJSONString(s) {
12449
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12450
+ if (c.length === 2) return c;
12451
+ if (c === '\\') return '\\\\';
12452
+ if (c === '"') return '\\"';
12453
+ if (c === '\n') return '\\n';
12454
+ if (c === '\r') return '\\r';
12455
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12456
+ });
12457
+ }
12458
+
12252
12459
  function JSONtoJS(obj, context, tableid, defcols) {
12253
12460
  var s = '';
12254
- if (typeof obj == 'string') s = '"' + obj + '"';
12461
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12255
12462
  else if (typeof obj == 'number') s = '(' + obj + ')';
12256
12463
  else if (typeof obj == 'boolean') s = obj;
12257
12464
  else if (typeof obj == 'object') {
@@ -12270,7 +12477,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12270
12477
  var ss = [];
12271
12478
  for (var k in obj) {
12272
12479
  var s1 = '';
12273
- if (typeof k == 'string') s1 += '"' + k + '"';
12480
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
12274
12481
  else if (typeof k == 'number') s1 += k;
12275
12482
  else if (typeof k == 'boolean') s1 += k;
12276
12483
  else {
@@ -15648,19 +15855,19 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
15648
15855
  }
15649
15856
  if (this.props && this.props.length > 0) {
15650
15857
  if (this.method == '@') {
15651
- var fs = "alasql.vars['" + this.variable + "']";
15858
+ var fs = "alasql.vars[k('"+this.variable+"')]";
15652
15859
  } else {
15653
- var fs = "params['" + this.variable + "']";
15860
+ var fs = "params[k('"+this.variable+"')]";
15654
15861
  }
15655
15862
  fs += this.props
15656
15863
  .map(function (prop) {
15657
15864
  if (typeof prop == 'string') {
15658
- return "['" + prop + "']";
15865
+ return "[k('"+prop+"')]";
15659
15866
  } else if (typeof prop == 'number') {
15660
15867
  return '[' + prop + ']';
15661
15868
  } else {
15662
15869
 
15663
- return '[' + prop.toJS() + ']';
15870
+ return "[k("+prop.toJS()+")]";
15664
15871
  // } else {
15665
15872
 
15666
15873
  // throw new Error('Wrong SET property');
@@ -15668,7 +15875,7 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
15668
15875
  })
15669
15876
  .join();
15670
15877
 
15671
- new Function('value,params,alasql', 'var y;' + fs + '=value')(res, params, alasql);
15878
+ 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);
15672
15879
  } else {
15673
15880
  if (this.method == '@') {
15674
15881
  alasql.vars[this.variable] = res;