diff --git a/src/components/common/Logo.tsx b/src/components/common/Logo.tsx
index 54ad0d9..a862b46 100644
--- a/src/components/common/Logo.tsx
+++ b/src/components/common/Logo.tsx
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
-import { useEffect, useSyncExternalStore } from 'react';
+import { useEffect, useState, useSyncExternalStore } from 'react';
import { useTranslation } from 'react-i18next';
import inbuxaMark from '@/assets/inbuxa-mark.png';
@@ -63,10 +63,22 @@ export function DefaultLogo() {
export default function Logo() {
const { t } = useTranslation();
const logo = useSyncExternalStore(subscribeToLogo, getLogoState, getLogoState);
+ const [broken, setBroken] = useState(false);
useEffect(loadLogoOnce, []);
switch (logo.status) {
case 'custom':
- return
;
+ // A logo that loaded once can still fail later (its host goes away
+ // after the cache runs out); the inbuxa logo beats a broken image.
+ return broken ? (
+
+ ) : (
+
setBroken(true)}
+ />
+ );
case 'loading':
return ;
default:
diff --git a/src/lib/logoCache.test.ts b/src/lib/logoCache.test.ts
index ae7b424..7de527c 100644
--- a/src/lib/logoCache.test.ts
+++ b/src/lib/logoCache.test.ts
@@ -8,38 +8,55 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
vi.mock('@/services/api', () => ({ getApiBaseUrl: () => 'https://mail.example' }));
-/** A fresh copy of the module, since it fetches once per page load. */
-async function load(response: Response | Error) {
- vi.resetModules();
- const fetchMock = vi.fn(() => (response instanceof Error ? Promise.reject(response) : Promise.resolve(response)));
+/** An `Image` that loads, or fails, the way the browser would for `/logo`. */
+function fakeImage(loads: boolean) {
+ const sources: string[] = [];
+ class FakeImage {
+ onload: (() => void) | null = null;
+ onerror: (() => void) | null = null;
+ set src(value: string) {
+ sources.push(value);
+ queueMicrotask(() => (loads ? this.onload?.() : this.onerror?.()));
+ }
+ }
+ vi.stubGlobal('Image', FakeImage);
+ const fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);
+ return { sources, fetchMock };
+}
+
+/** A fresh copy of the module, since it probes once per page load. */
+async function load(loads: boolean) {
+ vi.resetModules();
+ const image = fakeImage(loads);
const mod = await import('./logoCache');
const changed = vi.fn();
mod.subscribeToLogo(changed);
+ expect(mod.getLogoState()).toEqual({ status: 'loading' });
mod.loadLogoOnce();
mod.loadLogoOnce();
await vi.waitFor(() => expect(changed).toHaveBeenCalled());
- return { state: mod.getLogoState(), fetchMock };
+ return { state: mod.getLogoState(), ...image };
}
describe('logo cache', () => {
afterEach(() => vi.unstubAllGlobals());
- it('uses an image the server sends, fetching it only once', async () => {
- URL.createObjectURL = vi.fn(() => 'blob:logo');
- const { state, fetchMock } = await load(
- new Response(new Blob(['png']), { headers: { 'content-type': 'image/png' } }),
- );
- expect(state).toEqual({ status: 'custom', url: 'blob:logo' });
- expect(fetchMock).toHaveBeenCalledTimes(1);
- expect(fetchMock).toHaveBeenCalledWith('https://mail.example/logo');
+ it('shows the server logo when /logo loads as an image, probing only once', async () => {
+ const { state, sources } = await load(true);
+ expect(state).toEqual({ status: 'custom', url: 'https://mail.example/logo' });
+ expect(sources).toEqual(['https://mail.example/logo']);
});
- it('falls back to the inbuxa logo for anything else', async () => {
- expect((await load(new Response('nope', { status: 404 }))).state).toEqual({ status: 'default' });
- expect((await load(new Response('', { headers: { 'content-type': 'text/html' } }))).state).toEqual({
- status: 'default',
- });
- expect((await load(new Error('offline'))).state).toEqual({ status: 'default' });
+ // inbuxa-admin#225: a logo set as an https URL makes /logo redirect to
+ // another site. A fetch needs that site's CORS permission to follow it; an
+ // image doesn't, so the console must never fetch the logo.
+ it('loads it as an image, never with fetch', async () => {
+ const { fetchMock } = await load(true);
+ expect(fetchMock).not.toHaveBeenCalled();
+ });
+
+ it('falls back to the inbuxa logo when /logo is not an image', async () => {
+ expect((await load(false)).state).toEqual({ status: 'default' });
});
});
diff --git a/src/lib/logoCache.ts b/src/lib/logoCache.ts
index aae71af..99d514b 100644
--- a/src/lib/logoCache.ts
+++ b/src/lib/logoCache.ts
@@ -5,9 +5,14 @@
*/
/**
- * The server's own logo, if an admin set one: fetched once per page load
- * from `/logo` and kept as an object URL. Anything but an image (no logo, an
+ * The server's own logo, if an admin set one: probed once per page load by
+ * loading `/logo` as an image. Anything that isn't an image (no logo, an
* error page, no answer) means the inbuxa logo.
+ *
+ * An image, not a fetch: a logo set as an https URL makes `/logo` redirect to
+ * that URL, and a fetch only follows a redirect to another site when that
+ * site allows it (CORS), which most image hosts don't (inbuxa-admin#225). An
+ * image needs no such permission, the same as the server's sign-in page.
*/
import { getApiBaseUrl } from '@/services/api';
@@ -24,22 +29,23 @@ export function subscribeToLogo(watch: () => void): () => void {
return () => void watchers.delete(watch);
}
-async function fetchLogo(): Promise {
- try {
- const res = await fetch(`${getApiBaseUrl()}/logo`);
- if (!res.ok || !res.headers.get('content-type')?.startsWith('image/')) return null;
- return URL.createObjectURL(await res.blob());
- } catch {
- return null;
- }
+/** Whether `src` loads as an image; the browser caches it for the `
`. */
+function loadsAsImage(src: string): Promise {
+ return new Promise((resolve) => {
+ const image = new Image();
+ image.onload = () => resolve(true);
+ image.onerror = () => resolve(false);
+ image.src = src;
+ });
}
-/** Starts the fetch; later calls do nothing. */
+/** Starts the probe; later calls do nothing. */
export function loadLogoOnce(): void {
if (requested) return;
requested = true;
- void fetchLogo().then((url) => {
- logo = url ? { status: 'custom', url } : { status: 'default' };
+ const url = `${getApiBaseUrl()}/logo`;
+ void loadsAsImage(url).then((loaded) => {
+ logo = loaded ? { status: 'custom', url } : { status: 'default' };
for (const watch of watchers) watch();
});
}