🎥 Make videos programmatically with React
: Add staticFile fallback hint for public assets
Cursor Agent16a65019490d
diff --git a/packages/studio-server/src/preview-server/get-static-file-fallback-hint.ts b/packages/studio-server/src/preview-server/get-static-file-fallback-hint.tsnew file mode 100644index 0000000000..3069333c0b--- /dev/null+++ b/packages/studio-server/src/preview-server/get-static-file-fallback-hint.ts@@ -0,0 +1,53 @@+import {existsSync, statSync} from 'node:fs';+import path from 'node:path';++type GetStaticFileFallbackHintInput = {+ method: string | undefined;+ pathname: string;+ publicDir: string;+};++export const getStaticFileFallbackHint = ({+ method,+ pathname,+ publicDir,+}: GetStaticFileFallbackHintInput): string | null => {+ if (method !== 'GET' && method !== 'HEAD') {+ return null;+ }++ let normalizedPathname: string;+ try {+ normalizedPathname = decodeURIComponent(pathname);+ } catch {+ return null;+ }++ if (normalizedPathname === '/' || !path.extname(normalizedPathname)) {+ return null;+ }++ const relativePath = normalizedPathname.replace(/^\/+/, '');+ if (!relativePath) {+ return null;+ }++ const resolvedPublicDir = path.resolve(publicDir);+ const filePath = path.resolve(publicDir, relativePath);+ const relativeToPublicDir = path.relative(resolvedPublicDir, filePath);++ try {+ if (+ relativeToPublicDir.startsWith('..') ||+ path.isAbsolute(relativeToPublicDir) ||+ !existsSync(filePath) ||+ statSync(filePath).isDirectory()+ ) {+ return null;+ }+ } catch {+ return null;+ }++ return relativePath;+};diff --git a/packages/studio-server/src/routes.ts b/packages/studio-server/src/routes.tsindex 1a2f2a03a4..b3aa9d32da 100644--- a/packages/studio-server/src/routes.ts+++ b/packages/studio-server/src/routes.ts@@ -4,6 +4,7 @@ import type {IncomingMessage, ServerResponse} from 'node:http';import path, {join} from 'node:path';import {URLSearchParams} from 'node:url';import {BundlerInternals} from '@remotion/bundler';+import {RenderInternals} from '@remotion/renderer';import type {LogLevel} from '@remotion/renderer';import type {ApiRoutes,@@ -30,6 +31,7 @@ import {resolveOutputPath} from './helpers/resolve-output-path';import {allApiRoutes} from './preview-server/api-routes';import type {ApiHandler, QueueMethods} from './preview-server/api-types';import {getPackageManager} from './preview-server/get-package-manager';+import {getStaticFileFallbackHint} from './preview-server/get-static-file-fallback-hint';import {handleRequest} from './preview-server/handler';import type {LiveEventsServer} from './preview-server/live-events';import {parseRequestBody} from './preview-server/parse-body';@@ -38,6 +40,7 @@ import {serveStatic} from './preview-server/serve-static';import type {RemotionConfigResponse} from './remotion-config-response';const editorGuess = guessEditor();+const loggedStaticFileHints = new Set<string>();const static404 = (response: ServerResponse): Promise<void> => {response.writeHead(404);@@ -75,6 +78,7 @@ const handleFallback = async ({remotionRoot,hash,response,+ request,getCurrentInputProps,getEnvVariables,publicDir,@@ -89,6 +93,7 @@ const handleFallback = async ({remotionRoot: string;hash: string;response: ServerResponse;+ request: IncomingMessage;publicDir: string;getCurrentInputProps: () => object;getEnvVariables: () => Record<string, string>;@@ -100,6 +105,20 @@ const handleFallback = async ({logLevel: LogLevel;enableCrossSiteIsolation: boolean;}) => {+ const requestUrl = new URL(request.url as string, 'http://localhost');+ const staticFileHint = getStaticFileFallbackHint({+ method: request.method,+ pathname: requestUrl.pathname,+ publicDir,+ });+ if (staticFileHint && !loggedStaticFileHints.has(staticFileHint)) {+ loggedStaticFileHints.add(staticFileHint);+ RenderInternals.Log.warn(+ {indent: false, logLevel},+ `"${requestUrl.pathname}" does not resolve from the root URL, but "public/${staticFileHint}" exists. Use staticFile("${staticFileHint}") instead.`,+ );+ }+const [edit] = await editorGuess;const displayName = getDisplayNameForEditor(edit ? edit.command : null);@@ -618,6 +637,7 @@ export const handleRoutes = ({remotionRoot,hash: staticHash,response,+ request,getCurrentInputProps,getEnvVariables,publicDir,diff --git a/packages/studio-server/src/test/get-static-file-fallback-hint.test.ts b/packages/studio-server/src/test/get-static-file-fallback-hint.test.tsnew file mode 100644index 0000000000..60ae130c14--- /dev/null+++ b/packages/studio-server/src/test/get-static-file-fallback-hint.test.ts@@ -0,0 +1,78 @@+import {expect, test} from 'bun:test';+import {mkdirSync, writeFileSync} from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import {getStaticFileFallbackHint} from '../preview-server/get-static-file-fallback-hint';++const makeTempPublicDir = () => {+ const dir = path.join(+ os.tmpdir(),+ `studio-server-public-${Date.now()}-${Math.random().toString(36).slice(2)}`,+ );+ mkdirSync(dir, {recursive: true});+ return dir;+};++test('returns relative file path when file exists in public dir', () => {+ const publicDir = makeTempPublicDir();+ writeFileSync(path.join(publicDir, 'video.mp4'), 'test');++ const hint = getStaticFileFallbackHint({+ method: 'GET',+ pathname: '/video.mp4',+ publicDir,+ });++ expect(hint).toBe('video.mp4');+});++test('returns null for paths without extension', () => {+ const publicDir = makeTempPublicDir();+ writeFileSync(path.join(publicDir, 'video.mp4'), 'test');++ const hint = getStaticFileFallbackHint({+ method: 'GET',+ pathname: '/about',+ publicDir,+ });++ expect(hint).toBeNull();+});++test('returns null for non-GET and non-HEAD methods', () => {+ const publicDir = makeTempPublicDir();+ writeFileSync(path.join(publicDir, 'video.mp4'), 'test');++ const hint = getStaticFileFallbackHint({+ method: 'POST',+ pathname: '/video.mp4',+ publicDir,+ });++ expect(hint).toBeNull();+});++test('returns null for non-existing file', () => {+ const publicDir = makeTempPublicDir();++ const hint = getStaticFileFallbackHint({+ method: 'GET',+ pathname: '/missing.mp4',+ publicDir,+ });++ expect(hint).toBeNull();+});++test('returns null for path traversal attempts', () => {+ const publicDir = makeTempPublicDir();+ writeFileSync(path.join(publicDir, 'safe.mp4'), 'test');++ const hint = getStaticFileFallbackHint({+ method: 'GET',+ pathname: '/../safe.mp4',+ publicDir,+ });++ expect(hint).toBeNull();+});