alasql 4.21.2 → 4.21.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/bin/alasql-cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  //
4
4
  // Command line interface for Alasql
5
- // Version: 4.21.2
5
+ // Version: 4.21.3
6
6
  // Date: 28.07.2015
7
7
  // (c) 2014-2022, Andrey Gershun & Mathias Wulff
8
8
  //
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.21.2 build: develop-38bf3c39 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.21.3 build: develop-38bf3c39 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.21.2
4
+ @version 4.21.3
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2025 Andrey Gershun & Mathias Wulff
package/dist/alasql.fs.js CHANGED
@@ -1,7 +1,7 @@
1
- //! AlaSQL v4.21.2 build: develop-38bf3c39 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.21.3 build: develop-38bf3c39 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.21.2
4
+ @version 4.21.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.21.2';
144
+ alasql.version = '4.21.3';
145
145
  alasql.build = 'develop-38bf3c39';
146
146
 
147
147
  /**
@@ -5155,9 +5155,202 @@ alasql.parser.parseError = function (str, hash) {
5155
5155
  }
5156
5156
  */
5157
5157
  alasql.parse = function (sql) {
5158
- return alasqlparser.parse(alasql.utils.uncomment(sql));
5158
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
5159
+ checkSqlNames(ast);
5160
+ return ast;
5159
5161
  };
5160
5162
 
5163
+ /*
5164
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
5165
+ holds a quote, a backslash or a line break would end the string and make the rest code
5166
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
5167
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
5168
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
5169
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
5170
+ of a JSON value are data, not names.
5171
+ */
5172
+ var checkSqlNames = (function () {
5173
+ var hasOwn = Object.prototype.hasOwnProperty;
5174
+ var badName = /['\\\n\r\u2028\u2029]/;
5175
+ var nameKeys = {
5176
+ as: 1,
5177
+ columnid: 1,
5178
+ tableid: 1,
5179
+ databaseid: 1,
5180
+ viewid: 1,
5181
+ indexid: 1,
5182
+ trigger: 1,
5183
+ variable: 1,
5184
+ dropcolumn: 1,
5185
+ renamecolumn: 1,
5186
+ renameto: 1,
5187
+ to: 1,
5188
+ tocolumnid: 1,
5189
+ forcolumnid: 1,
5190
+ };
5191
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
5192
+
5193
+ function checkName(name) {
5194
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
5195
+ }
5196
+
5197
+ // ORDER BY: a quoted string is a column reference too. A name or string that equals an alias of
5198
+ // the select list is that alias and is allowed (the code escapes it), anything else is checked.
5199
+ function checkOrder(columns, order) {
5200
+ var aliases = {};
5201
+ (columns || []).forEach(function (col) {
5202
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
5203
+ });
5204
+ order.forEach(function (ord) {
5205
+ var e = ord && ord.expression;
5206
+ var name;
5207
+ if (e instanceof alasql.yy.StringValue) name = e.value;
5208
+ else if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
5209
+ if (typeof name === 'string') {
5210
+ if (!hasOwn.call(aliases, name)) checkName(name);
5211
+ return;
5212
+ }
5213
+ checkNames(ord, false, false);
5214
+ });
5215
+ }
5216
+
5217
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
5218
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
5219
+ // graph item, name is data.
5220
+ function entryOf(node, key) {
5221
+ var yy = alasql.yy;
5222
+ if (
5223
+ (node instanceof yy.Select && key === 'columns') ||
5224
+ (node instanceof yy.CreateGraph && key === 'graph') ||
5225
+ (node.srchid === 'RETURN' && key === 'args')
5226
+ )
5227
+ return 'alias';
5228
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
5229
+ return '';
5230
+ }
5231
+
5232
+ // Names that never reach generated code: they are used only at run time, as the key of a
5233
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
5234
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
5235
+ // names are free (what they hold is still walked), and skipChild are children that are not
5236
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
5237
+ var varValueFree = true;
5238
+ var intoVarFree = true;
5239
+ var freeKeys = [
5240
+ ['CreateIndex', {indexid: 1}],
5241
+ ['DropIndex', {indexid: 1}],
5242
+ ['Reindex', {indexid: 1}],
5243
+ ['CreateTrigger', {trigger: 1}],
5244
+ ['DropTrigger', {trigger: 1}],
5245
+ ['CreateDatabase', {databaseid: 1, as: 1}],
5246
+ ['AttachDatabase', {databaseid: 1, as: 1}],
5247
+ ['DetachDatabase', {databaseid: 1}],
5248
+ ['DropDatabase', {databaseid: 1}],
5249
+ ['ShowTables', {databaseid: 1}],
5250
+ ['ShowColumns', {databaseid: 1}],
5251
+ ['ShowIndex', {databaseid: 1}],
5252
+ ['ShowCreateTable', {databaseid: 1}],
5253
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
5254
+ ];
5255
+ var freeChild = [
5256
+ ['CreateTable', {table: 1}],
5257
+ ['DropTable', {tables: 1}],
5258
+ ['TruncateTable', {table: 1}],
5259
+ ['AlterTable', {table: 1}],
5260
+ ['ShowColumns', {table: 1}],
5261
+ ['ShowIndex', {table: 1}],
5262
+ ['ShowCreateTable', {table: 1}],
5263
+ ['CreateIndex', {table: 1}],
5264
+ ['CreateTrigger', {table: 1}],
5265
+ ['Declare', {declares: 1}],
5266
+ ];
5267
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
5268
+
5269
+ function freeOf(list, node) {
5270
+ var yy = alasql.yy;
5271
+ for (var i = 0; i < list.length; i++) {
5272
+ var cls = yy[list[i][0]];
5273
+ if (cls && node instanceof cls) return list[i][1];
5274
+ }
5275
+ return null;
5276
+ }
5277
+
5278
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
5279
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
5280
+ function freeVar(node, key, entry) {
5281
+ var yy = alasql.yy;
5282
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
5283
+ if (entry === 'source') return false;
5284
+ if (entry === 'into') return intoVarFree;
5285
+ return varValueFree;
5286
+ }
5287
+
5288
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
5289
+ function freeSet(node, key) {
5290
+ var yy = alasql.yy;
5291
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
5292
+ }
5293
+
5294
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
5295
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
5296
+ // names never reach generated code.
5297
+ function checkNames(node, data, entry) {
5298
+ if (node === null || typeof node !== 'object') return;
5299
+ var i;
5300
+ if (Array.isArray(node)) {
5301
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
5302
+ return;
5303
+ }
5304
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
5305
+ if (data && !isNode) {
5306
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
5307
+ return;
5308
+ }
5309
+ var yy = alasql.yy;
5310
+ if (node instanceof yy.Json) {
5311
+ checkNames(node.value, true, false);
5312
+ return;
5313
+ }
5314
+ var fk = freeOf(freeKeys, node);
5315
+ var fc = freeOf(freeChild, node);
5316
+ var sc = freeOf(skipChild, node);
5317
+ for (var key in node) {
5318
+ if (!hasOwn.call(node, key)) continue;
5319
+ var value = node[key];
5320
+ if (sc && sc[key] === 1) continue;
5321
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
5322
+ checkOrder(node.columns, value);
5323
+ continue;
5324
+ }
5325
+ // the column list of a WITH entry renames the result at run time
5326
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
5327
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
5328
+ if (typeof value === 'string') {
5329
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
5330
+ continue;
5331
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
5332
+ checkName(value);
5333
+ } else if (value !== null && typeof value === 'object') {
5334
+ if (Array.isArray(value)) {
5335
+ for (i = 0; i < value.length; i++) {
5336
+ if (typeof value[i] === 'string') {
5337
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
5338
+ } else {
5339
+ checkNames(value[i], false, child || entryOf(node, key));
5340
+ }
5341
+ }
5342
+ } else {
5343
+ checkNames(value, false, child);
5344
+ }
5345
+ }
5346
+ }
5347
+ }
5348
+
5349
+ return function (ast) {
5350
+ checkNames(ast, false, false);
5351
+ };
5352
+ })();
5353
+
5161
5354
  /**
5162
5355
  List of engines of external databases
5163
5356
  @type {object}
@@ -9117,7 +9310,7 @@ yy.Select.prototype.compileFrom = function (query) {
9117
9310
  return res;
9118
9311
  };
9119
9312
  } else if (tq instanceof yy.ParamValue) {
9120
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
9313
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
9121
9314
  if (tq.array) ps += ',true';
9122
9315
  ps += ');if(cb)res=cb(res,idx,query);return res';
9123
9316
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -9322,7 +9515,7 @@ yy.Select.prototype.compileJoins = function (query) {
9322
9515
  srcwherefn: returnTrue,
9323
9516
  };
9324
9517
  const jnparam = jn.param.param;
9325
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
9518
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
9326
9519
  if (jn.array) ps += ',true';
9327
9520
  ps += '); if(cb) res=cb(res, idx, query); return res';
9328
9521
 
@@ -10354,7 +10547,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10354
10547
  if (col.columnid === '*') {
10355
10548
  if (col.func) {
10356
10549
  sp +=
10357
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10550
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10358
10551
  } else if (col.tableid) {
10359
10552
  //Copy all
10360
10553
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -10406,7 +10599,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10406
10599
  if (isMultisheetParam) {
10407
10600
  sp =
10408
10601
  'var r={};var w=p["' +
10409
- tbid +
10602
+ escapeq(tbid) +
10410
10603
  '"];' +
10411
10604
  'var cols=[' +
10412
10605
  self.columns
@@ -10417,7 +10610,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
10417
10610
  '];var colas=[' +
10418
10611
  self.columns
10419
10612
  .map(function (col) {
10420
- return "'" + (col.as || col.columnid) + "'";
10613
+ return "'" + escapeq(col.as || col.columnid) + "'";
10421
10614
  })
10422
10615
  .join(',') +
10423
10616
  '];' +
@@ -10641,9 +10834,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
10641
10834
  // Use Object.keys to get column names and access by index
10642
10835
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10643
10836
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
10644
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10837
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10645
10838
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
10646
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
10839
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
10647
10840
  } else {
10648
10841
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
10649
10842
  }
@@ -10798,7 +10991,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
10798
10991
  query.groupColumns[colas] = col.nick;
10799
10992
 
10800
10993
  // if(col.as) {
10801
- s += "r['" + colas + "']=";
10994
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
10802
10995
  // // } else {
10803
10996
  // // s += 'r[\''+escapeq()+'\']=';
10804
10997
  // // };
@@ -10892,7 +11085,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10892
11085
  if (isInGroup) {
10893
11086
  // For columns in GROUP BY, use the GROUP BY column's nick if available
10894
11087
  var groupNick = (groupCol && groupCol.nick) || col.nick;
10895
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
11088
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
10896
11089
  }
10897
11090
  });
10898
11091
 
@@ -10908,9 +11101,9 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10908
11101
  // Use Object.keys to get column names and access by index
10909
11102
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10910
11103
  } else if (v instanceof yy.Column && groupProjectedColumnMap[groupKey]) {
10911
- s += "r['" + key + "']=r['" + groupProjectedColumnMap[groupKey] + "'];";
11104
+ s += "r['" + key + "']=r['" + escapeq(groupProjectedColumnMap[groupKey]) + "'];";
10912
11105
  } else if (v instanceof yy.Column && projectedSelectColumnMap[v.columnid]) {
10913
- s += "r['" + key + "']=r['" + v.columnid + "'];";
11106
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10914
11107
  } else if (v instanceof yy.Column && groupColMap[groupKey]) {
10915
11108
  s += "r['" + key + "']=g['" + groupColMap[groupKey].nick + "'];";
10916
11109
  } else {
@@ -12083,7 +12276,7 @@ yy.Over = class Over {
12083
12276
  }
12084
12277
  } else if (this.op === '!') {
12085
12278
  if (typeof this.right === 'string') {
12086
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
12279
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
12087
12280
  }
12088
12281
  // TODO - add other cases
12089
12282
  } else if (this.op === 'IS') {
@@ -12400,7 +12593,7 @@ yy.Over = class Over {
12400
12593
 
12401
12594
  toJS() {
12402
12595
  if (typeof this.param === 'string') {
12403
- return "params['" + this.param + "']";
12596
+ return "params['" + escapeq(this.param) + "']";
12404
12597
  }
12405
12598
 
12406
12599
  return 'params[' + this.param + ']';
@@ -13564,9 +13757,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
13564
13757
  }
13565
13758
  });
13566
13759
 
13760
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
13761
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
13762
+ // are escaped, so the string cannot end early.
13763
+ function escapeJSONString(s) {
13764
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
13765
+ if (c.length === 2) return c;
13766
+ if (c === '\\') return '\\\\';
13767
+ if (c === '"') return '\\"';
13768
+ if (c === '\n') return '\\n';
13769
+ if (c === '\r') return '\\r';
13770
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
13771
+ });
13772
+ }
13773
+
13567
13774
  function JSONtoJS(obj, context, tableid, defcols) {
13568
13775
  var s = '';
13569
- if (typeof obj == 'string') s = '"' + obj + '"';
13776
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
13570
13777
  else if (typeof obj == 'number') s = '(' + obj + ')';
13571
13778
  else if (typeof obj == 'boolean') s = obj;
13572
13779
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -13576,7 +13783,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
13576
13783
  } else if (!obj.toJS || obj instanceof yy.Json) {
13577
13784
  let ss = [];
13578
13785
  for (const k in obj) {
13579
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
13786
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
13580
13787
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
13581
13788
  ss.push(`${keyStr}:${valueStr}`);
13582
13789
  }