Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(webapp): share rate limit bucket across additional API keys per environment#4508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: fix | ||
| --- | ||
| API rate limits now apply per environment, so creating extra API keys no longer increases how many requests an environment can make. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -301,6 +301,89 @@ export async function findEnvironmentByApiKeyWithResolution( | ||
| return resolveEnvironmentByApiKey(apiKey, branchName, tx, additionalApiKeyLookupEnabled); | ||
| } | ||
| export type PrivateApiKeyRateLimitScope = { | ||
| environmentId: string; | ||
| apiRateLimiterConfig: unknown; | ||
| }; | ||
| export async function resolvePrivateApiKeyRateLimitScope( | ||
| apiKey: string, | ||
| tx: PrismaClientOrTransaction = $replica | ||
| ): Promise<PrivateApiKeyRateLimitScope | null> { | ||
| const now = new Date(); | ||
| if (isAdditionalApiKey(apiKey)) { | ||
| const match = await tx.apiKey.findFirst({ | ||
| where: { | ||
| keyHash: hashApiKey(apiKey), | ||
| revokedAt: null, | ||
| OR: [{ expiresAt: null }, { expiresAt: { gt: now } }], | ||
| }, | ||
| select: { | ||
| runtimeEnvironment: { | ||
| select: { | ||
| id: true, | ||
| project: { select: { deletedAt: true } }, | ||
| organization: { select: { apiRateLimiterConfig: true } }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
carderne marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (!match?.runtimeEnvironment || match.runtimeEnvironment.project.deletedAt) { | ||
| return null; | ||
| } | ||
| return { | ||
| environmentId: match.runtimeEnvironment.id, | ||
| apiRateLimiterConfig: match.runtimeEnvironment.organization.apiRateLimiterConfig, | ||
| }; | ||
| } | ||
| const environment = await tx.runtimeEnvironment.findFirst({ | ||
| where: { apiKey }, | ||
| select: { | ||
| id: true, | ||
| project: { select: { deletedAt: true } }, | ||
| organization: { select: { apiRateLimiterConfig: true } }, | ||
| }, | ||
| }); | ||
carderne marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (environment) { | ||
| if (environment.project.deletedAt) { | ||
| return null; | ||
| } | ||
| return { | ||
| environmentId: environment.id, | ||
| apiRateLimiterConfig: environment.organization.apiRateLimiterConfig, | ||
| }; | ||
| } | ||
| const revokedApiKey = await tx.revokedApiKey.findFirst({ | ||
| where: { apiKey, expiresAt: { gt: now } }, | ||
| select: { | ||
| runtimeEnvironment: { | ||
| select: { | ||
| id: true, | ||
| project: { select: { deletedAt: true } }, | ||
| organization: { select: { apiRateLimiterConfig: true } }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| const revokedEnvironment = revokedApiKey?.runtimeEnvironment; | ||
| if (!revokedEnvironment || revokedEnvironment.project.deletedAt) { | ||
| return null; | ||
| } | ||
| return { | ||
| environmentId: revokedEnvironment.id, | ||
| apiRateLimiterConfig: revokedEnvironment.organization.apiRateLimiterConfig, | ||
| }; | ||
| } | ||
| /** | ||
| * @deprecated We don't use public API keys (`pk_*` tokens) anymore — public | ||
| * access goes through public JWTs (see `isPublicJWT` / `validatePublicJwtKey`). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { tryCatch } from "@trigger.dev/core/v3"; | ||
| import { env } from "~/env.server"; | ||
| import { resolvePrivateApiKeyRateLimitScope } from "~/models/runtimeEnvironment.server"; | ||
| import { batchStreamGrants } from "~/runEngine/concerns/batchStreamGrantsInstance.server"; | ||
| import { authenticateAuthorizationHeader } from "./apiAuth.server"; | ||
| import { authorizationRateLimitMiddleware } from "./authorizationRateLimitMiddleware.server"; | ||
| @@ -29,6 +30,21 @@ export const apiRateLimiter = authorizationRateLimitMiddleware({ | ||
| maxItems: 1000, | ||
| }, | ||
| limiterConfigOverride: async (authorizationValue) => { | ||
| const rawApiKey = authorizationValue.replace(/^Bearer /, ""); | ||
| if (rawApiKey.startsWith("tr_")) { | ||
| const scope = await resolvePrivateApiKeyRateLimitScope(rawApiKey); | ||
| if (!scope) { | ||
| return; | ||
| } | ||
| return { | ||
| config: scope.apiRateLimiterConfig, | ||
| identifier: scope.environmentId, | ||
| }; | ||
| } | ||
| const authenticatedEnv = await authenticateAuthorizationHeader(authorizationValue, { | ||
| allowPublicKey: true, | ||
| allowJWT: true, | ||
| @@ -40,13 +56,19 @@ export const apiRateLimiter = authorizationRateLimitMiddleware({ | ||
| if (authenticatedEnv.type === "PUBLIC_JWT") { | ||
| return { | ||
| type: "fixedWindow", | ||
| window: env.API_RATE_LIMIT_JWT_WINDOW, | ||
| tokens: env.API_RATE_LIMIT_JWT_TOKENS, | ||
| config: { | ||
| type: "fixedWindow", | ||
| window: env.API_RATE_LIMIT_JWT_WINDOW, | ||
| tokens: env.API_RATE_LIMIT_JWT_TOKENS, | ||
| }, | ||
| }; | ||
| } else { | ||
| return authenticatedEnv.environment.organization.apiRateLimiterConfig; | ||
| } | ||
| return { | ||
| config: authenticatedEnv.environment.organization.apiRateLimiterConfig, | ||
| // Public keys are browser-distributed, so keep them on per-key buckets. | ||
| identifier: authenticatedEnv.type === "PRIVATE" ? authenticatedEnv.environment.id : undefined, | ||
| }; | ||
carderne marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. carderne marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }, | ||
| pathMatchers: [/^\/api/], | ||
| // Allow /api/v1/tasks/:id/callback/:secret | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.