mongoose-field-encryption icon indicating copy to clipboard operation
mongoose-field-encryption copied to clipboard

Is there a size limit when saving binary data from Buffer with mongoose-field-encryption?

Open bluepuma77 opened this issue 3 years ago • 1 comments

We would like to save images to MongoDB and want to encrypt the binary data field. And we are aware that the MongoDB object size limit is around 16MB (Stackoverflow info).

But even when the image only has 3.6MB I receive an error with file.save(): ERROR The value of "offset" is out of range. It must be >= 0 && <= 17825792. Received 17825794

// mongoose model
import mongoose from 'mongoose';
const { Schema, model } = mongoose;
import { v4 as uuidv4 } from 'uuid';
import { fieldEncryption } from 'mongoose-field-encryption';

export interface File extends Document {
    id: string,
    blob: Buffer,
    mime: string,
    size: number,
}

const FileSchema = new Schema<File>(
    {
        id: { type: String, required: true, unique: true, index: true, default: () => uuidv4()},
        blob: { type: Buffer },
        mime: { type: String },
        size: { type: Number },
    }
);

FileSchema.plugin(fieldEncryption, { fields: ['blob'], secret: process.env.DB_SECRET });

export default model<File>('Blob', FileSchema);

Is there a specific size limit when saving binary data from Buffer with mongoose-field-encryption?

bluepuma77 avatar Jun 09 '22 11:06 bluepuma77

Hi and sorry for the delay in responding to this. The issue at hand is that mfe does a JSON.stringify on a non-string field which then explodes in size for a Buffer.

I would highly recommend that you encrypt the files but store them somewhere else on a server and just save the encryption keys in Mongo.

wheresvic avatar Sep 12 '22 15:09 wheresvic