Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(loom-import): guard rate-limit header construction, scale HLS download timeout

Richie McIlroyef06773753ea
diff --git a/apps/media-server/src/lib/media-video.ts b/apps/media-server/src/lib/media-video.ts
index 2675ca210..0575f27c6 100644
--- a/apps/media-server/src/lib/media-video.ts
+++ b/apps/media-server/src/lib/media-video.ts
@@ -4,6 +4,7 @@ import { join } from "node:path";
import { type BunFile, file, spawn } from "bun";
import type { VideoMetadata } from "./job-manager";
import {
+ createMediaInput,
DOWNLOAD_TIMEOUT_MS,
PROCESS_TIMEOUT_MS,
type ProgressCallback,
@@ -21,6 +22,13 @@ import {
const PROCESS_TIMEOUT_PER_SECOND_MS = 20_000;
const MAX_PROCESS_TIMEOUT_MS = 2 * 60 * 60 * 1000;
+// HLS/DASH sources are pulled as many sequential segment requests rather than
+// one streamed fetch, so per-request overhead scales with video length. A
+// flat 10-minute budget is enough for typical short recordings but not for a
+// long (e.g. 60+ minute) manifest with 1000+ segments.
+const STREAMING_DOWNLOAD_TIMEOUT_PER_SECOND_MS = 5_000;
+const MAX_STREAMING_DOWNLOAD_TIMEOUT_MS = 30 * 60 * 1000;
+const STREAMING_DURATION_PROBE_TIMEOUT_MS = 15_000;
const THUMBNAIL_TIMEOUT_MS = 60_000;
const PREVIEW_GIF_TIMEOUT_MS = 30_000;
const PROBE_H264_LEVEL_TIMEOUT_MS = 10_000;
@@ -1107,6 +1115,38 @@ export function buildStreamingDownloadFfmpegArgs(
];
}
+async function probeStreamingDurationSeconds(
+ videoUrl: string,
+): Promise<number | null> {
+ try {
+ const input = createMediaInput(videoUrl);
+ try {
+ const duration = await withTimeout(
+ input.computeDuration(),
+ STREAMING_DURATION_PROBE_TIMEOUT_MS,
+ );
+ return Number.isFinite(duration) && duration > 0 ? duration : null;
+ } finally {
+ input.dispose();
+ }
+ } catch {
+ // Best-effort: fall back to the flat DOWNLOAD_TIMEOUT_MS budget below.
+ return null;
+ }
+}
+
+function getStreamingDownloadTimeoutMs(durationSeconds: number | null): number {
+ if (!durationSeconds) return DOWNLOAD_TIMEOUT_MS;
+
+ return Math.min(
+ MAX_STREAMING_DOWNLOAD_TIMEOUT_MS,
+ Math.max(
+ DOWNLOAD_TIMEOUT_MS,
+ Math.ceil(durationSeconds * STREAMING_DOWNLOAD_TIMEOUT_PER_SECOND_MS),
+ ),
+ );
+}
+
async function downloadStreamingVideoToTemp(
videoUrl: string,
abortSignal?: AbortSignal,
@@ -1126,6 +1166,8 @@ async function downloadStreamingVideoToTemp(
manifestDir,
abortSignal,
);
+ const durationSeconds = await probeStreamingDurationSeconds(videoUrl);
+ const downloadTimeoutMs = getStreamingDownloadTimeoutMs(durationSeconds);
await runFfmpegCommand(
buildStreamingDownloadFfmpegArgs(
@@ -1133,7 +1175,7 @@ async function downloadStreamingVideoToTemp(
tempFile.path,
ffmpegHlsCapabilities,
),
- DOWNLOAD_TIMEOUT_MS,
+ downloadTimeoutMs,
abortSignal,
);
diff --git a/apps/web/actions/loom.ts b/apps/web/actions/loom.ts
index e6429048d..ce2c4af5e 100644
--- a/apps/web/actions/loom.ts
+++ b/apps/web/actions/loom.ts
@@ -100,14 +100,17 @@ const LOOM_CSV_PERMISSION_ERROR =
async function createLoomImportRateLimitCheck(userId: User.UserId) {
if (NODE_ENV !== "production") return async () => false;
- const headersList = await headers();
- const request = new Request("https://cap.so/api/loom-import-rate-limit", {
- method: "POST",
- headers: headersList,
- });
-
return async () => {
try {
+ // Built inside the try: a header value that fails the Fetch spec's
+ // Headers validation (raw HTTP allows byte values that WHATWG
+ // Headers.set rejects) throws here, before checkRateLimit ever runs.
+ const headersList = 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, {
request,
rateLimitKey: `loom-import:${userId}`,