alasql 4.22.3 → 4.22.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.22.3 build: hotfix.v4.22.3-303856d6 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.22.4 build: hotfix.v4.22.4-eda2f684 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.22.3
4
+ @version 4.22.4
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2025 Andrey Gershun & Mathias Wulff
@@ -141,8 +141,8 @@ SOFTWARE.
141
141
  Current version of alasql
142
142
  @constant {string}
143
143
  */
144
- alasql.version = '4.22.3';
145
- alasql.build = 'hotfix.v4.22.3-303856d6';
144
+ alasql.version = '4.22.4';
145
+ alasql.build = 'hotfix.v4.22.4-eda2f684';
146
146
 
147
147
  /**
148
148
  Debug flag
@@ -5225,9 +5225,217 @@ alasql.parser.parseError = function (str, hash) {
5225
5225
  }
5226
5226
  */
5227
5227
  alasql.parse = function (sql) {
5228
- return alasqlparser.parse(alasql.utils.uncomment(sql));
5228
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
5229
+ checkSqlNames(ast);
5230
+ return ast;
5229
5231
  };
5230
5232
 
5233
+ /*
5234
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
5235
+ holds a quote, a backslash or a line break would end the string and make the rest code
5236
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
5237
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
5238
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
5239
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
5240
+ of a JSON value are data, not names.
5241
+ */
5242
+ var checkSqlNames = (function () {
5243
+ var hasOwn = Object.prototype.hasOwnProperty;
5244
+ var badName = /['\\\n\r\u2028\u2029]/;
5245
+ var nameKeys = {
5246
+ as: 1,
5247
+ columnid: 1,
5248
+ tableid: 1,
5249
+ databaseid: 1,
5250
+ viewid: 1,
5251
+ indexid: 1,
5252
+ trigger: 1,
5253
+ variable: 1,
5254
+ dropcolumn: 1,
5255
+ renamecolumn: 1,
5256
+ renameto: 1,
5257
+ to: 1,
5258
+ tocolumnid: 1,
5259
+ forcolumnid: 1,
5260
+ };
5261
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
5262
+
5263
+ function checkName(name) {
5264
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
5265
+ }
5266
+
5267
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
5268
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
5269
+ function checkOrder(columns, order) {
5270
+ var aliases = {};
5271
+ (columns || []).forEach(function (col) {
5272
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
5273
+ });
5274
+ order.forEach(function (ord) {
5275
+ var e = ord && ord.expression;
5276
+ var name;
5277
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
5278
+ if (typeof name === 'string') {
5279
+ if (!hasOwn.call(aliases, name)) checkName(name);
5280
+ return;
5281
+ }
5282
+ checkNames(ord, false, false);
5283
+ });
5284
+ }
5285
+
5286
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
5287
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
5288
+ // graph item, name is data.
5289
+ function entryOf(node, key) {
5290
+ var yy = alasql.yy;
5291
+ if (
5292
+ (node instanceof yy.Select && key === 'columns') ||
5293
+ (node instanceof yy.CreateGraph && key === 'graph') ||
5294
+ (node.srchid === 'RETURN' && key === 'args')
5295
+ )
5296
+ return 'alias';
5297
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
5298
+ return '';
5299
+ }
5300
+
5301
+ // Names that never reach generated code: they are used only at run time, as the key of a
5302
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
5303
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
5304
+ // names are free (what they hold is still walked), and skipChild are children that are not
5305
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
5306
+ var varValueFree = true;
5307
+ var intoVarFree = true;
5308
+ var freeKeys = [
5309
+ ['CreateIndex', {indexid: 1}],
5310
+ ['DropIndex', {indexid: 1}],
5311
+ ['Reindex', {indexid: 1}],
5312
+ ['CreateTrigger', {trigger: 1}],
5313
+ ['DropTrigger', {trigger: 1}],
5314
+ ['CreateDatabase', {databaseid: 1, as: 1}],
5315
+ ['AttachDatabase', {databaseid: 1, as: 1}],
5316
+ ['DetachDatabase', {databaseid: 1}],
5317
+ ['DropDatabase', {databaseid: 1}],
5318
+ ['ShowTables', {databaseid: 1}],
5319
+ ['ShowColumns', {databaseid: 1}],
5320
+ ['ShowIndex', {databaseid: 1}],
5321
+ ['ShowCreateTable', {databaseid: 1}],
5322
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
5323
+ ];
5324
+ var freeChild = [
5325
+ ['CreateTable', {table: 1}],
5326
+ ['DropTable', {tables: 1}],
5327
+ ['TruncateTable', {table: 1}],
5328
+ ['AlterTable', {table: 1}],
5329
+ ['ShowColumns', {table: 1}],
5330
+ ['ShowIndex', {table: 1}],
5331
+ ['ShowCreateTable', {table: 1}],
5332
+ ['CreateIndex', {table: 1}],
5333
+ ['CreateTrigger', {table: 1}],
5334
+ ['Declare', {declares: 1}],
5335
+ ];
5336
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
5337
+
5338
+ function freeOf(list, node) {
5339
+ var yy = alasql.yy;
5340
+ for (var i = 0; i < list.length; i++) {
5341
+ var cls = yy[list[i][0]];
5342
+ if (cls && node instanceof cls) return list[i][1];
5343
+ }
5344
+ return null;
5345
+ }
5346
+
5347
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
5348
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
5349
+ function freeVar(node, key, entry) {
5350
+ var yy = alasql.yy;
5351
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
5352
+ if (entry === 'source') return false;
5353
+ if (entry === 'into') return intoVarFree;
5354
+ return varValueFree;
5355
+ }
5356
+
5357
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
5358
+ function freeSet(node, key) {
5359
+ var yy = alasql.yy;
5360
+ return (
5361
+ key === 'variable' &&
5362
+ yy.SetVariable &&
5363
+ node instanceof yy.SetVariable &&
5364
+ !(node.props && node.props.length)
5365
+ );
5366
+ }
5367
+
5368
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
5369
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
5370
+ // names never reach generated code.
5371
+ function checkNames(node, data, entry) {
5372
+ if (node === null || typeof node !== 'object') return;
5373
+ var i;
5374
+ if (Array.isArray(node)) {
5375
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
5376
+ return;
5377
+ }
5378
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
5379
+ if (data && !isNode) {
5380
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
5381
+ return;
5382
+ }
5383
+ var yy = alasql.yy;
5384
+ if (node instanceof yy.Json) {
5385
+ checkNames(node.value, true, false);
5386
+ return;
5387
+ }
5388
+ var fk = freeOf(freeKeys, node);
5389
+ var fc = freeOf(freeChild, node);
5390
+ var sc = freeOf(skipChild, node);
5391
+ for (var key in node) {
5392
+ if (!hasOwn.call(node, key)) continue;
5393
+ var value = node[key];
5394
+ if (sc && sc[key] === 1) continue;
5395
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
5396
+ checkOrder(node.columns, value);
5397
+ continue;
5398
+ }
5399
+ // the column list of a WITH entry renames the result at run time
5400
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
5401
+ if (!child && node instanceof yy.Select)
5402
+ child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
5403
+ if (typeof value === 'string') {
5404
+ if (
5405
+ entry === 'free' ||
5406
+ (fk && fk[key] === 1) ||
5407
+ freeSet(node, key) ||
5408
+ freeVar(node, key, entry)
5409
+ )
5410
+ continue;
5411
+ if (
5412
+ key === 'name'
5413
+ ? entry === 'with'
5414
+ : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as')
5415
+ )
5416
+ checkName(value);
5417
+ } else if (value !== null && typeof value === 'object') {
5418
+ if (Array.isArray(value)) {
5419
+ for (i = 0; i < value.length; i++) {
5420
+ if (typeof value[i] === 'string') {
5421
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free')
5422
+ checkName(value[i]);
5423
+ } else {
5424
+ checkNames(value[i], false, child || entryOf(node, key));
5425
+ }
5426
+ }
5427
+ } else {
5428
+ checkNames(value, false, child);
5429
+ }
5430
+ }
5431
+ }
5432
+ }
5433
+
5434
+ return function (ast) {
5435
+ checkNames(ast, false, false);
5436
+ };
5437
+ })();
5438
+
5231
5439
  /**
5232
5440
  List of engines of external databases
5233
5441
  @type {object}
@@ -9283,7 +9491,7 @@ yy.Select.prototype.compileFrom = function (query) {
9283
9491
  return res;
9284
9492
  };
9285
9493
  } else if (tq instanceof yy.ParamValue) {
9286
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
9494
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
9287
9495
  if (tq.array) ps += ',true';
9288
9496
  ps += ');if(cb)res=cb(res,idx,query);return res';
9289
9497
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -9488,7 +9696,7 @@ yy.Select.prototype.compileJoins = function (query) {
9488
9696
  srcwherefn: returnTrue,
9489
9697
  };
9490
9698
  const jnparam = jn.param.param;
9491
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
9699
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
9492
9700
  if (jn.array) ps += ',true';
9493
9701
  ps += '); if(cb) res=cb(res, idx, query); return res';
9494
9702
 
@@ -10646,7 +10854,11 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10646
10854
  if (col.columnid === '*') {
10647
10855
  if (col.func) {
10648
10856
  sp +=
10649
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10857
+ "r=params['" +
10858
+ escapeq(col.param) +
10859
+ "'](p['" +
10860
+ query.sources[0].alias +
10861
+ "'],p,params,alasql);";
10650
10862
  } else if (col.tableid) {
10651
10863
  //Copy all
10652
10864
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -10698,7 +10910,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10698
10910
  if (isMultisheetParam) {
10699
10911
  sp =
10700
10912
  'var r={};var w=p["' +
10701
- tbid +
10913
+ escapeq(tbid) +
10702
10914
  '"];' +
10703
10915
  'var cols=[' +
10704
10916
  self.columns
@@ -10709,7 +10921,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10709
10921
  '];var colas=[' +
10710
10922
  self.columns
10711
10923
  .map(function (col) {
10712
- return "'" + (col.as || col.columnid) + "'";
10924
+ return "'" + escapeq(col.as || col.columnid) + "'";
10713
10925
  })
10714
10926
  .join(',') +
10715
10927
  '];' +
@@ -10938,9 +11150,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
10938
11150
  // Use Object.keys to get column names and access by index
10939
11151
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10940
11152
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
10941
- s += "r['" + key + "']=r['" + v.columnid + "'];";
11153
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10942
11154
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
10943
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
11155
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
10944
11156
  } else {
10945
11157
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
10946
11158
  }
@@ -11105,7 +11317,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
11105
11317
  query.groupColumns[colas] = col.nick;
11106
11318
 
11107
11319
  // if(col.as) {
11108
- s += "r['" + colas + "']=";
11320
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
11109
11321
  // // } else {
11110
11322
  // // s += 'r[\''+escapeq()+'\']=';
11111
11323
  // // };
@@ -11199,7 +11411,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
11199
11411
  if (isInGroup) {
11200
11412
  // For columns in GROUP BY, use the GROUP BY column's nick if available
11201
11413
  var groupNick = (groupCol && groupCol.nick) || col.nick;
11202
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
11414
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
11203
11415
  }
11204
11416
  });
11205
11417
 
@@ -11215,9 +11427,9 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
11215
11427
  // Use Object.keys to get column names and access by index
11216
11428
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
11217
11429
  } else if (v instanceof yy.Column && groupProjectedColumnMap[groupKey]) {
11218
- s += "r['" + key + "']=r['" + groupProjectedColumnMap[groupKey] + "'];";
11430
+ s += "r['" + key + "']=r['" + escapeq(groupProjectedColumnMap[groupKey]) + "'];";
11219
11431
  } else if (v instanceof yy.Column && projectedSelectColumnMap[v.columnid]) {
11220
- s += "r['" + key + "']=r['" + v.columnid + "'];";
11432
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
11221
11433
  } else if (v instanceof yy.Column && groupColMap[groupKey]) {
11222
11434
  s += "r['" + key + "']=g['" + groupColMap[groupKey].nick + "'];";
11223
11435
  } else {
@@ -12399,7 +12611,7 @@ yy.Over = class Over {
12399
12611
  }
12400
12612
  } else if (this.op === '!') {
12401
12613
  if (typeof this.right === 'string') {
12402
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
12614
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
12403
12615
  }
12404
12616
  // TODO - add other cases
12405
12617
  } else if (this.op === 'IS') {
@@ -12722,7 +12934,7 @@ yy.Over = class Over {
12722
12934
 
12723
12935
  toJS() {
12724
12936
  if (typeof this.param === 'string') {
12725
- return "params['" + this.param + "']";
12937
+ return "params['" + escapeq(this.param) + "']";
12726
12938
  }
12727
12939
 
12728
12940
  return 'params[' + this.param + ']';
@@ -13911,9 +14123,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
13911
14123
  }
13912
14124
  });
13913
14125
 
14126
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
14127
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
14128
+ // are escaped, so the string cannot end early.
14129
+ function escapeJSONString(s) {
14130
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
14131
+ if (c.length === 2) return c;
14132
+ if (c === '\\') return '\\\\';
14133
+ if (c === '"') return '\\"';
14134
+ if (c === '\n') return '\\n';
14135
+ if (c === '\r') return '\\r';
14136
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
14137
+ });
14138
+ }
14139
+
13914
14140
  function JSONtoJS(obj, context, tableid, defcols) {
13915
14141
  var s = '';
13916
- if (typeof obj == 'string') s = '"' + obj + '"';
14142
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
13917
14143
  else if (typeof obj == 'number') s = '(' + obj + ')';
13918
14144
  else if (typeof obj == 'boolean') s = obj;
13919
14145
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -13923,7 +14149,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
13923
14149
  } else if (!obj.toJS || obj instanceof yy.Json) {
13924
14150
  let ss = [];
13925
14151
  for (const k in obj) {
13926
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
14152
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
13927
14153
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
13928
14154
  ss.push(`${keyStr}:${valueStr}`);
13929
14155
  }