alasql 4.20.3 → 4.20.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.20.3 build: develop-a98910ff | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.20.4 build: develop-a98910ff | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.20.3
4
+ @version 4.20.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.20.3';
144
+ alasql.version = '4.20.4';
145
145
  alasql.build = 'develop-a98910ff';
146
146
 
147
147
  /**
@@ -5112,9 +5112,202 @@ alasql.parser.parseError = function (str, hash) {
5112
5112
  }
5113
5113
  */
5114
5114
  alasql.parse = function (sql) {
5115
- return alasqlparser.parse(alasql.utils.uncomment(sql));
5115
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
5116
+ checkSqlNames(ast);
5117
+ return ast;
5116
5118
  };
5117
5119
 
5120
+ /*
5121
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
5122
+ holds a quote, a backslash or a line break would end the string and make the rest code
5123
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
5124
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
5125
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
5126
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
5127
+ of a JSON value are data, not names.
5128
+ */
5129
+ var checkSqlNames = (function () {
5130
+ var hasOwn = Object.prototype.hasOwnProperty;
5131
+ var badName = /['\\\n\r\u2028\u2029]/;
5132
+ var nameKeys = {
5133
+ as: 1,
5134
+ columnid: 1,
5135
+ tableid: 1,
5136
+ databaseid: 1,
5137
+ viewid: 1,
5138
+ indexid: 1,
5139
+ trigger: 1,
5140
+ variable: 1,
5141
+ dropcolumn: 1,
5142
+ renamecolumn: 1,
5143
+ renameto: 1,
5144
+ to: 1,
5145
+ tocolumnid: 1,
5146
+ forcolumnid: 1,
5147
+ };
5148
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
5149
+
5150
+ function checkName(name) {
5151
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
5152
+ }
5153
+
5154
+ // ORDER BY: a quoted string is a column reference too. A name or string that equals an alias of
5155
+ // the select list is that alias and is allowed (the code escapes it), anything else is checked.
5156
+ function checkOrder(columns, order) {
5157
+ var aliases = {};
5158
+ (columns || []).forEach(function (col) {
5159
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
5160
+ });
5161
+ order.forEach(function (ord) {
5162
+ var e = ord && ord.expression;
5163
+ var name;
5164
+ if (e instanceof alasql.yy.StringValue) name = e.value;
5165
+ else if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
5166
+ if (typeof name === 'string') {
5167
+ if (!hasOwn.call(aliases, name)) checkName(name);
5168
+ return;
5169
+ }
5170
+ checkNames(ord, false, false);
5171
+ });
5172
+ }
5173
+
5174
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
5175
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
5176
+ // graph item, name is data.
5177
+ function entryOf(node, key) {
5178
+ var yy = alasql.yy;
5179
+ if (
5180
+ (node instanceof yy.Select && key === 'columns') ||
5181
+ (node instanceof yy.CreateGraph && key === 'graph') ||
5182
+ (node.srchid === 'RETURN' && key === 'args')
5183
+ )
5184
+ return 'alias';
5185
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
5186
+ return '';
5187
+ }
5188
+
5189
+ // Names that never reach generated code: they are used only at run time, as the key of a
5190
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
5191
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
5192
+ // names are free (what they hold is still walked), and skipChild are children that are not
5193
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
5194
+ var varValueFree = true;
5195
+ var intoVarFree = true;
5196
+ var freeKeys = [
5197
+ ['CreateIndex', {indexid: 1}],
5198
+ ['DropIndex', {indexid: 1}],
5199
+ ['Reindex', {indexid: 1}],
5200
+ ['CreateTrigger', {trigger: 1}],
5201
+ ['DropTrigger', {trigger: 1}],
5202
+ ['CreateDatabase', {databaseid: 1, as: 1}],
5203
+ ['AttachDatabase', {databaseid: 1, as: 1}],
5204
+ ['DetachDatabase', {databaseid: 1}],
5205
+ ['DropDatabase', {databaseid: 1}],
5206
+ ['ShowTables', {databaseid: 1}],
5207
+ ['ShowColumns', {databaseid: 1}],
5208
+ ['ShowIndex', {databaseid: 1}],
5209
+ ['ShowCreateTable', {databaseid: 1}],
5210
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
5211
+ ];
5212
+ var freeChild = [
5213
+ ['CreateTable', {table: 1}],
5214
+ ['DropTable', {tables: 1}],
5215
+ ['TruncateTable', {table: 1}],
5216
+ ['AlterTable', {table: 1}],
5217
+ ['ShowColumns', {table: 1}],
5218
+ ['ShowIndex', {table: 1}],
5219
+ ['ShowCreateTable', {table: 1}],
5220
+ ['CreateIndex', {table: 1}],
5221
+ ['CreateTrigger', {table: 1}],
5222
+ ['Declare', {declares: 1}],
5223
+ ];
5224
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
5225
+
5226
+ function freeOf(list, node) {
5227
+ var yy = alasql.yy;
5228
+ for (var i = 0; i < list.length; i++) {
5229
+ var cls = yy[list[i][0]];
5230
+ if (cls && node instanceof cls) return list[i][1];
5231
+ }
5232
+ return null;
5233
+ }
5234
+
5235
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
5236
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
5237
+ function freeVar(node, key, entry) {
5238
+ var yy = alasql.yy;
5239
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
5240
+ if (entry === 'source') return false;
5241
+ if (entry === 'into') return intoVarFree;
5242
+ return varValueFree;
5243
+ }
5244
+
5245
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
5246
+ function freeSet(node, key) {
5247
+ var yy = alasql.yy;
5248
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
5249
+ }
5250
+
5251
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
5252
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
5253
+ // names never reach generated code.
5254
+ function checkNames(node, data, entry) {
5255
+ if (node === null || typeof node !== 'object') return;
5256
+ var i;
5257
+ if (Array.isArray(node)) {
5258
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
5259
+ return;
5260
+ }
5261
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
5262
+ if (data && !isNode) {
5263
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
5264
+ return;
5265
+ }
5266
+ var yy = alasql.yy;
5267
+ if (node instanceof yy.Json) {
5268
+ checkNames(node.value, true, false);
5269
+ return;
5270
+ }
5271
+ var fk = freeOf(freeKeys, node);
5272
+ var fc = freeOf(freeChild, node);
5273
+ var sc = freeOf(skipChild, node);
5274
+ for (var key in node) {
5275
+ if (!hasOwn.call(node, key)) continue;
5276
+ var value = node[key];
5277
+ if (sc && sc[key] === 1) continue;
5278
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
5279
+ checkOrder(node.columns, value);
5280
+ continue;
5281
+ }
5282
+ // the column list of a WITH entry renames the result at run time
5283
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
5284
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
5285
+ if (typeof value === 'string') {
5286
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
5287
+ continue;
5288
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
5289
+ checkName(value);
5290
+ } else if (value !== null && typeof value === 'object') {
5291
+ if (Array.isArray(value)) {
5292
+ for (i = 0; i < value.length; i++) {
5293
+ if (typeof value[i] === 'string') {
5294
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
5295
+ } else {
5296
+ checkNames(value[i], false, child || entryOf(node, key));
5297
+ }
5298
+ }
5299
+ } else {
5300
+ checkNames(value, false, child);
5301
+ }
5302
+ }
5303
+ }
5304
+ }
5305
+
5306
+ return function (ast) {
5307
+ checkNames(ast, false, false);
5308
+ };
5309
+ })();
5310
+
5118
5311
  /**
5119
5312
  List of engines of external databases
5120
5313
  @type {object}
@@ -8979,7 +9172,7 @@ yy.Select.prototype.compileFrom = function (query) {
8979
9172
  return res;
8980
9173
  };
8981
9174
  } else if (tq instanceof yy.ParamValue) {
8982
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
9175
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
8983
9176
  if (tq.array) ps += ',true';
8984
9177
  ps += ');if(cb)res=cb(res,idx,query);return res';
8985
9178
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -9190,7 +9383,7 @@ yy.Select.prototype.compileJoins = function (query) {
9190
9383
  srcwherefn: returnTrue,
9191
9384
  };
9192
9385
  const jnparam = jn.param.param;
9193
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
9386
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
9194
9387
  if (jn.array) ps += ',true';
9195
9388
  ps += '); if(cb) res=cb(res, idx, query); return res';
9196
9389
 
@@ -10215,7 +10408,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10215
10408
  if (col.columnid === '*') {
10216
10409
  if (col.func) {
10217
10410
  sp +=
10218
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10411
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10219
10412
  } else if (col.tableid) {
10220
10413
  //Copy all
10221
10414
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -10267,7 +10460,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10267
10460
  if (isMultisheetParam) {
10268
10461
  sp =
10269
10462
  'var r={};var w=p["' +
10270
- tbid +
10463
+ escapeq(tbid) +
10271
10464
  '"];' +
10272
10465
  'var cols=[' +
10273
10466
  self.columns
@@ -10278,7 +10471,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10278
10471
  '];var colas=[' +
10279
10472
  self.columns
10280
10473
  .map(function (col) {
10281
- return "'" + (col.as || col.columnid) + "'";
10474
+ return "'" + escapeq(col.as || col.columnid) + "'";
10282
10475
  })
10283
10476
  .join(',') +
10284
10477
  '];' +
@@ -10498,9 +10691,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
10498
10691
  // Use Object.keys to get column names and access by index
10499
10692
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10500
10693
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
10501
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10694
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10502
10695
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
10503
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
10696
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
10504
10697
  } else {
10505
10698
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
10506
10699
  }
@@ -10655,7 +10848,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
10655
10848
  query.groupColumns[colas] = col.nick;
10656
10849
 
10657
10850
  // if(col.as) {
10658
- s += "r['" + colas + "']=";
10851
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
10659
10852
  // // } else {
10660
10853
  // // s += 'r[\''+escapeq()+'\']=';
10661
10854
  // // };
@@ -10749,7 +10942,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10749
10942
  if (isInGroup) {
10750
10943
  // For columns in GROUP BY, use the GROUP BY column's nick if available
10751
10944
  var groupNick = (groupCol && groupCol.nick) || col.nick;
10752
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
10945
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
10753
10946
  }
10754
10947
  });
10755
10948
 
@@ -10765,9 +10958,9 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10765
10958
  // Use Object.keys to get column names and access by index
10766
10959
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10767
10960
  } else if (v instanceof yy.Column && groupProjectedColumnMap[groupKey]) {
10768
- s += "r['" + key + "']=r['" + groupProjectedColumnMap[groupKey] + "'];";
10961
+ s += "r['" + key + "']=r['" + escapeq(groupProjectedColumnMap[groupKey]) + "'];";
10769
10962
  } else if (v instanceof yy.Column && projectedSelectColumnMap[v.columnid]) {
10770
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10963
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10771
10964
  } else if (v instanceof yy.Column && groupColMap[groupKey]) {
10772
10965
  s += "r['" + key + "']=g['" + groupColMap[groupKey].nick + "'];";
10773
10966
  } else {
@@ -11922,7 +12115,7 @@ yy.Over = class Over {
11922
12115
  }
11923
12116
  } else if (this.op === '!') {
11924
12117
  if (typeof this.right === 'string') {
11925
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
12118
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
11926
12119
  }
11927
12120
  // TODO - add other cases
11928
12121
  } else if (this.op === 'IS') {
@@ -12239,7 +12432,7 @@ yy.Over = class Over {
12239
12432
 
12240
12433
  toJS() {
12241
12434
  if (typeof this.param === 'string') {
12242
- return "params['" + this.param + "']";
12435
+ return "params['" + escapeq(this.param) + "']";
12243
12436
  }
12244
12437
 
12245
12438
  return 'params[' + this.param + ']';
@@ -13403,9 +13596,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
13403
13596
  }
13404
13597
  });
13405
13598
 
13599
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
13600
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
13601
+ // are escaped, so the string cannot end early.
13602
+ function escapeJSONString(s) {
13603
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
13604
+ if (c.length === 2) return c;
13605
+ if (c === '\\') return '\\\\';
13606
+ if (c === '"') return '\\"';
13607
+ if (c === '\n') return '\\n';
13608
+ if (c === '\r') return '\\r';
13609
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
13610
+ });
13611
+ }
13612
+
13406
13613
  function JSONtoJS(obj, context, tableid, defcols) {
13407
13614
  var s = '';
13408
- if (typeof obj == 'string') s = '"' + obj + '"';
13615
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
13409
13616
  else if (typeof obj == 'number') s = '(' + obj + ')';
13410
13617
  else if (typeof obj == 'boolean') s = obj;
13411
13618
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -13415,7 +13622,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
13415
13622
  } else if (!obj.toJS || obj instanceof yy.Json) {
13416
13623
  let ss = [];
13417
13624
  for (const k in obj) {
13418
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
13625
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
13419
13626
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
13420
13627
  ss.push(`${keyStr}:${valueStr}`);
13421
13628
  }