Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🥅 Validate license key input before rate limiting and querying (#3561)

Luke Vellaca8305bb17c6
diff --git a/apps/web/src/app/api/licensing/v1/[...route]/route.ts b/apps/web/src/app/api/licensing/v1/[...route]/route.ts
index ae1cfe09c..5c30d04d6 100644
--- a/apps/web/src/app/api/licensing/v1/[...route]/route.ts
+++ b/apps/web/src/app/api/licensing/v1/[...route]/route.ts
@@ -65,9 +65,14 @@ if (env.LICENSE_API_AUTH_TOKEN) {
app.post(
"/licenses/actions/validate-key",
+ zValidator("json", validateLicenseKeyInputSchema),
rateLimiter({
+ // rateLimiter's context is not typed with the validator's output, so
+ // reparse the body the validator above already accepted.
keyGenerator: async (c) => {
- const { key, fingerprint } = await c.req.json();
+ const { key, fingerprint } = validateLicenseKeyInputSchema.parse(
+ await c.req.json(),
+ );
return `validate-license-key:${key}:${fingerprint}`;
},
windowMs: 60 * 1000,
@@ -78,7 +83,6 @@ app.post(
})
: undefined,
}),
- zValidator("json", validateLicenseKeyInputSchema),
async (c) => {
const { key, fingerprint } = c.req.valid("json");
diff --git a/apps/web/src/features/licensing/schema.test.ts b/apps/web/src/features/licensing/schema.test.ts
new file mode 100644
index 000000000..4b959392e
--- /dev/null
+++ b/apps/web/src/features/licensing/schema.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from "vitest";
+import { validateLicenseKeyInputSchema } from "@/features/licensing/schema";
+import { generateLicenseKey } from "@/features/licensing/utils";
+
+describe("validateLicenseKeyInputSchema", () => {
+ it("accepts a generated key with a fingerprint", () => {
+ const key = generateLicenseKey({ version: 4 });
+ expect(
+ validateLicenseKeyInputSchema.safeParse({ key, fingerprint: "abc" })
+ .success,
+ ).toBe(true);
+ });
+
+ it("rejects a key containing a NUL byte, which Postgres text rejects", () => {
+ expect(
+ validateLicenseKeyInputSchema.safeParse({ key: "RLYV4-\0" }).success,
+ ).toBe(false);
+ });
+
+ it("rejects an oversized key, which would overflow the rate limit key", () => {
+ expect(
+ validateLicenseKeyInputSchema.safeParse({ key: "A".repeat(1_000_000) })
+ .success,
+ ).toBe(false);
+ });
+
+ it("rejects an oversized fingerprint", () => {
+ expect(
+ validateLicenseKeyInputSchema.safeParse({
+ key: generateLicenseKey({ version: 4 }),
+ fingerprint: "A".repeat(1_000_000),
+ }).success,
+ ).toBe(false);
+ });
+});
diff --git a/apps/web/src/features/licensing/schema.ts b/apps/web/src/features/licensing/schema.ts
index 116ecedbf..54bee936d 100644
--- a/apps/web/src/features/licensing/schema.ts
+++ b/apps/web/src/features/licensing/schema.ts
@@ -51,9 +51,17 @@ export type CreateLicenseResponse = z.infer<typeof createLicenseResponseSchema>;
// Validate License Key
// =========================
+// The public endpoint feeds these into a Redis rate limit key and Postgres
+// text columns, which cap key size and reject NUL bytes.
+const boundedText = (max: number) =>
+ z
+ .string()
+ .max(max)
+ .refine((value) => !value.includes("\0"), "Must not contain NUL bytes");
+
export const validateLicenseKeyInputSchema = z.object({
- key: z.string(),
- fingerprint: z.string().optional(),
+ key: boundedText(64),
+ fingerprint: boundedText(256).optional(),
});
export type ValidateLicenseInputKeySchema = z.infer<
typeof validateLicenseKeyInputSchema