AdamPflug / express-brute

Brute-force protection middleware for express routes by rate limiting incoming requests
MIT License
564 stars 90 forks source link

Duplicate key error in collection when using mongo or mongoose stores #56

Open adriaanmeuris opened 7 years ago

adriaanmeuris commented 7 years ago

I was working with the express-brute-mongo store, and encountered this error:

Error: Cannot increment request count
errmsg: 'E11000 duplicate key error collection: mydb.bruteforce index: _id_ dup key: { : "G/IAnx1Agvj9X0JzMvChzHFl6jKU0ps4dnfVvJ+ZYTc=" }' } }

Since I couldn't get this resolved in the the mongo store module, I've tried the exact same implementation with the express-brute-mongoose store, and encountered the same error. Since the error gets triggered in express-brute, I'm hoping to find answers here on how to resolve this.

This is the middleware I set up (same as defaults, but logging the dup key error):

var rateLimitMiddleware = new ExpressBrute(bruteStore, {
  failCallback: function (req, res, next, nextValidRequestDate) {
    var err = new Error('Too many attempts.');
    err.status = 429;
    next(err);
  },
  handleStoreError: function (error) {
    console.log(error);
  }
});

My implementation with express-brute-mongo was:

var MongoStore = require('express-brute-mongo');
var MongoClient = require('mongodb').MongoClient;

var bruteStore = new MongoStore(function (ready) {
  MongoClient.connect(process.env['DB_URL'], function (err, db) {
    if (err) throw err;
    db.collection('ratelimits').ensureIndex({expires: 1}, {expireAfterSeconds: 0});
    ready(db.collection('ratelimits'));
  });
});

My implementation with express-brute-mongoose is:

var MongooseStore = require('express-brute-mongoose');
var BruteForceSchema = require('express-brute-mongoose/dist/schema');
var bruteModel = mongoose.model('bruteforce', BruteForceSchema);
var bruteStore = new MongooseStore(bruteModel);

Any help why i'm hitting dup key MongoErrors would be greatly appreciated.

heartz66 commented 6 years ago

+1