alasql 4.11.1 → 4.11.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.11.1
5
+ // Version: 4.11.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.11.1 build: develop-bc5dc5de | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.11.3 build: develop-bc5dc5de | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.11.1
4
+ @version 4.11.3
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2025 Andrey Gershun & Mathias Wulff
package/dist/alasql.fs.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.11.1 build: develop-bc5dc5de | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.11.3 build: develop-bc5dc5de | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.11.1
4
+ @version 4.11.3
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.11.1';
144
+ alasql.version = '4.11.3';
145
145
  alasql.build = 'develop-bc5dc5de';
146
146
 
147
147
  /**
@@ -2570,7 +2570,7 @@ next:function () {
2570
2570
 
2571
2571
  // return next match that has a token
2572
2572
  lex:function lex () {
2573
- var r = this.next();
2573
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
2574
2574
  if (r) {
2575
2575
  return r;
2576
2576
  } else {
@@ -4598,9 +4598,202 @@ alasql.parser.parseError = function (str, hash) {
4598
4598
  }
4599
4599
  */
4600
4600
  alasql.parse = function (sql) {
4601
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4601
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4602
+ checkSqlNames(ast);
4603
+ return ast;
4602
4604
  };
4603
4605
 
4606
+ /*
4607
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4608
+ holds a quote, a backslash or a line break would end the string and make the rest code
4609
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4610
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4611
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4612
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4613
+ of a JSON value are data, not names.
4614
+ */
4615
+ var checkSqlNames = (function () {
4616
+ var hasOwn = Object.prototype.hasOwnProperty;
4617
+ var badName = /['\\\n\r\u2028\u2029]/;
4618
+ var nameKeys = {
4619
+ as: 1,
4620
+ columnid: 1,
4621
+ tableid: 1,
4622
+ databaseid: 1,
4623
+ viewid: 1,
4624
+ indexid: 1,
4625
+ trigger: 1,
4626
+ variable: 1,
4627
+ dropcolumn: 1,
4628
+ renamecolumn: 1,
4629
+ renameto: 1,
4630
+ to: 1,
4631
+ tocolumnid: 1,
4632
+ forcolumnid: 1,
4633
+ };
4634
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
4635
+
4636
+ function checkName(name) {
4637
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
4638
+ }
4639
+
4640
+ // ORDER BY: a quoted string is a column reference too. A name or string that equals an alias of
4641
+ // the select list is that alias and is allowed (the code escapes it), anything else is checked.
4642
+ function checkOrder(columns, order) {
4643
+ var aliases = {};
4644
+ (columns || []).forEach(function (col) {
4645
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
4646
+ });
4647
+ order.forEach(function (ord) {
4648
+ var e = ord && ord.expression;
4649
+ var name;
4650
+ if (e instanceof alasql.yy.StringValue) name = e.value;
4651
+ else if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
4652
+ if (typeof name === 'string') {
4653
+ if (!hasOwn.call(aliases, name)) checkName(name);
4654
+ return;
4655
+ }
4656
+ checkNames(ord, false, false);
4657
+ });
4658
+ }
4659
+
4660
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
4661
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
4662
+ // graph item, name is data.
4663
+ function entryOf(node, key) {
4664
+ var yy = alasql.yy;
4665
+ if (
4666
+ (node instanceof yy.Select && key === 'columns') ||
4667
+ (node instanceof yy.CreateGraph && key === 'graph') ||
4668
+ (node.srchid === 'RETURN' && key === 'args')
4669
+ )
4670
+ return 'alias';
4671
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
4672
+ return '';
4673
+ }
4674
+
4675
+ // Names that never reach generated code: they are used only at run time, as the key of a
4676
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
4677
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
4678
+ // names are free (what they hold is still walked), and skipChild are children that are not
4679
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
4680
+ var varValueFree = true;
4681
+ var intoVarFree = true;
4682
+ var freeKeys = [
4683
+ ['CreateIndex', {indexid: 1}],
4684
+ ['DropIndex', {indexid: 1}],
4685
+ ['Reindex', {indexid: 1}],
4686
+ ['CreateTrigger', {trigger: 1}],
4687
+ ['DropTrigger', {trigger: 1}],
4688
+ ['CreateDatabase', {databaseid: 1, as: 1}],
4689
+ ['AttachDatabase', {databaseid: 1, as: 1}],
4690
+ ['DetachDatabase', {databaseid: 1}],
4691
+ ['DropDatabase', {databaseid: 1}],
4692
+ ['ShowTables', {databaseid: 1}],
4693
+ ['ShowColumns', {databaseid: 1}],
4694
+ ['ShowIndex', {databaseid: 1}],
4695
+ ['ShowCreateTable', {databaseid: 1}],
4696
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
4697
+ ];
4698
+ var freeChild = [
4699
+ ['CreateTable', {table: 1}],
4700
+ ['DropTable', {tables: 1}],
4701
+ ['TruncateTable', {table: 1}],
4702
+ ['AlterTable', {table: 1}],
4703
+ ['ShowColumns', {table: 1}],
4704
+ ['ShowIndex', {table: 1}],
4705
+ ['ShowCreateTable', {table: 1}],
4706
+ ['CreateIndex', {table: 1}],
4707
+ ['CreateTrigger', {table: 1}],
4708
+ ['Declare', {declares: 1}],
4709
+ ];
4710
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
4711
+
4712
+ function freeOf(list, node) {
4713
+ var yy = alasql.yy;
4714
+ for (var i = 0; i < list.length; i++) {
4715
+ var cls = yy[list[i][0]];
4716
+ if (cls && node instanceof cls) return list[i][1];
4717
+ }
4718
+ return null;
4719
+ }
4720
+
4721
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
4722
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
4723
+ function freeVar(node, key, entry) {
4724
+ var yy = alasql.yy;
4725
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
4726
+ if (entry === 'source') return false;
4727
+ if (entry === 'into') return intoVarFree;
4728
+ return varValueFree;
4729
+ }
4730
+
4731
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
4732
+ function freeSet(node, key) {
4733
+ var yy = alasql.yy;
4734
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
4735
+ }
4736
+
4737
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
4738
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
4739
+ // names never reach generated code.
4740
+ function checkNames(node, data, entry) {
4741
+ if (node === null || typeof node !== 'object') return;
4742
+ var i;
4743
+ if (Array.isArray(node)) {
4744
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
4745
+ return;
4746
+ }
4747
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
4748
+ if (data && !isNode) {
4749
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
4750
+ return;
4751
+ }
4752
+ var yy = alasql.yy;
4753
+ if (node instanceof yy.Json) {
4754
+ checkNames(node.value, true, false);
4755
+ return;
4756
+ }
4757
+ var fk = freeOf(freeKeys, node);
4758
+ var fc = freeOf(freeChild, node);
4759
+ var sc = freeOf(skipChild, node);
4760
+ for (var key in node) {
4761
+ if (!hasOwn.call(node, key)) continue;
4762
+ var value = node[key];
4763
+ if (sc && sc[key] === 1) continue;
4764
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
4765
+ checkOrder(node.columns, value);
4766
+ continue;
4767
+ }
4768
+ // the column list of a WITH entry renames the result at run time
4769
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
4770
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
4771
+ if (typeof value === 'string') {
4772
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
4773
+ continue;
4774
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
4775
+ checkName(value);
4776
+ } else if (value !== null && typeof value === 'object') {
4777
+ if (Array.isArray(value)) {
4778
+ for (i = 0; i < value.length; i++) {
4779
+ if (typeof value[i] === 'string') {
4780
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
4781
+ } else {
4782
+ checkNames(value[i], false, child || entryOf(node, key));
4783
+ }
4784
+ }
4785
+ } else {
4786
+ checkNames(value, false, child);
4787
+ }
4788
+ }
4789
+ }
4790
+ }
4791
+
4792
+ return function (ast) {
4793
+ checkNames(ast, false, false);
4794
+ };
4795
+ })();
4796
+
4604
4797
  /**
4605
4798
  List of engines of external databases
4606
4799
  @type {object}
@@ -6688,7 +6881,7 @@ function queryfn3(query) {
6688
6881
 
6689
6882
  // Prepare variables
6690
6883
  query.data = [];
6691
- query.xgroups = {};
6884
+ query.xgroups=Object.create(null);
6692
6885
  query.groups = [];
6693
6886
 
6694
6887
  // Level of Joins
@@ -8194,7 +8387,7 @@ yy.Select.prototype.compileFrom = function (query) {
8194
8387
  return res;
8195
8388
  };
8196
8389
  } else if (tq instanceof yy.ParamValue) {
8197
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8390
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
8198
8391
  if (tq.array) ps += ',true';
8199
8392
  ps += ');if(cb)res=cb(res,idx,query);return res';
8200
8393
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8404,7 +8597,7 @@ yy.Select.prototype.compileJoins = function (query) {
8404
8597
  srcwherefn: returnTrue,
8405
8598
  };
8406
8599
  const jnparam = jn.param.param;
8407
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
8600
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8408
8601
  if (jn.array) ps += ',true';
8409
8602
  ps += '); if(cb) res=cb(res, idx, query); return res';
8410
8603
 
@@ -9320,7 +9513,7 @@ function compileSelectStar(query, aliases, joinstar) {
9320
9513
  } else {
9321
9514
 
9322
9515
  // if column not exist, then copy all
9323
- 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]};';
9516
+ 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]};';
9324
9517
 
9325
9518
  query.dirtyColumns = true;
9326
9519
  }
@@ -9374,7 +9567,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9374
9567
  if (col.columnid === '*') {
9375
9568
  if (col.func) {
9376
9569
  sp +=
9377
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9570
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9378
9571
  } else if (col.tableid) {
9379
9572
  //Copy all
9380
9573
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9426,7 +9619,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9426
9619
  if (isMultisheetParam) {
9427
9620
  sp =
9428
9621
  'var r={};var w=p["' +
9429
- tbid +
9622
+ escapeq(tbid) +
9430
9623
  '"];' +
9431
9624
  'var cols=[' +
9432
9625
  self.columns
@@ -9437,7 +9630,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9437
9630
  '];var colas=[' +
9438
9631
  self.columns
9439
9632
  .map(function (col) {
9440
- return "'" + (col.as || col.columnid) + "'";
9633
+ return "'" + escapeq(col.as || col.columnid) + "'";
9441
9634
  })
9442
9635
  .join(',') +
9443
9636
  '];' +
@@ -9650,9 +9843,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
9650
9843
  // Use Object.keys to get column names and access by index
9651
9844
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
9652
9845
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
9653
- s += "r['" + key + "']=r['" + v.columnid + "'];";
9846
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9654
9847
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
9655
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
9848
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
9656
9849
  } else {
9657
9850
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
9658
9851
  }
@@ -9759,7 +9952,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
9759
9952
  query.groupColumns[colas] = col.nick;
9760
9953
 
9761
9954
  // if(col.as) {
9762
- s += "r['" + colas + "']=";
9955
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
9763
9956
  // // } else {
9764
9957
  // // s += 'r[\''+escapeq()+'\']=';
9765
9958
  // // };
@@ -9810,7 +10003,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9810
10003
  if (isInGroup) {
9811
10004
  // For columns in GROUP BY, use the GROUP BY column's nick if available
9812
10005
  var groupNick = (groupCol && groupCol.nick) || col.nick;
9813
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
10006
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
9814
10007
  }
9815
10008
  });
9816
10009
 
@@ -9832,7 +10025,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
9832
10025
  // Use Object.keys to get column names and access by index
9833
10026
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
9834
10027
  } else if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
9835
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10028
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
9836
10029
  } else {
9837
10030
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
9838
10031
  }
@@ -10985,7 +11178,7 @@ yy.Over = class Over {
10985
11178
  }
10986
11179
  } else if (this.op === '!') {
10987
11180
  if (typeof this.right === 'string') {
10988
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
11181
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
10989
11182
  }
10990
11183
  // TODO - add other cases
10991
11184
  } else if (this.op === 'IS') {
@@ -11302,7 +11495,7 @@ yy.Over = class Over {
11302
11495
 
11303
11496
  toJS() {
11304
11497
  if (typeof this.param === 'string') {
11305
- return "params['" + this.param + "']";
11498
+ return "params['" + escapeq(this.param) + "']";
11306
11499
  }
11307
11500
 
11308
11501
  return 'params[' + this.param + ']';
@@ -12344,9 +12537,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12344
12537
  }
12345
12538
  });
12346
12539
 
12540
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
12541
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
12542
+ // are escaped, so the string cannot end early.
12543
+ function escapeJSONString(s) {
12544
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
12545
+ if (c.length === 2) return c;
12546
+ if (c === '\\') return '\\\\';
12547
+ if (c === '"') return '\\"';
12548
+ if (c === '\n') return '\\n';
12549
+ if (c === '\r') return '\\r';
12550
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
12551
+ });
12552
+ }
12553
+
12347
12554
  function JSONtoJS(obj, context, tableid, defcols) {
12348
12555
  var s = '';
12349
- if (typeof obj == 'string') s = '"' + obj + '"';
12556
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12350
12557
  else if (typeof obj == 'number') s = '(' + obj + ')';
12351
12558
  else if (typeof obj == 'boolean') s = obj;
12352
12559
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -12356,7 +12563,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12356
12563
  } else if (!obj.toJS || obj instanceof yy.Json) {
12357
12564
  let ss = [];
12358
12565
  for (const k in obj) {
12359
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
12566
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
12360
12567
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
12361
12568
  ss.push(`${keyStr}:${valueStr}`);
12362
12569
  }
@@ -15952,22 +16159,22 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
15952
16159
  if (this.props && this.props.length > 0) {
15953
16160
  let fs;
15954
16161
  if (this.method === '@') {
15955
- fs = `alasql.vars['${this.variable}']`;
16162
+ fs = `alasql.vars[k('${this.variable}')]`;
15956
16163
  } else {
15957
- fs = `params['${this.variable}']`;
16164
+ fs = `params[k('${this.variable}')]`;
15958
16165
  }
15959
16166
  this.props.forEach(prop => {
15960
16167
  if (typeof prop === 'string') {
15961
- fs += `['${prop}']`;
16168
+ fs += `[k('${prop}')]`;
15962
16169
  } else if (typeof prop === 'number') {
15963
16170
  fs += `[${prop}]`;
15964
16171
  } else {
15965
16172
  // Assuming prop.toJS() is a method that converts prop to a JavaScript expression.
15966
- fs += `[${prop.toJS()}]`;
16173
+ fs += `[k(${prop.toJS()})]`;
15967
16174
  }
15968
16175
  });
15969
16176
 
15970
- new Function('value, params, alasql', `${fs} = value`)(res, params, alasql);
16177
+ 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);
15971
16178
  } else {
15972
16179
  if (this.method === '@') {
15973
16180
  alasql.vars[this.variable] = res;