-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(text-normalization): add an IP based ratelimit
- Loading branch information
Showing
7 changed files
with
99 additions
and
45 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
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,36 @@ | ||
import {NextFunction, Request, Response} from 'express'; | ||
import {getAppCheck} from 'firebase-admin/app-check'; | ||
|
||
// The public APP ID of the sign-mt web app | ||
const APP_ID = '1:665830225099:web:18e0669d5847a4b047974e'; | ||
|
||
// Create and cache an App Check token for the sign-mt web app | ||
let appCheckAPIKey = Promise.resolve({token: '', expires: 0}); | ||
|
||
export async function createAppCheckKey(req: Request, res: Response, next: NextFunction) { | ||
async function safeGetToken() { | ||
// If there was a failure to get the token, reset the promise before trying again | ||
try { | ||
return await appCheckAPIKey; | ||
} catch (e) { | ||
appCheckAPIKey = Promise.resolve({token: '', expires: 0}); | ||
throw e; | ||
} | ||
} | ||
|
||
let {token, expires} = await safeGetToken(); | ||
if (expires < Date.now()) { | ||
appCheckAPIKey = getAppCheck() | ||
.createToken(APP_ID) | ||
.then(({token, ttlMillis}) => ({token, expires: Date.now() - 1000 + ttlMillis})); | ||
({token, expires} = await safeGetToken()); | ||
} | ||
|
||
res.locals.appCheckToken = token; | ||
res.locals.headers = { | ||
'X-Firebase-AppCheck': token, | ||
'X-AppCheck-Token': token, | ||
}; | ||
|
||
return next(); | ||
} |
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,39 @@ | ||
import {Duration, Ratelimit, RatelimitResponse} from '@unkey/ratelimit'; | ||
import * as httpErrors from 'http-errors'; | ||
import * as requestIp from 'request-ip'; | ||
import {NextFunction, Request, Response} from 'express'; | ||
import {defineString} from 'firebase-functions/params'; | ||
|
||
export function rateLimitHeaders(res: Response, ratelimitResponse: RatelimitResponse, duration?: Duration) { | ||
res.setHeader('X-RateLimit-Limit', ratelimitResponse.limit.toString()); | ||
res.setHeader('X-RateLimit-Remaining', ratelimitResponse.remaining.toString()); | ||
res.setHeader('X-RateLimit-Reset', ratelimitResponse.reset.toString()); | ||
if (duration) { | ||
res.setHeader('X-RateLimit-Policy', `${ratelimitResponse.limit};w=${duration}`); | ||
} | ||
} | ||
|
||
export function unkeyRatelimit(namespace: string, limit: number, duration: Duration) { | ||
const unkeyRootKey = defineString('UNKEY_ROOT_KEY'); | ||
|
||
return async function (req: Request, res: Response, next: NextFunction) { | ||
const identifier = requestIp.getClientIp(req) ?? 'unknown'; | ||
|
||
const rateLimit = new Ratelimit({ | ||
rootKey: unkeyRootKey.value(), | ||
namespace, | ||
limit, | ||
duration, | ||
async: true, | ||
}); | ||
|
||
const ratelimitResponse = await rateLimit.limit(identifier); | ||
rateLimitHeaders(res, ratelimitResponse, duration); | ||
|
||
if (!ratelimitResponse.success) { | ||
throw new httpErrors.TooManyRequests('Too many requests, please try again later'); | ||
} | ||
|
||
return next(); | ||
}; | ||
} |
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