-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rate limiting to web input deletion mutation resolver
- Loading branch information
Showing
7 changed files
with
110 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
import { redisClient } from './redisClient' | ||
|
||
import { RedisBasicRateLimiter } from './RedisBasicRateLimiter' | ||
|
||
describe('RedisBasicRateLimiter', () => { | ||
const limiter = new RedisBasicRateLimiter(redisClient, { | ||
maxHits: 2, | ||
intervalSeconds: 10, | ||
limiterPrefix: 'test' // hour, | ||
}) | ||
|
||
const reset = async () => { | ||
await limiter.reset('127.0.0.1') | ||
} | ||
beforeAll(reset) | ||
afterAll(reset) | ||
it('should not throw when hits are not exceeded', async () => { | ||
await limiter.increment('127.0.0.1') | ||
await limiter.increment('127.0.0.1') | ||
}) | ||
it('should throw when max count of hits is exceeded', async () => { | ||
await expect( | ||
limiter.increment('127.0.0.1') | ||
Check failure on line 24 in backend/lib/RedisBasicRateLimiter.fn.spec.ts GitHub Actions / runs typescript, tests, and deployslib/RedisBasicRateLimiter.fn.spec.ts > RedisBasicRateLimiter > should throw when max count of hits is exceeded
|
||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
'"rate limit exceeded, try in 10 seconds"' | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import ms from 'ms' | ||
|
||
import { GraphQLError } from 'graphql' | ||
import { Redis } from '@upstash/redis' | ||
|
||
export class RateLimitedError extends GraphQLError { | ||
constructor(message: string, errorCode: string) { | ||
super(message, { | ||
extensions: { | ||
code: errorCode | ||
} | ||
}) | ||
} | ||
} | ||
|
||
export class RedisBasicRateLimiter { | ||
duration: string | ||
constructor( | ||
private redisClient: Redis, | ||
private options: { | ||
limiterPrefix: string | ||
maxHits: number | ||
intervalSeconds: number | ||
} | ||
) { | ||
this.duration = ms(this.options.intervalSeconds * 1000, { | ||
long: true | ||
}) | ||
} | ||
getKey(ip: string) { | ||
return `${this.options.limiterPrefix}_rate_limit_counter:${ip}` | ||
} | ||
|
||
/** | ||
* @param resourceKey can be an ip address or a user idjk | ||
*/ | ||
async increment(resourceKey: string) { | ||
const key = this.getKey(resourceKey) | ||
const res = await this.redisClient | ||
.multi() | ||
.incr(key) | ||
.expire(key, this.options.intervalSeconds) | ||
.exec() | ||
|
||
if (res && res[0] && (res[0][1] as number) > this.options.maxHits) { | ||
throw new RateLimitedError( | ||
`rate limit exceeded, try in ${this.duration}`, | ||
'RATE_LIMIT_EXCEEDED' | ||
) | ||
} | ||
} | ||
|
||
async reset(ip: string) { | ||
const key = this.getKey(ip) | ||
await this.redisClient.del(key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
import { Field, ObjectType, Int, GraphQLISODateTime, Ctx } from 'type-graphql' | ||
import { WebInputGQL } from './generated/WebInputGQL' | ||
import { Field, ObjectType, Ctx } from 'type-graphql' | ||
import { WebInputGQL, WebInputGQLScalars } from './generated/WebInputGQL' | ||
import debug from 'debug' | ||
import { IContextAuthenticated } from '../schemas/RootResolver' | ||
import { RedisBasicRateLimiter } from '../lib/RedisBasicRateLimiter' | ||
import { redisClient } from '../lib/redisClient' | ||
|
||
const log = debug('au:WebInput') | ||
|
||
const rateLimiter = new RedisBasicRateLimiter(redisClient, { | ||
limiterPrefix: 'web_input_delete', | ||
maxHits: 1, | ||
intervalSeconds: 3600 | ||
}) | ||
|
||
@ObjectType() | ||
export class WebInputMutation extends WebInputGQL { | ||
@Field(() => Int) | ||
@Field(() => WebInputGQLScalars, { nullable: true }) | ||
async delete(@Ctx() ctx: IContextAuthenticated) { | ||
await rateLimiter.increment(ctx.jwtPayload.userId) | ||
log('delete of WebInput id: ', this.id) | ||
// TODO rate limit this to like 1 per hour | ||
|
||
return ctx.prisma.webInput.delete({ | ||
const res = await ctx.prisma.webInput.delete({ | ||
where: { id: this.id } | ||
}) | ||
|
||
return res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mutation removeWebInput($id: Int!) { | ||
webInput(id: $id) { | ||
delete | ||
delete { | ||
id | ||
} | ||
} | ||
} |