fix: crash when favicon endpoint returns non-image response - #173
Merged
Conversation
The favicon fetcher now rejects non-image responses at two levels: 1. Content-Type header check — rejects responses that are clearly not images (e.g. text/html served by misconfigured servers) 2. Magic byte validation — verifies the response body starts with recognized image format signatures (PNG, JPEG, GIF, ICO, BMP, WebP, SVG) Previously, servers returning HTML error pages (like nginx default pages) for /favicon.ico would pass raw HTML to Fyne's image renderer, causing XML parse errors and potential crashes. The cache reader also validates stored data on read, automatically evicting entries that don't pass the image format check (protecting against previously cached invalid responses).
The fetchAndUpdateFavicon goroutine was updating img.Resource and calling img.Refresh() directly from a background goroutine without synchronization. This could cause a nil pointer dereference when Fyne's renderer accessed the image concurrently. Wrap the UI update in fyne.Do() to ensure it runs on the main thread. Also rename the static resource from "favicon.png" to "favicon" to avoid Fyne making incorrect format assumptions from the file extension.
Update existing test servers to set Content-Type: image/* headers, matching the new validation requirements. Add new test cases: - TestFetcher_RejectsHTMLResponse: verifies HTML content-type is rejected - TestFetcher_RejectsNonImageData: verifies non-image bytes are rejected even when server claims image content-type - TestFetcher_CacheEvictsInvalidData: verifies invalid cached entries are evicted and fresh data is fetched - TestIsImageData: table-driven tests for magic byte detection - TestIsImageContentType: table-driven tests for content-type parsing
- Reduce cyclomatic complexity by using a table-driven magic byte check with bytes.HasPrefix instead of individual if-statements (gocyclo) - Extract GIF magic strings into the imageMagic table (goconst) - Use direct return of strings.Contains for SVG check (staticcheck S1008)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a crash (
nil pointer dereference) when the favicon fetcher receives non-image content (e.g. HTML error pages) from servers that don't serve a proper favicon.Reproducible with:
linkquisition "https://console.mistral.ai"(their/favicon.icoreturns an nginx HTML page).Changes
Validate favicon responses (
internal/favicon/favicon.go)image/*Content-Type headersFix thread-safety in UI update (
cmd/browser_picker.go)img.Resource/img.Refresh()infyne.Do()to prevent concurrent access from the favicon goroutine"favicon.png"to"favicon"to avoid format assumption from extensionTests (
internal/favicon/favicon_test.go)Content-Typeheaders on test serversTesting
linkquisition "https://console.mistral.ai"no longer crashes (favicon gracefully falls back to placeholder)