diff --git a/CHANGELOG.md b/CHANGELOG.md index 8357a441..1a638c90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Added Page Delete functionality - Dev locale .yml files in `server/locales` are now loaded - Added SQLite dependencies in Docker image +- Added rate limiting to login mutations ### Fixed - Fixed root admin refresh token fail diff --git a/package.json b/package.json index c13f6a72..8d1be298 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "getos": "3.1.1", "graphql": "14.1.1", "graphql-list-fields": "2.0.2", + "graphql-rate-limit-directive": "0.1.0", "graphql-subscriptions": "1.0.0", "graphql-tools": "4.0.4", "highlight.js": "9.14.2", diff --git a/server/graph/index.js b/server/graph/index.js index 349b64d7..c8856b57 100644 --- a/server/graph/index.js +++ b/server/graph/index.js @@ -6,6 +6,7 @@ const autoload = require('auto-load') const PubSub = require('graphql-subscriptions').PubSub const { LEVEL, MESSAGE } = require('triple-beam') const Transport = require('winston-transport') +const { createRateLimitTypeDef, createRateLimitDirective } = require('graphql-rate-limit-directive') /* global WIKI */ @@ -17,7 +18,7 @@ WIKI.GQLEmitter = new PubSub() // Schemas -let typeDefs = [] +let typeDefs = [createRateLimitTypeDef()] let schemas = fs.readdirSync(path.join(WIKI.SERVERPATH, 'graph/schemas')) schemas.forEach(schema => { typeDefs.push(fs.readFileSync(path.join(WIKI.SERVERPATH, `graph/schemas/${schema}`), 'utf8')) @@ -33,7 +34,12 @@ resolversObj.forEach(resolver => { // Directives -let schemaDirectives = autoload(path.join(WIKI.SERVERPATH, 'graph/directives')) +let schemaDirectives = { + ...autoload(path.join(WIKI.SERVERPATH, 'graph/directives')), + rateLimit: createRateLimitDirective({ + keyGenerator: (directiveArgs, source, args, context, info) => `${context.req.ip}:${info.parentType}.${info.fieldName}` + }) +} // Live Trail Logger (admin) diff --git a/server/graph/schemas/authentication.graphql b/server/graph/schemas/authentication.graphql index ee69dc23..b2c12145 100644 --- a/server/graph/schemas/authentication.graphql +++ b/server/graph/schemas/authentication.graphql @@ -29,12 +29,12 @@ type AuthenticationMutation { username: String! password: String! strategy: String! - ): AuthenticationLoginResponse + ): AuthenticationLoginResponse @rateLimit(limit: 5, duration: 60) loginTFA( loginToken: String! securityCode: String! - ): DefaultResponse + ): DefaultResponse @rateLimit(limit: 5, duration: 60) register( email: String! diff --git a/yarn.lock b/yarn.lock index 2454c23a..5e395fe3 100644 Binary files a/yarn.lock and b/yarn.lock differ