alasql 2.4.4 → 2.4.5

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.4.4 build: develop-1daec9bd | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v2.4.5 build: develop-1daec9bd | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 2.4.4
4
+ @version 2.4.5
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2023 Andrey Gershun & Mathias 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.4.4';
145
+ alasql.version = '2.4.5';
146
146
  alasql.build = 'develop-1daec9bd';
147
147
 
148
148
  /**
@@ -4450,9 +4450,201 @@ alasql.parser.parseError = function (str, hash) {
4450
4450
  }
4451
4451
  */
4452
4452
  alasql.parse = function (sql) {
4453
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4453
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4454
+ checkSqlNames(ast);
4455
+ return ast;
4454
4456
  };
4455
4457
 
4458
+ /*
4459
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4460
+ holds a quote, a backslash or a line break would end the string and make the rest code
4461
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4462
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4463
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4464
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4465
+ of a JSON value are data, not names.
4466
+ */
4467
+ var checkSqlNames = (function () {
4468
+ var hasOwn = Object.prototype.hasOwnProperty;
4469
+ var badName = /['\\\n\r\u2028\u2029]/;
4470
+ var nameKeys = {
4471
+ as: 1,
4472
+ columnid: 1,
4473
+ tableid: 1,
4474
+ databaseid: 1,
4475
+ viewid: 1,
4476
+ indexid: 1,
4477
+ trigger: 1,
4478
+ variable: 1,
4479
+ dropcolumn: 1,
4480
+ renamecolumn: 1,
4481
+ renameto: 1,
4482
+ to: 1,
4483
+ tocolumnid: 1,
4484
+ forcolumnid: 1,
4485
+ };
4486
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4487
+
4488
+ function checkName(name) {
4489
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4490
+ }
4491
+
4492
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4493
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4494
+ function checkOrder(columns, order) {
4495
+ var aliases = {};
4496
+ (columns || []).forEach(function (col) {
4497
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4498
+ });
4499
+ order.forEach(function (ord) {
4500
+ var e = ord && ord.expression;
4501
+ var name;
4502
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4503
+ if (typeof name === 'string') {
4504
+ if (!hasOwn.call(aliases, name)) checkName(name);
4505
+ return;
4506
+ }
4507
+ checkNames(ord, false, false);
4508
+ });
4509
+ }
4510
+
4511
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4512
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4513
+ // graph item, name is data.
4514
+ function entryOf(node, key) {
4515
+ var yy = alasql.yy;
4516
+ if (
4517
+ (node instanceof yy.Select && key === 'columns') ||
4518
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4519
+ (node.srchid === 'RETURN' && key === 'args')
4520
+ )
4521
+ return 'alias';
4522
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4523
+ return '';
4524
+ }
4525
+
4526
+ // Names that never reach generated code: they are used only at run time, as the key of a
4527
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4528
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4529
+ // names are free (what they hold is still walked), and skipChild are children that are not
4530
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4531
+ var varValueFree = false;
4532
+ var intoVarFree = false;
4533
+ var freeKeys = [
4534
+ ['CreateIndex', {indexid: 1}],
4535
+ ['DropIndex', {indexid: 1}],
4536
+ ['Reindex', {indexid: 1}],
4537
+ ['CreateTrigger', {trigger: 1}],
4538
+ ['DropTrigger', {trigger: 1}],
4539
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4540
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4541
+ ['DetachDatabase', {databaseid: 1}],
4542
+ ['DropDatabase', {databaseid: 1}],
4543
+ ['ShowTables', {databaseid: 1}],
4544
+ ['ShowColumns', {databaseid: 1}],
4545
+ ['ShowIndex', {databaseid: 1}],
4546
+ ['ShowCreateTable', {databaseid: 1}],
4547
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4548
+ ];
4549
+ var freeChild = [
4550
+ ['CreateTable', {table: 1}],
4551
+ ['DropTable', {tables: 1}],
4552
+ ['TruncateTable', {table: 1}],
4553
+ ['AlterTable', {table: 1}],
4554
+ ['ShowColumns', {table: 1}],
4555
+ ['ShowIndex', {table: 1}],
4556
+ ['ShowCreateTable', {table: 1}],
4557
+ ['CreateIndex', {table: 1}],
4558
+ ['CreateTrigger', {table: 1}],
4559
+ ['Declare', {declares: 1}],
4560
+ ];
4561
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4562
+
4563
+ function freeOf(list, node) {
4564
+ var yy = alasql.yy;
4565
+ for (var i = 0; i < list.length; i++) {
4566
+ var cls = yy[list[i][0]];
4567
+ if (cls && node instanceof cls) return list[i][1];
4568
+ }
4569
+ return null;
4570
+ }
4571
+
4572
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4573
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4574
+ function freeVar(node, key, entry) {
4575
+ var yy = alasql.yy;
4576
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4577
+ if (entry === 'source') return false;
4578
+ if (entry === 'into') return intoVarFree;
4579
+ return varValueFree;
4580
+ }
4581
+
4582
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4583
+ function freeSet(node, key) {
4584
+ var yy = alasql.yy;
4585
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4586
+ }
4587
+
4588
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4589
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4590
+ // names never reach generated code.
4591
+ function checkNames(node, data, entry) {
4592
+ if (node === null || typeof node !== 'object') return;
4593
+ var i;
4594
+ if (Array.isArray(node)) {
4595
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4596
+ return;
4597
+ }
4598
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4599
+ if (data && !isNode) {
4600
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4601
+ return;
4602
+ }
4603
+ var yy = alasql.yy;
4604
+ if (node instanceof yy.Json) {
4605
+ checkNames(node.value, true, false);
4606
+ return;
4607
+ }
4608
+ var fk = freeOf(freeKeys, node);
4609
+ var fc = freeOf(freeChild, node);
4610
+ var sc = freeOf(skipChild, node);
4611
+ for (var key in node) {
4612
+ if (!hasOwn.call(node, key)) continue;
4613
+ var value = node[key];
4614
+ if (sc && sc[key] === 1) continue;
4615
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4616
+ checkOrder(node.columns, value);
4617
+ continue;
4618
+ }
4619
+ // the column list of a WITH entry renames the result at run time
4620
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4621
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4622
+ if (typeof value === 'string') {
4623
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4624
+ continue;
4625
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4626
+ checkName(value);
4627
+ } else if (value !== null && typeof value === 'object') {
4628
+ if (Array.isArray(value)) {
4629
+ for (i = 0; i < value.length; i++) {
4630
+ if (typeof value[i] === 'string') {
4631
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4632
+ } else {
4633
+ checkNames(value[i], false, child || entryOf(node, key));
4634
+ }
4635
+ }
4636
+ } else {
4637
+ checkNames(value, false, child);
4638
+ }
4639
+ }
4640
+ }
4641
+ }
4642
+
4643
+ return function (ast) {
4644
+ checkNames(ast, false, false);
4645
+ };
4646
+ })();
4647
+
4456
4648
  /**
4457
4649
  List of engines of external databases
4458
4650
  @type {object}
@@ -7545,7 +7737,7 @@ yy.Select.prototype.compile = function (databaseid, params) {
7545
7737
  // Save data into parameters array
7546
7738
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
7547
7739
  //
7548
- query.intofns = "params['" + this.into.param + "'].push(r)";
7740
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
7549
7741
  }
7550
7742
 
7551
7743
  if (query.intofns) {
@@ -7950,7 +8142,7 @@ yy.Select.prototype.compileFrom = function (query) {
7950
8142
  return res;
7951
8143
  };
7952
8144
  } else if (tq instanceof yy.ParamValue) {
7953
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8145
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
7954
8146
 
7955
8147
  if (tq.array) ps += ',true';
7956
8148
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8199,7 +8391,7 @@ yy.Select.prototype.compileJoins = function (query) {
8199
8391
  // source.data = ;
8200
8392
  var jnparam = jn.param.param;
8201
8393
 
8202
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8394
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8203
8395
  if (jn.array) ps += ',true';
8204
8396
  ps += ');if(cb)res=cb(res, idx, query);return res';
8205
8397
 
@@ -8981,9 +9173,10 @@ function compileSelectStar(query, aliases, joinstar) {
8981
9173
 
8982
9174
  if (columns && columns.length > 0) {
8983
9175
  columns.forEach(function (tcol) {
9176
+ var escapedColumnId = escapeq(tcol.columnid);
8984
9177
  if (joinstar && alasql.options.joinstar == 'underscore') {
8985
9178
  ss.push(
8986
- "'" + alias + '_' + tcol.columnid + "':p['" + alias + "']['" + tcol.columnid + "']"
9179
+ "'" + alias + '_' + escapedColumnId + "':p['" + alias + "']['" + escapedColumnId + "']"
8987
9180
  );
8988
9181
  } else if (joinstar && alasql.options.joinstar == 'json') {
8989
9182
  // ss.push('\''+alias+'_'+tcol.columnid+'\':p[\''+alias+'\'][\''+tcol.columnid+'\']');
@@ -8991,16 +9184,16 @@ function compileSelectStar(query, aliases, joinstar) {
8991
9184
  "r['" +
8992
9185
  alias +
8993
9186
  "']['" +
8994
- tcol.columnid +
9187
+ escapedColumnId +
8995
9188
  "']=p['" +
8996
9189
  alias +
8997
9190
  "']['" +
8998
- tcol.columnid +
9191
+ escapedColumnId +
8999
9192
  "'];";
9000
9193
  } else {
9001
- var value = "p['" + alias + "']['" + tcol.columnid + "']";
9194
+ var value = "p['" + alias + "']['" + escapedColumnId + "']";
9002
9195
  if (!columnIds[tcol.columnid]) {
9003
- var key = "'" + tcol.columnid + "':";
9196
+ var key = "'" + escapedColumnId + "':";
9004
9197
  ss.push(key + value);
9005
9198
  columnIds[tcol.columnid] = {
9006
9199
  id: ss.length - 1,
@@ -9031,7 +9224,7 @@ function compileSelectStar(query, aliases, joinstar) {
9031
9224
  } else {
9032
9225
 
9033
9226
  // if column not exist, then copy all
9034
- 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]};';
9227
+ 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]};';
9035
9228
 
9036
9229
  query.dirtyColumns = true;
9037
9230
  }
@@ -9058,7 +9251,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9058
9251
  if (col.columnid === '*') {
9059
9252
  if (col.func) {
9060
9253
  sp +=
9061
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9254
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9062
9255
  } else if (col.tableid) {
9063
9256
  //Copy all
9064
9257
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9110,7 +9303,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9110
9303
  if (isMultisheetParam) {
9111
9304
  sp =
9112
9305
  'var r={};var w=p["' +
9113
- tbid +
9306
+ escapeq(tbid) +
9114
9307
  '"];' +
9115
9308
  'var cols=[' +
9116
9309
  self.columns
@@ -9121,7 +9314,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9121
9314
  '];var colas=[' +
9122
9315
  self.columns
9123
9316
  .map(function (col) {
9124
- return "'" + (col.as || col.columnid) + "'";
9317
+ return "'" + escapeq(col.as || col.columnid) + "'";
9125
9318
  })
9126
9319
  .join(',') +
9127
9320
  '];' +
@@ -9284,9 +9477,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9284
9477
  this.orderColumns.forEach(function (v, idx) {
9285
9478
  var key = '$$$' + idx;
9286
9479
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9287
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9480
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9288
9481
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9289
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9482
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9290
9483
  } else {
9291
9484
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9292
9485
  }
@@ -9379,7 +9572,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9379
9572
  query.groupColumns[colas] = col.nick;
9380
9573
 
9381
9574
  // if(col.as) {
9382
- s += "r['" + colas + "']=";
9575
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9383
9576
  // // } else {
9384
9577
  // // s += 'r[\''+escapeq()+'\']=';
9385
9578
  // // };
@@ -9406,7 +9599,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9406
9599
  self.columns.forEach(function (col) {
9407
9600
 
9408
9601
  if (query.ingroup.indexOf(col.nick) > -1) {
9409
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9602
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9410
9603
  }
9411
9604
  });
9412
9605
 
@@ -9416,7 +9609,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9416
9609
  var key = '$$$' + idx;
9417
9610
 
9418
9611
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9419
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9612
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9420
9613
  } else {
9421
9614
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9422
9615
  }
@@ -10486,7 +10679,7 @@ yy.Over.prototype.toString = function () {
10486
10679
  var ljs = '(' + leftJS() + '||{})';
10487
10680
 
10488
10681
  if (typeof this.right === 'string') {
10489
- s = ljs + '["' + this.right + '"]';
10682
+ s = ljs + '["' + escapeq(this.right) + '"]';
10490
10683
  } else if (typeof this.right === 'number') {
10491
10684
  s = ljs + '[' + this.right + ']';
10492
10685
  } else if (this.right instanceof yy.FuncValue) {
@@ -10502,7 +10695,7 @@ yy.Over.prototype.toString = function () {
10502
10695
 
10503
10696
  if (this.op === '!') {
10504
10697
  if (typeof this.right === 'string') {
10505
- s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + this.right + '"]';
10698
+ s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + escapeq(this.right) + '"]';
10506
10699
  }
10507
10700
  // TODO - add other cases
10508
10701
  }
@@ -10866,7 +11059,7 @@ yy.Over.prototype.toString = function () {
10866
11059
 
10867
11060
  toJS() {
10868
11061
  if (typeof this.param === 'string') {
10869
- return "params['" + this.param + "']";
11062
+ return "params['" + escapeq(this.param) + "']";
10870
11063
  }
10871
11064
 
10872
11065
  return 'params[' + this.param + ']';
@@ -11937,9 +12130,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function (obj) {
11937
12130
  return s;
11938
12131
  });
11939
12132
 
12133
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12134
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12135
+ // are escaped, so the string cannot end early.
12136
+ function escapeJSONString(s) {
12137
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12138
+ if (c.length === 2) return c;
12139
+ if (c === '\\') return '\\\\';
12140
+ if (c === '"') return '\\"';
12141
+ if (c === '\n') return '\\n';
12142
+ if (c === '\r') return '\\r';
12143
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12144
+ });
12145
+ }
12146
+
11940
12147
  function JSONtoJS(obj, context, tableid, defcols) {
11941
12148
  var s = '';
11942
- if (typeof obj == 'string') s = '"' + obj + '"';
12149
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
11943
12150
  else if (typeof obj == 'number') s = '(' + obj + ')';
11944
12151
  else if (typeof obj == 'boolean') s = obj;
11945
12152
  else if (typeof obj == 'object') {
@@ -11958,7 +12165,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
11958
12165
  var ss = [];
11959
12166
  for (var k in obj) {
11960
12167
  var s1 = '';
11961
- if (typeof k == 'string') s1 += '"' + k + '"';
12168
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
11962
12169
  else if (typeof k == 'number') s1 += k;
11963
12170
  else if (typeof k == 'boolean') s1 += k;
11964
12171
  else {