Skip to content

Commit 0726c7c

Browse files
committed
T3C-559 Adds environment configuration to rate limits
1 parent bbe68e4 commit 0726c7c

File tree

6 files changed

+10
-2
lines changed

6 files changed

+10
-2
lines changed

deploy/cloudrun/express-server.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ spec:
8787
secretKeyRef:
8888
key: latest
8989
name: analytics-api-key
90+
- name: RATE_LIMIT_PREFIX
91+
value: dev-PR_NUMBER
9092

9193
resources:
9294
limits:

express-server/src/__tests__/analytics-integration.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe("Analytics Integration with Common Package", () => {
5959
ANALYTICS_ENABLED: true,
6060
ANALYTICS_DEBUG: false,
6161
FIREBASE_ADMIN_PROJECT_ID: undefined,
62+
RATE_LIMIT_PREFIX: "test",
6263
...overrides,
6364
});
6465

express-server/src/__tests__/featureFlagsIntegration.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const createMockEnv = (
5353
ANALYTICS_ENABLED: false,
5454
ANALYTICS_DEBUG: false,
5555
FIREBASE_ADMIN_PROJECT_ID: undefined,
56+
RATE_LIMIT_PREFIX: "test",
5657
});
5758

5859
// Mock validateEnv to return a valid test environment

express-server/src/routes/__tests__/create.csv-security.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ describe("CSV Security in Create Route", () => {
9090
ANALYTICS_FLUSH_AT: 20,
9191
ANALYTICS_FLUSH_INTERVAL: 10000,
9292
ANALYTICS_DEBUG: false,
93+
RATE_LIMIT_PREFIX: "test",
9394
},
9495
};
9596
// Mock logger for RequestWithLogger interface

express-server/src/server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ initializeAnalyticsClient(env);
107107
// This is added here so that the worker gets initialized. Queue is referenced in /create, so its initialized there.
108108
setupWorkers(connection, env.REDIS_QUEUE_NAME);
109109

110+
const rateLimitPrefix = env.RATE_LIMIT_PREFIX;
111+
110112
const defaultRateLimiter = rateLimit({
111113
windowMs: 15 * 60 * 1000, // 15 minutes
112114
max: 100, // Limit each IP to 100 requests per windowMs
@@ -119,7 +121,7 @@ const defaultRateLimiter = rateLimit({
119121
store: new RedisStore({
120122
sendCommand: (command: string, ...args: string[]) =>
121123
connection.call(command, ...args) as Promise<RedisReply>,
122-
prefix: "rate-limit-default",
124+
prefix: `${rateLimitPrefix}-rate-limit-default`,
123125
}),
124126
});
125127

@@ -136,7 +138,7 @@ const reportRateLimiter = rateLimit({
136138
store: new RedisStore({
137139
sendCommand: (command: string, ...args: string[]) =>
138140
connection.call(command, ...args) as Promise<RedisReply>,
139-
prefix: "rate-limit-report",
141+
prefix: `${rateLimitPrefix}-rate-limit-report`,
140142
}),
141143
});
142144

express-server/src/types/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export const env = z.object({
135135
.default("false")
136136
.transform((val) => val.toLowerCase() === "true"),
137137
FIREBASE_ADMIN_PROJECT_ID: z.string().optional(),
138+
RATE_LIMIT_PREFIX: z.string().optional().default("dev"),
138139
});
139140

140141
export type Env = z.infer<typeof env>;

0 commit comments

Comments
 (0)