summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/ApiCallService.ts
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2024-12-08 11:33:57 -0500
committerHazelnoot <acomputerdog@gmail.com>2024-12-08 11:33:57 -0500
commit7c002ce56ef86f8a375275a78c0bda38d540c131 (patch)
tree50141866b198b8cbedec7237f742010b3699d0ae /packages/backend/src/server/api/ApiCallService.ts
parentcheck for invalid rate limit inputs (diff)
downloadsharkey-7c002ce56ef86f8a375275a78c0bda38d540c131.tar.gz
sharkey-7c002ce56ef86f8a375275a78c0bda38d540c131.tar.bz2
sharkey-7c002ce56ef86f8a375275a78c0bda38d540c131.zip
move all Rate Limit type defs to rate-limit-utils.ts
Diffstat (limited to 'packages/backend/src/server/api/ApiCallService.ts')
-rw-r--r--packages/backend/src/server/api/ApiCallService.ts19
1 files changed, 9 insertions, 10 deletions
diff --git a/packages/backend/src/server/api/ApiCallService.ts b/packages/backend/src/server/api/ApiCallService.ts
index 38d33c761d..6ad4bc8cb5 100644
--- a/packages/backend/src/server/api/ApiCallService.ts
+++ b/packages/backend/src/server/api/ApiCallService.ts
@@ -18,8 +18,8 @@ import { createTemp } from '@/misc/create-temp.js';
import { bindThis } from '@/decorators.js';
import { RoleService } from '@/core/RoleService.js';
import type { Config } from '@/config.js';
-import { sendRateLimitHeaders } from '@/misc/rate-limit-utils.js';
-import { LegacyRateLimit, SkRateLimiterService } from '@/server/api/SkRateLimiterService.js';
+import { RateLimit, sendRateLimitHeaders } from '@/misc/rate-limit-utils.js';
+import { SkRateLimiterService } from '@/server/api/SkRateLimiterService.js';
import { ApiError } from './error.js';
import { ApiLoggerService } from './ApiLoggerService.js';
import { AuthenticateService, AuthenticationError } from './AuthenticateService.js';
@@ -304,7 +304,7 @@ export class ApiCallService implements OnApplicationShutdown {
}
// For endpoints without a limit, the default is 10 calls per second
- const endpointLimit: IEndpointMeta['limit'] = ep.meta.limit ?? {
+ const endpointLimit = ep.meta.limit ?? {
duration: 1000,
max: 10,
};
@@ -320,18 +320,17 @@ export class ApiCallService implements OnApplicationShutdown {
limitActor = getIpHash(request.ip);
}
- const limit = Object.assign({}, endpointLimit);
-
- if (limit.key == null) {
- (limit as any).key = ep.name;
- }
-
// TODO: 毎リクエスト計算するのもあれだしキャッシュしたい
const factor = user ? (await this.roleService.getUserPolicies(user.id)).rateLimitFactor : 1;
if (factor > 0) {
+ const limit = {
+ key: ep.name,
+ ...endpointLimit,
+ } as RateLimit;
+
// Rate limit
- const info = await this.rateLimiterService.limit(limit as LegacyRateLimit, limitActor, factor);
+ const info = await this.rateLimiterService.limit(limit, limitActor, factor);
sendRateLimitHeaders(reply, info);