-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from serenity-kit/OPA-01-005
fix OPA-01-005: add rate limiting
- Loading branch information
Showing
22 changed files
with
265 additions
and
18 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
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
50 changes: 50 additions & 0 deletions
50
examples/fullstack-e2e-encrypted-locker-nextjs/app/api/rateLimiter.ts
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,50 @@ | ||
import { NextRequest } from "next/server"; | ||
|
||
const limit = 40; | ||
const limitWindow = 1000 * 60; // 1 minute | ||
|
||
const rateLimiter = new Map< | ||
string, | ||
{ requestCount: number; firstRequest: Date } | ||
>(); | ||
|
||
type Params = { | ||
request: NextRequest; | ||
}; | ||
|
||
/** Simple rate-limiter based on the IP */ | ||
export const checkRateLimit = ({ request }: Params) => { | ||
const ip = request.ip || "anonymous"; | ||
const existingEntry = rateLimiter.get(ip); | ||
if (!existingEntry) { | ||
rateLimiter.set(ip, { | ||
requestCount: 1, | ||
firstRequest: new Date(), | ||
}); | ||
return false; | ||
} | ||
|
||
const { requestCount, firstRequest } = existingEntry; | ||
const now = new Date(); | ||
|
||
// if the first request was more than 1 minute ago, reset the counter | ||
if (now.getTime() - firstRequest.getTime() > limitWindow) { | ||
rateLimiter.set(ip, { | ||
requestCount: 1, | ||
firstRequest: now, | ||
}); | ||
return false; | ||
} | ||
|
||
// if the request count is more than the limit, block the request | ||
if (requestCount > limit) { | ||
return true; | ||
} | ||
|
||
// otherwise, increment the request count and allow the request | ||
rateLimiter.set(ip, { | ||
requestCount: requestCount + 1, | ||
firstRequest, | ||
}); | ||
return false; | ||
}; |
8 changes: 8 additions & 0 deletions
8
examples/fullstack-e2e-encrypted-locker-nextjs/app/api/recovery/login/finish/route.ts
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
8 changes: 8 additions & 0 deletions
8
examples/fullstack-e2e-encrypted-locker-nextjs/app/api/recovery/login/start/route.ts
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
12 changes: 10 additions & 2 deletions
12
examples/fullstack-e2e-encrypted-locker-nextjs/app/api/recovery/register/finish/route.ts
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
12 changes: 10 additions & 2 deletions
12
examples/fullstack-e2e-encrypted-locker-nextjs/app/api/recovery/route.ts
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
12 changes: 10 additions & 2 deletions
12
examples/fullstack-e2e-encrypted-locker-nextjs/app/api/register/finish/route.ts
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
8 changes: 8 additions & 0 deletions
8
examples/fullstack-simple-nextjs/app/api/login/finish/route.ts
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,50 @@ | ||
import { NextRequest } from "next/server"; | ||
|
||
const limit = 40; | ||
const limitWindow = 1000 * 60; // 1 minute | ||
|
||
const rateLimiter = new Map< | ||
string, | ||
{ requestCount: number; firstRequest: Date } | ||
>(); | ||
|
||
type Params = { | ||
request: NextRequest; | ||
}; | ||
|
||
/** Simple rate-limiter based on the IP */ | ||
export const checkRateLimit = ({ request }: Params) => { | ||
const ip = request.ip || "anonymous"; | ||
const existingEntry = rateLimiter.get(ip); | ||
if (!existingEntry) { | ||
rateLimiter.set(ip, { | ||
requestCount: 1, | ||
firstRequest: new Date(), | ||
}); | ||
return false; | ||
} | ||
|
||
const { requestCount, firstRequest } = existingEntry; | ||
const now = new Date(); | ||
|
||
// if the first request was more than 1 minute ago, reset the counter | ||
if (now.getTime() - firstRequest.getTime() > limitWindow) { | ||
rateLimiter.set(ip, { | ||
requestCount: 1, | ||
firstRequest: now, | ||
}); | ||
return false; | ||
} | ||
|
||
// if the request count is more than the limit, block the request | ||
if (requestCount > limit) { | ||
return true; | ||
} | ||
|
||
// otherwise, increment the request count and allow the request | ||
rateLimiter.set(ip, { | ||
requestCount: requestCount + 1, | ||
firstRequest, | ||
}); | ||
return false; | ||
}; |
12 changes: 10 additions & 2 deletions
12
examples/fullstack-simple-nextjs/app/api/register/finish/route.ts
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
Oops, something went wrong.