alasql 4.6.8 → 4.6.9

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.6.8
5
+ // Version: 4.6.9
6
6
  // Date: 28.07.2015
7
7
  // (c) 2014-2022, Andrey Gershun & Mathias Wulff
8
8
  //
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.6.8 build: develop-f6e298b2 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.6.9 build: develop-f6e298b2 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.6.8
4
+ @version 4.6.9
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.6.8 build: develop-f6e298b2 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.6.9 build: develop-f6e298b2 | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.6.8
4
+ @version 4.6.9
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.6.8';
144
+ alasql.version = '4.6.9';
145
145
  alasql.build = 'develop-f6e298b2';
146
146
 
147
147
  /**
@@ -4478,9 +4478,201 @@ alasql.parser.parseError = function (str, hash) {
4478
4478
  }
4479
4479
  */
4480
4480
  alasql.parse = function (sql) {
4481
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4481
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4482
+ checkSqlNames(ast);
4483
+ return ast;
4482
4484
  };
4483
4485
 
4486
+ /*
4487
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4488
+ holds a quote, a backslash or a line break would end the string and make the rest code
4489
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4490
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4491
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4492
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4493
+ of a JSON value are data, not names.
4494
+ */
4495
+ var checkSqlNames = (function () {
4496
+ var hasOwn = Object.prototype.hasOwnProperty;
4497
+ var badName = /['\\\n\r\u2028\u2029]/;
4498
+ var nameKeys = {
4499
+ as: 1,
4500
+ columnid: 1,
4501
+ tableid: 1,
4502
+ databaseid: 1,
4503
+ viewid: 1,
4504
+ indexid: 1,
4505
+ trigger: 1,
4506
+ variable: 1,
4507
+ dropcolumn: 1,
4508
+ renamecolumn: 1,
4509
+ renameto: 1,
4510
+ to: 1,
4511
+ tocolumnid: 1,
4512
+ forcolumnid: 1,
4513
+ };
4514
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4515
+
4516
+ function checkName(name) {
4517
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4518
+ }
4519
+
4520
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4521
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4522
+ function checkOrder(columns, order) {
4523
+ var aliases = {};
4524
+ (columns || []).forEach(function (col) {
4525
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4526
+ });
4527
+ order.forEach(function (ord) {
4528
+ var e = ord && ord.expression;
4529
+ var name;
4530
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4531
+ if (typeof name === 'string') {
4532
+ if (!hasOwn.call(aliases, name)) checkName(name);
4533
+ return;
4534
+ }
4535
+ checkNames(ord, false, false);
4536
+ });
4537
+ }
4538
+
4539
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4540
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4541
+ // graph item, name is data.
4542
+ function entryOf(node, key) {
4543
+ var yy = alasql.yy;
4544
+ if (
4545
+ (node instanceof yy.Select && key === 'columns') ||
4546
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4547
+ (node.srchid === 'RETURN' && key === 'args')
4548
+ )
4549
+ return 'alias';
4550
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4551
+ return '';
4552
+ }
4553
+
4554
+ // Names that never reach generated code: they are used only at run time, as the key of a
4555
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4556
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4557
+ // names are free (what they hold is still walked), and skipChild are children that are not
4558
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4559
+ var varValueFree = true;
4560
+ var intoVarFree = true;
4561
+ var freeKeys = [
4562
+ ['CreateIndex', {indexid: 1}],
4563
+ ['DropIndex', {indexid: 1}],
4564
+ ['Reindex', {indexid: 1}],
4565
+ ['CreateTrigger', {trigger: 1}],
4566
+ ['DropTrigger', {trigger: 1}],
4567
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4568
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4569
+ ['DetachDatabase', {databaseid: 1}],
4570
+ ['DropDatabase', {databaseid: 1}],
4571
+ ['ShowTables', {databaseid: 1}],
4572
+ ['ShowColumns', {databaseid: 1}],
4573
+ ['ShowIndex', {databaseid: 1}],
4574
+ ['ShowCreateTable', {databaseid: 1}],
4575
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4576
+ ];
4577
+ var freeChild = [
4578
+ ['CreateTable', {table: 1}],
4579
+ ['DropTable', {tables: 1}],
4580
+ ['TruncateTable', {table: 1}],
4581
+ ['AlterTable', {table: 1}],
4582
+ ['ShowColumns', {table: 1}],
4583
+ ['ShowIndex', {table: 1}],
4584
+ ['ShowCreateTable', {table: 1}],
4585
+ ['CreateIndex', {table: 1}],
4586
+ ['CreateTrigger', {table: 1}],
4587
+ ['Declare', {declares: 1}],
4588
+ ];
4589
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4590
+
4591
+ function freeOf(list, node) {
4592
+ var yy = alasql.yy;
4593
+ for (var i = 0; i < list.length; i++) {
4594
+ var cls = yy[list[i][0]];
4595
+ if (cls && node instanceof cls) return list[i][1];
4596
+ }
4597
+ return null;
4598
+ }
4599
+
4600
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4601
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4602
+ function freeVar(node, key, entry) {
4603
+ var yy = alasql.yy;
4604
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4605
+ if (entry === 'source') return false;
4606
+ if (entry === 'into') return intoVarFree;
4607
+ return varValueFree;
4608
+ }
4609
+
4610
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4611
+ function freeSet(node, key) {
4612
+ var yy = alasql.yy;
4613
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4614
+ }
4615
+
4616
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4617
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4618
+ // names never reach generated code.
4619
+ function checkNames(node, data, entry) {
4620
+ if (node === null || typeof node !== 'object') return;
4621
+ var i;
4622
+ if (Array.isArray(node)) {
4623
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4624
+ return;
4625
+ }
4626
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4627
+ if (data && !isNode) {
4628
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4629
+ return;
4630
+ }
4631
+ var yy = alasql.yy;
4632
+ if (node instanceof yy.Json) {
4633
+ checkNames(node.value, true, false);
4634
+ return;
4635
+ }
4636
+ var fk = freeOf(freeKeys, node);
4637
+ var fc = freeOf(freeChild, node);
4638
+ var sc = freeOf(skipChild, node);
4639
+ for (var key in node) {
4640
+ if (!hasOwn.call(node, key)) continue;
4641
+ var value = node[key];
4642
+ if (sc && sc[key] === 1) continue;
4643
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4644
+ checkOrder(node.columns, value);
4645
+ continue;
4646
+ }
4647
+ // the column list of a WITH entry renames the result at run time
4648
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4649
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4650
+ if (typeof value === 'string') {
4651
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4652
+ continue;
4653
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4654
+ checkName(value);
4655
+ } else if (value !== null && typeof value === 'object') {
4656
+ if (Array.isArray(value)) {
4657
+ for (i = 0; i < value.length; i++) {
4658
+ if (typeof value[i] === 'string') {
4659
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4660
+ } else {
4661
+ checkNames(value[i], false, child || entryOf(node, key));
4662
+ }
4663
+ }
4664
+ } else {
4665
+ checkNames(value, false, child);
4666
+ }
4667
+ }
4668
+ }
4669
+ }
4670
+
4671
+ return function (ast) {
4672
+ checkNames(ast, false, false);
4673
+ };
4674
+ })();
4675
+
4484
4676
  /**
4485
4677
  List of engines of external databases
4486
4678
  @type {object}
@@ -7852,7 +8044,7 @@ yy.Select.prototype.compileFrom = function (query) {
7852
8044
  return res;
7853
8045
  };
7854
8046
  } else if (tq instanceof yy.ParamValue) {
7855
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8047
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
7856
8048
  if (tq.array) ps += ',true';
7857
8049
  ps += ');if(cb)res=cb(res,idx,query);return res';
7858
8050
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8062,7 +8254,7 @@ yy.Select.prototype.compileJoins = function (query) {
8062
8254
  srcwherefn: returnTrue,
8063
8255
  };
8064
8256
  const jnparam = jn.param.param;
8065
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
8257
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8066
8258
  if (jn.array) ps += ',true';
8067
8259
  ps += '); if(cb) res=cb(res, idx, query); return res';
8068
8260
 
@@ -8853,7 +9045,7 @@ function compileSelectStar(query, aliases, joinstar) {
8853
9045
  } else {
8854
9046
 
8855
9047
  // if column not exist, then copy all
8856
- 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]};';
9048
+ 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]};';
8857
9049
 
8858
9050
  query.dirtyColumns = true;
8859
9051
  }
@@ -8879,7 +9071,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8879
9071
  if (col.columnid === '*') {
8880
9072
  if (col.func) {
8881
9073
  sp +=
8882
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9074
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
8883
9075
  } else if (col.tableid) {
8884
9076
  //Copy all
8885
9077
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -8931,7 +9123,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8931
9123
  if (isMultisheetParam) {
8932
9124
  sp =
8933
9125
  'var r={};var w=p["' +
8934
- tbid +
9126
+ escapeq(tbid) +
8935
9127
  '"];' +
8936
9128
  'var cols=[' +
8937
9129
  self.columns
@@ -8942,7 +9134,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
8942
9134
  '];var colas=[' +
8943
9135
  self.columns
8944
9136
  .map(function (col) {
8945
- return "'" + (col.as || col.columnid) + "'";
9137
+ return "'" + escapeq(col.as || col.columnid) + "'";
8946
9138
  })
8947
9139
  .join(',') +
8948
9140
  '];' +
@@ -9106,9 +9298,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9106
9298
  this.orderColumns.forEach(function (v, idx) {
9107
9299
  var key = '$$$' + idx;
9108
9300
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9109
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9301
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9110
9302
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9111
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9303
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9112
9304
  } else {
9113
9305
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9114
9306
  }
@@ -9200,7 +9392,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9200
9392
  query.groupColumns[colas] = col.nick;
9201
9393
 
9202
9394
  // if(col.as) {
9203
- s += "r['" + colas + "']=";
9395
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9204
9396
  // // } else {
9205
9397
  // // s += 'r[\''+escapeq()+'\']=';
9206
9398
  // // };
@@ -9227,7 +9419,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9227
9419
  self.columns.forEach(function (col) {
9228
9420
 
9229
9421
  if (query.ingroup.indexOf(col.nick) > -1) {
9230
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9422
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9231
9423
  }
9232
9424
  });
9233
9425
 
@@ -9237,7 +9429,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9237
9429
  var key = '$$$' + idx;
9238
9430
 
9239
9431
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9240
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9432
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9241
9433
  } else {
9242
9434
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9243
9435
  }
@@ -10363,7 +10555,7 @@ yy.Over = class Over {
10363
10555
  }
10364
10556
  } else if (this.op === '!') {
10365
10557
  if (typeof this.right === 'string') {
10366
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
10558
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
10367
10559
  }
10368
10560
  // TODO - add other cases
10369
10561
  } else if (this.op === 'IS') {
@@ -10641,7 +10833,7 @@ yy.Over = class Over {
10641
10833
 
10642
10834
  toJS() {
10643
10835
  if (typeof this.param === 'string') {
10644
- return "params['" + this.param + "']";
10836
+ return "params['" + escapeq(this.param) + "']";
10645
10837
  }
10646
10838
 
10647
10839
  return 'params[' + this.param + ']';
@@ -11636,9 +11828,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
11636
11828
  }
11637
11829
  });
11638
11830
 
11831
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
11832
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
11833
+ // are escaped, so the string cannot end early.
11834
+ function escapeJSONString(s) {
11835
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
11836
+ if (c.length === 2) return c;
11837
+ if (c === '\\') return '\\\\';
11838
+ if (c === '"') return '\\"';
11839
+ if (c === '\n') return '\\n';
11840
+ if (c === '\r') return '\\r';
11841
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
11842
+ });
11843
+ }
11844
+
11639
11845
  function JSONtoJS(obj, context, tableid, defcols) {
11640
11846
  var s = '';
11641
- if (typeof obj == 'string') s = '"' + obj + '"';
11847
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
11642
11848
  else if (typeof obj == 'number') s = '(' + obj + ')';
11643
11849
  else if (typeof obj == 'boolean') s = obj;
11644
11850
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -11648,7 +11854,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
11648
11854
  } else if (!obj.toJS || obj instanceof yy.Json) {
11649
11855
  let ss = [];
11650
11856
  for (const k in obj) {
11651
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
11857
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
11652
11858
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
11653
11859
  ss.push(`${keyStr}:${valueStr}`);
11654
11860
  }