Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): neutralize spreadsheet formulas in Loom import results CSV

Richie McIlroy5afb4823b9c2
diff --git a/apps/web/__tests__/unit/loom-csv.test.ts b/apps/web/__tests__/unit/loom-csv.test.ts
index 90d8db8f1..b1433c2a4 100644
--- a/apps/web/__tests__/unit/loom-csv.test.ts
+++ b/apps/web/__tests__/unit/loom-csv.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { parseConciergeLoomCsv } from "@/lib/loom-csv";
+import { formatLoomCsvCell, parseConciergeLoomCsv } from "@/lib/loom-csv";
describe("concierge Loom CSV", () => {
it("keeps quoted workspace names and source row numbers", () => {
@@ -25,3 +25,32 @@ describe("concierge Loom CSV", () => {
);
});
});
+
+describe("Loom CSV results cells", () => {
+ it("keeps plain values unchanged", () => {
+ expect(formatLoomCsvCell("[email protected]")).toBe("[email protected]");
+ expect(
+ formatLoomCsvCell("https://www.loom.com/share/0123456789abcdef"),
+ ).toBe("https://www.loom.com/share/0123456789abcdef");
+ });
+
+ it("quotes delimiters and doubles embedded quotes", () => {
+ expect(formatLoomCsvCell("Sales, Europe")).toBe('"Sales, Europe"');
+ expect(formatLoomCsvCell('The "A" team')).toBe('"The ""A"" team"');
+ });
+
+ it.each([
+ ["=HYPERLINK(1)", "'=HYPERLINK(1)"],
+ ["+1", "'+1"],
+ ["-1", "'-1"],
+ ["@SUM(A1)", "'@SUM(A1)"],
+ ["\tTab", "'\tTab"],
+ ])("neutralizes the formula prefix in %j", (value, expected) => {
+ expect(formatLoomCsvCell(value)).toBe(expected);
+ });
+
+ it("neutralizes formulas that also need quoting", () => {
+ expect(formatLoomCsvCell("=1,2")).toBe(`"'=1,2"`);
+ expect(formatLoomCsvCell("\r=1")).toBe(`"'\r=1"`);
+ });
+});
diff --git a/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx b/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
index a07460f5b..53ffddf7e 100644
--- a/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
+++ b/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
@@ -56,7 +56,7 @@ import {
} from "@/actions/loom";
import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
import { UpgradeModal } from "@/components/UpgradeModal";
-import { parseLoomCsvRecords } from "@/lib/loom-csv";
+import { formatLoomCsvCell, parseLoomCsvRecords } from "@/lib/loom-csv";
import {
type LoomImportDestination,
loomImportDestinationHref,
@@ -190,13 +190,6 @@ function importErrorMessage(error: unknown) {
: "The import request failed for this batch.";
}
-function escapeCsvValue(value: string) {
- if (/[",\n\r]/.test(value)) {
- return `"${value.replace(/"/g, '""')}"`;
- }
- return value;
-}
-
function buildResultsCsv(
results: LoomCsvImportRowResult[],
sourceRows: MappedRow[],
@@ -217,7 +210,7 @@ function buildResultsCsv(
row.error ?? "",
]),
];
- return `${lines.map((line) => line.map(escapeCsvValue).join(",")).join("\n")}\n`;
+ return `${lines.map((line) => line.map(formatLoomCsvCell).join(",")).join("\n")}\n`;
}
function parseCsv(text: string, fileName: string): CsvData {
diff --git a/apps/web/lib/loom-csv.ts b/apps/web/lib/loom-csv.ts
index 3aaea1f19..25c5cf24b 100644
--- a/apps/web/lib/loom-csv.ts
+++ b/apps/web/lib/loom-csv.ts
@@ -50,6 +50,16 @@ export function parseLoomCsvRecords(text: string) {
return records;
}
+const SPREADSHEET_FORMULA_PREFIX = /^[=+\-@\t\r]/;
+
+export function formatLoomCsvCell(value: string) {
+ const literal = SPREADSHEET_FORMULA_PREFIX.test(value) ? `'${value}` : value;
+ if (/[",\n\r]/.test(literal)) {
+ return `"${literal.replace(/"/g, '""')}"`;
+ }
+ return literal;
+}
+
export function parseConciergeLoomCsv(text: string): LoomCsvImportRow[] {
const records = parseLoomCsvRecords(text);
const headers = records[0]?.map((value) => value.toLowerCase()) ?? [];