alasql 2.4.4 → 2.4.5

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.
@@ -1,7 +1,7 @@
1
- //! AlaSQL v2.4.4 | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
1
+ //! AlaSQL v2.4.5 | © 2014-2022 Andrey Gershun & Mathias Wulff | License: MIT
2
2
  /*
3
3
  @module alasql
4
- @version 2.4.4
4
+ @version 2.4.5
5
5
 
6
6
  AlaSQL - JavaScript SQL database
7
7
  © 2014-2023 Andrey Gershun & Mathias Wulff
@@ -844,15 +844,16 @@ var __generator = this && this.__generator || function(thisArg, body) {
844
844
  }
845
845
  if (columns && columns.length > 0) {
846
846
  columns.forEach(function(tcol) {
847
+ var escapedColumnId = escapeq(tcol.columnid);
847
848
  if (joinstar && alasql.options.joinstar == "underscore") {
848
- ss.push("'" + alias + "_" + tcol.columnid + "':p['" + alias + "']['" + tcol.columnid + "']");
849
+ ss.push("'" + alias + "_" + escapedColumnId + "':p['" + alias + "']['" + escapedColumnId + "']");
849
850
  } else if (joinstar && alasql.options.joinstar == "json") {
850
851
  // ss.push('\''+alias+'_'+tcol.columnid+'\':p[\''+alias+'\'][\''+tcol.columnid+'\']');
851
- sp += "r['" + alias + "']['" + tcol.columnid + "']=p['" + alias + "']['" + tcol.columnid + "'];";
852
+ sp += "r['" + alias + "']['" + escapedColumnId + "']=p['" + alias + "']['" + escapedColumnId + "'];";
852
853
  } else {
853
- var value = "p['" + alias + "']['" + tcol.columnid + "']";
854
+ var value = "p['" + alias + "']['" + escapedColumnId + "']";
854
855
  if (!columnIds[tcol.columnid]) {
855
- var key = "'" + tcol.columnid + "':";
856
+ var key = "'" + escapedColumnId + "':";
856
857
  ss.push(key + value);
857
858
  columnIds[tcol.columnid] = {
858
859
  id: ss.length - 1,
@@ -878,7 +879,7 @@ var __generator = this && this.__generator || function(thisArg, body) {
878
879
  });
879
880
  } else {
880
881
  // if column not exist, then copy all
881
- sp += 'var w=p["' + alias + '"];for(var k in w){if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=w[k]};';
882
+ sp += 'var w=p["' + escapeq(alias) + '"];for(var k in w){if(/^__proto__$/.test(k))Object.defineProperty(r,k,{writable:true,enumerable:true,configurable:true});r[k]=w[k]};';
882
883
  query.dirtyColumns = true;
883
884
  }
884
885
  // }
@@ -1934,7 +1935,7 @@ var __generator = this && this.__generator || function(thisArg, body) {
1934
1935
  /**
1935
1936
  Current version of alasql
1936
1937
  @constant {string}
1937
- */ alasql.version = "2.4.4";
1938
+ */ alasql.version = "2.4.5";
1938
1939
  /**
1939
1940
  Debug flag
1940
1941
  @type {boolean}
@@ -48990,8 +48991,200 @@ var __dirname = '';
48990
48991
  // My own parser here
48991
48992
  }
48992
48993
  */ alasql.parse = function(sql) {
48993
- return alasqlparser.parse(alasql.utils.uncomment(sql));
48994
+ var ast = alasqlparser.parse(alasql.utils.uncomment(sql));
48995
+ checkSqlNames(ast);
48996
+ return ast;
48994
48997
  };
48998
+
48999
+ /*
49000
+ A name from the SQL text is pasted into generated code between single quotes, so a name that
49001
+ holds a quote, a backslash or a line break would end the string and make the rest code
49002
+ (GHSA-67vj-hh8j-j94f). Such a name is refused here, once, after parsing. A name is a table,
49003
+ column, database, view, index, trigger or CTE name, a variable (and the keys of its path) and a
49004
+ table alias. The alias of a column in the select list is not a name: it is a key of the result,
49005
+ and the code that writes it escapes it. Strings, numbers, parameters, JavaScript and the keys
49006
+ of a JSON value are data, not names.
49007
+ */
49008
+ var checkSqlNames = (function () {
49009
+ var hasOwn = Object.prototype.hasOwnProperty;
49010
+ var badName = /['\\\n\r\u2028\u2029]/;
49011
+ var nameKeys = {
49012
+ as: 1,
49013
+ columnid: 1,
49014
+ tableid: 1,
49015
+ databaseid: 1,
49016
+ viewid: 1,
49017
+ indexid: 1,
49018
+ trigger: 1,
49019
+ variable: 1,
49020
+ dropcolumn: 1,
49021
+ renamecolumn: 1,
49022
+ renameto: 1,
49023
+ to: 1,
49024
+ tocolumnid: 1,
49025
+ forcolumnid: 1,
49026
+ };
49027
+ var nameListKeys = {columns: 1, fkcolumns: 1, props: 1};
49028
+
49029
+ function checkName(name) {
49030
+ if (badName.test(name)) throw new SyntaxError('Not a valid name: ' + name);
49031
+ }
49032
+
49033
+ // ORDER BY: a name that equals an alias of the select list is that alias and is allowed (the code
49034
+ // escapes it), anything else is checked. (In this version a string in ORDER BY is a constant.)
49035
+ function checkOrder(columns, order) {
49036
+ var aliases = {};
49037
+ (columns || []).forEach(function (col) {
49038
+ if (col && typeof col.as === 'string') aliases[col.as] = true;
49039
+ });
49040
+ order.forEach(function (ord) {
49041
+ var e = ord && ord.expression;
49042
+ var name;
49043
+ if (e instanceof alasql.yy.Column && !e.tableid) name = e.columnid;
49044
+ if (typeof name === 'string') {
49045
+ if (!hasOwn.call(aliases, name)) checkName(name);
49046
+ return;
49047
+ }
49048
+ checkNames(ord, false, false);
49049
+ });
49050
+ }
49051
+
49052
+ // The alias of an entry of the select list, of SEARCH RETURNS and of CREATE GRAPH is a key of
49053
+ // the result or a variable and is allowed. The name of an entry of WITH is a table name; in a
49054
+ // graph item, name is data.
49055
+ function entryOf(node, key) {
49056
+ var yy = alasql.yy;
49057
+ if (
49058
+ (node instanceof yy.Select && key === 'columns') ||
49059
+ (node instanceof yy.CreateGraph && key === 'graph') ||
49060
+ (node.srchid === 'RETURN' && key === 'args')
49061
+ )
49062
+ return 'alias';
49063
+ if (node instanceof yy.WithSelect && key === 'withs') return 'with';
49064
+ return '';
49065
+ }
49066
+
49067
+ // Names that never reach generated code: they are used only at run time, as the key of a
49068
+ // database, a table, an index, a trigger or a variable, so the walk leaves them alone and such a
49069
+ // name keeps working. freeKeys are names of the node itself, freeChild are children whose own
49070
+ // names are free (what they hold is still walked), and skipChild are children that are not
49071
+ // walked at all (PIVOT and UNPIVOT work on the result and build no code).
49072
+ var varValueFree = false;
49073
+ var intoVarFree = false;
49074
+ var freeKeys = [
49075
+ ['CreateIndex', {indexid: 1}],
49076
+ ['DropIndex', {indexid: 1}],
49077
+ ['Reindex', {indexid: 1}],
49078
+ ['CreateTrigger', {trigger: 1}],
49079
+ ['DropTrigger', {trigger: 1}],
49080
+ ['CreateDatabase', {databaseid: 1, as: 1}],
49081
+ ['AttachDatabase', {databaseid: 1, as: 1}],
49082
+ ['DetachDatabase', {databaseid: 1}],
49083
+ ['DropDatabase', {databaseid: 1}],
49084
+ ['ShowTables', {databaseid: 1}],
49085
+ ['ShowColumns', {databaseid: 1}],
49086
+ ['ShowIndex', {databaseid: 1}],
49087
+ ['ShowCreateTable', {databaseid: 1}],
49088
+ ['AlterTable', {renameto: 1, renamecolumn: 1, dropcolumn: 1}],
49089
+ ];
49090
+ var freeChild = [
49091
+ ['CreateTable', {table: 1}],
49092
+ ['DropTable', {tables: 1}],
49093
+ ['TruncateTable', {table: 1}],
49094
+ ['AlterTable', {table: 1}],
49095
+ ['ShowColumns', {table: 1}],
49096
+ ['ShowIndex', {table: 1}],
49097
+ ['ShowCreateTable', {table: 1}],
49098
+ ['CreateIndex', {table: 1}],
49099
+ ['CreateTrigger', {table: 1}],
49100
+ ['Declare', {declares: 1}],
49101
+ ];
49102
+ var skipChild = [['Select', {pivot: 1, unpivot: 1}]];
49103
+
49104
+ function freeOf(list, node) {
49105
+ var yy = alasql.yy;
49106
+ for (var i = 0; i < list.length; i++) {
49107
+ var cls = yy[list[i][0]];
49108
+ if (cls && node instanceof cls) return list[i][1];
49109
+ }
49110
+ return null;
49111
+ }
49112
+
49113
+ // An @variable: an expression pastes it escaped where varValueFree is true, FROM @x pastes it
49114
+ // raw, and SELECT INTO @x pastes it raw where intoVarFree is false.
49115
+ function freeVar(node, key, entry) {
49116
+ var yy = alasql.yy;
49117
+ if (key !== 'variable' || !(node instanceof yy.VarValue)) return false;
49118
+ if (entry === 'source') return false;
49119
+ if (entry === 'into') return intoVarFree;
49120
+ return varValueFree;
49121
+ }
49122
+
49123
+ // SET @x = value without a path is a run-time assignment; with a path the name is pasted
49124
+ function freeSet(node, key) {
49125
+ var yy = alasql.yy;
49126
+ return key === 'variable' && yy.SetVariable && node instanceof yy.SetVariable && !(node.props && node.props.length);
49127
+ }
49128
+
49129
+ // data is true inside a JSON value, where a plain object is data and only a node is looked at.
49130
+ // entry is what entryOf says about the node as an entry of its parent, or 'free' when its own
49131
+ // names never reach generated code.
49132
+ function checkNames(node, data, entry) {
49133
+ if (node === null || typeof node !== 'object') return;
49134
+ var i;
49135
+ if (Array.isArray(node)) {
49136
+ for (i = 0; i < node.length; i++) checkNames(node[i], data, false);
49137
+ return;
49138
+ }
49139
+ var isNode = Object.getPrototypeOf(node) !== Object.prototype;
49140
+ if (data && !isNode) {
49141
+ for (i in node) if (hasOwn.call(node, i)) checkNames(node[i], true, false);
49142
+ return;
49143
+ }
49144
+ var yy = alasql.yy;
49145
+ if (node instanceof yy.Json) {
49146
+ checkNames(node.value, true, false);
49147
+ return;
49148
+ }
49149
+ var fk = freeOf(freeKeys, node);
49150
+ var fc = freeOf(freeChild, node);
49151
+ var sc = freeOf(skipChild, node);
49152
+ for (var key in node) {
49153
+ if (!hasOwn.call(node, key)) continue;
49154
+ var value = node[key];
49155
+ if (sc && sc[key] === 1) continue;
49156
+ if (key === 'order' && node instanceof yy.Select && Array.isArray(value)) {
49157
+ checkOrder(node.columns, value);
49158
+ continue;
49159
+ }
49160
+ // the column list of a WITH entry renames the result at run time
49161
+ var child = (fc && fc[key] === 1) || (entry === 'with' && key === 'columns') ? 'free' : '';
49162
+ if (!child && node instanceof yy.Select) child = key === 'from' ? 'source' : key === 'into' ? 'into' : '';
49163
+ if (typeof value === 'string') {
49164
+ if (entry === 'free' || (fk && fk[key] === 1) || freeSet(node, key) || freeVar(node, key, entry))
49165
+ continue;
49166
+ if (key === 'name' ? entry === 'with' : nameKeys[key] === 1 && !(entry === 'alias' && key === 'as'))
49167
+ checkName(value);
49168
+ } else if (value !== null && typeof value === 'object') {
49169
+ if (Array.isArray(value)) {
49170
+ for (i = 0; i < value.length; i++) {
49171
+ if (typeof value[i] === 'string') {
49172
+ if (nameListKeys[key] === 1 && entry !== 'free' && child !== 'free') checkName(value[i]);
49173
+ } else {
49174
+ checkNames(value[i], false, child || entryOf(node, key));
49175
+ }
49176
+ }
49177
+ } else {
49178
+ checkNames(value, false, child);
49179
+ }
49180
+ }
49181
+ }
49182
+ }
49183
+
49184
+ return function (ast) {
49185
+ checkNames(ast, false, false);
49186
+ };
49187
+ })();
48995
49188
  /**
48996
49189
  List of engines of external databases
48997
49190
  @type {object}
@@ -50782,7 +50975,7 @@ var __dirname = '';
50782
50975
  // Save data into parameters array
50783
50976
  // like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
50784
50977
  //
50785
- query.intofns = "params['" + this.into.param + "'].push(r)";
50978
+ query.intofns = "params['" + escapeq(this.into.param) + "'].push(r)";
50786
50979
  }
50787
50980
  if (query.intofns) {
50788
50981
  // Create intofn function
@@ -51012,7 +51205,7 @@ var __dirname = '';
51012
51205
  return res;
51013
51206
  };
51014
51207
  } else if (_instanceof(tq, yy.ParamValue)) {
51015
- ps = "var res = alasql.prepareFromData(params['" + tq.param + "']";
51208
+ ps = "var res = alasql.prepareFromData(params['" + escapeq(tq.param) + "']";
51016
51209
  if (tq.array) ps += ",true";
51017
51210
  ps += ");if(cb)res=cb(res,idx,query);return res";
51018
51211
  source.datafn = new Function("query,params,cb,idx,alasql", ps);
@@ -51224,7 +51417,7 @@ var __dirname = '';
51224
51417
  };
51225
51418
  // source.data = ;
51226
51419
  var jnparam = jn.param.param;
51227
- ps = "var res=alasql.prepareFromData(params['" + jnparam + "']";
51420
+ ps = "var res=alasql.prepareFromData(params['" + escapeq(jnparam) + "']";
51228
51421
  if (jn.array) ps += ",true";
51229
51422
  ps += ");if(cb)res=cb(res, idx, query);return res";
51230
51423
  source.datafn = new Function("query,params,cb,idx, alasql", ps);
@@ -51722,7 +51915,7 @@ var __dirname = '';
51722
51915
  if (_instanceof(col, yy.Column)) {
51723
51916
  if (col.columnid === "*") {
51724
51917
  if (col.func) {
51725
- sp += "r=params['" + col.param + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
51918
+ sp += "r=params['" + escapeq(col.param) + "'](p['" + query.sources[0].alias + "'],p,params,alasql);";
51726
51919
  } else if (col.tableid) {
51727
51920
  //Copy all
51728
51921
  var ret = compileSelectStar(query, [
@@ -51756,10 +51949,10 @@ var __dirname = '';
51756
51949
  // workaround for multisheet xlsx export with custom COLUMNS
51757
51950
  var isMultisheetParam = params && params.length > 1 && Array.isArray(params[0]) && params[0].length >= 1 && params[0][0].hasOwnProperty("sheetid");
51758
51951
  if (isMultisheetParam) {
51759
- sp = 'var r={};var w=p["' + tbid + '"];' + "var cols=[" + _$self.columns.map(function(col) {
51952
+ sp = 'var r={};var w=p["' + escapeq(tbid) + '"];' + "var cols=[" + _$self.columns.map(function(col) {
51760
51953
  return "'" + col.columnid + "'";
51761
51954
  }).join(",") + "];var colas=[" + _$self.columns.map(function(col) {
51762
- return "'" + (col.as || col.columnid) + "'";
51955
+ return "'" + escapeq(col.as || col.columnid) + "'";
51763
51956
  }).join(",") + "];" + "for (var i=0;i<Object.keys(p['" + tbid + "']).length;i++)" + " for(var k=0;k<cols.length;k++){if (!r.hasOwnProperty(i)) r[i]={}; r[i][colas[k]]=w[i][cols[k]];}";
51764
51957
  } else {
51765
51958
  ss.push("'" + escapeq(col.as || col.columnid) + "':p['" + tbid + "']['" + col.columnid + "']");
@@ -51856,9 +52049,9 @@ var __dirname = '';
51856
52049
  this.orderColumns.forEach(function(v, idx) {
51857
52050
  var key = "$$$" + idx;
51858
52051
  if (_instanceof(v, yy.Column) && query.xcolumns[v.columnid]) {
51859
- s += "r['" + key + "']=r['" + v.columnid + "'];";
52052
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
51860
52053
  } else if (_instanceof(v, yy.ParamValue) && query.xcolumns[params[v.param]]) {
51861
- s += "r['" + key + "']=r['" + params[v.param] + "'];";
52054
+ s += "r['" + key + "']=r['" + escapeq(params[v.param]) + "'];";
51862
52055
  } else {
51863
52056
  s += "r['" + key + "']=" + v.toJS("p", query.defaultTableid, query.defcols) + ";";
51864
52057
  }
@@ -51934,7 +52127,7 @@ var __dirname = '';
51934
52127
  }
51935
52128
  query.groupColumns[colas] = col.nick;
51936
52129
  // if(col.as) {
51937
- s += "r['" + colas + "']=";
52130
+ s += "r['" + (col.as !== undefined ? escapeq(colas) : colas) + "']=";
51938
52131
  // // } else {
51939
52132
  // // s += 'r[\''+escapeq()+'\']=';
51940
52133
  // // };
@@ -51957,14 +52150,14 @@ var __dirname = '';
51957
52150
  var s = query.selectgfns;
51958
52151
  _$self.columns.forEach(function(col) {
51959
52152
  if (query.ingroup.indexOf(col.nick) > -1) {
51960
- s += "r['" + (col.as || col.nick) + "']=g['" + col.nick + "'];";
52153
+ s += "r['" + (col.as ? escapeq(col.as) : col.nick) + "']=g['" + col.nick + "'];";
51961
52154
  }
51962
52155
  });
51963
52156
  if (this.orderColumns && this.orderColumns.length > 0) {
51964
52157
  this.orderColumns.forEach(function(v, idx) {
51965
52158
  var key = "$$$" + idx;
51966
52159
  if (_instanceof(v, yy.Column) && query.groupColumns[v.columnid]) {
51967
- s += "r['" + key + "']=r['" + v.columnid + "'];";
52160
+ s += "r['" + key + "']=r['" + escapeq(v.columnid) + "'];";
51968
52161
  } else {
51969
52162
  s += "r['" + key + "']=" + v.toJS("g", "") + ";";
51970
52163
  }
@@ -52814,7 +53007,7 @@ if(false) {
52814
53007
  // Expression to prevent error if object is empty (#344)
52815
53008
  var ljs = "(" + leftJS() + "||{})";
52816
53009
  if (typeof this.right === "string") {
52817
- s = ljs + '["' + this.right + '"]';
53010
+ s = ljs + '["' + escapeq(this.right) + '"]';
52818
53011
  } else if (typeof this.right === "number") {
52819
53012
  s = ljs + "[" + this.right + "]";
52820
53013
  } else if (_instanceof(this.right, yy.FuncValue)) {
@@ -52829,7 +53022,7 @@ if(false) {
52829
53022
  }
52830
53023
  if (this.op === "!") {
52831
53024
  if (typeof this.right === "string") {
52832
- s = "" + "alasql.databases[alasql.useid].objects[" + leftJS() + ']["' + this.right + '"]';
53025
+ s = "" + "alasql.databases[alasql.useid].objects[" + leftJS() + ']["' + escapeq(this.right) + '"]';
52833
53026
  }
52834
53027
  // TODO - add other cases
52835
53028
  }
@@ -53165,7 +53358,7 @@ if(false) {
53165
53358
  key: "toJS",
53166
53359
  value: function toJS() {
53167
53360
  if (typeof this.param === "string") {
53168
- return "params['" + this.param + "']";
53361
+ return "params['" + escapeq(this.param) + "']";
53169
53362
  }
53170
53363
  return "params[" + this.param + "]";
53171
53364
  }
@@ -54139,9 +54332,23 @@ if(false) {
54139
54332
  }
54140
54333
  return s;
54141
54334
  };
54335
+ // A string value or key of a JSON value goes between double quotes in the generated code. A
54336
+ // backslash pair keeps its meaning; a lone backslash at the end, a double quote and a line break
54337
+ // are escaped, so the string cannot end early.
54338
+ function escapeJSONString(s) {
54339
+ return s.replace(/\\[\s\S]|\\$|"|\n|\r|\u2028|\u2029/g, function (c) {
54340
+ if (c.length === 2) return c;
54341
+ if (c === '\\') return '\\\\';
54342
+ if (c === '"') return '\\"';
54343
+ if (c === '\n') return '\\n';
54344
+ if (c === '\r') return '\\r';
54345
+ return c === '\u2028' ? '\\u2028' : '\\u2029';
54346
+ });
54347
+ }
54348
+
54142
54349
  function JSONtoJS(obj, context, tableid, defcols) {
54143
54350
  var s = "";
54144
- if (typeof obj == "string") s = '"' + obj + '"';
54351
+ if (typeof obj == "string") s = '"' + escapeJSONString(obj) + '"';
54145
54352
  else if (typeof obj == "number") s = "(" + obj + ")";
54146
54353
  else if (typeof obj == "boolean") s = obj;
54147
54354
  else if (typeof obj == "object") {
@@ -54155,7 +54362,7 @@ if(false) {
54155
54362
  var ss = [];
54156
54363
  for(var k in obj){
54157
54364
  var s1 = "";
54158
- if (typeof k == "string") s1 += '"' + k + '"';
54365
+ if (typeof k == "string") s1 += '"' + escapeJSONString(k) + '"';
54159
54366
  else if (typeof k == "number") s1 += k;
54160
54367
  else if (typeof k == "boolean") s1 += k;
54161
54368
  else {