Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: accept the configured web host in share URL parsing

Mangesh Raut84fda4545c98
diff --git a/apps/web/__tests__/unit/oembed.test.ts b/apps/web/__tests__/unit/oembed.test.ts
index eba9f7021..d3dea51f5 100644
--- a/apps/web/__tests__/unit/oembed.test.ts
+++ b/apps/web/__tests__/unit/oembed.test.ts
@@ -1,8 +1,16 @@
import { Video } from "@cap/web-domain";
-import { describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
import { buildOEmbedVideo, parseCapShareUrl } from "@/lib/oembed";
import type { PublicShareVideo } from "@/lib/public-share-video";
+const { webEnv } = vi.hoisted(() => ({
+ webEnv: { WEB_URL: "https://cap.so" },
+}));
+
+vi.mock("@cap/env", () => ({
+ serverEnv: () => webEnv,
+}));
+
const video: PublicShareVideo = {
id: Video.VideoId.make("abc123"),
name: "Cap walkthrough",
@@ -15,6 +23,10 @@ const video: PublicShareVideo = {
};
describe("Cap oEmbed", () => {
+ beforeEach(() => {
+ webEnv.WEB_URL = "https://cap.so";
+ });
+
it.each([
["https://cap.so/s/abc123", "abc123"],
["https://www.cap.so/s/abc123?t=3", "abc123"],
@@ -35,6 +47,31 @@ describe("Cap oEmbed", () => {
expect(parseCapShareUrl(url)).toBeNull();
});
+ it("parses /s/<id> on the configured web host", () => {
+ webEnv.WEB_URL = "https://www.Video.example.com";
+ expect(parseCapShareUrl("https://video.example.com/s/abc123")).toBe(
+ "abc123",
+ );
+ expect(parseCapShareUrl("https://www.video.example.com/s/abc123?t=3")).toBe(
+ "abc123",
+ );
+ });
+
+ it("keeps cap.so and cap.link links when the server is self-hosted", () => {
+ webEnv.WEB_URL = "https://video.example.com";
+ expect(parseCapShareUrl("https://cap.so/s/abc123")).toBe("abc123");
+ expect(parseCapShareUrl("https://www.cap.so/s/abc123")).toBe("abc123");
+ expect(parseCapShareUrl("https://cap.link/abc123")).toBe("abc123");
+ });
+
+ it("rejects an unrelated host, http, and the short cap.link shape on the configured host", () => {
+ webEnv.WEB_URL = "https://video.example.com";
+ expect(parseCapShareUrl("https://example.com/s/abc123")).toBeNull();
+ expect(parseCapShareUrl("http://video.example.com/s/abc123")).toBeNull();
+ expect(parseCapShareUrl("http://cap.so/s/abc123")).toBeNull();
+ expect(parseCapShareUrl("https://video.example.com/abc123")).toBeNull();
+ });
+
it("returns a responsive video response with preserved dimensions", () => {
const result = buildOEmbedVideo({
video,
diff --git a/apps/web/__tests__/unit/slack-unfurl.test.ts b/apps/web/__tests__/unit/slack-unfurl.test.ts
index 510cb4256..dc5292bcd 100644
--- a/apps/web/__tests__/unit/slack-unfurl.test.ts
+++ b/apps/web/__tests__/unit/slack-unfurl.test.ts
@@ -1,5 +1,5 @@
import { Video } from "@cap/web-domain";
-import { describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
import type { PublicShareVideo } from "@/lib/public-share-video";
import {
buildSlackVideoBlock,
@@ -7,6 +7,14 @@ import {
processSlackEvent,
} from "@/lib/slack/unfurl";
+const { webEnv } = vi.hoisted(() => ({
+ webEnv: { WEB_URL: "https://cap.so" },
+}));
+
+vi.mock("@cap/env", () => ({
+ serverEnv: () => webEnv,
+}));
+
const video: PublicShareVideo = {
id: Video.VideoId.make("abc123"),
name: "Cap walkthrough",
@@ -38,6 +46,10 @@ const payload = {
};
describe("Slack video unfurls", () => {
+ beforeEach(() => {
+ webEnv.WEB_URL = "https://cap.so";
+ });
+
it("parses link and uninstall events without accepting malformed payloads", () => {
expect(parseSlackEventPayload(payload)).toEqual(payload);
expect(
@@ -115,6 +127,47 @@ describe("Slack video unfurls", () => {
});
});
+ it("unfurls a share link on the configured self-hosted host", async () => {
+ webEnv.WEB_URL = "https://video.example.com";
+ const sendUnfurl = vi.fn(async () => undefined);
+ await processSlackEvent({
+ payload: {
+ ...payload,
+ event: {
+ ...payload.event,
+ links: [
+ {
+ domain: "video.example.com",
+ url: "https://video.example.com/s/abc123",
+ },
+ ],
+ },
+ },
+ webUrl: "https://video.example.com",
+ dependencies: {
+ deleteInstallation: vi.fn(async () => undefined),
+ getInstallationToken: vi.fn(async () => "xoxb-token"),
+ getVideo: vi.fn(async () => video),
+ sendUnfurl,
+ },
+ });
+
+ expect(sendUnfurl).toHaveBeenCalledWith({
+ token: "xoxb-token",
+ event: {
+ channel: "C123",
+ messageTs: "123.456",
+ unfurlId: "U123",
+ source: "conversations_history",
+ },
+ unfurls: {
+ "https://video.example.com/s/abc123": {
+ blocks: [expect.objectContaining({ type: "video" })],
+ },
+ },
+ });
+ });
+
it("uses the canonical Cap recording URL for legacy cap.link unfurls", async () => {
const sendUnfurl = vi.fn(async () => undefined);
await processSlackEvent({
diff --git a/apps/web/lib/oembed.ts b/apps/web/lib/oembed.ts
index d6d72965a..73ea2929a 100644
--- a/apps/web/lib/oembed.ts
+++ b/apps/web/lib/oembed.ts
@@ -1,3 +1,4 @@
+import { serverEnv } from "@cap/env";
import type { PublicShareVideo } from "@/lib/public-share-video";
const DEFAULT_WIDTH = 800;
@@ -21,6 +22,16 @@ export type OEmbedVideo = {
duration?: number;
};
+const configuredShareHostname = () => {
+ try {
+ return new URL(serverEnv().WEB_URL).hostname
+ .toLowerCase()
+ .replace(/^www\./, "");
+ } catch {
+ return null;
+ }
+};
+
const parsePositiveDimension = (value: string | undefined) => {
if (value === undefined) return undefined;
const number = Number(value);
@@ -72,6 +83,14 @@ export const parseCapShareUrl = (value: string) => {
) {
videoId = segments[0];
}
+ if (
+ !videoId &&
+ segments.length === 2 &&
+ segments[0] === "s" &&
+ hostname === configuredShareHostname()
+ ) {
+ videoId = segments[1];
+ }
if (!videoId || !/^[A-Za-z0-9_-]{1,128}$/.test(videoId)) return null;
return videoId;
diff --git a/apps/web/lib/share-video-metadata.ts b/apps/web/lib/share-video-metadata.ts
index fe832eb21..969d25e53 100644
--- a/apps/web/lib/share-video-metadata.ts
+++ b/apps/web/lib/share-video-metadata.ts
@@ -18,8 +18,8 @@ export type ShareVideoMetadataInput = {
/**
* Two surfaces only answer on the default Cap origin: `proxy.ts` redirects
* `/embed/` away from a custom domain, and `parseCapShareUrl` accepts share
- * URLs on `cap.so` and `cap.link` alone, so `/api/oembed` rejects a custom
- * domain `url`. Defaults to `webUrl`.
+ * URLs on `cap.so`, `cap.link`, and the configured web host, so `/api/oembed`
+ * rejects an unrelated custom domain `url`. Defaults to `webUrl`.
*/
canonicalWebUrl?: string;
advertiseIframelyPlayer?: boolean;