Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(billing): require the deployment environment for price checks and reject invalid quantities

Richie McIlroy237122a75aab
diff --git a/apps/web/__tests__/unit/mobile-checkout.test.ts b/apps/web/__tests__/unit/mobile-checkout.test.ts
index c74f55175..ab52feaa5 100644
--- a/apps/web/__tests__/unit/mobile-checkout.test.ts
+++ b/apps/web/__tests__/unit/mobile-checkout.test.ts
@@ -60,6 +60,15 @@ describe("checkout redirects", () => {
});
});
+ it("rejects an invalid guest checkout quantity before creating a session", async () => {
+ const response = await startGuestCheckout(
+ makeGuestCheckoutRequest({ priceId: "price_pro", quantity: -5 }),
+ );
+
+ expect(response.status).toBe(400);
+ expect(checkoutMocks.create).not.toHaveBeenCalled();
+ });
+
it("keeps existing guest checkout requests on the web flow", async () => {
const response = await startGuestCheckout(
makeGuestCheckoutRequest({ priceId: "price_pro", quantity: 1 }),
diff --git a/apps/web/__tests__/unit/subscribe-checkout.test.ts b/apps/web/__tests__/unit/subscribe-checkout.test.ts
index c7ad64a44..afef86e61 100644
--- a/apps/web/__tests__/unit/subscribe-checkout.test.ts
+++ b/apps/web/__tests__/unit/subscribe-checkout.test.ts
@@ -62,23 +62,28 @@ const makeSubscribeRequest = (body: Record<string, unknown>) =>
}) as unknown as import("next/server").NextRequest;
describe("Stripe plan allowlist", () => {
- it("accepts only legitimate Pro plan price IDs", () => {
- expect(isValidStripePlanPriceId(STRIPE_PLAN_IDS.development.yearly)).toBe(
- true,
- );
- expect(isValidStripePlanPriceId(STRIPE_PLAN_IDS.development.monthly)).toBe(
- true,
- );
- expect(isValidStripePlanPriceId(STRIPE_PLAN_IDS.production.yearly)).toBe(
- true,
- );
- expect(isValidStripePlanPriceId(STRIPE_PLAN_IDS.production.monthly)).toBe(
- true,
- );
-
- expect(isValidStripePlanPriceId("price_arbitrary_attacker_id")).toBe(false);
- expect(isValidStripePlanPriceId("price_free_tier")).toBe(false);
- expect(isValidStripePlanPriceId("")).toBe(false);
+ it("rejects IDs that are not a Pro plan price in either environment", () => {
+ for (const environment of ["development", "production"] as const) {
+ expect(
+ isValidStripePlanPriceId("price_arbitrary_attacker_id", environment),
+ ).toBe(false);
+ expect(isValidStripePlanPriceId("price_free_tier", environment)).toBe(
+ false,
+ );
+ expect(isValidStripePlanPriceId("", environment)).toBe(false);
+ }
+ expect(
+ isValidStripePlanPriceId(
+ STRIPE_PLAN_IDS.development.monthly,
+ "development",
+ ),
+ ).toBe(true);
+ expect(
+ isValidStripePlanPriceId(
+ STRIPE_PLAN_IDS.production.monthly,
+ "production",
+ ),
+ ).toBe(true);
});
it("validates price IDs against specific deployment environments", () => {
@@ -156,4 +161,34 @@ describe("POST /api/settings/billing/subscribe", () => {
}),
);
});
+
+ it.each([0, -5, 1.5, "3", 10001])(
+ "rejects quantity %s with 400 before creating a session",
+ async (quantity) => {
+ const response = await subscribe(
+ makeSubscribeRequest({
+ priceId: STRIPE_PLAN_IDS.development.monthly,
+ quantity,
+ }),
+ );
+
+ expect(response.status).toBe(400);
+ expect(checkoutMocks.create).not.toHaveBeenCalled();
+ },
+ );
+
+ it("defaults a missing quantity to one seat", async () => {
+ const response = await subscribe(
+ makeSubscribeRequest({ priceId: STRIPE_PLAN_IDS.development.monthly }),
+ );
+
+ expect(response.status).toBe(200);
+ expect(checkoutMocks.create).toHaveBeenCalledWith(
+ expect.objectContaining({
+ line_items: [
+ { price: STRIPE_PLAN_IDS.development.monthly, quantity: 1 },
+ ],
+ }),
+ );
+ });
});
diff --git a/apps/web/app/api/settings/billing/guest-checkout/route.ts b/apps/web/app/api/settings/billing/guest-checkout/route.ts
index a437d2290..5f0813e34 100644
--- a/apps/web/app/api/settings/billing/guest-checkout/route.ts
+++ b/apps/web/app/api/settings/billing/guest-checkout/route.ts
@@ -1,6 +1,7 @@
import { serverEnv } from "@cap/env";
import { isValidStripePlanPriceId, stripe } from "@cap/utils";
import type { NextRequest } from "next/server";
+import { parseCheckoutQuantity } from "@/lib/checkout-quantity";
import { getCheckoutRedirectUrls } from "@/lib/mobile-checkout";
import { trackServerEvent } from "@/lib/server-analytics";
@@ -23,13 +24,11 @@ export async function POST(request: NextRequest) {
return Response.json({ error: "Invalid priceId" }, { status: 400 });
}
- const safeQuantity =
- typeof quantity === "number" &&
- Number.isInteger(quantity) &&
- quantity >= 1 &&
- quantity <= 1000
- ? quantity
- : 1;
+ const safeQuantity = parseCheckoutQuantity(quantity);
+ if (safeQuantity === null) {
+ console.error("Invalid quantity");
+ return Response.json({ error: "Invalid quantity" }, { status: 400 });
+ }
try {
console.log("Creating guest checkout session");
@@ -57,7 +56,7 @@ export async function POST(request: NextRequest) {
"guest_checkout_started",
{
price_id: priceId,
- quantity: quantity || 1,
+ quantity: safeQuantity,
platform: checkoutPlatform,
session_id: checkoutSession.id,
},
diff --git a/apps/web/app/api/settings/billing/subscribe/route.ts b/apps/web/app/api/settings/billing/subscribe/route.ts
index 42dc62fe7..fe71ca1bb 100644
--- a/apps/web/app/api/settings/billing/subscribe/route.ts
+++ b/apps/web/app/api/settings/billing/subscribe/route.ts
@@ -6,6 +6,7 @@ import { isValidStripePlanPriceId, stripe, userIsPro } from "@cap/utils";
import { eq } from "drizzle-orm";
import type { NextRequest } from "next/server";
import type Stripe from "stripe";
+import { parseCheckoutQuantity } from "@/lib/checkout-quantity";
import { trackServerEvent } from "@/lib/server-analytics";
export async function POST(request: NextRequest) {
@@ -28,13 +29,14 @@ export async function POST(request: NextRequest) {
);
}
- const safeQuantity =
- typeof quantity === "number" &&
- Number.isInteger(quantity) &&
- quantity >= 1 &&
- quantity <= 1000
- ? quantity
- : 1;
+ const safeQuantity = parseCheckoutQuantity(quantity);
+ if (safeQuantity === null) {
+ console.error("Invalid quantity");
+ return Response.json(
+ { error: true, message: "Invalid quantity" },
+ { status: 400 },
+ );
+ }
if (!user) {
console.error("User not found");
diff --git a/apps/web/lib/checkout-quantity.ts b/apps/web/lib/checkout-quantity.ts
new file mode 100644
index 000000000..131b84a1f
--- /dev/null
+++ b/apps/web/lib/checkout-quantity.ts
@@ -0,0 +1,11 @@
+export const MAX_CHECKOUT_QUANTITY = 10000;
+
+export const parseCheckoutQuantity = (quantity: unknown): number | null => {
+ if (quantity === undefined || quantity === null) return 1;
+ return typeof quantity === "number" &&
+ Number.isInteger(quantity) &&
+ quantity >= 1 &&
+ quantity <= MAX_CHECKOUT_QUANTITY
+ ? quantity
+ : null;
+};
diff --git a/packages/utils/src/constants/plans.ts b/packages/utils/src/constants/plans.ts
index e6f1b44d7..c93f216c0 100644
--- a/packages/utils/src/constants/plans.ts
+++ b/packages/utils/src/constants/plans.ts
@@ -16,31 +16,12 @@ export const STRIPE_PLAN_IDS = {
},
};
-export const VALID_STRIPE_PLAN_PRICE_IDS = new Set<string>([
- STRIPE_PLAN_IDS.development.yearly,
- STRIPE_PLAN_IDS.development.monthly,
- STRIPE_PLAN_IDS.production.yearly,
- STRIPE_PLAN_IDS.production.monthly,
-]);
-
export const isValidStripePlanPriceId = (
priceId: string,
- environment?: "development" | "production",
+ environment: keyof typeof STRIPE_PLAN_IDS,
): boolean => {
- const env =
- environment ??
- (process.env.VERCEL_ENV === "production"
- ? "production"
- : process.env.VERCEL_ENV
- ? "development"
- : undefined);
-
- if (env) {
- const envPlans = STRIPE_PLAN_IDS[env];
- return priceId === envPlans.yearly || priceId === envPlans.monthly;
- }
-
- return VALID_STRIPE_PLAN_PRICE_IDS.has(priceId);
+ const envPlans = STRIPE_PLAN_IDS[environment];
+ return priceId === envPlans.yearly || priceId === envPlans.monthly;
};
export const STRIPE_SIGNED_BAA_PRICE_IDS: Record<string, string> = {