Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: stop firewall probe from wiping Loom import results

Richie McIlroy9bf576051d57
diff --git a/apps/web/__tests__/unit/loom-import.test.ts b/apps/web/__tests__/unit/loom-import.test.ts
index d7717586b..765347c06 100644
--- a/apps/web/__tests__/unit/loom-import.test.ts
+++ b/apps/web/__tests__/unit/loom-import.test.ts
@@ -369,53 +369,8 @@ describe("importFromLoom", () => {
expect(valuesMock).not.toHaveBeenCalled();
});
- it("rate limits single Loom imports per user", async () => {
+ it("does not call the Vercel firewall from the Loom import server action", async () => {
checkRateLimitMock.mockResolvedValueOnce({ rateLimited: true });
- headersMock.mockResolvedValue(
- new Headers({
- host: "cap.test",
- "x-real-ip": "127.0.0.1",
- "next-action": "40382acacfe2b9dd0581bfc42bc9a2c02535278e38",
- }),
- );
-
- const fetchMock = vi.mocked(fetch);
- const { importFromLoom } = await import("@/actions/loom");
-
- const result = await importFromLoom({
- loomUrl: "https://www.loom.com/share/loom-abc1234567",
- orgId: "org-1" as never,
- });
-
- expect(result).toEqual({
- success: false,
- error:
- "Too many Loom imports started. Please wait a few minutes, then try again.",
- });
- expect(checkRateLimitMock).toHaveBeenCalledWith(
- "rl_loom_import_per_user",
- expect.objectContaining({
- rateLimitKey: "loom-import:user-123",
- }),
- );
- const rateLimitOptions = checkRateLimitMock.mock.calls[0]?.[1] as {
- headers?: Headers;
- request?: Request;
- };
- expect(rateLimitOptions.headers?.get("next-action")).toBeNull();
- expect(rateLimitOptions.request?.headers.get("next-action")).toBeNull();
- expect(rateLimitOptions.headers?.get("host")).toBe("cap.test");
- expect(fetchMock).not.toHaveBeenCalled();
- expect(valuesMock).not.toHaveBeenCalled();
- });
-
- it("fails open when the Loom import rate limit check throws", async () => {
- checkRateLimitMock.mockRejectedValueOnce(
- new Error("Unexpected response 494"),
- );
- const consoleErrorSpy = vi
- .spyOn(console, "error")
- .mockImplementation(() => {});
const fetchMock = vi.mocked(fetch);
fetchMock.mockImplementation(async (input) => {
@@ -460,10 +415,7 @@ describe("importFromLoom", () => {
success: true,
videoId: "video-123",
});
- expect(consoleErrorSpy).toHaveBeenCalledWith(
- 'Rate limit check failed for "rl_loom_import_per_user":',
- expect.any(Error),
- );
+ expect(checkRateLimitMock).not.toHaveBeenCalled();
expect(startMock).toHaveBeenCalledTimes(1);
});
@@ -666,53 +618,7 @@ describe("importFromLoom", () => {
);
});
- it("rejects CSV imports when the current user is rate limited", async () => {
- whereMock.mockReturnValueOnce(
- withLimit([{ userId: "member-123", email: "[email protected]" }]),
- );
- checkRateLimitMock.mockResolvedValueOnce({ rateLimited: true });
-
- const fetchMock = vi.mocked(fetch);
- const { importFromLoomCsv } = await import("@/actions/loom");
-
- const result = await importFromLoomCsv({
- orgId: "org-1" as never,
- rows: [
- {
- rowNumber: 2,
- loomUrl: "https://www.loom.com/share/loom-abc1234567",
- userEmail: "[email protected]",
- },
- ],
- });
-
- expect(result).toEqual({
- success: false,
- importedCount: 0,
- failedCount: 1,
- results: [
- {
- rowNumber: 2,
- userEmail: "[email protected]",
- spaceName: undefined,
- success: false,
- error:
- "Too many Loom imports started. Please wait a few minutes, then try again.",
- },
- ],
- error: "No Loom videos were imported.",
- });
- expect(checkRateLimitMock).toHaveBeenCalledWith(
- "rl_loom_import_per_user",
- expect.objectContaining({
- rateLimitKey: "loom-import:user-123",
- }),
- );
- expect(fetchMock).not.toHaveBeenCalled();
- expect(valuesMock).not.toHaveBeenCalled();
- });
-
- it("skips rate limit checks for csv rows that were already imported", async () => {
+ it("imports a CSV row without calling the Vercel firewall", async () => {
whereMock.mockImplementation((conditions: unknown) => {
const serializedConditions = JSON.stringify(conditions);
@@ -800,7 +706,7 @@ describe("importFromLoom", () => {
],
error: undefined,
});
- expect(checkRateLimitMock).toHaveBeenCalledTimes(1);
+ expect(checkRateLimitMock).not.toHaveBeenCalled();
expect(fetchMock).toHaveBeenCalled();
expect(startMock).toHaveBeenCalledTimes(1);
});
diff --git a/apps/web/actions/loom.ts b/apps/web/actions/loom.ts
index 822f5fc86..75f3672a2 100644
--- a/apps/web/actions/loom.ts
+++ b/apps/web/actions/loom.ts
@@ -24,11 +24,9 @@ import {
type User,
Video,
} from "@cap/web-domain";
-import { checkRateLimit } from "@vercel/firewall";
import { and, eq } from "drizzle-orm";
import { Option } from "effect";
import { revalidatePath } from "next/cache";
-import { headers } from "next/headers";
import { start } from "workflow/api";
import {
getOrganizationAccess,
@@ -36,7 +34,6 @@ import {
} from "@/actions/organization/authorization";
import { provisionOrganizationInvitee } from "@/lib/organization-provisioning";
import { canManageOrganizationSettings } from "@/lib/permissions/roles";
-import { firewallRequestHeaders } from "@/lib/rate-limit";
import { runPromise } from "@/lib/server";
import { importLoomVideoWorkflow } from "@/workflows/import-loom-video";
@@ -91,41 +88,10 @@ export interface LoomCsvImportResult {
const MAX_LOOM_CSV_ROWS = 500;
const MAX_LOOM_SPACE_NAME_LENGTH = 255;
-const LOOM_IMPORT_RATE_LIMIT_ID = "rl_loom_import_per_user";
-const LOOM_IMPORT_RATE_LIMIT_ERROR =
- "Too many Loom imports started. Please wait a few minutes, then try again.";
const LOOM_CSV_LIMIT_ERROR = `CSV imports are limited to ${MAX_LOOM_CSV_ROWS} rows at a time. Contact support to raise this limit.`;
const LOOM_CSV_PERMISSION_ERROR =
"Only organization admins and owners can import Loom videos from a CSV.";
-async function createLoomImportRateLimitCheck(userId: User.UserId) {
- if (NODE_ENV !== "production") return async () => false;
-
- return async () => {
- try {
- const headersList = firewallRequestHeaders(await headers());
- const request = new Request("https://cap.so/api/loom-import-rate-limit", {
- method: "POST",
- headers: headersList,
- });
-
- const { rateLimited } = await checkRateLimit(LOOM_IMPORT_RATE_LIMIT_ID, {
- headers: headersList,
- request,
- rateLimitKey: `loom-import:${userId}`,
- });
- return rateLimited;
- } catch (error) {
- // Fail open: a firewall/edge outage must never block Loom imports.
- console.error(
- `Rate limit check failed for "${LOOM_IMPORT_RATE_LIMIT_ID}":`,
- error,
- );
- return false;
- }
- };
-}
-
function extractLoomVideoId(url: string): string | null {
try {
const parsed = new URL(url);
@@ -328,12 +294,10 @@ async function importLoomVideoForOwner({
loomUrl,
orgId,
ownerId,
- isRateLimited,
}: {
loomUrl: string;
orgId: Organisation.OrganisationId;
ownerId: User.UserId;
- isRateLimited?: () => Promise<boolean>;
}): Promise<LoomImportResult> {
const loomVideoId = extractLoomVideoId(loomUrl.trim());
if (!loomVideoId) {
@@ -371,13 +335,6 @@ async function importLoomVideoForOwner({
};
}
- if (isRateLimited && (await isRateLimited())) {
- return {
- success: false,
- error: LOOM_IMPORT_RATE_LIMIT_ERROR,
- };
- }
-
if (existing.length > 0) {
await db()
.delete(importedVideos)
@@ -506,14 +463,6 @@ export async function importFromLoom({
await requireOrganizationAccess(user.id, orgId);
- const isRateLimited = await createLoomImportRateLimitCheck(user.id);
- if (await isRateLimited()) {
- return {
- success: false,
- error: LOOM_IMPORT_RATE_LIMIT_ERROR,
- };
- }
-
return importLoomVideoForOwner({
loomUrl,
orgId,
@@ -766,7 +715,6 @@ export async function importFromLoomCsv({
const results: LoomCsvImportRowResult[] = [];
const spaceCache = new Map<string, ImportSpaceCacheValue>();
const touchedSpaceIds = new Set<Space.SpaceIdOrOrganisationId>();
- const isRateLimited = await createLoomImportRateLimitCheck(user.id);
for (const row of normalizedRows) {
if (!row.loomUrl) {
@@ -833,7 +781,6 @@ export async function importFromLoomCsv({
loomUrl: row.loomUrl,
orgId,
ownerId: member.userId,
- isRateLimited,
});
let spaceName = row.spaceName || undefined;