瀏覽代碼

reg exp in config

yuken 3 年之前
父節點
當前提交
476a2298c9

+ 6 - 1
packages/app/src/server/middlewares/api-rate-limiter.ts

@@ -40,7 +40,12 @@ module.exports = () => {
     const endpoint = req.path;
     const key = md5(req.ip + endpoint);
 
-    const customizedConfig = apiRateLimitConfig[endpoint];
+    const filterdKeys = Object.keys(apiRateLimitConfig).filter((key) => {
+      const keyRegExp = new RegExp(key);
+      return keyRegExp.test(endpoint);
+    });
+
+    const customizedConfig = apiRateLimitConfig[filterdKeys[0]];
 
     try {
       if (customizedConfig === undefined) {

+ 4 - 4
packages/app/src/server/util/api-rate-limit-config/defaultApiRateLimitConfig.ts

@@ -25,11 +25,11 @@ const defaultStrictConfigKey: IApiRateLimitConfig = {
     method: 'POST',
     maxRequests: defaultStrictMaxRequests,
   },
-  '/forgot-password/:token': {
+  '/forgot-password/.*': {
     method: 'GET',
     maxRequests: defaultStrictMaxRequests,
   },
-  '/user-activation/:token': {
+  '/user-activation/.*': {
     method: 'GET',
     maxRequests: defaultStrictMaxRequests,
   },
@@ -37,11 +37,11 @@ const defaultStrictConfigKey: IApiRateLimitConfig = {
     method: 'POST',
     maxRequests: defaultStrictMaxRequests,
   },
-  '/download/:id([0-9a-z]{24})': {
+  '/download/[0-9a-z]{24}': {
     method: 'GET',
     maxRequests: defaultStrictMaxRequests,
   },
-  '/share/:linkId': {
+  '/share/[0-9a-z]{24}': {
     method: 'GET',
     maxRequests: defaultStrictMaxRequests,
   },