wheresvic / mongoose-field-encryption

A simple symmetric encryption plugin for individual fields. Dependency free, only mongoose peer dependency.
MIT License
74 stars 32 forks source link

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

Open bluepuma77 opened 2 years ago

bluepuma77 commented 2 years ago

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?

wheresvic commented 2 years ago

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.