From f9977fb29e7fdaf18f5a24fceddb96840ec85721 Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Thu, 27 Feb 2025 16:11:23 +0100 Subject: [PATCH] Update favicon display in client to handle SVG (#622) --- docs/misc/dev/browser-extensions.md | 4 +- .../Credentials/DisplayFavicon.razor | 43 ++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/docs/misc/dev/browser-extensions.md b/docs/misc/dev/browser-extensions.md index ef8e1eb5a..0a96d2644 100644 --- a/docs/misc/dev/browser-extensions.md +++ b/docs/misc/dev/browser-extensions.md @@ -56,5 +56,5 @@ The following websites have been known to cause issues in the past (but should b | --- | --- | | https://www.paprika-shopping.nl/nieuwsbrief/newsletter-register-landing.html | Popup CSS style conflicts | | https://bloshing.com/inschrijven-nieuwsbrief | Popup CSS style conflicts | -| https://gamefaqs.gamespot.com/user | Popup buttons not working | -| https://news.ycombinator.com/login?goto=news | Popup logo not showing due to SVG format after identity creation | \ No newline at end of file +| https://gamefaqs.gamespot.com/user | Popup buttons not working | +| https://news.ycombinator.com/login?goto=news | Popup and client favicon not showing due to SVG format | diff --git a/src/AliasVault.Client/Main/Components/Credentials/DisplayFavicon.razor b/src/AliasVault.Client/Main/Components/Credentials/DisplayFavicon.razor index a3186fc28..fe0237ede 100644 --- a/src/AliasVault.Client/Main/Components/Credentials/DisplayFavicon.razor +++ b/src/AliasVault.Client/Main/Components/Credentials/DisplayFavicon.razor @@ -26,6 +26,9 @@ else [Parameter] public bool Padding { get; set; } + /// + /// The data URL of the favicon. + /// private string? _faviconDataUrl; /// @@ -33,8 +36,46 @@ else { if (FaviconBytes is not null) { + string mimeType = DetectMimeType(FaviconBytes); string base64String = Convert.ToBase64String(FaviconBytes); - _faviconDataUrl = $"data:image/x-icon;base64,{base64String}"; + _faviconDataUrl = $"data:{mimeType};base64,{base64String}"; } } + + /// + /// Detect the mime type of the favicon. + /// + /// The bytes of the favicon. + /// The mime type of the favicon. + private string DetectMimeType(byte[] bytes) + { + // Check for SVG. + if (bytes.Length >= 5) + { + string header = System.Text.Encoding.ASCII.GetString(bytes.Take(5).ToArray()).ToLower(); + if (header.Contains("= 4 && + bytes[0] == 0x00 && bytes[1] == 0x00 && + bytes[2] == 0x01 && bytes[3] == 0x00) + { + return "image/x-icon"; + } + + // Check for PNG. + if (bytes.Length >= 4 && + bytes[0] == 0x89 && bytes[1] == 0x50 && + bytes[2] == 0x4E && bytes[3] == 0x47) + { + return "image/png"; + } + + // Default to x-icon if unknown. + return "image/x-icon"; + } }