From a43c7b04d366f8eddf77911e47253c31ba8b08b8 Mon Sep 17 00:00:00 2001 From: Frank Chen Date: Fri, 13 Aug 2021 08:11:32 +0000 Subject: [PATCH 1/2] fix: Add 10 MB max lnegth checks to form field editor --- src/app/models/form.server.model.ts | 27 +------------------ .../edit-fields-modal.client.controller.js | 7 +++++ .../admin/views/edit-fields.client.modal.html | 12 +++++++++ 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/app/models/form.server.model.ts b/src/app/models/form.server.model.ts index 07dd28a682..3601be434b 100644 --- a/src/app/models/form.server.model.ts +++ b/src/app/models/form.server.model.ts @@ -1,4 +1,4 @@ -import BSON, { ObjectId } from 'bson-ext' +import { ObjectId } from 'bson-ext' import { compact, omit, pick, uniq } from 'lodash' import mongoose, { Mongoose, @@ -9,7 +9,6 @@ import mongoose, { } from 'mongoose' import validator from 'validator' -import { MB } from '../../../shared/constants/file' import { ADMIN_FORM_META_FIELDS, EMAIL_FORM_SETTINGS_FIELDS, @@ -91,23 +90,6 @@ import getUserModel from './user.server.model' export const FORM_SCHEMA_ID = 'Form' -const bson = new BSON([ - BSON.Binary, - BSON.Code, - BSON.DBRef, - BSON.Decimal128, - BSON.Double, - BSON.Int32, - BSON.Long, - BSON.Map, - BSON.MaxKey, - BSON.MinKey, - BSON.ObjectId, - BSON.BSONRegExp, - BSON.Symbol, - BSON.Timestamp, -]) - const formSchemaOptions: SchemaOptions = { id: false, toJSON: { @@ -857,13 +839,6 @@ const compileFormModel = (db: Mongoose): IFormModel => { // Hooks FormSchema.pre('validate', function (next) { - // Reject save if form document is too large - if (bson.calculateObjectSize(this) > 10 * MB) { - const err = new Error('Form size exceeded.') - err.name = 'FormSizeError' - return next(err) - } - // Validate that admin exists before form is created. return User.findById(this.admin).then((admin) => { if (!admin) { diff --git a/src/public/modules/forms/admin/controllers/edit-fields-modal.client.controller.js b/src/public/modules/forms/admin/controllers/edit-fields-modal.client.controller.js index f0c41eb7ab..938ad97bed 100644 --- a/src/public/modules/forms/admin/controllers/edit-fields-modal.client.controller.js +++ b/src/public/modules/forms/admin/controllers/edit-fields-modal.client.controller.js @@ -172,6 +172,10 @@ function EditFieldsModalController( return Object.keys(myDict).length !== field.fieldOptions.length } + vm.showFieldDataTooLongError = function (field) { + return JSON.stringify(field).length > 10 * MB + } + vm.showEmptyOptionsError = function (field) { // Empty options should only appear on fields that have options to choose from // A blank option is considered to be a non-empty option @@ -384,6 +388,9 @@ function EditFieldsModalController( } vm.disableSave = function () { + if (vm.showFieldDataTooLongError(vm.field)) { + return true + } if ( vm.field.fieldType === 'attachment' || vm.field.fieldType === 'dropdown' || diff --git a/src/public/modules/forms/admin/views/edit-fields.client.modal.html b/src/public/modules/forms/admin/views/edit-fields.client.modal.html index 6dfd863430..4d5e76d7b7 100644 --- a/src/public/modules/forms/admin/views/edit-fields.client.modal.html +++ b/src/public/modules/forms/admin/views/edit-fields.client.modal.html @@ -969,6 +969,18 @@