Skip to content

Commit

Permalink
Merge pull request #384 from lanedirt/383-add-try-catch-around-favico…
Browse files Browse the repository at this point in the history
…n-extractor-to-prevent-hostname-could-not-be-parsed-exceptions

Log failed favicon extraction as information instead of warning
  • Loading branch information
lanedirt authored Nov 20, 2024
2 parents ba17474 + cc4a2e0 commit 0f377bd
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/AliasVault.Api/Controllers/FaviconController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ namespace AliasVault.Api.Controllers;
/// Controller for retrieving favicons from external websites.
/// </summary>
/// <param name="userManager">UserManager instance.</param>
/// <param name="logger">Logger instance.</param>
[ApiVersion("1")]
public class FaviconController(UserManager<AliasVaultUser> userManager) : AuthenticatedRequestController(userManager)
public class FaviconController(UserManager<AliasVaultUser> userManager, ILogger<FaviconController> logger) : AuthenticatedRequestController(userManager)
{
/// <summary>
/// Proxies the request to the identity generator to generate a random identity.
Expand All @@ -36,9 +37,22 @@ public async Task<IActionResult> Extract(string url)
}

// Get the favicon from the URL.
var image = await FaviconExtractor.FaviconExtractor.GetFaviconAsync(url);
try
{
var image = await FaviconExtractor.FaviconExtractor.GetFaviconAsync(url);

// Return the favicon as base64 string of image representation.
return Ok(new FaviconExtractModel { Image = image });
}
catch (Exception ex)
{
// Anonymize the URL by replacing all a-Z characters with 'x' before logging.
// This will still allow to see the host structure but not the actual domain.
var anonymizedUrl = new string(url.Select(c => char.IsLetter(c) ? 'x' : c).ToArray());
logger.LogInformation(ex, "Failed to extract favicon from {Url}", anonymizedUrl);
}

// Return the favicon as base64 string of image representation.
return Ok(new FaviconExtractModel { Image = image });
// Return null if favicon extraction failed.
return Ok(new FaviconExtractModel { Image = null });
}
}

0 comments on commit 0f377bd

Please sign in to comment.