-
Notifications
You must be signed in to change notification settings - Fork 1
π‘οΈ Sentinel: [CRITICAL] Fix SSRF vulnerability in web_fetch tool #594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mudcube
wants to merge
1
commit into
main
Choose a base branch
from
sentinel-fix-ssrf-web-fetch-11692208846023727398
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| import re | ||
|
|
||
| with open("crates/mill-plugin-system/src/system_tools_plugin.rs", "r") as f: | ||
| content = f.read() | ||
|
|
||
| ssrf_code = """ | ||
| /// Helper to validate if an IP address is allowed (mitigates SSRF) | ||
| fn is_allowed_ip(ip: &std::net::IpAddr) -> bool { | ||
| use std::net::IpAddr; | ||
| match ip { | ||
| IpAddr::V4(v4) => is_allowed_ipv4(v4), | ||
| IpAddr::V6(v6) => { | ||
| if let Some(v4) = v6.to_ipv4_mapped() { | ||
| is_allowed_ipv4(&v4) | ||
| } else { | ||
| let segments = v6.segments(); | ||
| // Block loopback (::1), unspecified (::), multicast (ff00::/8) | ||
| if v6.is_loopback() || v6.is_unspecified() || v6.is_multicast() { | ||
| return false; | ||
| } | ||
| // Block Unique Local (fc00::/7) | ||
| if (segments[0] & 0xfe00) == 0xfc00 { | ||
| return false; | ||
| } | ||
| // Block Link-Local (fe80::/10) | ||
| if (segments[0] & 0xffc0) == 0xfe80 { | ||
| return false; | ||
| } | ||
| true | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn is_allowed_ipv4(v4: &std::net::Ipv4Addr) -> bool { | ||
| let octets = v4.octets(); | ||
| // Block 0.0.0.0/8, loopback (127.0.0.0/8), 10.0.0.0/8, link-local (169.254.0.0/16) | ||
| if octets[0] == 0 || octets[0] == 127 || octets[0] == 10 || (octets[0] == 169 && octets[1] == 254) { | ||
| return false; | ||
| } | ||
| // Block 172.16.0.0/12 | ||
| if octets[0] == 172 && (16..=31).contains(&octets[1]) { | ||
| return false; | ||
| } | ||
| // Block 192.168.0.0/16 | ||
| if octets[0] == 192 && octets[1] == 168 { | ||
| return false; | ||
| } | ||
| // Block broadcast (255.255.255.255) | ||
| if octets[0] == 255 && octets[1] == 255 && octets[2] == 255 && octets[3] == 255 { | ||
| return false; | ||
| } | ||
| // Block multicast (224.0.0.0/4) | ||
| if (224..=239).contains(&octets[0]) { | ||
| return false; | ||
| } | ||
| true | ||
| } | ||
|
|
||
| /// Handle web_fetch tool | ||
| fn handle_web_fetch(params: Value) -> PluginResult<Value> { | ||
| #[derive(Debug, Deserialize)] | ||
| #[serde(rename_all = "snake_case")] | ||
| struct WebFetchArgs { | ||
| url: String, | ||
| } | ||
|
|
||
| let args: WebFetchArgs = | ||
| serde_json::from_value(params).map_err(|e| PluginSystemError::SerializationError { | ||
| message: format!("Invalid web_fetch args: {}", e), | ||
| })?; | ||
|
|
||
| debug!(url = %args.url, "Fetching URL content"); | ||
|
|
||
| let mut current_url = url::Url::parse(&args.url).map_err(|e| PluginSystemError::IoError { | ||
| message: format!("Invalid URL: {}", e), | ||
| })?; | ||
|
|
||
| let mut redirect_count = 0; | ||
| let max_redirects = 10; | ||
| let mut response = None; | ||
|
|
||
| // Manual redirect tracking loop for IP pinning | ||
| while redirect_count < max_redirects { | ||
| let host = current_url.host_str().ok_or_else(|| PluginSystemError::IoError { | ||
| message: "URL missing host".to_string(), | ||
| })?; | ||
|
|
||
| let port = current_url.port_or_known_default().ok_or_else(|| PluginSystemError::IoError { | ||
| message: "Unknown port for URL scheme".to_string(), | ||
| })?; | ||
|
|
||
| // Format host for resolution, explicitly wrapping IPv6 in brackets if needed | ||
| let host_for_resolution = if host.contains(':') && !host.starts_with('[') { | ||
| format!("[{}]:{}", host, port) | ||
| } else { | ||
| format!("{}:{}", host, port) | ||
| }; | ||
|
|
||
| use std::net::ToSocketAddrs; | ||
| let mut addrs = host_for_resolution.to_socket_addrs().map_err(|e| PluginSystemError::IoError { | ||
| message: format!("Failed to resolve host {}: {}", host_for_resolution, e), | ||
| })?.peekable(); | ||
|
|
||
| if addrs.peek().is_none() { | ||
| return Err(PluginSystemError::IoError { | ||
| message: format!("Could not resolve host {}", host), | ||
| }); | ||
| } | ||
|
|
||
| let mut validated_ip = None; | ||
| for addr in addrs { | ||
| let ip = addr.ip(); | ||
| if !is_allowed_ip(&ip) { | ||
| return Err(PluginSystemError::IoError { | ||
| message: format!("Access to IP {} is forbidden", ip), | ||
| }); | ||
| } | ||
| // Use the first resolved IP for pinning | ||
| if validated_ip.is_none() { | ||
| validated_ip = Some(addr); | ||
| } | ||
| } | ||
|
|
||
| let pinned_addr = validated_ip.unwrap(); | ||
|
|
||
| // Use ClientBuilder::resolve for IP pinning to mitigate TOCTOU DNS Rebinding attacks | ||
| // while preserving SNI and disabling automatic redirects to manually check the next hop. | ||
| let client = reqwest::blocking::Client::builder() | ||
| .redirect(reqwest::redirect::Policy::none()) | ||
| .resolve(host, pinned_addr) | ||
| .build() | ||
| .map_err(|e| PluginSystemError::IoError { | ||
| message: format!("Failed to build client: {}", e), | ||
| })?; | ||
|
|
||
| let resp = client.get(current_url.clone()).send().map_err(|e| PluginSystemError::IoError { | ||
| message: format!("Failed to fetch URL: {}", e), | ||
| })?; | ||
|
|
||
| if resp.status().is_redirection() { | ||
| if let Some(loc) = resp.headers().get(reqwest::header::LOCATION) { | ||
| let loc_str = loc.to_str().map_err(|e| PluginSystemError::IoError { | ||
| message: format!("Invalid location header: {}", e), | ||
| })?; | ||
| current_url = current_url.join(loc_str).map_err(|e| PluginSystemError::IoError { | ||
| message: format!("Invalid redirect URL: {}", e), | ||
| })?; | ||
| redirect_count += 1; | ||
| continue; | ||
| } else { | ||
| return Err(PluginSystemError::IoError { | ||
| message: "Redirect missing location header".to_string(), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| response = Some(resp); | ||
| break; | ||
| } | ||
|
|
||
| let response = response.ok_or_else(|| PluginSystemError::IoError { | ||
| message: "Too many redirects".to_string(), | ||
| })?; | ||
|
|
||
| let html_content = response.text().map_err(|e| PluginSystemError::IoError { | ||
| message: format!("Failed to read response text: {}", e), | ||
| })?; | ||
| """ | ||
|
|
||
| search = """/// Handle web_fetch tool | ||
| fn handle_web_fetch(params: Value) -> PluginResult<Value> { | ||
| #[derive(Debug, Deserialize)] | ||
| #[serde(rename_all = "snake_case")] | ||
| struct WebFetchArgs { | ||
| url: String, | ||
| } | ||
|
|
||
| let args: WebFetchArgs = | ||
| serde_json::from_value(params).map_err(|e| PluginSystemError::SerializationError { | ||
| message: format!("Invalid web_fetch args: {}", e), | ||
| })?; | ||
|
|
||
| debug!(url = %args.url, "Fetching URL content"); | ||
|
|
||
| // Use reqwest to fetch the URL content | ||
| let response = reqwest::blocking::get(&args.url).map_err(|e| PluginSystemError::IoError { | ||
| message: format!("Failed to fetch URL: {}", e), | ||
| })?; | ||
|
|
||
| let html_content = response.text().map_err(|e| PluginSystemError::IoError { | ||
| message: format!("Failed to read response text: {}", e), | ||
| })?;""" | ||
|
|
||
| content = content.replace(search, ssrf_code) | ||
|
|
||
| with open("crates/mill-plugin-system/src/system_tools_plugin.rs", "w") as f: | ||
| f.write(content) |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fallback allows any IPv4 address that is not RFC1918/loopback/link-local/multicast/broadcast, so
web_fetchstill accepts non-global ranges such as100.64.0.0/10and198.18.0.0/15. In environments that place internal services on shared-address or benchmarking ranges, an attacker-controlled hostname resolving there will pass validation and the pinned request will still SSRF those internal endpoints; the filter should reject all non-global/special-use IPv4 ranges rather than returningtruehere.Useful? React with πΒ / π.