Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion apps/web/__tests__/unit/oembed.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { Video } from "@cap/web-domain";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { buildOEmbedVideo, parseCapShareUrl } from "@/lib/oembed";
import type { PublicShareVideo } from "@/lib/public-share-video";

const serverEnvMock = vi.hoisted(() =>
vi.fn(() => ({ WEB_URL: "https://www.video.example.com" })),
);

vi.mock("@cap/env", () => ({
serverEnv: serverEnvMock,
}));

const video: PublicShareVideo = {
id: Video.VideoId.make("abc123"),
name: "Cap walkthrough",
Expand All @@ -20,13 +28,19 @@ describe("Cap oEmbed", () => {
["https://www.cap.so/s/abc123?t=3", "abc123"],
["https://cap.link/abc123", "abc123"],
["https://www.cap.link/abc123?foo=bar", "abc123"],
["https://video.example.com/s/abc123", "abc123"],
["https://www.video.example.com/s/abc123?t=3", "abc123"],
])("parses supported share URL %s", (url, expected) => {
expect(parseCapShareUrl(url)).toBe(expected);
});

it.each([
"http://cap.so/s/abc123",
"http://video.example.com/s/abc123",
"https://example.com/s/abc123",
"https://video.example.com/abc123",
"https://video.example.com/embed/abc123",
"https://cap.link/s/abc123",
"https://cap.so/embed/abc123",
"https://cap.link/video/demo.mp4",
"https://cap.so/s/abc123/extra",
Expand All @@ -35,6 +49,16 @@ describe("Cap oEmbed", () => {
expect(parseCapShareUrl(url)).toBeNull();
});

it("ignores an unreadable WEB_URL instead of failing the parse", () => {
serverEnvMock.mockImplementationOnce(() => {
throw new Error("Invalid environment variables");
});

expect(parseCapShareUrl("https://video.example.com/s/abc123")).toBeNull();
expect(parseCapShareUrl("https://cap.so/s/abc123")).toBe("abc123");
expect(parseCapShareUrl("https://cap.link/abc123")).toBe("abc123");
});

it("returns a responsive video response with preserved dimensions", () => {
const result = buildOEmbedVideo({
video,
Expand Down
18 changes: 17 additions & 1 deletion apps/web/lib/oembed.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { serverEnv } from "@cap/env";
import type { PublicShareVideo } from "@/lib/public-share-video";

const DEFAULT_WIDTH = 800;
Expand Down Expand Up @@ -49,6 +50,17 @@ const scaleDimensions = ({
};
};

const configuredShareHost = () => {
try {
return new URL(serverEnv().WEB_URL).hostname
.toLowerCase()
.replace(/^www\./, "");
} catch {
// A missing or invalid WEB_URL must not reject cap.so / cap.link links.
return undefined;
}
};

export const parseCapShareUrl = (value: string) => {
let url: URL;
try {
Expand All @@ -62,7 +74,11 @@ export const parseCapShareUrl = (value: string) => {
const segments = url.pathname.split("/").filter(Boolean);
let videoId: string | undefined;

if (hostname === "cap.so" && segments.length === 2 && segments[0] === "s") {
if (
segments.length === 2 &&
segments[0] === "s" &&
(hostname === "cap.so" || hostname === configuredShareHost())
) {
videoId = segments[1];
}
if (
Expand Down
4 changes: 2 additions & 2 deletions apps/web/lib/share-video-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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_URL` host, so
* `/api/oembed` rejects a different custom-domain `url`. Defaults to `webUrl`.
*/
canonicalWebUrl?: string;
advertiseIframelyPlayer?: boolean;
Expand Down