Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Use Rust favicon logic in browser extension (#2385)

Leendert de Borstb1cf8174387c
diff --git a/apps/browser-extension/src/utils/FaviconService.ts b/apps/browser-extension/src/utils/FaviconService.ts
index eca31ae27..b091fee63 100644
--- a/apps/browser-extension/src/utils/FaviconService.ts
+++ b/apps/browser-extension/src/utils/FaviconService.ts
@@ -1,6 +1,8 @@
import { Buffer } from 'buffer';
import type { Item } from '@/utils/dist/core/models/vault';
+import type { FaviconTarget } from '@/utils/RustCore';
+import { selectFaviconTarget, toUrlList } from '@/utils/RustCore';
import type { SqliteClient } from '@/utils/SqliteClient';
import type { WebApiService } from '@/utils/WebApiService';
@@ -8,13 +10,9 @@ import type { WebApiService } from '@/utils/WebApiService';
* Result of a favicon fetch operation.
*/
export type FaviconFetchResult = {
- /** Whether a favicon was successfully fetched */
success: boolean;
- /** The decoded favicon image data (if successful) */
imageData?: Uint8Array;
- /** Whether the fetch was skipped because a logo already exists */
skipped?: boolean;
- /** Error message (if failed) */
error?: string;
};
@@ -25,124 +23,34 @@ const FAVICON_FETCH_TIMEOUT_MS = 5000;
/**
* Centralized service for favicon/logo operations.
- * Handles URL normalization, deduplication, and favicon fetching.
*/
export class FaviconService {
/**
- * Extract and normalize source domain from a URL string.
- * This matches the server-side migration logic for consistent deduplication.
- * Uses lowercase and removes www. prefix for case-insensitive matching.
- * @param urlString The URL to extract the domain from
- * @returns The normalized source domain (e.g., 'github.com'), or 'unknown' if extraction fails
+ * Pick the URL a favicon should be fetched from.
+ * @param urlValue The URL field value (single string or multi-value array)
+ * @returns The target, or null when no valid URL is found.
*/
- public static extractSourceFromUrl(urlString: string | undefined | null): string {
- if (!urlString) {
- return 'unknown';
- }
-
- try {
- const url = new URL(urlString.startsWith('http') ? urlString : `https://${urlString}`);
- // Normalize hostname: lowercase and remove www. prefix
- return url.hostname.toLowerCase().replace(/^www\./, '');
- } catch {
- return 'unknown';
- }
- }
-
- /**
- * Normalize a URL string for favicon fetching.
- * Prepends https:// if the URL starts with www.
- * @param url The URL to normalize
- * @returns The normalized URL, or undefined if invalid
- */
- public static normalizeUrl(url: string | undefined | null): string | undefined {
- if (!url) {
- return undefined;
- }
-
- const trimmed = url.trim();
- if (!trimmed) {
- return undefined;
- }
-
- // Check if it's a valid URL format
- if (!trimmed.startsWith('http://') && !trimmed.startsWith('https://') && !trimmed.startsWith('www.')) {
- return undefined;
- }
-
- // Prepend https:// if starts with www.
- return trimmed.startsWith('www.') ? `https://${trimmed}` : trimmed;
- }
-
- /**
- * Extract the first valid URL from a field value (which can be string or string[]).
- * @param urlValue The URL field value
- * @returns The first valid URL, or undefined if none found
- */
- public static extractFirstValidUrl(urlValue: string | string[] | undefined | null): string | undefined {
- if (!urlValue) {
- return undefined;
- }
-
- const urlList = Array.isArray(urlValue) ? urlValue : [urlValue];
-
- // Find the first valid URL (starts with http://, https://, or www.)
- const validUrl = urlList.find(url => {
- const trimmed = url?.trim();
- return trimmed && (trimmed.startsWith('http://') || trimmed.startsWith('https://') || trimmed.startsWith('www.'));
- });
-
- return FaviconService.normalizeUrl(validUrl);
+ public static async resolveTarget(urlValue: string | string[] | undefined | null): Promise<FaviconTarget | null> {
+ return selectFaviconTarget(toUrlList(urlValue));
}
/**
- * Check if a logo already exists for the given URL.
- * @param urlString The URL to check
- * @param sqliteClient The SQLite client instance
- * @returns True if a logo exists for the normalized source domain
- */
- public static hasLogoForUrl(urlString: string | undefined | null, sqliteClient: SqliteClient): boolean {
- if (!urlString) {
- return false;
- }
-
- const source = FaviconService.extractSourceFromUrl(urlString);
- if (source === 'unknown') {
- return false;
- }
-
- return sqliteClient.logos.hasLogoForSource(source);
- }
-
- /**
- * Fetch favicon for a URL from the server API.
+ * Fetch the favicon for a resolved target from the server API.
* Includes deduplication check and timeout handling.
- * @param urlString The URL to fetch favicon for
+ * @param target The favicon target to fetch
* @param sqliteClient The SQLite client for deduplication check
* @param webApi The WebAPI service for making the request
* @param timeoutMs Optional timeout in milliseconds (default: 5000ms)
* @returns FaviconFetchResult with success status and image data
*/
public static async fetchFavicon(
- urlString: string | undefined | null,
+ target: FaviconTarget,
sqliteClient: SqliteClient,
webApi: WebApiService,
timeoutMs: number = FAVICON_FETCH_TIMEOUT_MS
): Promise<FaviconFetchResult> {
- // Validate URL
- const normalizedUrl = FaviconService.normalizeUrl(urlString);
- if (!normalizedUrl) {
- return { success: false, error: 'Invalid URL' };
- }
-
- // Extract source for deduplication check
- const source = FaviconService.extractSourceFromUrl(normalizedUrl);
- if (source === 'unknown') {
- return { success: false, error: 'Could not extract domain from URL' };
- }
-
// Check if logo already exists (deduplication)
- if (sqliteClient.logos.hasLogoForSource(source)) {
+ if (sqliteClient.logos.hasLogoForSource(target.source)) {
return { success: false, skipped: true };
}
@@ -153,7 +61,7 @@ export class FaviconService {
);
// Fetch favicon from API
- const faviconPromise = webApi.get<{ image: string }>(`Favicon/Extract?url=${encodeURIComponent(normalizedUrl)}`);
+ const faviconPromise = webApi.get<{ image: string }>(`Favicon/Extract?url=${encodeURIComponent(target.url)}`);
const faviconResponse = await Promise.race([faviconPromise, timeoutPromise]);
if (faviconResponse?.image) {
@@ -172,13 +80,13 @@ export class FaviconService {
/**
* Fetch and attach favicon to an item if needed.
- * This is a convenience method that combines URL extraction, deduplication, and fetching.
- * If the URL is empty or invalid, clears any existing logo from the item.
+ * This is a convenience method that combines URL selection, deduplication, and fetching.
+ * If no valid URL is found, clears any existing logo from the item.
* @param item The item to potentially update with a logo
* @param urlFieldValue The value of the URL field (can be string or string[])
* @param sqliteClient The SQLite client for deduplication check
* @param webApi The WebAPI service for making the request
- * @returns The updated item with Logo attached (if favicon was fetched), cleared (if URL is empty), or the original item
+ * @returns The updated item with Logo attached (if favicon was fetched), cleared (if no usable URL), or the original item
*/
public static async fetchAndAttachFavicon(
item: Item,
@@ -186,17 +94,17 @@ export class FaviconService {
sqliteClient: SqliteClient,
webApi: WebApiService
): Promise<Item> {
- const urlString = FaviconService.extractFirstValidUrl(urlFieldValue);
+ const target = await FaviconService.resolveTarget(urlFieldValue);
- // If URL is empty or invalid, explicitly clear logo to signal that any existing logo should be removed
- if (!urlString) {
+ // No valid URL found: clear any existing logo.
+ if (!target) {
return {
...item,
Logo: undefined
};
}
- const result = await FaviconService.fetchFavicon(urlString, sqliteClient, webApi);
+ const result = await FaviconService.fetchFavicon(target, sqliteClient, webApi);
if (result.success && result.imageData) {
return {
diff --git a/apps/browser-extension/src/utils/RustCore.ts b/apps/browser-extension/src/utils/RustCore.ts
index b2cce4b17..de2dd47b8 100644
--- a/apps/browser-extension/src/utils/RustCore.ts
+++ b/apps/browser-extension/src/utils/RustCore.ts
@@ -61,6 +61,36 @@ export async function extractRootDomain(domain: string): Promise<string> {
return core.extractRootDomain(domain);
}
+/**
+ * The URL a favicon is fetched from, paired with the `Logos.Source` key it is stored under.
+ */
+export type FaviconTarget = {
+ url: string;
+ source: string;
+};
+
+/**
+ * Read a URL field value as an ordered list, accepting the single-string and multi-value
+ * shapes a field can hold.
+ */
+export function toUrlList(urlValue: string | string[] | undefined | null): string[] {
+ if (!urlValue) {
+ return [];
+ }
+
+ const urlList = Array.isArray(urlValue) ? urlValue : [urlValue];
+ return urlList.filter((url): url is string => Boolean(url?.trim()));
+}
+
+/**
+ * Pick which of an item's URLs a favicon comes from, and the key to store it under.
+ * Returns null when no URL qualifies.
+ */
+export async function selectFaviconTarget(urls: string[]): Promise<FaviconTarget | null> {
+ await initRustCore();
+ return core.selectFaviconTarget(urls) as FaviconTarget | null;
+}
+
/**
* Generate a password or passphrase from the given settings.
*
diff --git a/apps/browser-extension/src/utils/SqliteClient.ts b/apps/browser-extension/src/utils/SqliteClient.ts
index 8fd131039..040d09bf0 100644
--- a/apps/browser-extension/src/utils/SqliteClient.ts
+++ b/apps/browser-extension/src/utils/SqliteClient.ts
@@ -415,24 +415,6 @@ export class SqliteClient implements IDatabaseClient {
}
}
- /**
- * Extract and normalize source domain from a URL string.
- * @param urlString - The URL to extract the domain from
- * @returns The normalized source domain (e.g., 'github.com'), or 'unknown' if extraction fails
- */
- public static extractSourceFromUrl(urlString: string | undefined | null): string {
- if (!urlString) {
- return 'unknown';
- }
-
- try {
- const url = new URL(urlString.startsWith('http') ? urlString : `https://${urlString}`);
- return url.hostname.toLowerCase().replace(/^www\./, '');
- } catch {
- return 'unknown';
- }
- }
-
/**
* Detect MIME type from file signature (magic numbers).
* @param bytes - Binary data to analyze
diff --git a/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts b/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts
index 3c8e18ef1..507a884e2 100644
--- a/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts
+++ b/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts
@@ -1,6 +1,7 @@
import type { Item, ItemField, Attachment, TotpCode, FieldHistory } from '@/utils/dist/core/models/vault';
import { FieldKey, MAX_FIELD_HISTORY_RECORDS } from '@/utils/dist/core/models/vault';
import { getFolderPath } from '@/utils/FolderUtils';
+import { selectFaviconTarget, toUrlList } from '@/utils/RustCore';
import { BaseRepository, type IDatabaseClient } from '../BaseRepository';
import { FieldMapper, type FieldRow } from '../mappers/FieldMapper';
@@ -189,7 +190,7 @@ export class ItemRepository extends BaseRepository {
const itemId = item.Id || this.generateId();
// 1. Handle Logo
- const logoId = this.resolveLogoId(item, currentDateTime);
+ const logoId = await this.resolveLogoIdAsync(item, currentDateTime);
// 2. Insert Item
this.client.executeUpdate(ItemQueries.INSERT_ITEM, [
@@ -363,7 +364,7 @@ export class ItemRepository extends BaseRepository {
const currentDateTime = this.now();
// 1. Handle Logo
- const logoId = this.resolveLogoId(item, currentDateTime);
+ const logoId = await this.resolveLogoIdAsync(item, currentDateTime);
// 2. Update Item only if item-level fields changed
const existing = this.client.executeQuery<{
@@ -585,22 +586,21 @@ export class ItemRepository extends BaseRepository {
/**
* Resolve the logo ID for an item (create new or reuse existing).
*/
- private resolveLogoId(item: Item, currentDateTime: string): string | null {
+ private async resolveLogoIdAsync(item: Item, currentDateTime: string): Promise<string | null> {
const urlField = item.Fields?.find(f => f.FieldKey === 'login.url');
- const urlValue = urlField?.Value;
- const urlString = Array.isArray(urlValue) ? urlValue[0] : urlValue;
- const source = this.logoRepository.extractSourceFromUrl(urlString);
+ const target = await selectFaviconTarget(toUrlList(urlField?.Value));
+
+ // No URL a favicon can come from: the item carries no logo.
+ if (!target) {
+ return null;
+ }
if (item.Logo) {
const logoData = this.logoRepository.convertLogoToUint8Array(item.Logo);
- if (logoData) {
- return this.logoRepository.getOrCreate(source, logoData, currentDateTime);
- }
- } else if (source !== 'unknown') {
- return this.logoRepository.getIdForSource(source);
+ return logoData ? this.logoRepository.getOrCreate(target.source, logoData, currentDateTime) : null;
}
- return null;
+ return this.logoRepository.getIdForSource(target.source);
}
/**
diff --git a/apps/browser-extension/src/utils/db/repositories/LogoRepository.ts b/apps/browser-extension/src/utils/db/repositories/LogoRepository.ts
index b755a9d3d..4efd7dbaf 100644
--- a/apps/browser-extension/src/utils/db/repositories/LogoRepository.ts
+++ b/apps/browser-extension/src/utils/db/repositories/LogoRepository.ts
@@ -73,26 +73,6 @@ export class LogoRepository extends BaseRepository {
return logoId;
}
- /**
- * Extract and normalize source domain from a URL string.
- * Uses lowercase and removes www. prefix for case-insensitive matching.
- * @param urlString The URL to extract the domain from
- * @returns The normalized source domain (e.g., 'github.com'), or 'unknown' if extraction fails
- */
- public extractSourceFromUrl(urlString: string | undefined | null): string {
- if (!urlString) {
- return 'unknown';
- }
-
- try {
- const url = new URL(urlString.startsWith('http') ? urlString : `https://${urlString}`);
- // Normalize hostname: lowercase and remove www. prefix
- return url.hostname.toLowerCase().replace(/^www\./, '');
- } catch {
- return 'unknown';
- }
- }
-
/**
* Convert logo data from various formats to Uint8Array.
* @param logo The logo data in various possible formats