alasql 4.18.1 → 4.18.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/dist/alasql.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.18.1 build: develop-a80b2965 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.18.3 build: develop-a80b2965 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.18.1
4
+ @version 4.18.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.18.1';
144
+ alasql.version = '4.18.3';
145
145
  alasql.build = 'develop-a80b2965';
146
146
 
147
147
  /**
@@ -3003,7 +3003,7 @@ next:function () {
3003
3003
 
3004
3004
  // return next match that has a token
3005
3005
  lex:function lex () {
3006
- var r = this.next();
3006
+ var r = this.next();if(r&&/^(N?['"]__proto__['"]|\[__proto__\]|`__proto__`|__proto__)$/i.test(this.yytext))throw new Error("__proto__ is not allowed");
3007
3007
  if (r) {
3008
3008
  return r;
3009
3009
  } else {
@@ -5100,9 +5100,202 @@ alasql.parser.parseError = function (str, hash) {
5100
5100
  }
5101
5101
  */
5102
5102
  alasql.parse = function (sql) {
5103
- return alasqlparser.parse(alasql.utils.uncomment(sql));
5103
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
5104
+ checkSqlNames(ast);
5105
+ return ast;
5104
5106
  };
5105
5107
 
5108
+ /*
5109
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
5110
+ holds a quote, a backslash or a line break would end the string and make the rest code
5111
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
5112
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
5113
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
5114
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
5115
+ of a JSON value are data, not names.
5116
+ */
5117
+ var checkSqlNames = (function () {
5118
+ var hasOwn = Object.prototype.hasOwnProperty;
5119
+ var badName = /['\\\n\r\u2028\u2029]/;
5120
+ var nameKeys = {
5121
+ as: 1,
5122
+ columnid: 1,
5123
+ tableid: 1,
5124
+ databaseid: 1,
5125
+ viewid: 1,
5126
+ indexid: 1,
5127
+ trigger: 1,
5128
+ variable: 1,
5129
+ dropcolumn: 1,
5130
+ renamecolumn: 1,
5131
+ renameto: 1,
5132
+ to: 1,
5133
+ tocolumnid: 1,
5134
+ forcolumnid: 1,
5135
+ };
5136
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
5137
+
5138
+ function checkName(name) {
5139
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
5140
+ }
5141
+
5142
+ // ORDER BY: a quoted string is a column reference too. A name or string that equals an alias of
5143
+ // the select list is that alias and is allowed (the code escapes it), anything else is checked.
5144
+ function checkOrder(columns, order) {
5145
+ var aliases = {};
5146
+ (columns || []).forEach(function (col) {
5147
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
5148
+ });
5149
+ order.forEach(function (ord) {
5150
+ var e = ord && ord.expression;
5151
+ var name;
5152
+ if (e instanceof alasql.yy.StringValue) name = e.value;
5153
+ else if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
5154
+ if (typeof name === 'string') {
5155
+ if (!hasOwn.call(aliases, name)) checkName(name);
5156
+ return;
5157
+ }
5158
+ checkNames(ord, false, false);
5159
+ });
5160
+ }
5161
+
5162
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
5163
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
5164
+ // graph item, name is data.
5165
+ function entryOf(node, key) {
5166
+ var yy = alasql.yy;
5167
+ if (
5168
+ (node instanceof yy.Select && key === 'columns') ||
5169
+ (node instanceof yy.CreateGraph && key === 'graph') ||
5170
+ (node.srchid === 'RETURN' && key === 'args')
5171
+ )
5172
+ return 'alias';
5173
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
5174
+ return '';
5175
+ }
5176
+
5177
+ // Names that never reach generated code: they are used only at run time, as the key of a
5178
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
5179
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
5180
+ // names are free (what they hold is still walked), and skipChild are children that are not
5181
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
5182
+ var varValueFree = true;
5183
+ var intoVarFree = true;
5184
+ var freeKeys = [
5185
+ ['CreateIndex', {indexid: 1}],
5186
+ ['DropIndex', {indexid: 1}],
5187
+ ['Reindex', {indexid: 1}],
5188
+ ['CreateTrigger', {trigger: 1}],
5189
+ ['DropTrigger', {trigger: 1}],
5190
+ ['CreateDatabase', {databaseid: 1, as: 1}],
5191
+ ['AttachDatabase', {databaseid: 1, as: 1}],
5192
+ ['DetachDatabase', {databaseid: 1}],
5193
+ ['DropDatabase', {databaseid: 1}],
5194
+ ['ShowTables', {databaseid: 1}],
5195
+ ['ShowColumns', {databaseid: 1}],
5196
+ ['ShowIndex', {databaseid: 1}],
5197
+ ['ShowCreateTable', {databaseid: 1}],
5198
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
5199
+ ];
5200
+ var freeChild = [
5201
+ ['CreateTable', {table: 1}],
5202
+ ['DropTable', {tables: 1}],
5203
+ ['TruncateTable', {table: 1}],
5204
+ ['AlterTable', {table: 1}],
5205
+ ['ShowColumns', {table: 1}],
5206
+ ['ShowIndex', {table: 1}],
5207
+ ['ShowCreateTable', {table: 1}],
5208
+ ['CreateIndex', {table: 1}],
5209
+ ['CreateTrigger', {table: 1}],
5210
+ ['Declare', {declares: 1}],
5211
+ ];
5212
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
5213
+
5214
+ function freeOf(list, node) {
5215
+ var yy = alasql.yy;
5216
+ for (var i = 0; i < list.length; i++) {
5217
+ var cls = yy[list[i][0]];
5218
+ if (cls && node instanceof cls) return list[i][1];
5219
+ }
5220
+ return null;
5221
+ }
5222
+
5223
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
5224
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
5225
+ function freeVar(node, key, entry) {
5226
+ var yy = alasql.yy;
5227
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
5228
+ if (entry === 'source') return false;
5229
+ if (entry === 'into') return intoVarFree;
5230
+ return varValueFree;
5231
+ }
5232
+
5233
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
5234
+ function freeSet(node, key) {
5235
+ var yy = alasql.yy;
5236
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
5237
+ }
5238
+
5239
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
5240
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
5241
+ // names never reach generated code.
5242
+ function checkNames(node, data, entry) {
5243
+ if (node === null || typeof node !== 'object') return;
5244
+ var i;
5245
+ if (Array.isArray(node)) {
5246
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
5247
+ return;
5248
+ }
5249
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
5250
+ if (data && !isNode) {
5251
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
5252
+ return;
5253
+ }
5254
+ var yy = alasql.yy;
5255
+ if (node instanceof yy.Json) {
5256
+ checkNames(node.value, true, false);
5257
+ return;
5258
+ }
5259
+ var fk = freeOf(freeKeys, node);
5260
+ var fc = freeOf(freeChild, node);
5261
+ var sc = freeOf(skipChild, node);
5262
+ for (var key in node) {
5263
+ if (!hasOwn.call(node, key)) continue;
5264
+ var value = node[key];
5265
+ if (sc && sc[key] === 1) continue;
5266
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
5267
+ checkOrder(node.columns, value);
5268
+ continue;
5269
+ }
5270
+ // the column list of a WITH entry renames the result at run time
5271
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
5272
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
5273
+ if (typeof value === 'string') {
5274
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
5275
+ continue;
5276
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
5277
+ checkName(value);
5278
+ } else if (value !== null && typeof value === 'object') {
5279
+ if (Array.isArray(value)) {
5280
+ for (i = 0; i < value.length; i++) {
5281
+ if (typeof value[i] === 'string') {
5282
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
5283
+ } else {
5284
+ checkNames(value[i], false, child || entryOf(node, key));
5285
+ }
5286
+ }
5287
+ } else {
5288
+ checkNames(value, false, child);
5289
+ }
5290
+ }
5291
+ }
5292
+ }
5293
+
5294
+ return function (ast) {
5295
+ checkNames(ast, false, false);
5296
+ };
5297
+ })();
5298
+
5106
5299
  /**
5107
5300
  List of engines of external databases
5108
5301
  @type {object}
@@ -7254,7 +7447,7 @@ function queryfn3(query) {
7254
7447
 
7255
7448
  // Prepare variables
7256
7449
  query.data = [];
7257
- query.xgroups = {};
7450
+ query.xgroups=Object.create(null);
7258
7451
  query.groups = [];
7259
7452
 
7260
7453
  // Level of Joins
@@ -8326,7 +8519,7 @@ yy.Select = class Select {
8326
8519
  var converted = {};
8327
8520
  for (var key in r) {
8328
8521
  var colDef = table.xcolumns && table.xcolumns[key];
8329
- converted[key] = alasql.utils.typeConverter(r[key], colDef ? colDef.dbtypeid : null);
8522
+ var cv=alasql.utils.typeConverter(r[key], colDef ? colDef.dbtypeid : null);if(key==="__proto__"){Object.defineProperty(converted,key,{value:cv,writable:true,enumerable:true,configurable:true})}else{converted[key]=cv}
8330
8523
  }
8331
8524
  table.data.push(converted);
8332
8525
  `;
@@ -8953,7 +9146,7 @@ yy.Select.prototype.compileFrom = function (query) {
8953
9146
  return res;
8954
9147
  };
8955
9148
  } else if (tq instanceof yy.ParamValue) {
8956
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
9149
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
8957
9150
  if (tq.array) ps += ',true';
8958
9151
  ps += ');if(cb)res=cb(res,idx,query);return res';
8959
9152
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -9164,7 +9357,7 @@ yy.Select.prototype.compileJoins = function (query) {
9164
9357
  srcwherefn: returnTrue,
9165
9358
  };
9166
9359
  const jnparam = jn.param.param;
9167
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
9360
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
9168
9361
  if (jn.array) ps += ',true';
9169
9362
  ps += '); if(cb) res=cb(res, idx, query); return res';
9170
9363
 
@@ -10189,7 +10382,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10189
10382
  if (col.columnid === '*') {
10190
10383
  if (col.func) {
10191
10384
  sp +=
10192
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10385
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10193
10386
  } else if (col.tableid) {
10194
10387
  //Copy all
10195
10388
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -10241,7 +10434,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10241
10434
  if (isMultisheetParam) {
10242
10435
  sp =
10243
10436
  'var r={};var w=p["' +
10244
- tbid +
10437
+ escapeq(tbid) +
10245
10438
  '"];' +
10246
10439
  'var cols=[' +
10247
10440
  self.columns
@@ -10252,7 +10445,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10252
10445
  '];var colas=[' +
10253
10446
  self.columns
10254
10447
  .map(function (col) {
10255
- return "'" + (col.as || col.columnid) + "'";
10448
+ return "'" + escapeq(col.as || col.columnid) + "'";
10256
10449
  })
10257
10450
  .join(',') +
10258
10451
  '];' +
@@ -10472,9 +10665,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
10472
10665
  // Use Object.keys to get column names and access by index
10473
10666
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10474
10667
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
10475
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10668
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10476
10669
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
10477
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
10670
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
10478
10671
  } else {
10479
10672
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
10480
10673
  }
@@ -10629,7 +10822,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
10629
10822
  query.groupColumns[colas] = col.nick;
10630
10823
 
10631
10824
  // if(col.as) {
10632
- s += "r['" + colas + "']=";
10825
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
10633
10826
  // // } else {
10634
10827
  // // s += 'r[\''+escapeq()+'\']=';
10635
10828
  // // };
@@ -10685,7 +10878,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10685
10878
  if (isInGroup) {
10686
10879
  // For columns in GROUP BY, use the GROUP BY column's nick if available
10687
10880
  var groupNick = (groupCol && groupCol.nick) || col.nick;
10688
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
10881
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
10689
10882
  }
10690
10883
  });
10691
10884
 
@@ -10707,7 +10900,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10707
10900
  // Use Object.keys to get column names and access by index
10708
10901
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10709
10902
  } else if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
10710
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10903
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10711
10904
  } else {
10712
10905
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
10713
10906
  }
@@ -11860,7 +12053,7 @@ yy.Over = class Over {
11860
12053
  }
11861
12054
  } else if (this.op === '!') {
11862
12055
  if (typeof this.right === 'string') {
11863
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
12056
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
11864
12057
  }
11865
12058
  // TODO - add other cases
11866
12059
  } else if (this.op === 'IS') {
@@ -12177,7 +12370,7 @@ yy.Over = class Over {
12177
12370
 
12178
12371
  toJS() {
12179
12372
  if (typeof this.param === 'string') {
12180
- return "params['" + this.param + "']";
12373
+ return "params['" + escapeq(this.param) + "']";
12181
12374
  }
12182
12375
 
12183
12376
  return 'params[' + this.param + ']';
@@ -13294,9 +13487,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
13294
13487
  }
13295
13488
  });
13296
13489
 
13490
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
13491
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
13492
+ // are escaped, so the string cannot end early.
13493
+ function escapeJSONString(s) {
13494
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
13495
+ if (c.length === 2) return c;
13496
+ if (c === '\\') return '\\\\';
13497
+ if (c === '"') return '\\"';
13498
+ if (c === '\n') return '\\n';
13499
+ if (c === '\r') return '\\r';
13500
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
13501
+ });
13502
+ }
13503
+
13297
13504
  function JSONtoJS(obj, context, tableid, defcols) {
13298
13505
  var s = '';
13299
- if (typeof obj == 'string') s = '"' + obj + '"';
13506
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
13300
13507
  else if (typeof obj == 'number') s = '(' + obj + ')';
13301
13508
  else if (typeof obj == 'boolean') s = obj;
13302
13509
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -13306,7 +13513,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
13306
13513
  } else if (!obj.toJS || obj instanceof yy.Json) {
13307
13514
  let ss = [];
13308
13515
  for (const k in obj) {
13309
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
13516
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
13310
13517
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
13311
13518
  ss.push(`${keyStr}:${valueStr}`);
13312
13519
  }
@@ -17060,22 +17267,22 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
17060
17267
  if (this.props && this.props.length > 0) {
17061
17268
  let fs;
17062
17269
  if (this.method === '@') {
17063
- fs = `alasql.vars['${this.variable}']`;
17270
+ fs = `alasql.vars[k('${this.variable}')]`;
17064
17271
  } else {
17065
- fs = `params['${this.variable}']`;
17272
+ fs = `params[k('${this.variable}')]`;
17066
17273
  }
17067
17274
  this.props.forEach(prop => {
17068
17275
  if (typeof prop === 'string') {
17069
- fs += `['${prop}']`;
17276
+ fs += `[k('${prop}')]`;
17070
17277
  } else if (typeof prop === 'number') {
17071
17278
  fs += `[${prop}]`;
17072
17279
  } else {
17073
17280
  // Assuming prop.toJS() is a method that converts prop to a JavaScript expression.
17074
- fs += `[${prop.toJS()}]`;
17281
+ fs += `[k(${prop.toJS()})]`;
17075
17282
  }
17076
17283
  });
17077
17284
 
17078
- new Function('value, params, alasql', `${fs} = value`)(res, params, alasql);
17285
+ 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);
17079
17286
  } else {
17080
17287
  if (this.method === '@') {
17081
17288
  alasql.vars[this.variable] = res;