-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Harden Electron renderer security #532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,89 @@ import { | |
| type MotionBlurState, | ||
| } from "./videoPlayback/zoomTransform"; | ||
|
|
||
| const REMOTE_MEDIA_URL_RE = /^(https?:|blob:|data:)/i; | ||
|
|
||
| function fileUrlToPath(fileUrl: string): string { | ||
| const url = new URL(fileUrl); | ||
| const pathname = decodeURIComponent(url.pathname); | ||
|
|
||
| if (url.host && url.host !== "localhost") { | ||
| return `//${url.host}${pathname}`; | ||
| } | ||
|
|
||
| return pathname; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Handle Windows Useful? React with 👍 / 👎. |
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| function getReadableMediaPath(mediaPath: string): string | null { | ||
| if (!mediaPath || REMOTE_MEDIA_URL_RE.test(mediaPath)) { | ||
| return null; | ||
| } | ||
|
|
||
| if (/^file:\/\//i.test(mediaPath)) { | ||
| try { | ||
| return fileUrlToPath(mediaPath); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| return mediaPath; | ||
| } | ||
|
|
||
| function getVideoMimeType(mediaPath: string): string { | ||
| const lowerPath = mediaPath.toLowerCase(); | ||
| if (lowerPath.endsWith(".mp4")) return "video/mp4"; | ||
| if (lowerPath.endsWith(".mov")) return "video/quicktime"; | ||
| if (lowerPath.endsWith(".webm")) return "video/webm"; | ||
| return "application/octet-stream"; | ||
| } | ||
|
Comment on lines
+95
to
+102
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid double-wrapping already-converted media URLs. If quick guard function getPlayableMediaPath(mediaPath: string): string {
+ if (new RegExp(`^${LOCAL_MEDIA_PROTOCOL}://`, "i").test(mediaPath)) {
+ return mediaPath;
+ }
+
const readablePath = getReadableMediaPath(mediaPath);
if (!readablePath) {
return mediaPath;
}🤖 Prompt for AI Agents |
||
|
|
||
| function usePlayableMediaUrl(mediaPath?: string): string | undefined { | ||
| const [playableUrl, setPlayableUrl] = useState(mediaPath); | ||
|
|
||
| useEffect(() => { | ||
| if (!mediaPath) { | ||
| setPlayableUrl(undefined); | ||
| return; | ||
| } | ||
|
|
||
| const readablePath = getReadableMediaPath(mediaPath); | ||
| if (!readablePath || !window.electronAPI?.readBinaryFile) { | ||
| setPlayableUrl(mediaPath); | ||
| return; | ||
| } | ||
| const localPath = readablePath; | ||
|
|
||
| let canceled = false; | ||
| let objectUrl: string | null = null; | ||
|
|
||
| async function loadLocalMedia() { | ||
| const result = await window.electronAPI.readBinaryFile(localPath); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This now loads each local video via Useful? React with 👍 / 👎. |
||
| if (canceled) return; | ||
|
|
||
| if (!result.success || !result.data) { | ||
| setPlayableUrl(mediaPath); | ||
| return; | ||
| } | ||
|
|
||
| const blob = new Blob([result.data], { type: getVideoMimeType(localPath) }); | ||
| objectUrl = URL.createObjectURL(blob); | ||
| setPlayableUrl(objectUrl); | ||
| } | ||
|
|
||
| void loadLocalMedia(); | ||
|
|
||
| return () => { | ||
| canceled = true; | ||
| if (objectUrl) { | ||
| URL.revokeObjectURL(objectUrl); | ||
| } | ||
| }; | ||
| }, [mediaPath]); | ||
|
Comment on lines
+104
to
+116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don’t feed the blocked local path into
safer loading flow function usePlayableMediaUrl(mediaPath?: string): string | undefined {
- const [playableUrl, setPlayableUrl] = useState(mediaPath);
+ const [playableUrl, setPlayableUrl] = useState<string | undefined>(() => {
+ if (!mediaPath) return undefined;
+ return getReadableMediaPath(mediaPath) ? undefined : mediaPath;
+ });
useEffect(() => {
if (!mediaPath) {
setPlayableUrl(undefined);
return;
@@
const readablePath = getReadableMediaPath(mediaPath);
if (!readablePath || !window.electronAPI?.readBinaryFile) {
setPlayableUrl(mediaPath);
return;
}
+ setPlayableUrl(undefined);
const localPath = readablePath;
@@
- if (!result.success || !result.data) {
- setPlayableUrl(mediaPath);
+ if (!result.success || !result.data) {
+ setPlayableUrl(undefined);
return;
}🤖 Prompt for AI Agents |
||
|
|
||
| return playableUrl; | ||
| } | ||
|
|
||
| interface VideoPlaybackProps { | ||
| videoPath: string; | ||
| webcamVideoPath?: string; | ||
|
|
@@ -250,6 +333,8 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>( | |
| const videoReadyRafRef = useRef<number | null>(null); | ||
| const smoothedAutoFocusRef = useRef<ZoomFocus | null>(null); | ||
| const prevTargetProgressRef = useRef(0); | ||
| const playableVideoPath = usePlayableMediaUrl(videoPath); | ||
| const playableWebcamVideoPath = usePlayableMediaUrl(webcamVideoPath); | ||
|
|
||
| const clampFocusToStage = useCallback((focus: ZoomFocus, depth: ZoomDepth) => { | ||
| return clampFocusToStageUtil(focus, depth, stageSizeRef.current); | ||
|
|
@@ -1184,7 +1269,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>( | |
|
|
||
| useEffect(() => { | ||
| const webcamVideo = webcamVideoRef.current; | ||
| if (!webcamVideo || !webcamVideoPath) { | ||
| if (!webcamVideo || !playableWebcamVideoPath) { | ||
| setWebcamDimensions(null); | ||
| return; | ||
| } | ||
|
|
@@ -1203,11 +1288,11 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>( | |
| return () => { | ||
| webcamVideo.removeEventListener("loadedmetadata", handleLoadedMetadata); | ||
| }; | ||
| }, [webcamVideoPath]); | ||
| }, [playableWebcamVideoPath]); | ||
|
|
||
| useEffect(() => { | ||
| const webcamVideo = webcamVideoRef.current; | ||
| if (!webcamVideo || !webcamVideoPath) { | ||
| if (!webcamVideo || !playableWebcamVideoPath) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -1232,17 +1317,17 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>( | |
| webcamVideo.play().catch(() => { | ||
| // Ignore webcam autoplay restoration failures. | ||
| }); | ||
| }, [currentTime, isPlaying, speedRegions, webcamVideoPath]); | ||
| }, [currentTime, isPlaying, speedRegions, playableWebcamVideoPath]); | ||
|
|
||
| useEffect(() => { | ||
| const webcamVideo = webcamVideoRef.current; | ||
| if (!webcamVideo || !webcamVideoPath) { | ||
| if (!webcamVideo || !playableWebcamVideoPath) { | ||
| return; | ||
| } | ||
|
|
||
| webcamVideo.pause(); | ||
| webcamVideo.currentTime = 0; | ||
| }, [webcamVideoPath]); | ||
| }, [playableWebcamVideoPath]); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
|
|
@@ -1303,7 +1388,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>( | |
| : "none", | ||
| }} | ||
| /> | ||
| {webcamVideoPath && | ||
| {playableWebcamVideoPath && | ||
| (() => { | ||
| const clipPath = getCssClipPath(webcamLayout?.maskShape ?? "rectangle"); | ||
| const useClipPath = !!clipPath; | ||
|
|
@@ -1325,7 +1410,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>( | |
| > | ||
| <video | ||
| ref={webcamVideoRef} | ||
| src={webcamVideoPath} | ||
| src={playableWebcamVideoPath} | ||
| className={`w-full h-full object-cover ${webcamLayoutPreset === "picture-in-picture" ? "cursor-grab active:cursor-grabbing" : "pointer-events-none"}`} | ||
| style={{ | ||
| borderRadius: useClipPath ? 0 : (webcamLayout?.borderRadius ?? 0), | ||
|
|
@@ -1479,7 +1564,7 @@ const VideoPlayback = forwardRef<VideoPlaybackRef, VideoPlaybackProps>( | |
| )} | ||
| <video | ||
| ref={videoRef} | ||
| src={videoPath} | ||
| src={playableVideoPath} | ||
| className="hidden" | ||
| preload="metadata" | ||
| playsInline | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.