Skip to content

Commit 040f840

Browse files
committed
feat: graphql rate limiting directive
1 parent 2b54f65 commit 040f840

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010
- Added Page Delete functionality
1111
- Dev locale .yml files in `server/locales` are now loaded
1212
- Added SQLite dependencies in Docker image
13+
- Added rate limiting to login mutations
1314

1415
### Fixed
1516
- Fixed root admin refresh token fail

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"getos": "3.1.1",
6969
"graphql": "14.1.1",
7070
"graphql-list-fields": "2.0.2",
71+
"graphql-rate-limit-directive": "0.1.0",
7172
"graphql-subscriptions": "1.0.0",
7273
"graphql-tools": "4.0.4",
7374
"highlight.js": "9.14.2",

server/graph/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const autoload = require('auto-load')
66
const PubSub = require('graphql-subscriptions').PubSub
77
const { LEVEL, MESSAGE } = require('triple-beam')
88
const Transport = require('winston-transport')
9+
const { createRateLimitTypeDef, createRateLimitDirective } = require('graphql-rate-limit-directive')
910

1011
/* global WIKI */
1112

@@ -17,7 +18,7 @@ WIKI.GQLEmitter = new PubSub()
1718

1819
// Schemas
1920

20-
let typeDefs = []
21+
let typeDefs = [createRateLimitTypeDef()]
2122
let schemas = fs.readdirSync(path.join(WIKI.SERVERPATH, 'graph/schemas'))
2223
schemas.forEach(schema => {
2324
typeDefs.push(fs.readFileSync(path.join(WIKI.SERVERPATH, `graph/schemas/${schema}`), 'utf8'))
@@ -33,7 +34,12 @@ resolversObj.forEach(resolver => {
3334

3435
// Directives
3536

36-
let schemaDirectives = autoload(path.join(WIKI.SERVERPATH, 'graph/directives'))
37+
let schemaDirectives = {
38+
...autoload(path.join(WIKI.SERVERPATH, 'graph/directives')),
39+
rateLimit: createRateLimitDirective({
40+
keyGenerator: (directiveArgs, source, args, context, info) => `${context.req.ip}:${info.parentType}.${info.fieldName}`
41+
})
42+
}
3743

3844
// Live Trail Logger (admin)
3945

server/graph/schemas/authentication.graphql

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ type AuthenticationMutation {
2929
username: String!
3030
password: String!
3131
strategy: String!
32-
): AuthenticationLoginResponse
32+
): AuthenticationLoginResponse @rateLimit(limit: 5, duration: 60)
3333

3434
loginTFA(
3535
loginToken: String!
3636
securityCode: String!
37-
): DefaultResponse
37+
): DefaultResponse @rateLimit(limit: 5, duration: 60)
3838

3939
register(
4040
email: String!

yarn.lock

+17-2
Original file line numberDiff line numberDiff line change
@@ -5582,6 +5582,16 @@ [email protected]:
55825582
loader-utils "^1.1.0"
55835583
persistgraphql "^0.3.11"
55845584

5585+
5586+
version "0.1.0"
5587+
resolved "https://registry.yarnpkg.com/graphql-rate-limit-directive/-/graphql-rate-limit-directive-0.1.0.tgz#70888cd4fa84fcde731ffcb445fbc7a25fa5a790"
5588+
integrity sha512-mVZzuZUl6gmIHYaaMvO7YncHhW2R9B/6Tbr2Ka+kQT39KFIFTHh5+DTqUU4O/GxKcNfWO7Xm3dVF+dLcS+eF3g==
5589+
dependencies:
5590+
graphql "^14.0.2"
5591+
graphql-tag "^2.10.0"
5592+
graphql-tools "^4.0.0"
5593+
rate-limiter-flexible "^0.22.0"
5594+
55855595
graphql-request@^1.5.0:
55865596
version "1.8.2"
55875597
resolved "https://registry.yarnpkg.com/graphql-request/-/graphql-request-1.8.2.tgz#398d10ae15c585676741bde3fc01d5ca948f8fbe"
@@ -5596,7 +5606,7 @@ [email protected], graphql-subscriptions@^1.0.0:
55965606
dependencies:
55975607
iterall "^1.2.1"
55985608

5599-
graphql-tag@^2.0.0, graphql-tag@^2.10.1, graphql-tag@^2.4.2, graphql-tag@^2.9.2:
5609+
graphql-tag@^2.0.0, graphql-tag@^2.10.0, graphql-tag@^2.10.1, graphql-tag@^2.4.2, graphql-tag@^2.9.2:
56005610
version "2.10.1"
56015611
resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.10.1.tgz#10aa41f1cd8fae5373eaf11f1f67260a3cad5e02"
56025612
integrity sha512-jApXqWBzNXQ8jYa/HLkZJaVw9jgwNqZkywa2zfFn16Iv1Zb7ELNHkJaXHR7Quvd5SIGsy6Ny7SUKATgnu05uEg==
@@ -5640,7 +5650,7 @@ [email protected]:
56405650
svg-pan-zoom "^3.5.2"
56415651
viz.js "2.0.0"
56425652

5643-
5653+
[email protected], graphql@^14.0.2:
56445654
version "14.1.1"
56455655
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.1.1.tgz#d5d77df4b19ef41538d7215d1e7a28834619fac0"
56465656
integrity sha512-C5zDzLqvfPAgTtP8AUPIt9keDabrdRAqSWjj2OPRKrKxI9Fb65I36s1uCs1UUBFnSWTdO7hyHi7z1ZbwKMKF6Q==
@@ -10589,6 +10599,11 @@ range-parser@^1.0.3, range-parser@~1.2.0:
1058910599
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.0.tgz#f49be6b487894ddc40dcc94a322f611092e00d5e"
1059010600
integrity sha1-9JvmtIeJTdxA3MlKMi9hEJLgDV4=
1059110601

10602+
rate-limiter-flexible@^0.22.0:
10603+
version "0.22.4"
10604+
resolved "https://registry.yarnpkg.com/rate-limiter-flexible/-/rate-limiter-flexible-0.22.4.tgz#bdaee6968f527b9f8577163d7cca7a3620bb0d0f"
10605+
integrity sha512-DKGzgcB8JDLxnojhnhr62D+eqIHjIfXCOpdeHiE77TJves/qiLGHrZpqEpWZ3f3aNDnvA6YB4FrM3/zorETrMw==
10606+
1059210607
1059310608
version "2.6.4"
1059410609
resolved "https://registry.yarnpkg.com/raven/-/raven-2.6.4.tgz#458d4a380c8fbb59e0150c655625aaf60c167ea3"

0 commit comments

Comments
 (0)