forms-angular 0.12.0-beta.342 → 0.12.0-beta.344

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.
@@ -1099,31 +1099,44 @@ export class FormsAngular {
1099
1099
  }
1100
1100
  }
1101
1101
  }
1102
+ rejectForbiddenPipelineOperators(obj) {
1103
+ if (Array.isArray(obj)) {
1104
+ obj.forEach((elm) => this.rejectForbiddenPipelineOperators(elm));
1105
+ }
1106
+ else if (obj && typeof obj === "object" && !(obj instanceof Types.ObjectId) && !(obj instanceof Date)) {
1107
+ for (const key of Object.keys(obj)) {
1108
+ if (FormsAngular.forbiddenPipelineOperators.includes(key)) {
1109
+ throw new Error(`Unsupported pipeline operator ${key}`);
1110
+ }
1111
+ this.rejectForbiddenPipelineOperators(obj[key]);
1112
+ }
1113
+ }
1114
+ }
1102
1115
  async sanitisePipeline(aggregationParam, hiddenFields, findFuncQry, req) {
1103
1116
  let that = this;
1104
1117
  let array = Array.isArray(aggregationParam)
1105
1118
  ? aggregationParam
1106
1119
  : [aggregationParam];
1120
+ this.rejectForbiddenPipelineOperators(array);
1107
1121
  let retVal = [];
1108
1122
  let doneHiddenFields = false;
1123
+ const haveHiddenFields = hiddenFields && Object.keys(hiddenFields).length > 0;
1109
1124
  if (findFuncQry) {
1110
1125
  retVal.unshift({ $match: findFuncQry });
1111
1126
  }
1112
- async function sanitiseLookupPipeline(stage) {
1113
- // can't think of a way to exploit this (if you add a findFunc) but it is the responsibility of the user
1114
- // to ensure that the pipeline returns fields used by the findFunc
1115
- console.log(`In some scenarios $lookups that use pipelines may not provide all the fields used by the 'findFunc' of a collection.
1116
- If you get no results returned this might be the explanation.`);
1117
- /* Sanitise the pipeline we are doing a lookup with, removing hidden fields from that collection */
1118
- const lookupCollectionName = stage.$lookup.from;
1119
- const lookupResource = that.getResourceFromCollection(lookupCollectionName);
1120
- let lookupHiddenLookupFields = {};
1121
- if (lookupResource) {
1122
- if (lookupResource.options?.hide?.length > 0) {
1123
- lookupHiddenLookupFields = that.generateHiddenFields(lookupResource, false);
1124
- }
1125
- }
1126
- stage.$lookup.pipeline = await that.sanitisePipeline(stage.$lookup.pipeline, lookupHiddenLookupFields, findFuncQry, req);
1127
+ // Sanitise a pipeline that runs against a joined collection, restricting it with that collection's own findFunc
1128
+ // and removing that collection's hidden fields. Collections that are not resources are assumed to hold nothing
1129
+ // sensitive, so are not restricted.
1130
+ async function sanitiseJoinedPipeline(pipeline, resource) {
1131
+ let joinedHiddenFields = {};
1132
+ let joinedFindFuncQry;
1133
+ if (resource) {
1134
+ if (resource.options?.hide?.length > 0) {
1135
+ joinedHiddenFields = that.generateHiddenFields(resource, false);
1136
+ }
1137
+ joinedFindFuncQry = await that.doFindFuncPromise(req, resource);
1138
+ }
1139
+ return that.sanitisePipeline(pipeline || [], joinedHiddenFields, joinedFindFuncQry, req);
1127
1140
  }
1128
1141
  for (let pipelineSection = 0; pipelineSection < array.length; pipelineSection++) {
1129
1142
  let stage = array[pipelineSection];
@@ -1131,11 +1144,17 @@ export class FormsAngular {
1131
1144
  if (keys.length !== 1) {
1132
1145
  throw new Error("Invalid pipeline instruction");
1133
1146
  }
1147
+ if (!doneHiddenFields && keys[0] !== "$match") {
1148
+ // Leading $match stages may select on hidden fields, but nothing after them can see those fields
1149
+ if (haveHiddenFields) {
1150
+ retVal.push({ $project: hiddenFields });
1151
+ }
1152
+ doneHiddenFields = true;
1153
+ }
1134
1154
  switch (keys[0]) {
1135
1155
  case "$project":
1136
1156
  case "$addFields":
1137
1157
  case "$count":
1138
- case "$facet":
1139
1158
  case "$group":
1140
1159
  case "$unset":
1141
1160
  case "$limit":
@@ -1145,39 +1164,31 @@ export class FormsAngular {
1145
1164
  case "$unwind":
1146
1165
  // We don't care about these - they are all (as far as we know) safe
1147
1166
  break;
1167
+ case "$facet":
1168
+ // Each facet is a pipeline in its own right, running against the documents that reach this stage (which
1169
+ // have already been through our findFunc and had hidden fields removed), but it can join other collections
1170
+ for (const facetName of Object.keys(stage.$facet)) {
1171
+ stage.$facet[facetName] = await that.sanitisePipeline(stage.$facet[facetName], {}, undefined, req);
1172
+ }
1173
+ break;
1148
1174
  case "$unionWith":
1149
- /*
1150
- Sanitise the pipeline we are doing a union with, removing hidden fields from that collection
1151
- */
1152
1175
  if (!stage.$unionWith.coll) {
1153
1176
  stage.$unionWith = { coll: stage.$unionWith, pipeline: [] };
1154
1177
  }
1155
- const unionCollectionName = stage.$unionWith.coll;
1156
- const unionResource = that.getResourceFromCollection(unionCollectionName);
1157
- let unionHiddenLookupFields = {};
1158
- if (unionResource) {
1159
- if (unionResource.options?.hide?.length > 0) {
1160
- unionHiddenLookupFields = this.generateHiddenFields(unionResource, false);
1161
- }
1162
- }
1163
- stage.$unionWith.pipeline = await that.sanitisePipeline(stage.$unionWith.pipeline, unionHiddenLookupFields, findFuncQry, req);
1178
+ stage.$unionWith.pipeline = await sanitiseJoinedPipeline(stage.$unionWith.pipeline, that.getResourceFromCollection(stage.$unionWith.coll));
1164
1179
  break;
1165
1180
  case "$match":
1166
1181
  this.hackVariables(array[pipelineSection]["$match"]);
1167
- retVal.push(array[pipelineSection]);
1168
- if (!doneHiddenFields &&
1169
- Object.keys(hiddenFields) &&
1170
- Object.keys(hiddenFields).length > 0) {
1171
- // We can now project out the hidden fields (we wait for the $match to make sure we don't break
1172
- // a select that uses a hidden field
1173
- retVal.push({ $project: hiddenFields });
1174
- doneHiddenFields = true;
1175
- }
1176
- stage = null;
1177
1182
  break;
1178
1183
  case "$lookup":
1179
1184
  case "$graphLookup":
1180
1185
  let needFindFunc = true;
1186
+ const collectionName = stage[keys[0]].from;
1187
+ const lookupField = stage[keys[0]].as;
1188
+ if (typeof collectionName !== "string" || typeof lookupField !== "string" || (collectionName + lookupField).indexOf("$") !== -1) {
1189
+ throw new Error('No support for lookups where the "from" or "as" is anything other than a simple string');
1190
+ }
1191
+ const resource = that.getResourceFromCollection(collectionName);
1181
1192
  if (keys[0] === "$lookup") {
1182
1193
  let lookupProps = Object.keys(stage.$lookup);
1183
1194
  // First deal with simple $lookups with a single join field equality
@@ -1190,7 +1201,7 @@ export class FormsAngular {
1190
1201
  // nothing to do
1191
1202
  }
1192
1203
  else if (lookupProps.length === 5 && lookupProps.includes("pipeline")) {
1193
- await sanitiseLookupPipeline.call(this, stage);
1204
+ stage.$lookup.pipeline = await sanitiseJoinedPipeline(stage.$lookup.pipeline, resource);
1194
1205
  }
1195
1206
  else {
1196
1207
  throw new Error("Unsupported $lookup format");
@@ -1201,19 +1212,12 @@ export class FormsAngular {
1201
1212
  lookupProps.includes("let") &&
1202
1213
  lookupProps.includes("pipeline") &&
1203
1214
  lookupProps.includes("as")) {
1204
- await sanitiseLookupPipeline.call(this, stage);
1215
+ stage.$lookup.pipeline = await sanitiseJoinedPipeline(stage.$lookup.pipeline, resource);
1205
1216
  }
1206
1217
  else {
1207
1218
  throw new Error(`No support for $lookup of with properties ${lookupProps.join(', ')}`);
1208
1219
  }
1209
1220
  }
1210
- // hide any hiddenfields in the lookup collection
1211
- const collectionName = stage[keys[0]].from;
1212
- const lookupField = stage[keys[0]].as;
1213
- if ((collectionName + lookupField).indexOf("$") !== -1) {
1214
- throw new Error('No support for lookups where the "from" or "as" is anything other than a simple string');
1215
- }
1216
- const resource = that.getResourceFromCollection(collectionName);
1217
1221
  if (resource) {
1218
1222
  retVal.push(stage);
1219
1223
  stage = null;
@@ -1230,8 +1234,8 @@ export class FormsAngular {
1230
1234
  let allowNulls = false;
1231
1235
  // If the next stage is an $unwind
1232
1236
  let nextStageIsUnwind = false;
1233
- if (array.length >= pipelineSection) {
1234
- const nextStage = array[pipelineSection + 1];
1237
+ const nextStage = array[pipelineSection + 1];
1238
+ if (nextStage) {
1235
1239
  let nextKeys = Object.keys(nextStage);
1236
1240
  if (nextKeys.length !== 1) {
1237
1241
  throw new Error("Invalid pipeline instruction");
@@ -1286,11 +1290,9 @@ export class FormsAngular {
1286
1290
  retVal.push(stage);
1287
1291
  }
1288
1292
  }
1289
- if (!doneHiddenFields &&
1290
- Object.keys(hiddenFields) &&
1291
- Object.keys(hiddenFields).length > 0) {
1292
- // If there was no $match we still need to hide the hidden fields
1293
- retVal.unshift({ $project: hiddenFields });
1293
+ if (!doneHiddenFields && haveHiddenFields) {
1294
+ // The pipeline was nothing but $match stages
1295
+ retVal.push({ $project: hiddenFields });
1294
1296
  }
1295
1297
  return retVal;
1296
1298
  }
@@ -1363,7 +1365,7 @@ export class FormsAngular {
1363
1365
  });
1364
1366
  })
1365
1367
  .catch((err) => {
1366
- throw new Error("Error in sanitisePipeline " + err);
1368
+ cb(err);
1367
1369
  });
1368
1370
  },
1369
1371
  };
@@ -2286,6 +2288,9 @@ export class FormsAngular {
2286
2288
  return new Date(parseInt(timestamp, 16) * 1000);
2287
2289
  }
2288
2290
  }
2291
+ // Operators that run server-side JavaScript, which can read beyond the documents a findFunc allows. They are
2292
+ // rejected wherever they appear in a pipeline - as a stage, inside a $match, or nested in an expression.
2293
+ FormsAngular.forbiddenPipelineOperators = ["$where", "$function", "$accumulator"];
2289
2294
  export class ForeignKeyError extends global.Error {
2290
2295
  constructor(resourceName, foreignKeys) {
2291
2296
  super(`Cannot delete this ${resourceName}, as it is: ${foreignKeys.map((d) => ` the ${d.key} on ${d.resourceName} ${d.id}`).join("; ")}`);
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  },
7
7
  "description": "A form builder that sits on top of Angular.js, Twitter Bootstrap, jQuery UI, Angular-UI, Express and Mongoose. Opinionated or what?",
8
8
  "homepage": "http://forms-angular.org",
9
- "version": "0.12.0-beta.342",
9
+ "version": "0.12.0-beta.344",
10
10
  "packageManager": "npm@10.2.4",
11
11
  "type": "module",
12
12
  "engines": {