alasql 4.19.2 → 4.19.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.19.2 build: develop-98e852ad | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.19.4 build: develop-98e852ad | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.19.2
4
+ @version 4.19.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.19.2';
144
+ alasql.version = '4.19.4';
145
145
  alasql.build = 'develop-98e852ad';
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
@@ -8330,7 +8523,7 @@ yy.Select = class Select {
8330
8523
  var converted = {};
8331
8524
  for (var key in r) {
8332
8525
  var colDef = table.xcolumns && table.xcolumns[key];
8333
- converted[key] = alasql.utils.typeConverter(r[key], colDef ? colDef.dbtypeid : null);
8526
+ 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}
8334
8527
  }
8335
8528
  table.data.push(converted);
8336
8529
  `;
@@ -8957,7 +9150,7 @@ yy.Select.prototype.compileFrom = function (query) {
8957
9150
  return res;
8958
9151
  };
8959
9152
  } else if (tq instanceof yy.ParamValue) {
8960
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
9153
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
8961
9154
  if (tq.array) ps += ',true';
8962
9155
  ps += ');if(cb)res=cb(res,idx,query);return res';
8963
9156
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -9168,7 +9361,7 @@ yy.Select.prototype.compileJoins = function (query) {
9168
9361
  srcwherefn: returnTrue,
9169
9362
  };
9170
9363
  const jnparam = jn.param.param;
9171
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
9364
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
9172
9365
  if (jn.array) ps += ',true';
9173
9366
  ps += '); if(cb) res=cb(res, idx, query); return res';
9174
9367
 
@@ -10193,7 +10386,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10193
10386
  if (col.columnid === '*') {
10194
10387
  if (col.func) {
10195
10388
  sp +=
10196
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10389
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10197
10390
  } else if (col.tableid) {
10198
10391
  //Copy all
10199
10392
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -10245,7 +10438,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10245
10438
  if (isMultisheetParam) {
10246
10439
  sp =
10247
10440
  'var r={};var w=p["' +
10248
- tbid +
10441
+ escapeq(tbid) +
10249
10442
  '"];' +
10250
10443
  'var cols=[' +
10251
10444
  self.columns
@@ -10256,7 +10449,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10256
10449
  '];var colas=[' +
10257
10450
  self.columns
10258
10451
  .map(function (col) {
10259
- return "'" + (col.as || col.columnid) + "'";
10452
+ return "'" + escapeq(col.as || col.columnid) + "'";
10260
10453
  })
10261
10454
  .join(',') +
10262
10455
  '];' +
@@ -10476,9 +10669,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
10476
10669
  // Use Object.keys to get column names and access by index
10477
10670
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10478
10671
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
10479
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10672
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10480
10673
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
10481
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
10674
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
10482
10675
  } else {
10483
10676
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
10484
10677
  }
@@ -10633,7 +10826,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
10633
10826
  query.groupColumns[colas] = col.nick;
10634
10827
 
10635
10828
  // if(col.as) {
10636
- s += "r['" + colas + "']=";
10829
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
10637
10830
  // // } else {
10638
10831
  // // s += 'r[\''+escapeq()+'\']=';
10639
10832
  // // };
@@ -10727,7 +10920,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10727
10920
  if (isInGroup) {
10728
10921
  // For columns in GROUP BY, use the GROUP BY column's nick if available
10729
10922
  var groupNick = (groupCol && groupCol.nick) || col.nick;
10730
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
10923
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
10731
10924
  }
10732
10925
  });
10733
10926
 
@@ -10743,9 +10936,9 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10743
10936
  // Use Object.keys to get column names and access by index
10744
10937
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10745
10938
  } else if (v instanceof yy.Column && groupProjectedColumnMap[groupKey]) {
10746
- s += "r['" + key + "']=r['" + groupProjectedColumnMap[groupKey] + "'];";
10939
+ s += "r['" + key + "']=r['" + escapeq(groupProjectedColumnMap[groupKey]) + "'];";
10747
10940
  } else if (v instanceof yy.Column && projectedSelectColumnMap[v.columnid]) {
10748
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10941
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10749
10942
  } else if (v instanceof yy.Column && groupColMap[groupKey]) {
10750
10943
  s += "r['" + key + "']=g['" + groupColMap[groupKey].nick + "'];";
10751
10944
  } else {
@@ -11900,7 +12093,7 @@ yy.Over = class Over {
11900
12093
  }
11901
12094
  } else if (this.op === '!') {
11902
12095
  if (typeof this.right === 'string') {
11903
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
12096
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
11904
12097
  }
11905
12098
  // TODO - add other cases
11906
12099
  } else if (this.op === 'IS') {
@@ -12217,7 +12410,7 @@ yy.Over = class Over {
12217
12410
 
12218
12411
  toJS() {
12219
12412
  if (typeof this.param === 'string') {
12220
- return "params['" + this.param + "']";
12413
+ return "params['" + escapeq(this.param) + "']";
12221
12414
  }
12222
12415
 
12223
12416
  return 'params[' + this.param + ']';
@@ -13381,9 +13574,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
13381
13574
  }
13382
13575
  });
13383
13576
 
13577
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
13578
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
13579
+ // are escaped, so the string cannot end early.
13580
+ function escapeJSONString(s) {
13581
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
13582
+ if (c.length === 2) return c;
13583
+ if (c === '\\') return '\\\\';
13584
+ if (c === '"') return '\\"';
13585
+ if (c === '\n') return '\\n';
13586
+ if (c === '\r') return '\\r';
13587
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
13588
+ });
13589
+ }
13590
+
13384
13591
  function JSONtoJS(obj, context, tableid, defcols) {
13385
13592
  var s = '';
13386
- if (typeof obj == 'string') s = '"' + obj + '"';
13593
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
13387
13594
  else if (typeof obj == 'number') s = '(' + obj + ')';
13388
13595
  else if (typeof obj == 'boolean') s = obj;
13389
13596
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -13393,7 +13600,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
13393
13600
  } else if (!obj.toJS || obj instanceof yy.Json) {
13394
13601
  let ss = [];
13395
13602
  for (const k in obj) {
13396
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
13603
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
13397
13604
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
13398
13605
  ss.push(`${keyStr}:${valueStr}`);
13399
13606
  }
@@ -17148,22 +17355,22 @@ yy.SetVariable.prototype.execute = function (databaseid, params, cb) {
17148
17355
  if (this.props && this.props.length > 0) {
17149
17356
  let fs;
17150
17357
  if (this.method === '@') {
17151
- fs = `alasql.vars['${this.variable}']`;
17358
+ fs = `alasql.vars[k('${this.variable}')]`;
17152
17359
  } else {
17153
- fs = `params['${this.variable}']`;
17360
+ fs = `params[k('${this.variable}')]`;
17154
17361
  }
17155
17362
  this.props.forEach(prop => {
17156
17363
  if (typeof prop === 'string') {
17157
- fs += `['${prop}']`;
17364
+ fs += `[k('${prop}')]`;
17158
17365
  } else if (typeof prop === 'number') {
17159
17366
  fs += `[${prop}]`;
17160
17367
  } else {
17161
17368
  // Assuming prop.toJS() is a method that converts prop to a JavaScript expression.
17162
- fs += `[${prop.toJS()}]`;
17369
+ fs += `[k(${prop.toJS()})]`;
17163
17370
  }
17164
17371
  });
17165
17372
 
17166
- new Function('value, params, alasql', `${fs} = value`)(res, params, alasql);
17373
+ 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);
17167
17374
  } else {
17168
17375
  if (this.method === '@') {
17169
17376
  alasql.vars[this.variable] = res;