alasql 4.17.5 → 4.17.6

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.17.5 build: develop-f960d23a | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.17.6 build: develop-f960d23a | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.17.5
4
+ @version 4.17.6
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.17.5';
144
+ alasql.version = '4.17.6';
145
145
  alasql.build = 'develop-f960d23a';
146
146
 
147
147
  /**
@@ -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}
@@ -8919,7 +9112,7 @@ yy.Select.prototype.compileFrom = function (query) {
8919
9112
  return res;
8920
9113
  };
8921
9114
  } else if (tq instanceof yy.ParamValue) {
8922
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
9115
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
8923
9116
  if (tq.array) ps += ',true';
8924
9117
  ps += ');if(cb)res=cb(res,idx,query);return res';
8925
9118
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -9130,7 +9323,7 @@ yy.Select.prototype.compileJoins = function (query) {
9130
9323
  srcwherefn: returnTrue,
9131
9324
  };
9132
9325
  const jnparam = jn.param.param;
9133
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
9326
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
9134
9327
  if (jn.array) ps += ',true';
9135
9328
  ps += '); if(cb) res=cb(res, idx, query); return res';
9136
9329
 
@@ -10154,7 +10347,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10154
10347
  if (col.columnid === '*') {
10155
10348
  if (col.func) {
10156
10349
  sp +=
10157
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10350
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10158
10351
  } else if (col.tableid) {
10159
10352
  //Copy all
10160
10353
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -10206,7 +10399,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10206
10399
  if (isMultisheetParam) {
10207
10400
  sp =
10208
10401
  'var r={};var w=p["' +
10209
- tbid +
10402
+ escapeq(tbid) +
10210
10403
  '"];' +
10211
10404
  'var cols=[' +
10212
10405
  self.columns
@@ -10217,7 +10410,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10217
10410
  '];var colas=[' +
10218
10411
  self.columns
10219
10412
  .map(function (col) {
10220
- return "'" + (col.as || col.columnid) + "'";
10413
+ return "'" + escapeq(col.as || col.columnid) + "'";
10221
10414
  })
10222
10415
  .join(',') +
10223
10416
  '];' +
@@ -10437,9 +10630,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
10437
10630
  // Use Object.keys to get column names and access by index
10438
10631
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10439
10632
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
10440
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10633
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10441
10634
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
10442
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
10635
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
10443
10636
  } else {
10444
10637
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
10445
10638
  }
@@ -10594,7 +10787,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
10594
10787
  query.groupColumns[colas] = col.nick;
10595
10788
 
10596
10789
  // if(col.as) {
10597
- s += "r['" + colas + "']=";
10790
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
10598
10791
  // // } else {
10599
10792
  // // s += 'r[\''+escapeq()+'\']=';
10600
10793
  // // };
@@ -10645,7 +10838,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10645
10838
  if (isInGroup) {
10646
10839
  // For columns in GROUP BY, use the GROUP BY column's nick if available
10647
10840
  var groupNick = (groupCol && groupCol.nick) || col.nick;
10648
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
10841
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
10649
10842
  }
10650
10843
  });
10651
10844
 
@@ -10667,7 +10860,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10667
10860
  // Use Object.keys to get column names and access by index
10668
10861
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10669
10862
  } else if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
10670
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10863
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10671
10864
  } else {
10672
10865
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
10673
10866
  }
@@ -11820,7 +12013,7 @@ yy.Over = class Over {
11820
12013
  }
11821
12014
  } else if (this.op === '!') {
11822
12015
  if (typeof this.right === 'string') {
11823
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
12016
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
11824
12017
  }
11825
12018
  // TODO - add other cases
11826
12019
  } else if (this.op === 'IS') {
@@ -12137,7 +12330,7 @@ yy.Over = class Over {
12137
12330
 
12138
12331
  toJS() {
12139
12332
  if (typeof this.param === 'string') {
12140
- return "params['" + this.param + "']";
12333
+ return "params['" + escapeq(this.param) + "']";
12141
12334
  }
12142
12335
 
12143
12336
  return 'params[' + this.param + ']';
@@ -13254,9 +13447,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
13254
13447
  }
13255
13448
  });
13256
13449
 
13450
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
13451
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
13452
+ // are escaped, so the string cannot end early.
13453
+ function escapeJSONString(s) {
13454
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
13455
+ if (c.length === 2) return c;
13456
+ if (c === '\\') return '\\\\';
13457
+ if (c === '"') return '\\"';
13458
+ if (c === '\n') return '\\n';
13459
+ if (c === '\r') return '\\r';
13460
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
13461
+ });
13462
+ }
13463
+
13257
13464
  function JSONtoJS(obj, context, tableid, defcols) {
13258
13465
  var s = '';
13259
- if (typeof obj == 'string') s = '"' + obj + '"';
13466
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
13260
13467
  else if (typeof obj == 'number') s = '(' + obj + ')';
13261
13468
  else if (typeof obj == 'boolean') s = obj;
13262
13469
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -13266,7 +13473,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
13266
13473
  } else if (!obj.toJS || obj instanceof yy.Json) {
13267
13474
  let ss = [];
13268
13475
  for (const k in obj) {
13269
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
13476
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
13270
13477
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
13271
13478
  ss.push(`${keyStr}:${valueStr}`);
13272
13479
  }