Skip to content

Commit e35856c

Browse files
Fix rate limiting early exit (#55141)
Co-authored-by: Evan Bonsignori <[email protected]>
1 parent 3c89b6d commit e35856c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/shielding/middleware/rate-limit.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if (isNaN(MAX)) {
1414
}
1515

1616
// We apply this rate limiter to _all_ routes in src/shielding/index.ts except for `/api/*` routes
17-
export function createRateLimiter(max = MAX, isAPILimiter = false) {
17+
export function createRateLimiter(max = MAX) {
1818
return rateLimit({
1919
// 1 minute
2020
windowMs: EXPIRES_IN_AS_SECONDS * 1000,
@@ -47,14 +47,9 @@ export function createRateLimiter(max = MAX, isAPILimiter = false) {
4747
return true
4848
}
4949

50-
// We handle /api/* routes with a separate rate limiter
51-
// When it is a separate rate limiter, isAPILimiter will be passed as true
52-
if (req.path.startsWith('/api/') || isAPILimiter) {
53-
return false
54-
}
55-
56-
// If the request is not suspicious, don't rate limit it
57-
if (!isSuspiciousRequest(req)) {
50+
// If the query string looks totally regular and is not a
51+
// search endpoint, then skip
52+
if (!isSuspiciousSearchRequest(req)) {
5853
return true
5954
}
6055

@@ -137,7 +132,14 @@ const MISC_KEYS = [
137132
* @param {Request} req
138133
* @returns boolean
139134
*/
140-
function isSuspiciousRequest(req: Request) {
135+
function isSuspiciousSearchRequest(req: Request) {
136+
if (
137+
req.originalUrl.includes('/api/search') ||
138+
req.originalUrl.includes('/api/ai-search') ||
139+
req.originalUrl.includes('/api/combined-search')
140+
)
141+
return false
142+
141143
const keys = Object.keys(req.query)
142144

143145
// Since this function can only speculate by query strings (at the

0 commit comments

Comments
 (0)