Skip to content

Conversation

@oxqnd
Copy link
Contributor

@oxqnd oxqnd commented Jul 20, 2025

Fixes #2203

Problem

-favicon mis-resolved directory-relative favicon paths when the final page is under a subdirectory.
Example final URL: /global-protect/login.esp
HTML:
The code treated it as /portal/images/... (dropped /global-protect/), fetched 404, so no hash.

Additional issues:

  • No support
  • .ico prioritization inverted
  • No root fallback after directory-relative failure
  • No data: (base64) favicon support

Root Cause

Relative href joined against root instead of the final redirect URL directory; did not use net/url ResolveReference.

Changes

  • Use final redirect URL directory as base (finalURL)
  • Support
  • Stable sort preferring .ico
  • Support data: URL (base64) favicons
  • Root fallback (if relative no leading slash fails once)
  • Keep max 2 network attempts
  • Debug log (resolved URL, raw href, size)

Result

Before (no favicon hash printed):
https://211.148.131.226:10443/ [302,200] []

After (favicon hash appended):
https://211.148.131.226:10443/ [302,200] [-631559155]

Debug log:
[DBG] favicon resolved url=https://211.148.131.226:10443/global-protect/portal/images/favicon.ico raw_href=portal/images/favicon.ico size=2518 bytes

Summary by CodeRabbit

  • New Features

    • Improved favicon detection by accurately resolving favicon URLs using the final redirected URL and any base URL specified in the page.
    • Enhanced support for extracting various favicon link types, including base64-encoded icons.
    • Added prioritization for .ico favicon files and improved fallback handling for default favicon paths.
  • Bug Fixes

    • Increased reliability of favicon fetching and hashing, especially on sites with redirects or complex HTML structures.

@coderabbitai
Copy link

coderabbitai bot commented Jul 20, 2025

Walkthrough

The changes refactor and enhance the favicon extraction and hashing logic. The HandleFaviconHash method and its helper now consider the final redirected URL and <base href> tags for more accurate favicon URL resolution. The extraction process is improved, supporting better candidate prioritization, base64 data URLs, and robust fallback handling.

Changes

File(s) Change Summary
runner/runner.go Refactored favicon extraction and hashing logic: updated method signatures, improved URL resolution using finalURL and <base href>, enhanced favicon candidate extraction and sorting, added base64 data URL support, and refined fallback handling. Updated call site to pass new parameter.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Runner
    participant HTTPX
    participant TargetServer

    User->>Runner: Initiate analysis with favicon flag
    Runner->>HTTPX: Send HTTP request
    HTTPX->>TargetServer: Fetch URL (handle redirects)
    TargetServer-->>HTTPX: Return HTML response (possibly with redirects)
    HTTPX-->>Runner: Provide response, finalURL

    Runner->>Runner: extractPotentialFavIconsURLs(response)
    Runner->>Runner: Determine base URL (finalURL, <base href>)
    alt Favicon link is base64 data URL
        Runner->>Runner: Decode and hash favicon
    else Favicon link is URL
        Runner->>TargetServer: Fetch favicon from resolved URL
        TargetServer-->>Runner: Return favicon data
        alt Fetch fails and candidate is relative
            Runner->>TargetServer: Retry with root-relative path
            TargetServer-->>Runner: Return favicon data
        end
        Runner->>Runner: Hash favicon data
    end
    Runner-->>User: Return favicon hash and info
Loading

Poem

In the warren where favicons hide,
A rabbit hops with logic as its guide.
Through redirects and bases, it sniffs each clue,
Decoding, fetching, hashing anew.
Now icons are found, no more a ghost—
With every hop, accuracy’s toast!
🥕✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
runner/runner.go (1)

2339-2451: Excellent refactoring that addresses all PR objectives!

The implementation correctly:

  • Uses finalURL for base URL determination
  • Supports <base href> tags
  • Handles data URLs for base64-encoded favicons
  • Implements root fallback logic
  • Limits network attempts to prevent excessive requests
  • Includes debug logging for troubleshooting

Minor suggestions for consideration:

  1. Consider extracting the network attempt limit as a constant:
+const maxFaviconFetchAttempts = 2
+
 func (r *Runner) HandleFaviconHash(hp *httpx.HTTPX, req *retryablehttp.Request, currentResp []byte, finalURL string, defaultProbe bool) (string, string, string, []byte, string, error) {
  1. The debug log could include the base URL for better debugging:
-gologger.Debug().Msgf("favicon resolved url=%s raw_href=%s size=%d bytes", faviconURL, faviconPath, len(faviconData))
+gologger.Debug().Msgf("favicon resolved url=%s raw_href=%s base=%s size=%d bytes", faviconURL, faviconPath, baseNet.String(), len(faviconData))
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bbba454 and bd660bd.

📒 Files selected for processing (1)
  • runner/runner.go (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
runner/runner.go (2)
common/httpx/httpx.go (2)
  • HTTPX (33-43)
  • UnsafeOptions (346-348)
common/stringz/stringz.go (2)
  • IsBase64Icon (161-171)
  • DecodeBase64Icon (173-182)
🔇 Additional comments (4)
runner/runner.go (4)

16-16: LGTM!

The net/url import is correctly added and necessary for the new URL resolution logic.


2028-2028: Method signature correctly updated to support redirect-aware favicon resolution.

The addition of the finalURL parameter enables proper favicon resolution based on the final redirected URL, which addresses the core issue described in the PR objectives.

Also applies to: 2337-2337


2461-2498: Well-implemented favicon extraction with proper .ico prioritization!

The function correctly:

  • Extracts <base href> for proper URL resolution
  • Supports all common favicon rel attribute values
  • Uses sort.SliceStable to prioritize .ico files while preserving order
  • Handles case-insensitive matching and empty hrefs properly

The stable sort implementation is particularly elegant, ensuring .ico files are tried first as specified in the PR objectives.


2418-2434: Verify root fallback behavior for edge cases.

The root fallback logic only triggers when the href doesn't start with "/". This handles cases like href="portal/images/favicon.ico" but not href="/portal/images/favicon.ico".

While this might be intentional, please verify that this behavior is correct for all expected use cases.

Consider if you need to handle other edge cases where directory-relative resolution might fail, such as:

  • Hrefs starting with "./" or "../"
  • Protocol-relative URLs (//example.com/favicon.ico)

@ehsandeep ehsandeep changed the base branch from main to dev July 20, 2025 13:46
@dogancanbakir dogancanbakir requested review from ehsandeep and removed request for dwisiswant0 July 23, 2025 09:34
Copy link
Member

@ehsandeep ehsandeep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ ./httpx -favicon -u https://211.148.131.226:10443 -fr -silent
https://211.148.131.226:10443 [-631559155]

@ehsandeep ehsandeep merged commit 809b888 into projectdiscovery:dev Jul 24, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Icon retrieval error

4 participants