alasql 4.4.2 → 4.4.3

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/bin/alasql-cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  //
4
4
  // Command line interface for Alasql
5
- // Version: 4.4.2
5
+ // Version: 4.4.3
6
6
  // Date: 28.07.2015
7
7
  // (c) 2014-2022, Andrey Gershun & Mathias Wulff
8
8
  //
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.4.2 build: develop-b9447ae3 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.4.3 build: develop-b9447ae3 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.4.2
4
+ @version 4.4.3
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2024 Andrey Gershun & Mathias Wulff
package/dist/alasql.fs.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.4.2 build: develop-b9447ae3 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.4.3 build: develop-b9447ae3 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.4.2
4
+ @version 4.4.3
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2024 Andrey Gershun & Mathias Wulff
@@ -141,7 +141,7 @@ SOFTWARE.
141
141
  Current version of alasql
142
142
  @constant {string}
143
143
  */
144
- alasql.version = '4.4.2';
144
+ alasql.version = '4.4.3';
145
145
  alasql.build = 'develop-b9447ae3';
146
146
 
147
147
  /**
@@ -4469,9 +4469,201 @@ alasql.parser.parseError = function (str, hash) {
4469
4469
  }
4470
4470
  */
4471
4471
  alasql.parse = function (sql) {
4472
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4472
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4473
+ checkSqlNames(ast);
4474
+ return ast;
4473
4475
  };
4474
4476
 
4477
+ /*
4478
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4479
+ holds a quote, a backslash or a line break would end the string and make the rest code
4480
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4481
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4482
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4483
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4484
+ of a JSON value are data, not names.
4485
+ */
4486
+ var checkSqlNames = (function () {
4487
+ var hasOwn = Object.prototype.hasOwnProperty;
4488
+ var badName = /['\\\n\r\u2028\u2029]/;
4489
+ var nameKeys = {
4490
+ as: 1,
4491
+ columnid: 1,
4492
+ tableid: 1,
4493
+ databaseid: 1,
4494
+ viewid: 1,
4495
+ indexid: 1,
4496
+ trigger: 1,
4497
+ variable: 1,
4498
+ dropcolumn: 1,
4499
+ renamecolumn: 1,
4500
+ renameto: 1,
4501
+ to: 1,
4502
+ tocolumnid: 1,
4503
+ forcolumnid: 1,
4504
+ };
4505
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4506
+
4507
+ function checkName(name) {
4508
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4509
+ }
4510
+
4511
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4512
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4513
+ function checkOrder(columns, order) {
4514
+ var aliases = {};
4515
+ (columns || []).forEach(function (col) {
4516
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4517
+ });
4518
+ order.forEach(function (ord) {
4519
+ var e = ord && ord.expression;
4520
+ var name;
4521
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4522
+ if (typeof name === 'string') {
4523
+ if (!hasOwn.call(aliases, name)) checkName(name);
4524
+ return;
4525
+ }
4526
+ checkNames(ord, false, false);
4527
+ });
4528
+ }
4529
+
4530
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4531
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4532
+ // graph item, name is data.
4533
+ function entryOf(node, key) {
4534
+ var yy = alasql.yy;
4535
+ if (
4536
+ (node instanceof yy.Select && key === 'columns') ||
4537
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4538
+ (node.srchid === 'RETURN' && key === 'args')
4539
+ )
4540
+ return 'alias';
4541
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4542
+ return '';
4543
+ }
4544
+
4545
+ // Names that never reach generated code: they are used only at run time, as the key of a
4546
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4547
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4548
+ // names are free (what they hold is still walked), and skipChild are children that are not
4549
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4550
+ var varValueFree = true;
4551
+ var intoVarFree = true;
4552
+ var freeKeys = [
4553
+ ['CreateIndex', {indexid: 1}],
4554
+ ['DropIndex', {indexid: 1}],
4555
+ ['Reindex', {indexid: 1}],
4556
+ ['CreateTrigger', {trigger: 1}],
4557
+ ['DropTrigger', {trigger: 1}],
4558
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4559
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4560
+ ['DetachDatabase', {databaseid: 1}],
4561
+ ['DropDatabase', {databaseid: 1}],
4562
+ ['ShowTables', {databaseid: 1}],
4563
+ ['ShowColumns', {databaseid: 1}],
4564
+ ['ShowIndex', {databaseid: 1}],
4565
+ ['ShowCreateTable', {databaseid: 1}],
4566
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4567
+ ];
4568
+ var freeChild = [
4569
+ ['CreateTable', {table: 1}],
4570
+ ['DropTable', {tables: 1}],
4571
+ ['TruncateTable', {table: 1}],
4572
+ ['AlterTable', {table: 1}],
4573
+ ['ShowColumns', {table: 1}],
4574
+ ['ShowIndex', {table: 1}],
4575
+ ['ShowCreateTable', {table: 1}],
4576
+ ['CreateIndex', {table: 1}],
4577
+ ['CreateTrigger', {table: 1}],
4578
+ ['Declare', {declares: 1}],
4579
+ ];
4580
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4581
+
4582
+ function freeOf(list, node) {
4583
+ var yy = alasql.yy;
4584
+ for (var i = 0; i < list.length; i++) {
4585
+ var cls = yy[list[i][0]];
4586
+ if (cls && node instanceof cls) return list[i][1];
4587
+ }
4588
+ return null;
4589
+ }
4590
+
4591
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4592
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4593
+ function freeVar(node, key, entry) {
4594
+ var yy = alasql.yy;
4595
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4596
+ if (entry === 'source') return false;
4597
+ if (entry === 'into') return intoVarFree;
4598
+ return varValueFree;
4599
+ }
4600
+
4601
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4602
+ function freeSet(node, key) {
4603
+ var yy = alasql.yy;
4604
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4605
+ }
4606
+
4607
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4608
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4609
+ // names never reach generated code.
4610
+ function checkNames(node, data, entry) {
4611
+ if (node === null || typeof node !== 'object') return;
4612
+ var i;
4613
+ if (Array.isArray(node)) {
4614
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4615
+ return;
4616
+ }
4617
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4618
+ if (data && !isNode) {
4619
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4620
+ return;
4621
+ }
4622
+ var yy = alasql.yy;
4623
+ if (node instanceof yy.Json) {
4624
+ checkNames(node.value, true, false);
4625
+ return;
4626
+ }
4627
+ var fk = freeOf(freeKeys, node);
4628
+ var fc = freeOf(freeChild, node);
4629
+ var sc = freeOf(skipChild, node);
4630
+ for (var key in node) {
4631
+ if (!hasOwn.call(node, key)) continue;
4632
+ var value = node[key];
4633
+ if (sc && sc[key] === 1) continue;
4634
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4635
+ checkOrder(node.columns, value);
4636
+ continue;
4637
+ }
4638
+ // the column list of a WITH entry renames the result at run time
4639
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4640
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4641
+ if (typeof value === 'string') {
4642
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4643
+ continue;
4644
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4645
+ checkName(value);
4646
+ } else if (value !== null && typeof value === 'object') {
4647
+ if (Array.isArray(value)) {
4648
+ for (i = 0; i < value.length; i++) {
4649
+ if (typeof value[i] === 'string') {
4650
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4651
+ } else {
4652
+ checkNames(value[i], false, child || entryOf(node, key));
4653
+ }
4654
+ }
4655
+ } else {
4656
+ checkNames(value, false, child);
4657
+ }
4658
+ }
4659
+ }
4660
+ }
4661
+
4662
+ return function (ast) {
4663
+ checkNames(ast, false, false);
4664
+ };
4665
+ })();
4666
+
4475
4667
  /**
4476
4668
  List of engines of external databases
4477
4669
  @type {object}
@@ -7810,7 +8002,7 @@ yy.Select.prototype.compileFrom = function (query) {
7810
8002
  return res;
7811
8003
  };
7812
8004
  } else if (tq instanceof yy.ParamValue) {
7813
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8005
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
7814
8006
  if (tq.array) ps += ',true';
7815
8007
  ps += ');if(cb)res=cb(res,idx,query);return res';
7816
8008
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8020,7 +8212,7 @@ yy.Select.prototype.compileJoins = function (query) {
8020
8212
  srcwherefn: returnTrue,
8021
8213
  };
8022
8214
  const jnparam = jn.param.param;
8023
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
8215
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8024
8216
  if (jn.array) ps += ',true';
8025
8217
  ps += '); if(cb) res=cb(res, idx, query); return res';
8026
8218
 
@@ -8758,7 +8950,7 @@ function compileSelectStar(query, aliases, joinstar) {
8758
8950
  } else {
8759
8951
 
8760
8952
  // if column not exist, then copy all
8761
- 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]};';
8953
+ 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]};';
8762
8954
 
8763
8955
  query.dirtyColumns = true;
8764
8956
  }
@@ -8784,7 +8976,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8784
8976
  if (col.columnid === '*') {
8785
8977
  if (col.func) {
8786
8978
  sp +=
8787
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
8979
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
8788
8980
  } else if (col.tableid) {
8789
8981
  //Copy all
8790
8982
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -8836,7 +9028,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8836
9028
  if (isMultisheetParam) {
8837
9029
  sp =
8838
9030
  'var r={};var w=p["' +
8839
- tbid +
9031
+ escapeq(tbid) +
8840
9032
  '"];' +
8841
9033
  'var cols=[' +
8842
9034
  self.columns
@@ -8847,7 +9039,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8847
9039
  '];var colas=[' +
8848
9040
  self.columns
8849
9041
  .map(function (col) {
8850
- return "'" + (col.as || col.columnid) + "'";
9042
+ return "'" + escapeq(col.as || col.columnid) + "'";
8851
9043
  })
8852
9044
  .join(',') +
8853
9045
  '];' +
@@ -9011,9 +9203,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9011
9203
  this.orderColumns.forEach(function (v, idx) {
9012
9204
  var key = '$$$' + idx;
9013
9205
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9014
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9206
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9015
9207
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9016
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9208
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9017
9209
  } else {
9018
9210
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9019
9211
  }
@@ -9105,7 +9297,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9105
9297
  query.groupColumns[colas] = col.nick;
9106
9298
 
9107
9299
  // if(col.as) {
9108
- s += "r['" + colas + "']=";
9300
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9109
9301
  // // } else {
9110
9302
  // // s += 'r[\''+escapeq()+'\']=';
9111
9303
  // // };
@@ -9132,7 +9324,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9132
9324
  self.columns.forEach(function (col) {
9133
9325
 
9134
9326
  if (query.ingroup.indexOf(col.nick) > -1) {
9135
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9327
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9136
9328
  }
9137
9329
  });
9138
9330
 
@@ -9142,7 +9334,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9142
9334
  var key = '$$$' + idx;
9143
9335
 
9144
9336
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9145
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9337
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9146
9338
  } else {
9147
9339
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9148
9340
  }
@@ -10152,7 +10344,7 @@ yy.Over = class Over {
10152
10344
  }
10153
10345
  } else if (this.op === '!') {
10154
10346
  if (typeof this.right === 'string') {
10155
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
10347
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
10156
10348
  }
10157
10349
  // TODO - add other cases
10158
10350
  } else if (this.op === 'IS') {
@@ -10430,7 +10622,7 @@ yy.Over = class Over {
10430
10622
 
10431
10623
  toJS() {
10432
10624
  if (typeof this.param === 'string') {
10433
- return "params['" + this.param + "']";
10625
+ return "params['" + escapeq(this.param) + "']";
10434
10626
  }
10435
10627
 
10436
10628
  return 'params[' + this.param + ']';
@@ -11424,9 +11616,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
11424
11616
  }
11425
11617
  });
11426
11618
 
11619
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
11620
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
11621
+ // are escaped, so the string cannot end early.
11622
+ function escapeJSONString(s) {
11623
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
11624
+ if (c.length === 2) return c;
11625
+ if (c === '\\') return '\\\\';
11626
+ if (c === '"') return '\\"';
11627
+ if (c === '\n') return '\\n';
11628
+ if (c === '\r') return '\\r';
11629
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
11630
+ });
11631
+ }
11632
+
11427
11633
  function JSONtoJS(obj, context, tableid, defcols) {
11428
11634
  var s = '';
11429
- if (typeof obj == 'string') s = '"' + obj + '"';
11635
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
11430
11636
  else if (typeof obj == 'number') s = '(' + obj + ')';
11431
11637
  else if (typeof obj == 'boolean') s = obj;
11432
11638
  else if (typeof obj === 'object') {
@@ -11435,7 +11641,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
11435
11641
  } else if (!obj.toJS || obj instanceof yy.Json) {
11436
11642
  let ss = [];
11437
11643
  for (const k in obj) {
11438
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
11644
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
11439
11645
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
11440
11646
  ss.push(`${keyStr}:${valueStr}`);
11441
11647
  }