Skip to content

Commit 6010a97

Browse files
christopherholland-workdaygithub-advanced-security[bot]christopherholland-workday
authored
Potential fix for code scanning alert no. 84: Unvalidated dynamic method call (#5746)
* Potential fix for code scanning alert no. 84: Unvalidated dynamic method call Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Potential fix for code scanning alert no. 84: Unvalidated dynamic method call#5746 * Potential fix for code scanning alert no. 100: Unvalidated dynamic method call Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Potential fix for code scanning alert no. 84: Unvalidated dynamic method call#5746 --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: christopherholland-workday <christopher.holland+evisort@workday.com>
1 parent 56428cc commit 6010a97

1 file changed

Lines changed: 32 additions & 26 deletions

File tree

packages/server/src/utils/rateLimit.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const QUEUE_NAME = 'ratelimit'
1414
const QUEUE_EVENT_NAME = 'updateRateLimiter'
1515

1616
export class RateLimiterManager {
17-
private rateLimiters: Record<string, RateLimitRequestHandler> = {}
17+
private rateLimiters: Map<string, RateLimitRequestHandler> = new Map()
1818
private rateLimiterMutex: Mutex = new Mutex()
1919
private redisClient: Redis
2020
private static instance: RateLimiterManager
@@ -95,50 +95,56 @@ export class RateLimiterManager {
9595
const release = await this.rateLimiterMutex.acquire()
9696
try {
9797
if (process.env.MODE === MODE.QUEUE) {
98-
this.rateLimiters[id] = rateLimit({
99-
windowMs: duration * 1000,
100-
max: limit,
101-
standardHeaders: true,
102-
legacyHeaders: false,
103-
message,
104-
store: new RedisStore({
105-
prefix: `rl:${id}`,
106-
// @ts-expect-error - Known issue: the `call` function is not present in @types/ioredis
107-
sendCommand: (...args: string[]) => this.redisClient.call(...args)
98+
this.rateLimiters.set(
99+
id,
100+
rateLimit({
101+
windowMs: duration * 1000,
102+
max: limit,
103+
standardHeaders: true,
104+
legacyHeaders: false,
105+
message,
106+
store: new RedisStore({
107+
prefix: `rl:${id}`,
108+
// @ts-expect-error - Known issue: the `call` function is not present in @types/ioredis
109+
sendCommand: (...args: string[]) => this.redisClient.call(...args)
110+
})
108111
})
109-
})
112+
)
110113
} else {
111-
this.rateLimiters[id] = rateLimit({
112-
windowMs: duration * 1000,
113-
max: limit,
114-
message
115-
})
114+
this.rateLimiters.set(
115+
id,
116+
rateLimit({
117+
windowMs: duration * 1000,
118+
max: limit,
119+
message
120+
})
121+
)
116122
}
117123
} finally {
118124
release()
119125
}
120126
}
121127

122128
public removeRateLimiter(id: string): void {
123-
if (this.rateLimiters[id]) {
124-
delete this.rateLimiters[id]
125-
}
129+
this.rateLimiters.delete(id)
126130
}
127131

128132
public getRateLimiter(): (req: Request, res: Response, next: NextFunction) => void {
129133
return (req: Request, res: Response, next: NextFunction) => {
130134
const id = req.params.id
131-
if (!this.rateLimiters[id]) return next()
132-
const idRateLimiter = this.rateLimiters[id]
133-
return idRateLimiter(req, res, next)
135+
if (typeof id === 'string' && id.length > 0 && this.rateLimiters.has(id)) {
136+
return this.rateLimiters.get(id)!(req, res, next)
137+
}
138+
return next()
134139
}
135140
}
136141

137142
public getRateLimiterById(id: string): (req: Request, res: Response, next: NextFunction) => void {
138143
return (req: Request, res: Response, next: NextFunction) => {
139-
if (!this.rateLimiters[id]) return next()
140-
const idRateLimiter = this.rateLimiters[id]
141-
return idRateLimiter(req, res, next)
144+
if (this.rateLimiters.has(id)) {
145+
return this.rateLimiters.get(id)!(req, res, next)
146+
}
147+
return next()
142148
}
143149
}
144150

0 commit comments

Comments
 (0)