alasql 4.10.2 → 4.10.4

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 v4.10.2 build: develop-4b9a20d8 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.10.4 build: develop-4b9a20d8 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.10.2
4
+ @version 4.10.4
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2025 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.10.2';
144
+ alasql.version = '4.10.4';
145
145
  alasql.build = 'develop-4b9a20d8';
146
146
 
147
147
  /**
@@ -2540,7 +2540,7 @@ next:function () {
2540
2540
 
2541
2541
  // return next match that has a token
2542
2542
  lex:function lex () {
2543
- var r = this.next();
2543
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2544
2544
  if (r) {
2545
2545
  return r;
2546
2546
  } else {
@@ -4526,9 +4526,201 @@ alasql.parser.parseError = function (str, hash) {
4526
4526
  }
4527
4527
  */
4528
4528
  alasql.parse = function (sql) {
4529
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4529
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4530
+ checkSqlNames(ast);
4531
+ return ast;
4530
4532
  };
4531
4533
 
4534
+ /*
4535
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4536
+ holds a quote, a backslash or a line break would end the string and make the rest code
4537
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4538
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4539
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4540
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4541
+ of a JSON value are data, not names.
4542
+ */
4543
+ var checkSqlNames = (function () {
4544
+ var hasOwn = Object.prototype.hasOwnProperty;
4545
+ var badName = /['\\\n\r\u2028\u2029]/;
4546
+ var nameKeys = {
4547
+ as: 1,
4548
+ columnid: 1,
4549
+ tableid: 1,
4550
+ databaseid: 1,
4551
+ viewid: 1,
4552
+ indexid: 1,
4553
+ trigger: 1,
4554
+ variable: 1,
4555
+ dropcolumn: 1,
4556
+ renamecolumn: 1,
4557
+ renameto: 1,
4558
+ to: 1,
4559
+ tocolumnid: 1,
4560
+ forcolumnid: 1,
4561
+ };
4562
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4563
+
4564
+ function checkName(name) {
4565
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4566
+ }
4567
+
4568
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
4569
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
4570
+ function checkOrder(columns, order) {
4571
+ var aliases = {};
4572
+ (columns || []).forEach(function (col) {
4573
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4574
+ });
4575
+ order.forEach(function (ord) {
4576
+ var e = ord && ord.expression;
4577
+ var name;
4578
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4579
+ if (typeof name === 'string') {
4580
+ if (!hasOwn.call(aliases, name)) checkName(name);
4581
+ return;
4582
+ }
4583
+ checkNames(ord, false, false);
4584
+ });
4585
+ }
4586
+
4587
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4588
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4589
+ // graph item, name is data.
4590
+ function entryOf(node, key) {
4591
+ var yy = alasql.yy;
4592
+ if (
4593
+ (node instanceof yy.Select && key === 'columns') ||
4594
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4595
+ (node.srchid === 'RETURN' && key === 'args')
4596
+ )
4597
+ return 'alias';
4598
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4599
+ return '';
4600
+ }
4601
+
4602
+ // Names that never reach generated code: they are used only at run time, as the key of a
4603
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4604
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4605
+ // names are free (what they hold is still walked), and skipChild are children that are not
4606
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4607
+ var varValueFree = true;
4608
+ var intoVarFree = true;
4609
+ var freeKeys = [
4610
+ ['CreateIndex', {indexid: 1}],
4611
+ ['DropIndex', {indexid: 1}],
4612
+ ['Reindex', {indexid: 1}],
4613
+ ['CreateTrigger', {trigger: 1}],
4614
+ ['DropTrigger', {trigger: 1}],
4615
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4616
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4617
+ ['DetachDatabase', {databaseid: 1}],
4618
+ ['DropDatabase', {databaseid: 1}],
4619
+ ['ShowTables', {databaseid: 1}],
4620
+ ['ShowColumns', {databaseid: 1}],
4621
+ ['ShowIndex', {databaseid: 1}],
4622
+ ['ShowCreateTable', {databaseid: 1}],
4623
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4624
+ ];
4625
+ var freeChild = [
4626
+ ['CreateTable', {table: 1}],
4627
+ ['DropTable', {tables: 1}],
4628
+ ['TruncateTable', {table: 1}],
4629
+ ['AlterTable', {table: 1}],
4630
+ ['ShowColumns', {table: 1}],
4631
+ ['ShowIndex', {table: 1}],
4632
+ ['ShowCreateTable', {table: 1}],
4633
+ ['CreateIndex', {table: 1}],
4634
+ ['CreateTrigger', {table: 1}],
4635
+ ['Declare', {declares: 1}],
4636
+ ];
4637
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4638
+
4639
+ function freeOf(list, node) {
4640
+ var yy = alasql.yy;
4641
+ for (var i = 0; i < list.length; i++) {
4642
+ var cls = yy[list[i][0]];
4643
+ if (cls && node instanceof cls) return list[i][1];
4644
+ }
4645
+ return null;
4646
+ }
4647
+
4648
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4649
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4650
+ function freeVar(node, key, entry) {
4651
+ var yy = alasql.yy;
4652
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4653
+ if (entry === 'source') return false;
4654
+ if (entry === 'into') return intoVarFree;
4655
+ return varValueFree;
4656
+ }
4657
+
4658
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4659
+ function freeSet(node, key) {
4660
+ var yy = alasql.yy;
4661
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4662
+ }
4663
+
4664
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4665
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4666
+ // names never reach generated code.
4667
+ function checkNames(node, data, entry) {
4668
+ if (node === null || typeof node !== 'object') return;
4669
+ var i;
4670
+ if (Array.isArray(node)) {
4671
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4672
+ return;
4673
+ }
4674
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4675
+ if (data && !isNode) {
4676
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4677
+ return;
4678
+ }
4679
+ var yy = alasql.yy;
4680
+ if (node instanceof yy.Json) {
4681
+ checkNames(node.value, true, false);
4682
+ return;
4683
+ }
4684
+ var fk = freeOf(freeKeys, node);
4685
+ var fc = freeOf(freeChild, node);
4686
+ var sc = freeOf(skipChild, node);
4687
+ for (var key in node) {
4688
+ if (!hasOwn.call(node, key)) continue;
4689
+ var value = node[key];
4690
+ if (sc && sc[key] === 1) continue;
4691
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4692
+ checkOrder(node.columns, value);
4693
+ continue;
4694
+ }
4695
+ // the column list of a WITH entry renames the result at run time
4696
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4697
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4698
+ if (typeof value === 'string') {
4699
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4700
+ continue;
4701
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4702
+ checkName(value);
4703
+ } else if (value !== null && typeof value === 'object') {
4704
+ if (Array.isArray(value)) {
4705
+ for (i = 0; i < value.length; i++) {
4706
+ if (typeof value[i] === 'string') {
4707
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4708
+ } else {
4709
+ checkNames(value[i], false, child || entryOf(node, key));
4710
+ }
4711
+ }
4712
+ } else {
4713
+ checkNames(value, false, child);
4714
+ }
4715
+ }
4716
+ }
4717
+ }
4718
+
4719
+ return function (ast) {
4720
+ checkNames(ast, false, false);
4721
+ };
4722
+ })();
4723
+
4532
4724
  /**
4533
4725
  List of engines of external databases
4534
4726
  @type {object}
@@ -6610,7 +6802,7 @@ function queryfn3(query) {
6610
6802
 
6611
6803
  // Prepare variables
6612
6804
  query.data = [];
6613
- query.xgroups = {};
6805
+ query.xgroups=Object.create(null);
6614
6806
  query.groups = [];
6615
6807
 
6616
6808
  // Level of Joins
@@ -7999,7 +8191,7 @@ yy.Select.prototype.compileFrom = function (query) {
7999
8191
  return res;
8000
8192
  };
8001
8193
  } else if (tq instanceof yy.ParamValue) {
8002
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8194
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
8003
8195
  if (tq.array) ps += ',true';
8004
8196
  ps += ');if(cb)res=cb(res,idx,query);return res';
8005
8197
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8209,7 +8401,7 @@ yy.Select.prototype.compileJoins = function (query) {
8209
8401
  srcwherefn: returnTrue,
8210
8402
  };
8211
8403
  const jnparam = jn.param.param;
8212
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
8404
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8213
8405
  if (jn.array) ps += ',true';
8214
8406
  ps += '); if(cb) res=cb(res, idx, query); return res';
8215
8407
 
@@ -9007,7 +9199,7 @@ function compileSelectStar(query, aliases, joinstar) {
9007
9199
  } else {
9008
9200
 
9009
9201
  // if column not exist, then copy all
9010
- 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]};';
9202
+ 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]};';
9011
9203
 
9012
9204
  query.dirtyColumns = true;
9013
9205
  }
@@ -9033,7 +9225,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9033
9225
  if (col.columnid === '*') {
9034
9226
  if (col.func) {
9035
9227
  sp +=
9036
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9228
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9037
9229
  } else if (col.tableid) {
9038
9230
  //Copy all
9039
9231
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9085,7 +9277,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9085
9277
  if (isMultisheetParam) {
9086
9278
  sp =
9087
9279
  'var r={};var w=p["' +
9088
- tbid +
9280
+ escapeq(tbid) +
9089
9281
  '"];' +
9090
9282
  'var cols=[' +
9091
9283
  self.columns
@@ -9096,7 +9288,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9096
9288
  '];var colas=[' +
9097
9289
  self.columns
9098
9290
  .map(function (col) {
9099
- return "'" + (col.as || col.columnid) + "'";
9291
+ return "'" + escapeq(col.as || col.columnid) + "'";
9100
9292
  })
9101
9293
  .join(',') +
9102
9294
  '];' +
@@ -9272,9 +9464,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9272
9464
  // Use Object.keys to get column names and access by index
9273
9465
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
9274
9466
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9275
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9467
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9276
9468
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9277
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9469
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9278
9470
  } else {
9279
9471
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9280
9472
  }
@@ -9381,7 +9573,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9381
9573
  query.groupColumns[colas] = col.nick;
9382
9574
 
9383
9575
  // if(col.as) {
9384
- s += "r['" + colas + "']=";
9576
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9385
9577
  // // } else {
9386
9578
  // // s += 'r[\''+escapeq()+'\']=';
9387
9579
  // // };
@@ -9408,7 +9600,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9408
9600
  self.columns.forEach(function (col) {
9409
9601
 
9410
9602
  if (query.ingroup.indexOf(col.nick) > -1) {
9411
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
9603
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
9412
9604
  }
9413
9605
  });
9414
9606
 
@@ -9430,7 +9622,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9430
9622
  // Use Object.keys to get column names and access by index
9431
9623
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
9432
9624
  } else if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9433
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9625
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9434
9626
  } else {
9435
9627
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9436
9628
  }
@@ -10589,7 +10781,7 @@ yy.Over = class Over {
10589
10781
  }
10590
10782
  } else if (this.op === '!') {
10591
10783
  if (typeof this.right === 'string') {
10592
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
10784
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
10593
10785
  }
10594
10786
  // TODO - add other cases
10595
10787
  } else if (this.op === 'IS') {
@@ -10867,7 +11059,7 @@ yy.Over = class Over {
10867
11059
 
10868
11060
  toJS() {
10869
11061
  if (typeof this.param === 'string') {
10870
- return "params['" + this.param + "']";
11062
+ return "params['" + escapeq(this.param) + "']";
10871
11063
  }
10872
11064
 
10873
11065
  return 'params[' + this.param + ']';
@@ -11883,9 +12075,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
11883
12075
  }
11884
12076
  });
11885
12077
 
12078
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12079
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12080
+ // are escaped, so the string cannot end early.
12081
+ function escapeJSONString(s) {
12082
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12083
+ if (c.length === 2) return c;
12084
+ if (c === '\\') return '\\\\';
12085
+ if (c === '"') return '\\"';
12086
+ if (c === '\n') return '\\n';
12087
+ if (c === '\r') return '\\r';
12088
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12089
+ });
12090
+ }
12091
+
11886
12092
  function JSONtoJS(obj, context, tableid, defcols) {
11887
12093
  var s = '';
11888
- if (typeof obj == 'string') s = '"' + obj + '"';
12094
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
11889
12095
  else if (typeof obj == 'number') s = '(' + obj + ')';
11890
12096
  else if (typeof obj == 'boolean') s = obj;
11891
12097
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -11895,7 +12101,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
11895
12101
  } else if (!obj.toJS || obj instanceof yy.Json) {
11896
12102
  let ss = [];
11897
12103
  for (const k in obj) {
11898
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
12104
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
11899
12105
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
11900
12106
  ss.push(`${keyStr}:${valueStr}`);
11901
12107
  }
@@ -15127,22 +15333,22 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
15127
15333
  if (this.props && this.props.length > 0) {
15128
15334
  let fs;
15129
15335
  if (this.method === '@') {
15130
- fs = `alasql.vars['${this.variable}']`;
15336
+ fs = `alasql.vars[k('${this.variable}')]`;
15131
15337
  } else {
15132
- fs = `params['${this.variable}']`;
15338
+ fs = `params[k('${this.variable}')]`;
15133
15339
  }
15134
15340
  this.props.forEach(prop => {
15135
15341
  if (typeof prop === 'string') {
15136
- fs += `['${prop}']`;
15342
+ fs += `[k('${prop}')]`;
15137
15343
  } else if (typeof prop === 'number') {
15138
15344
  fs += `[${prop}]`;
15139
15345
  } else {
15140
15346
  // Assuming prop.toJS() is a method that converts prop to a JavaScript expression.
15141
- fs += `[${prop.toJS()}]`;
15347
+ fs += `[k(${prop.toJS()})]`;
15142
15348
  }
15143
15349
  });
15144
15350
 
15145
- new Function('value, params, alasql', `${fs} = value`)(res, params, alasql);
15351
+ new Function('value, params, alasql', `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);
15146
15352
  } else {
15147
15353
  if (this.method === '@') {
15148
15354
  alasql.vars[this.variable] = res;