alasql 4.16.2 → 4.16.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.16.2
5
+ // Version: 4.16.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.16.2 build: develop-ff717ec7 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.16.3 build: develop-ff717ec7 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.16.2
4
+ @version 4.16.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.16.2 build: develop-ff717ec7 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v4.16.3 build: develop-ff717ec7 | © 2014-2025 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 4.16.2
4
+ @version 4.16.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.16.2';
144
+ alasql.version = '4.16.3';
145
145
  alasql.build = 'develop-ff717ec7';
146
146
 
147
147
  /**
@@ -4982,9 +4982,202 @@ alasql.parser.parseError = function (str, hash) {
4982
4982
  }
4983
4983
  */
4984
4984
  alasql.parse = function (sql) {
4985
- return alasqlparser.parse(alasql.utils.uncomment(sql));
4985
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
4986
+ checkSqlNames(ast);
4987
+ return ast;
4986
4988
  };
4987
4989
 
4990
+ /*
4991
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
4992
+ holds a quote, a backslash or a line break would end the string and make the rest code
4993
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
4994
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
4995
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
4996
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
4997
+ of a JSON value are data, not names.
4998
+ */
4999
+ var checkSqlNames = (function () {
5000
+ var hasOwn = Object.prototype.hasOwnProperty;
5001
+ var badName = /['\\\n\r\u2028\u2029]/;
5002
+ var nameKeys = {
5003
+ as: 1,
5004
+ columnid: 1,
5005
+ tableid: 1,
5006
+ databaseid: 1,
5007
+ viewid: 1,
5008
+ indexid: 1,
5009
+ trigger: 1,
5010
+ variable: 1,
5011
+ dropcolumn: 1,
5012
+ renamecolumn: 1,
5013
+ renameto: 1,
5014
+ to: 1,
5015
+ tocolumnid: 1,
5016
+ forcolumnid: 1,
5017
+ };
5018
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
5019
+
5020
+ function checkName(name) {
5021
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
5022
+ }
5023
+
5024
+ // ORDER BY: a quoted string is a column reference too. A name or string that equals an alias of
5025
+ // the select list is that alias and is allowed (the code escapes it), anything else is checked.
5026
+ function checkOrder(columns, order) {
5027
+ var aliases = {};
5028
+ (columns || []).forEach(function (col) {
5029
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
5030
+ });
5031
+ order.forEach(function (ord) {
5032
+ var e = ord && ord.expression;
5033
+ var name;
5034
+ if (e instanceof alasql.yy.StringValue) name = e.value;
5035
+ else if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
5036
+ if (typeof name === 'string') {
5037
+ if (!hasOwn.call(aliases, name)) checkName(name);
5038
+ return;
5039
+ }
5040
+ checkNames(ord, false, false);
5041
+ });
5042
+ }
5043
+
5044
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
5045
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
5046
+ // graph item, name is data.
5047
+ function entryOf(node, key) {
5048
+ var yy = alasql.yy;
5049
+ if (
5050
+ (node instanceof yy.Select && key === 'columns') ||
5051
+ (node instanceof yy.CreateGraph && key === 'graph') ||
5052
+ (node.srchid === 'RETURN' && key === 'args')
5053
+ )
5054
+ return 'alias';
5055
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
5056
+ return '';
5057
+ }
5058
+
5059
+ // Names that never reach generated code: they are used only at run time, as the key of a
5060
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
5061
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
5062
+ // names are free (what they hold is still walked), and skipChild are children that are not
5063
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
5064
+ var varValueFree = true;
5065
+ var intoVarFree = true;
5066
+ var freeKeys = [
5067
+ ['CreateIndex', {indexid: 1}],
5068
+ ['DropIndex', {indexid: 1}],
5069
+ ['Reindex', {indexid: 1}],
5070
+ ['CreateTrigger', {trigger: 1}],
5071
+ ['DropTrigger', {trigger: 1}],
5072
+ ['CreateDatabase', {databaseid: 1, as: 1}],
5073
+ ['AttachDatabase', {databaseid: 1, as: 1}],
5074
+ ['DetachDatabase', {databaseid: 1}],
5075
+ ['DropDatabase', {databaseid: 1}],
5076
+ ['ShowTables', {databaseid: 1}],
5077
+ ['ShowColumns', {databaseid: 1}],
5078
+ ['ShowIndex', {databaseid: 1}],
5079
+ ['ShowCreateTable', {databaseid: 1}],
5080
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
5081
+ ];
5082
+ var freeChild = [
5083
+ ['CreateTable', {table: 1}],
5084
+ ['DropTable', {tables: 1}],
5085
+ ['TruncateTable', {table: 1}],
5086
+ ['AlterTable', {table: 1}],
5087
+ ['ShowColumns', {table: 1}],
5088
+ ['ShowIndex', {table: 1}],
5089
+ ['ShowCreateTable', {table: 1}],
5090
+ ['CreateIndex', {table: 1}],
5091
+ ['CreateTrigger', {table: 1}],
5092
+ ['Declare', {declares: 1}],
5093
+ ];
5094
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
5095
+
5096
+ function freeOf(list, node) {
5097
+ var yy = alasql.yy;
5098
+ for (var i = 0; i < list.length; i++) {
5099
+ var cls = yy[list[i][0]];
5100
+ if (cls && node instanceof cls) return list[i][1];
5101
+ }
5102
+ return null;
5103
+ }
5104
+
5105
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
5106
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
5107
+ function freeVar(node, key, entry) {
5108
+ var yy = alasql.yy;
5109
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
5110
+ if (entry === 'source') return false;
5111
+ if (entry === 'into') return intoVarFree;
5112
+ return varValueFree;
5113
+ }
5114
+
5115
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
5116
+ function freeSet(node, key) {
5117
+ var yy = alasql.yy;
5118
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
5119
+ }
5120
+
5121
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
5122
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
5123
+ // names never reach generated code.
5124
+ function checkNames(node, data, entry) {
5125
+ if (node === null || typeof node !== 'object') return;
5126
+ var i;
5127
+ if (Array.isArray(node)) {
5128
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
5129
+ return;
5130
+ }
5131
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
5132
+ if (data && !isNode) {
5133
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
5134
+ return;
5135
+ }
5136
+ var yy = alasql.yy;
5137
+ if (node instanceof yy.Json) {
5138
+ checkNames(node.value, true, false);
5139
+ return;
5140
+ }
5141
+ var fk = freeOf(freeKeys, node);
5142
+ var fc = freeOf(freeChild, node);
5143
+ var sc = freeOf(skipChild, node);
5144
+ for (var key in node) {
5145
+ if (!hasOwn.call(node, key)) continue;
5146
+ var value = node[key];
5147
+ if (sc && sc[key] === 1) continue;
5148
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
5149
+ checkOrder(node.columns, value);
5150
+ continue;
5151
+ }
5152
+ // the column list of a WITH entry renames the result at run time
5153
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
5154
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
5155
+ if (typeof value === 'string') {
5156
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
5157
+ continue;
5158
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
5159
+ checkName(value);
5160
+ } else if (value !== null && typeof value === 'object') {
5161
+ if (Array.isArray(value)) {
5162
+ for (i = 0; i < value.length; i++) {
5163
+ if (typeof value[i] === 'string') {
5164
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
5165
+ } else {
5166
+ checkNames(value[i], false, child || entryOf(node, key));
5167
+ }
5168
+ }
5169
+ } else {
5170
+ checkNames(value, false, child);
5171
+ }
5172
+ }
5173
+ }
5174
+ }
5175
+
5176
+ return function (ast) {
5177
+ checkNames(ast, false, false);
5178
+ };
5179
+ })();
5180
+
4988
5181
  /**
4989
5182
  List of engines of external databases
4990
5183
  @type {object}
@@ -8664,7 +8857,7 @@ yy.Select.prototype.compileFrom = function (query) {
8664
8857
  return res;
8665
8858
  };
8666
8859
  } else if (tq instanceof yy.ParamValue) {
8667
- let ps = `var res = alasql.prepareFromData(params['${tq.param}']`;
8860
+ let ps = `var res = alasql.prepareFromData(params['${escapeq(tq.param)}']`;
8668
8861
  if (tq.array) ps += ',true';
8669
8862
  ps += ');if(cb)res=cb(res,idx,query);return res';
8670
8863
  source.datafn = new Function('query,params,cb,idx,alasql', ps);
@@ -8875,7 +9068,7 @@ yy.Select.prototype.compileJoins = function (query) {
8875
9068
  srcwherefn: returnTrue,
8876
9069
  };
8877
9070
  const jnparam = jn.param.param;
8878
- ps = "let res=alasql.prepareFromData(params['" + jnparam + "']";
9071
+ ps = "let res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
8879
9072
  if (jn.array) ps += ',true';
8880
9073
  ps += '); if(cb) res=cb(res, idx, query); return res';
8881
9074
 
@@ -9859,7 +10052,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9859
10052
  if (col.columnid === '*') {
9860
10053
  if (col.func) {
9861
10054
  sp +=
9862
- "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
10055
+ "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
9863
10056
  } else if (col.tableid) {
9864
10057
  //Copy all
9865
10058
  var ret = compileSelectStar(query, [col.tableid], false);
@@ -9911,7 +10104,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9911
10104
  if (isMultisheetParam) {
9912
10105
  sp =
9913
10106
  'var r={};var w=p["' +
9914
- tbid +
10107
+ escapeq(tbid) +
9915
10108
  '"];' +
9916
10109
  'var cols=[' +
9917
10110
  self.columns
@@ -9922,7 +10115,7 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
9922
10115
  '];var colas=[' +
9923
10116
  self.columns
9924
10117
  .map(function (col) {
9925
- return "'" + (col.as || col.columnid) + "'";
10118
+ return "'" + escapeq(col.as || col.columnid) + "'";
9926
10119
  })
9927
10120
  .join(',') +
9928
10121
  '];' +
@@ -10153,9 +10346,9 @@ yy.Select.prototype.compileSelect2 = function (query, params) {
10153
10346
  // Use Object.keys to get column names and access by index
10154
10347
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10155
10348
  } else if (v instanceof yy.Column && query.xcolumns[v.columnid]) {
10156
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10349
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10157
10350
  } else if (v instanceof yy.ParamValue && query.xcolumns[params[v.param]]) {
10158
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
10351
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
10159
10352
  } else {
10160
10353
  s += "r['" + key + "']=" + v.toJS('p', query.defaultTableid, query.defcols) + ';';
10161
10354
  }
@@ -10262,7 +10455,7 @@ yy.Select.prototype.compileSelectGroup1 = function (query) {
10262
10455
  query.groupColumns[colas] = col.nick;
10263
10456
 
10264
10457
  // if(col.as) {
10265
- s += "r['" + colas + "']=";
10458
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
10266
10459
  // // } else {
10267
10460
  // // s += 'r[\''+escapeq()+'\']=';
10268
10461
  // // };
@@ -10313,7 +10506,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10313
10506
  if (isInGroup) {
10314
10507
  // For columns in GROUP BY, use the GROUP BY column's nick if available
10315
10508
  var groupNick = (groupCol && groupCol.nick) || col.nick;
10316
- s += "r['" + (col.as || col.nick) + "']=g['" + groupNick + "'];";
10509
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + groupNick + "'];";
10317
10510
  }
10318
10511
  });
10319
10512
 
@@ -10335,7 +10528,7 @@ yy.Select.prototype.compileSelectGroup2 = function (query) {
10335
10528
  // Use Object.keys to get column names and access by index
10336
10529
  s += "var keys=Object.keys(r);r['" + key + "']=r[keys[" + v.columnIndex + ']];';
10337
10530
  } else if (v instanceof yy.Column && query.groupColumns[v.columnid]) {
10338
- s += "r['" + key + "']=r['" + v.columnid + "'];";
10531
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
10339
10532
  } else {
10340
10533
  s += "r['" + key + "']=" + v.toJS('g', '') + ';';
10341
10534
  }
@@ -11488,7 +11681,7 @@ yy.Over = class Over {
11488
11681
  }
11489
11682
  } else if (this.op === '!') {
11490
11683
  if (typeof this.right === 'string') {
11491
- s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${this.right}"]`;
11684
+ s = `alasql.databases[alasql.useid].objects[${leftJS()}]["${escapeq(this.right)}"]`;
11492
11685
  }
11493
11686
  // TODO - add other cases
11494
11687
  } else if (this.op === 'IS') {
@@ -11805,7 +11998,7 @@ yy.Over = class Over {
11805
11998
 
11806
11999
  toJS() {
11807
12000
  if (typeof this.param === 'string') {
11808
- return "params['" + this.param + "']";
12001
+ return "params['" + escapeq(this.param) + "']";
11809
12002
  }
11810
12003
 
11811
12004
  return 'params[' + this.param + ']';
@@ -12866,9 +13059,23 @@ const JSONtoString = (alasql.utils.JSONtoString = function (obj) {
12866
13059
  }
12867
13060
  });
12868
13061
 
13062
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
13063
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
13064
+ // are escaped, so the string cannot end early.
13065
+ function escapeJSONString(s) {
13066
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
13067
+ if (c.length === 2) return c;
13068
+ if (c === '\\') return '\\\\';
13069
+ if (c === '"') return '\\"';
13070
+ if (c === '\n') return '\\n';
13071
+ if (c === '\r') return '\\r';
13072
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
13073
+ });
13074
+ }
13075
+
12869
13076
  function JSONtoJS(obj, context, tableid, defcols) {
12870
13077
  var s = '';
12871
- if (typeof obj == 'string') s = '"' + obj + '"';
13078
+ if (typeof obj == 'string') s = '"' + escapeJSONString(obj) + '"';
12872
13079
  else if (typeof obj == 'number') s = '(' + obj + ')';
12873
13080
  else if (typeof obj == 'boolean') s = obj;
12874
13081
  else if (typeof obj === 'bigint') s = obj.toString() + 'n';
@@ -12878,7 +13085,7 @@ function JSONtoJS(obj, context, tableid, defcols) {
12878
13085
  } else if (!obj.toJS || obj instanceof yy.Json) {
12879
13086
  let ss = [];
12880
13087
  for (const k in obj) {
12881
- let keyStr = typeof k === 'string' ? `"${k}"` : k.toString();
13088
+ let keyStr = typeof k === 'string' ? `"${escapeJSONString(k)}"` : k.toString();
12882
13089
  let valueStr = JSONtoJS(obj[k], context, tableid, defcols);
12883
13090
  ss.push(`${keyStr}:${valueStr}`);
12884
13091
  }