Skip to content

Commit

Permalink
fix: Missing plan check and ip whitelist parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Flo4604 committed Oct 13, 2024
1 parent 2028fe3 commit 648dcd3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
20 changes: 16 additions & 4 deletions apps/api/src/pkg/keys/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ type InvalidResponse = {
| "DISABLED"
| "INSUFFICIENT_PERMISSIONS";
key: Key;
identity: { id: string; externalId: string; meta: Record<string, unknown> | null } | null;
identity: {
id: string;
externalId: string;
meta: Record<string, unknown> | null;
} | null;
api: Api;
ratelimit?: {
remaining: number;
Expand All @@ -73,7 +77,11 @@ type ValidResponse = {
code?: never;
valid: true;
key: Key;
identity: { id: string; externalId: string; meta: Record<string, unknown> | null } | null;
identity: {
id: string;
externalId: string;
meta: Record<string, unknown> | null;
} | null;
api: Api;
ratelimit?: {
remaining: number;
Expand Down Expand Up @@ -285,7 +293,9 @@ export class KeyService {
* Merge ratelimits from the identity and the key
* Key limits take pecedence
*/
const ratelimits: { [name: string]: Pick<Ratelimit, "name" | "limit" | "duration"> } = {};
const ratelimits: {
[name: string]: Pick<Ratelimit, "name" | "limit" | "duration">;
} = {};

if (
dbRes.ratelimitAsync !== null &&
Expand Down Expand Up @@ -397,6 +407,7 @@ export class KeyService {

if (data.api.ipWhitelist) {
const ip = c.req.header("True-Client-IP") ?? c.req.header("CF-Connecting-IP");

if (!ip) {
return Ok({
key: data.key,
Expand All @@ -407,7 +418,8 @@ export class KeyService {
permissions: data.permissions,
});
}
const ipWhitelist = JSON.parse(data.api.ipWhitelist) as string[];

const ipWhitelist = data.api.ipWhitelist.split(",").map((s) => s.trim());
if (!ipWhitelist.includes(ip)) {
return Ok({
key: data.key,
Expand Down
16 changes: 15 additions & 1 deletion apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,27 @@ export const updateApiIpWhitelist = rateLimitedProcedure(ratelimit.update)
"We are unable to update the API whitelist. Please try again or contact [email protected]",
});
});
if (!api || api.workspace.tenantId !== ctx.tenant.id) {

if (
!api ||
api.workspace.tenantId !== ctx.tenant.id ||
input.workspaceId !== api.workspace.id
) {
throw new TRPCError({
code: "NOT_FOUND",
message:
"We are unable to find the correct API. Please try again or contact [email protected].",
});
}

if (api.workspace.plan !== "enterprise") {
throw new TRPCError({
code: "UNAUTHORIZED",
message:
"IP Whitelisting is only available for enterprise plans. Please contact [email protected].",
});
}

const newIpWhitelist = input.ipWhitelist === null ? null : input.ipWhitelist.join(",");

await db
Expand All @@ -68,6 +81,7 @@ export const updateApiIpWhitelist = rateLimitedProcedure(ratelimit.update)
"We are unable to update the API whitelist. Please try again or contact [email protected]",
});
});

await insertAuditLogs(tx, {
workspaceId: api.workspace.id,
actor: {
Expand Down

0 comments on commit 648dcd3

Please sign in to comment.