alasql 4.2.9 → 4.2.10

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.2.9
5
+ // Version: 4.2.10
6
6
  // Date: 28.07.2015
7
7
  // (c) 2014-2022, Andrey Gershun & Mathias Wulff
8
8
  //
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.2.9 build: develop-6db74fcc | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.2.10 build: develop-6db74fcc | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.2.9
4
+ @version 4.2.10
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.2.9 build: develop-6db74fcc | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.2.10 build: develop-6db74fcc | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.2.9
4
+ @version 4.2.10
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.2.9';
144
+ alasql.version = '4.2.10';
145
145
  alasql.build = 'develop-6db74fcc';
146
146
 
147
147
  /**
@@ -4466,9 +4466,201 @@ alasql.parser.parseError = function (str, hash) {
4466
4466
  }
4467
4467
  */
4468
4468
  alasql.parse = function (sql) {
4469
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4469
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4470
+ checkSqlNames(ast);
4471
+ return ast;
4470
4472
  };
4471
4473
 
4474
+ /*
4475
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4476
+ holds a quote, a backslash or a line break would end the string and make the rest code
4477
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4478
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4479
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4480
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4481
+ of a JSON value are data, not names.
4482
+ */
4483
+ var checkSqlNames = (function () {
4484
+ var hasOwn = Object.prototype.hasOwnProperty;
4485
+ var badName = /['\\\n\r\u2028\u2029]/;
4486
+ var nameKeys = {
4487
+ as: 1,
4488
+ columnid: 1,
4489
+ tableid: 1,
4490
+ databaseid: 1,
4491
+ viewid: 1,
4492
+ indexid: 1,
4493
+ trigger: 1,
4494
+ variable: 1,
4495
+ dropcolumn: 1,
4496
+ renamecolumn: 1,
4497
+ renameto: 1,
4498
+ to: 1,
4499
+ tocolumnid: 1,
4500
+ forcolumnid: 1,
4501
+ };
4502
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4503
+
4504
+ function checkName(name) {
4505
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4506
+ }
4507
+
4508
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4509
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4510
+ function checkOrder(columns, order) {
4511
+ var aliases = {};
4512
+ (columns || []).forEach(function (col) {
4513
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4514
+ });
4515
+ order.forEach(function (ord) {
4516
+ var e = ord && ord.expression;
4517
+ var name;
4518
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4519
+ if (typeof name === 'string') {
4520
+ if (!hasOwn.call(aliases, name)) checkName(name);
4521
+ return;
4522
+ }
4523
+ checkNames(ord, false, false);
4524
+ });
4525
+ }
4526
+
4527
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4528
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4529
+ // graph item, name is data.
4530
+ function entryOf(node, key) {
4531
+ var yy = alasql.yy;
4532
+ if (
4533
+ (node instanceof yy.Select && key === 'columns') ||
4534
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4535
+ (node.srchid === 'RETURN' && key === 'args')
4536
+ )
4537
+ return 'alias';
4538
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4539
+ return '';
4540
+ }
4541
+
4542
+ // Names that never reach generated code: they are used only at run time, as the key of a
4543
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4544
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4545
+ // names are free (what they hold is still walked), and skipChild are children that are not
4546
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4547
+ var varValueFree = true;
4548
+ var intoVarFree = true;
4549
+ var freeKeys = [
4550
+ ['CreateIndex', {indexid: 1}],
4551
+ ['DropIndex', {indexid: 1}],
4552
+ ['Reindex', {indexid: 1}],
4553
+ ['CreateTrigger', {trigger: 1}],
4554
+ ['DropTrigger', {trigger: 1}],
4555
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4556
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4557
+ ['DetachDatabase', {databaseid: 1}],
4558
+ ['DropDatabase', {databaseid: 1}],
4559
+ ['ShowTables', {databaseid: 1}],
4560
+ ['ShowColumns', {databaseid: 1}],
4561
+ ['ShowIndex', {databaseid: 1}],
4562
+ ['ShowCreateTable', {databaseid: 1}],
4563
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4564
+ ];
4565
+ var freeChild = [
4566
+ ['CreateTable', {table: 1}],
4567
+ ['DropTable', {tables: 1}],
4568
+ ['TruncateTable', {table: 1}],
4569
+ ['AlterTable', {table: 1}],
4570
+ ['ShowColumns', {table: 1}],
4571
+ ['ShowIndex', {table: 1}],
4572
+ ['ShowCreateTable', {table: 1}],
4573
+ ['CreateIndex', {table: 1}],
4574
+ ['CreateTrigger', {table: 1}],
4575
+ ['Declare', {declares: 1}],
4576
+ ];
4577
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4578
+
4579
+ function freeOf(list, node) {
4580
+ var yy = alasql.yy;
4581
+ for (var i = 0; i < list.length; i++) {
4582
+ var cls = yy[list[i][0]];
4583
+ if (cls && node instanceof cls) return list[i][1];
4584
+ }
4585
+ return null;
4586
+ }
4587
+
4588
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4589
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4590
+ function freeVar(node, key, entry) {
4591
+ var yy = alasql.yy;
4592
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4593
+ if (entry === 'source') return false;
4594
+ if (entry === 'into') return intoVarFree;
4595
+ return varValueFree;
4596
+ }
4597
+
4598
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4599
+ function freeSet(node, key) {
4600
+ var yy = alasql.yy;
4601
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4602
+ }
4603
+
4604
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4605
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4606
+ // names never reach generated code.
4607
+ function checkNames(node, data, entry) {
4608
+ if (node === null || typeof node !== 'object') return;
4609
+ var i;
4610
+ if (Array.isArray(node)) {
4611
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4612
+ return;
4613
+ }
4614
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4615
+ if (data && !isNode) {
4616
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4617
+ return;
4618
+ }
4619
+ var yy = alasql.yy;
4620
+ if (node instanceof yy.Json) {
4621
+ checkNames(node.value, true, false);
4622
+ return;
4623
+ }
4624
+ var fk = freeOf(freeKeys, node);
4625
+ var fc = freeOf(freeChild, node);
4626
+ var sc = freeOf(skipChild, node);
4627
+ for (var key in node) {
4628
+ if (!hasOwn.call(node, key)) continue;
4629
+ var value = node[key];
4630
+ if (sc && sc[key] === 1) continue;
4631
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4632
+ checkOrder(node.columns, value);
4633
+ continue;
4634
+ }
4635
+ // the column list of a WITH entry renames the result at run time
4636
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4637
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4638
+ if (typeof value === 'string') {
4639
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4640
+ continue;
4641
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4642
+ checkName(value);
4643
+ } else if (value !== null && typeof value === 'object') {
4644
+ if (Array.isArray(value)) {
4645
+ for (i = 0; i < value.length; i++) {
4646
+ if (typeof value[i] === 'string') {
4647
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4648
+ } else {
4649
+ checkNames(value[i], false, child || entryOf(node, key));
4650
+ }
4651
+ }
4652
+ } else {
4653
+ checkNames(value, false, child);
4654
+ }
4655
+ }
4656
+ }
4657
+ }
4658
+
4659
+ return function (ast) {
4660
+ checkNames(ast, false, false);
4661
+ };
4662
+ })();
4663
+
4472
4664
  /**
4473
4665
  List of engines of external databases
4474
4666
  @type {object}
@@ -7951,7 +8143,7 @@ yy.Select.prototype.compileFrom = function (query) {
7951
8143
  return res;
7952
8144
  };
7953
8145
  } else if (tq instanceof yy.ParamValue) {
7954
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
8146
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
7955
8147
 
7956
8148
  if (tq.array) ps += ',true';
7957
8149
  ps += ');if(cb)res=cb(res,idx,query);return res';
@@ -8204,7 +8396,7 @@ yy.Select.prototype.compileJoins = function (query) {
8204
8396
  // source.data = ;
8205
8397
  var jnparam = jn.param.param;
8206
8398
 
8207
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
8399
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8208
8400
  if (jn.array) ps += ',true';
8209
8401
  ps += ');if(cb)res=cb(res, idx, query);return res';
8210
8402
 
@@ -9068,7 +9260,7 @@ function compileSelectStar(query, aliases, joinstar) {
9068
9260
  } else {
9069
9261
 
9070
9262
  // if column not exist, then copy all
9071
- 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]};';
9263
+ 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]};';
9072
9264
 
9073
9265
  query.dirtyColumns = true;
9074
9266
  }
@@ -9094,7 +9286,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9094
9286
  if (col.columnid === '*') {
9095
9287
  if (col.func) {
9096
9288
  sp +=
9097
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9289
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9098
9290
  } else if (col.tableid) {
9099
9291
  //Copy all
9100
9292
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9146,7 +9338,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9146
9338
  if (isMultisheetParam) {
9147
9339
  sp =
9148
9340
  'var r={};var w=p["' +
9149
- tbid +
9341
+ escapeq(tbid) +
9150
9342
  '"];' +
9151
9343
  'var cols=[' +
9152
9344
  self.columns
@@ -9157,7 +9349,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9157
9349
  '];var colas=[' +
9158
9350
  self.columns
9159
9351
  .map(function (col) {
9160
- return "'" + (col.as || col.columnid) + "'";
9352
+ return "'" + escapeq(col.as || col.columnid) + "'";
9161
9353
  })
9162
9354
  .join(',') +
9163
9355
  '];' +
@@ -9321,9 +9513,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9321
9513
  this.orderColumns.forEach(function (v, idx) {
9322
9514
  var key = '$$$' + idx;
9323
9515
  if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9324
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9516
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9325
9517
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9326
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9518
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9327
9519
  } else {
9328
9520
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9329
9521
  }
@@ -9415,7 +9607,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9415
9607
  query.groupColumns[colas] = col.nick;
9416
9608
 
9417
9609
  // if(col.as) {
9418
- s += "r['" + colas + "']=";
9610
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9419
9611
  // // } else {
9420
9612
  // // s += 'r[\''+escapeq()+'\']=';
9421
9613
  // // };
@@ -9442,7 +9634,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9442
9634
  self.columns.forEach(function (col) {
9443
9635
 
9444
9636
  if (query.ingroup.indexOf(col.nick) > -1) {
9445
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9637
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9446
9638
  }
9447
9639
  });
9448
9640
 
@@ -9452,7 +9644,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9452
9644
  var key = '$$$' + idx;
9453
9645
 
9454
9646
  if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9455
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9647
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9456
9648
  } else {
9457
9649
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9458
9650
  }
@@ -10486,7 +10678,7 @@ yy.Over = class Over {
10486
10678
 
10487
10679
  if (this.op === '!') {
10488
10680
  if (typeof this.right === 'string') {
10489
- s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + this.right + '"]';
10681
+ s = '' + 'alasql.databases[alasql.useid].objects[' + leftJS() + ']["' + escapeq(this.right) + '"]';
10490
10682
  }
10491
10683
  // TODO - add other cases
10492
10684
  }
@@ -10873,7 +11065,7 @@ yy.Over = class Over {
10873
11065
 
10874
11066
  toJS() {
10875
11067
  if (typeof this.param === 'string') {
10876
- return "params['" + this.param + "']";
11068
+ return "params['" + escapeq(this.param) + "']";
10877
11069
  }
10878
11070
 
10879
11071
  return 'params[' + this.param + ']';
@@ -11875,9 +12067,23 @@ var JSONtoString = (alasql.utils.JSONtoString = function (obj) {
11875
12067
  return s;
11876
12068
  });
11877
12069
 
12070
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12071
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12072
+ // are escaped, so the string cannot end early.
12073
+ function escapeJSONString(s) {
12074
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12075
+ if (c.length === 2) return c;
12076
+ if (c === '\\') return '\\\\';
12077
+ if (c === '"') return '\\"';
12078
+ if (c === '\n') return '\\n';
12079
+ if (c === '\r') return '\\r';
12080
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12081
+ });
12082
+ }
12083
+
11878
12084
  function JSONtoJS(obj, context, tableid, defcols) {
11879
12085
  var s = '';
11880
- if (typeof obj == 'string') s = '"' + obj + '"';
12086
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
11881
12087
  else if (typeof obj == 'number') s = '(' + obj + ')';
11882
12088
  else if (typeof obj == 'boolean') s = obj;
11883
12089
  else if (typeof obj == 'object') {
@@ -11896,7 +12102,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
11896
12102
  var ss = [];
11897
12103
  for (var k in obj) {
11898
12104
  var s1 = '';
11899
- if (typeof k == 'string') s1 += '"' + k + '"';
12105
+ if (typeof k == 'string') s1 += '"' + escapeJSONString(k) + '"';
11900
12106
  else if (typeof k == 'number') s1 += k;
11901
12107
  else if (typeof k == 'boolean') s1 += k;
11902
12108
  else {