Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/fetchkit-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl PyFetchKitTool {
respect_proxy_env=false,
allowed_ports=None,
blocked_hosts=None,
same_host_redirects_only=false,
same_host_redirects_only=None,
hardened=false
))]
fn new(
Expand All @@ -203,7 +203,7 @@ impl PyFetchKitTool {
respect_proxy_env: bool,
allowed_ports: Option<Vec<u16>>,
blocked_hosts: Option<Vec<String>>,
same_host_redirects_only: bool,
same_host_redirects_only: Option<bool>,
hardened: bool,
) -> PyResult<Self> {
let mut builder = ToolBuilder::new()
Expand All @@ -217,7 +217,7 @@ impl PyFetchKitTool {

builder = builder
.block_private_ips(block_private_ips)
.same_host_redirects_only(same_host_redirects_only);
.same_host_redirects_only_if_set(same_host_redirects_only);

if let Some(ua) = user_agent {
builder = builder.user_agent(ua);
Expand Down Expand Up @@ -324,7 +324,7 @@ fn fetch(
as_text: Option<bool>,
) -> PyResult<PyFetchResponse> {
let tool = PyFetchKitTool::new(
true, true, None, None, None, None, true, false, None, None, false, false,
true, true, None, None, None, None, true, false, None, None, None, false,
)?;
tool.fetch(url, method, as_markdown, as_text)
}
Expand Down
28 changes: 28 additions & 0 deletions crates/fetchkit/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ impl ToolBuilder {
self
}

/// Restrict redirects to the original host only when the caller set a value.
pub fn same_host_redirects_only_if_set(mut self, enable: Option<bool>) -> Self {
if let Some(enable) = enable {
self.same_host_redirects_only = enable;
}
self
}

/// Control private/reserved IP range blocking (SSRF prevention)
///
/// Enabled by default. When enabled, FetchKit resolves hostnames to IP
Expand Down Expand Up @@ -1112,6 +1120,26 @@ mod tests {
assert!(tool.same_host_redirects_only);
}

#[test]
fn test_tool_builder_preserves_hardened_redirect_policy_when_override_is_unset() {
let tool = Tool::builder()
.hardened()
.same_host_redirects_only_if_set(None)
.build();

assert!(tool.same_host_redirects_only);
}

#[test]
fn test_tool_builder_allows_explicit_redirect_policy_override() {
let tool = Tool::builder()
.hardened()
.same_host_redirects_only_if_set(Some(false))
.build();

assert!(!tool.same_host_redirects_only);
}

#[test]
fn test_tool_metadata() {
let tool = Tool::default();
Expand Down
Loading