[PR #2374] [MERGED] Enforce the SSRF guard in http_binary #5561

Closed
opened 2026-10-22 04:17:23 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2374
Author: @Romuromylus
Created: 6/5/2026
Status: ✅ Merged
Merged: 6/6/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/ssrf-http-binary-guard


📝 Commits (3)

  • 5c091ed Enforce the SSRF guard in http_binary
  • f5ac1dd Re-validate redirects and pin DNS in http_binary against SSRF
  • 6a9faf3 Add tests for the http_binary DNS resolve/pin guard

📊 Changes

1 file changed (+187 additions, -7 deletions)

View changed files

📝 src-tauri/src/commands/storage/http.rs (+187 -7)

📄 Description

Linked issue

Closes #2300

Why this change

http_binary resolves content-supplied URLs (avatar/background/image references from imported character cards and lorebooks via load_url_binary), so it is an SSRF sink. It passed allow_local=true to is_allowed_outbound_url, disabling the internal-host guard entirely — and even with the guard enabled, the original one-line flip left two bypasses the issue calls out: redirect-following (a public host can 302 into 169.254.169.254) and DNS rebinding (a public-looking host resolving to a reserved IP).

What changed

  • Screen the URL with is_allowed_outbound_url(url, false) so local/reserved hosts (IMDS, localhost, private ranges) are blocked.
  • Follow redirects manually with redirect::Policy::none (max 5) and re-validate every Location through the same SSRF guard, instead of blindly chasing reqwest's default redirect policy into an internal address.
  • Resolve the host and reject local/reserved IPs, pinning the connection via resolve_to_addrs so DNS rebinding can't slip a reserved address past the host check. IP-literal hosts are already screened by the URL guard and skip pinning.
  • Mirrors the existing custom_tools.rs webhook hardening (redirected_custom_tool_webhook_url / custom_tool_webhook_resolved_addresses).

Refactor impact

Primary owner: Rust storage (src-tauri/src/commands/storage/http.rs)

Impact areas reviewed:

  • load_url_binary_for_state resolves asset://localhost via load_local_asset_binary before http_binary, so managed local-asset loading is unaffected.
  • bot_browser.rs fetches public card hosts; CDN redirects to public hosts are still followed (now re-validated and pinned).

Boundary notes:

  • Reuses marinara_security::{is_allowed_outbound_url, is_local_or_reserved_ip}; no new layer boundary crossed.

Pressure points touched:

  • none

Validation

  • Matching validation command passes locally
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • cargo clippy --workspace --all-targets -- -D warnings — clean
  • cargo test --workspace — green, including:
    • http_binary_blocks_local_and_reserved_hosts (IMDS / localhost / 127.0.0.1 / 10.x / 172.16.x / 192.168.x / [::1] rejected before any network call),
    • redirect_revalidation_blocks_reserved_locations (a 302 to a reserved host is rejected),
    • redirect_revalidation_allows_public_relative_locations (a public relative redirect still resolves).

Feature Discoverability

  • N/A because this PR is a security bugfix and does not add a new thing users need to find.

Reason:

  • Hardening of an existing fetch path.

Docs and release impact

  • No docs changes needed

Summary by CodeRabbit

  • Bug Fixes
    • Improved binary file fetching with enhanced URL validation and stricter host verification.
    • Added limits on redirect chains to prevent excessive network hops during downloads.
    • Strengthened network destination validation for more reliable and stable file operations.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2374 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/5/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/ssrf-http-binary-guard` --- ### 📝 Commits (3) - [`5c091ed`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5c091ed1821ed90eb7aa25abc7fa941d469d5643) Enforce the SSRF guard in http_binary - [`f5ac1dd`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f5ac1dd110f0f81a6667465fe1d498d8e98ad373) Re-validate redirects and pin DNS in http_binary against SSRF - [`6a9faf3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6a9faf3c6e5c192e4843e5d439992621cb2c2838) Add tests for the http_binary DNS resolve/pin guard ### 📊 Changes **1 file changed** (+187 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `src-tauri/src/commands/storage/http.rs` (+187 -7) </details> ### 📄 Description ## Linked issue Closes #2300 ## Why this change `http_binary` resolves content-supplied URLs (avatar/background/image references from imported character cards and lorebooks via `load_url_binary`), so it is an SSRF sink. It passed `allow_local=true` to `is_allowed_outbound_url`, disabling the internal-host guard entirely — and even with the guard enabled, the original one-line flip left two bypasses the issue calls out: redirect-following (a public host can `302` into `169.254.169.254`) and DNS rebinding (a public-looking host resolving to a reserved IP). ## What changed - Screen the URL with `is_allowed_outbound_url(url, false)` so local/reserved hosts (IMDS, localhost, private ranges) are blocked. - Follow redirects manually with `redirect::Policy::none` (max 5) and re-validate every `Location` through the same SSRF guard, instead of blindly chasing reqwest's default redirect policy into an internal address. - Resolve the host and reject local/reserved IPs, pinning the connection via `resolve_to_addrs` so DNS rebinding can't slip a reserved address past the host check. IP-literal hosts are already screened by the URL guard and skip pinning. - Mirrors the existing `custom_tools.rs` webhook hardening (`redirected_custom_tool_webhook_url` / `custom_tool_webhook_resolved_addresses`). ## Refactor impact Primary owner: Rust storage (`src-tauri/src/commands/storage/http.rs`) Impact areas reviewed: - `load_url_binary_for_state` resolves `asset://localhost` via `load_local_asset_binary` **before** `http_binary`, so managed local-asset loading is unaffected. - `bot_browser.rs` fetches public card hosts; CDN redirects to public hosts are still followed (now re-validated and pinned). Boundary notes: - Reuses `marinara_security::{is_allowed_outbound_url, is_local_or_reserved_ip}`; no new layer boundary crossed. Pressure points touched: - none ## Validation - [ ] Matching validation command passes locally - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `cargo clippy --workspace --all-targets -- -D warnings` — clean - `cargo test --workspace` — green, including: - `http_binary_blocks_local_and_reserved_hosts` (IMDS / localhost / 127.0.0.1 / 10.x / 172.16.x / 192.168.x / `[::1]` rejected before any network call), - `redirect_revalidation_blocks_reserved_locations` (a `302` to a reserved host is rejected), - `redirect_revalidation_allows_public_relative_locations` (a public relative redirect still resolves). ## Feature Discoverability - [x] N/A because this PR is a security bugfix and does not add a new thing users need to find. Reason: - Hardening of an existing fetch path. ## Docs and release impact - [x] No docs changes needed <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved binary file fetching with enhanced URL validation and stricter host verification. * Added limits on redirect chains to prevent excessive network hops during downloads. * Strengthened network destination validation for more reliable and stable file operations. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:17:23 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5561
No description provided.