FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(fetch): reject binary responses and harden HTML sniffing by amitksingh1490 · Pull Request #2690 · tailcallhq/forgecode · GitHub

fix(fetch): reject binary responses and harden HTML sniffing - #2690

Merged
tusharmath merged 5 commits into
mainfrom
cp-9246df63b
Mar 26, 2026
Merged

fix(fetch): reject binary responses and harden HTML sniffing#2690
tusharmath merged 5 commits into
mainfrom
cp-9246df63b

Conversation

amitksingh1490 commented Mar 25, 2026
edited
Loading

Copy link
Copy Markdown
Contributor

Summary

Reject binary responses in fetch and harden HTML sniffing to avoid UTF-8 boundary panics.

Context

The fetch tool is designed for text-based web content. Binary endpoints (for example archive downloads) can produce unreadable output and confusing failures when treated as text. The initial HTML sniffing logic also sliced by byte index, which could panic on multibyte UTF-8 boundaries.

Changes

  • Added binary content-type detection before response body parsing
  • Returned a clear, actionable error for binary responses with a suggested curl -fLo command
  • Made HTML sniffing UTF-8-safe by selecting a valid character boundary before slicing
  • Added unit tests for text allowlist, binary detection, and case-insensitive content types
  • Updated the net fetch tool description to explicitly document binary-content behavior and fallback guidance

Key Implementation Details

Binary detection is implemented via is_binary_content_type, which allowlists text-oriented content types (text/*, JSON, XML, JavaScript, YAML, TOML, CSV, HTML, SVG, Markdown, and empty content-type) and treats all others as binary.

Use Cases

  • Fetching a .tar.gz URL now fails fast with clear guidance instead of trying to parse binary bytes as text
  • Responses containing multibyte UTF-8 characters near the sniffing boundary no longer risk panics
  • Regular text endpoints (HTML, JSON, XML) continue to work as expected

Testing

cargo test -p forge_services test_is_binary_content_type -- --nocapture

Links

  • Related issues: N/A

}

/// Returns true if the Content-Type header indicates binary (non-text) content.
fn is_binary_content_type(content_type: &str) -> bool {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

There is a util function we created to detect binary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

ok let me find it don't think it was based on contect-type header

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

there is no binary detection on http-headers, its on file system. I have moved this to util as we don't want to download and then fail we should fail fast.

amitksingh1490 changed the title fix(fetch): enhance binary content detection and error handling fix(fetch): reject binary responses and harden HTML sniffing Mar 25, 2026
tusharmath merged commit 02aef23 into main Mar 26, 2026
10 checks passed
tusharmath deleted the cp-9246df63b branch March 26, 2026 05:04
amitksingh1490 added the type: fix Iterations on existing features or infrastructure. label Mar 28, 2026
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

Labels

type: fix Iterations on existing features or infrastructure.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL