mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Mongoose Plugin - Range Error: Maximum call stack size exceeded

Open thauburger opened this issue 14 years ago • 5 comments

Hi,

I'm currently working on a simple plugin for mongoose that abstracts some basic user authentication functionality.

The plugin is stored in user-basic-auth-mongoose.js:

var crypto = require('crypto');
var util = require('util');

module.exports = exports = function (schema, options) {

    options = options || {};
    options.encryptionMethod = options.encryptionMethod || 'sha1';

    schema.add({ 
        username : { type : String, required : true, index : { unique : true }},
        password : { type : String, required : true },
        salt     : { type : String, required : true }
    });

    schema.path('password').set(function (password) {
        this.salt = this.createSalt();
        this.password = this.encryptPassword(password);
    });

    schema.methods.createSalt   = function () {
        return Math.round(new Date().valueOf() * Math.random()) + '';
    };

    schema.methods.encryptPassword = function (plaintext) {
        return crypto.createHmac(options.encryptionMethod, this.salt).update(plaintext).digest('hex');
    };

    schema.methods.authenticate = function (plaintext) {
        return this.encryptPassword(plaintext) === this.password;
    };

    schema.virtual('id').get(function () {
        return this._id.toHexString();
    });

};

To test the plugin, I created simple userSchema in user.js:

var mongoose = require('mongoose');
var user_basic_auth = require('../user-basic-auth-mongoose');

var userSchema = new mongoose.Schema({
    email : String
});

userSchema.plugin(user_basic_auth);

module.exports = userSchema;

The schema is then used to create a simple test user:

var util = require('util');

var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://localhost/testusersas');


db.on('error', function (e) {
    console.log('Error: ' + e);
});

db.once('open', function () {

    var userSchema = require('./models/user');

    var User = db.model('User', userSchema);

    var u = new User({username: 'Tom', password: 'test', email: '[email protected]'});

    u.save(function (err, user) {
        if (err) console.error('Save error: ' + err);
        else {
            console.log('User saved! ' + util.inspect(user));
        }
    });

});

During the save operation, RangeError: Maximum call stack size exceeded is returned.

The problem seems to be with this section of the plugin code:

schema.path('password').set(function (password) {
    this.salt = this.createSalt();
    this.password = this.encryptPassword(password);
});

When I comment this out, I can successfully save an object (minus the password and salt, obviously).

Am I using the .path(), .set() functionality incorrectly? Or is this something else?

Thanks!

Tom

thauburger avatar Nov 27 '12 23:11 thauburger

I think I'm essentially creating an infinite loop here, as this.password is being set inside the set handler. If this is the case, I'm wondering if this is by design, or if the set handler should be aware that it is being called by itself.

Else, what is the prescribed usage for actually storing the password value in a case like this?

thauburger avatar Nov 27 '12 23:11 thauburger

design. usually you'd set up a virtual property to do the setting or actually set a property with a different name.

aheckmann avatar Nov 28 '12 00:11 aheckmann

@aheckmann Thanks. I just finished implementing a virtual password property.

thauburger avatar Nov 28 '12 00:11 thauburger

Hi, Same error I am getting while trying to save mongo document after encrypting it using a plugin name 'mongoose-encryption'. The error seems to be appearing because of encryption if I remove the encryption plugin it works fine. Any suggestions in this regard. PostSchema.plugin(encrypt, { encryptionKey: encKey, signingKey: sigKey }); <-plugin used

platinum736 avatar Jan 11 '18 09:01 platinum736

@platinum736 please open up a separate issue with code samples

vkarpov15 avatar Jan 11 '18 21:01 vkarpov15