Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Load and proxy desktop profile images safely

Richie McIlroy466cdd5d2521
diff --git a/apps/desktop/src/routes/(window-chrome)/settings.tsx b/apps/desktop/src/routes/(window-chrome)/settings.tsx
index d721b5ee5..88fdb04db 100644
--- a/apps/desktop/src/routes/(window-chrome)/settings.tsx
+++ b/apps/desktop/src/routes/(window-chrome)/settings.tsx
@@ -3,12 +3,15 @@ import { A, type RouteSectionProps, useNavigate } from "@solidjs/router";
import { createQuery, useQueryClient } from "@tanstack/solid-query";
import { getVersion } from "@tauri-apps/api/app";
import * as dialog from "@tauri-apps/plugin-dialog";
+import { fetch as tauriFetch } from "@tauri-apps/plugin-http";
import * as shell from "@tauri-apps/plugin-shell";
import { check } from "@tauri-apps/plugin-updater";
import {
+ createEffect,
createMemo,
createSignal,
For,
+ on,
onCleanup,
onMount,
Show,
@@ -26,6 +29,7 @@ import IconLucideUserRound from "~icons/lucide/user-round";
const USER_PROFILE_CACHE_GC_MS = 2 * 60 * 60 * 1000;
const USER_PROFILE_REFRESH_INTERVAL_MS = 5 * 60 * 1000;
+const MAX_PROFILE_IMAGE_BYTES = 5 * 1024 * 1024;
type AuthState = Awaited<ReturnType<typeof authStore.get>>;
type CachedUserProfile = Awaited<ReturnType<typeof userProfileStore.get>>;
@@ -46,6 +50,36 @@ function isCachedProfileForUser(
return cachedProfile?.userId === (userId ?? null);
}
+async function loadProfileImageObjectUrl(signal: AbortSignal) {
+ const imageUrl = new URL(
+ "/api/desktop/user/profile/image",
+ clientEnv.VITE_SERVER_URL,
+ ).toString();
+
+ const response = await tauriFetch(imageUrl, {
+ headers: await protectedHeaders(),
+ signal,
+ });
+ if (!response.ok) throw new Error("Failed to load profile image");
+
+ const contentType = response.headers.get("content-type");
+ if (contentType && !contentType.toLowerCase().startsWith("image/")) {
+ throw new Error("Invalid profile image response");
+ }
+
+ const contentLength = Number(response.headers.get("content-length"));
+ if (contentLength > MAX_PROFILE_IMAGE_BYTES) {
+ throw new Error("Profile image is too large");
+ }
+
+ const blob = await response.blob();
+ if (blob.size > MAX_PROFILE_IMAGE_BYTES) {
+ throw new Error("Profile image is too large");
+ }
+
+ return URL.createObjectURL(blob);
+}
+
function SettingsContentSkeleton() {
return (
<div class="cap-settings-page flex flex-col h-full custom-scroll">
@@ -100,6 +134,12 @@ export default function Settings(props: RouteSectionProps) {
const [authLoaded, setAuthLoaded] = createSignal(false);
const [version, setVersion] = createSignal<string | null>(null);
const [isCheckingForUpdates, setIsCheckingForUpdates] = createSignal(false);
+ const [failedProfileImageUrl, setFailedProfileImageUrl] = createSignal<
+ string | null
+ >(null);
+ const [profileImageObjectUrl, setProfileImageObjectUrl] = createSignal<
+ string | null
+ >(null);
const clearLocalAuth = async () => {
setAuth(undefined);
queryClient.removeQueries({ queryKey: ["settings-user-profile"] });
@@ -210,12 +250,15 @@ export default function Settings(props: RouteSectionProps) {
return "Signed in";
});
- const accountImageUrl = createMemo(() => {
+ const accountRemoteImageUrl = createMemo(() => {
if (!userProfile.isSuccess) return null;
const imageUrl = userProfile.data?.imageUrl?.trim();
+ if (imageUrl && imageUrl === failedProfileImageUrl()) return null;
+
return imageUrl || null;
});
+ const accountImageUrl = createMemo(() => profileImageObjectUrl());
const openDashboard = () => {
void shell.open(
new URL("/dashboard", clientEnv.VITE_SERVER_URL).toString(),
@@ -235,6 +278,44 @@ export default function Settings(props: RouteSectionProps) {
signIn.mutate(new AbortController());
};
+ const handleProfileImageError = (imageUrl: string) => {
+ setFailedProfileImageUrl(imageUrl);
+ void userProfile.refetch();
+ };
+
+ createEffect(
+ on(accountRemoteImageUrl, (imageUrl) => {
+ setProfileImageObjectUrl(null);
+
+ if (!imageUrl) return;
+
+ const abort = new AbortController();
+ let disposed = false;
+ let objectUrl: string | null = null;
+
+ void loadProfileImageObjectUrl(abort.signal)
+ .then((url) => {
+ if (disposed) {
+ URL.revokeObjectURL(url);
+ return;
+ }
+
+ objectUrl = url;
+ setProfileImageObjectUrl(url);
+ })
+ .catch(() => {
+ if (!disposed && !abort.signal.aborted) {
+ handleProfileImageError(imageUrl);
+ }
+ });
+
+ onCleanup(() => {
+ disposed = true;
+ abort.abort();
+ if (objectUrl) URL.revokeObjectURL(objectUrl);
+ });
+ }),
+ );
onMount(() => {
void getVersion()
@@ -371,6 +452,11 @@ export default function Settings(props: RouteSectionProps) {
src={imageUrl()}
alt=""
draggable={false}
+ onError={() => {
+ const remoteUrl = accountRemoteImageUrl();
+ if (remoteUrl) handleProfileImageError(remoteUrl);
+ setProfileImageObjectUrl(null);
+ }}
/>
)}
</Show>
diff --git a/apps/web/app/api/desktop/[...route]/root.ts b/apps/web/app/api/desktop/[...route]/root.ts
index 556ef72e9..eb948067f 100644
--- a/apps/web/app/api/desktop/[...route]/root.ts
+++ b/apps/web/app/api/desktop/[...route]/root.ts
@@ -35,6 +35,8 @@ import {
export const app = new Hono();
+const MAX_DESKTOP_PROFILE_IMAGE_BYTES = 5 * 1024 * 1024;
+
async function resolveOrganizationIconUrl(iconUrl: string | null) {
if (!iconUrl) return null;
@@ -57,6 +59,22 @@ async function resolveUserImageUrl(imageUrl: string | null) {
}).pipe(runPromise);
}
+async function fetchDesktopProfileImage(imageUrl: string) {
+ const response = await fetch(imageUrl);
+ if (!response.ok) return null;
+
+ const contentType = response.headers.get("content-type");
+ if (!contentType?.toLowerCase().startsWith("image/")) return null;
+
+ const contentLength = Number(response.headers.get("content-length"));
+ if (contentLength > MAX_DESKTOP_PROFILE_IMAGE_BYTES) return null;
+
+ const bytes = await response.arrayBuffer();
+ if (bytes.byteLength > MAX_DESKTOP_PROFILE_IMAGE_BYTES) return null;
+
+ return { bytes, contentType };
+}
+
type DesktopProfileUser = {
name: string | null;
lastName: string | null;
@@ -486,6 +504,25 @@ app.get("/user/profile", async (c) => {
});
});
+app.get("/user/profile/image", async (c) => {
+ const user = await getDesktopProfileUser(c);
+ if (!user) return c.text("User not authenticated", 401);
+ if (!user.image) return c.body(null, 404);
+
+ const imageUrl = await resolveUserImageUrl(user.image);
+ if (!imageUrl) return c.body(null, 404);
+
+ const image = await fetchDesktopProfileImage(imageUrl);
+ if (!image) return c.body(null, 404);
+
+ return new Response(image.bytes, {
+ headers: {
+ "Cache-Control": "private, max-age=300",
+ "Content-Type": image.contentType,
+ },
+ });
+});
+
app.get("/organizations", withAuth, async (c) => {
const user = c.get("user");