Skip to content

Commit 9c8d6c7

Browse files
authored
fix(sandbox): eliminate Box::leak memory leak in rewrite_forward_request (#715)
Remove Box::leak usage that permanently leaked one String allocation per forward proxy request. Write the rewritten request line directly to the output buffer during iteration instead of mutating a Vec<&str> element. Closes #709 Co-authored-by: John Myers <johntmyers@users.noreply.github.com>
1 parent a2f9da5 commit 9c8d6c7

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

crates/openshell-sandbox/src/proxy.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,16 +1624,7 @@ fn rewrite_forward_request(
16241624
.map_or(used, |p| p + 4);
16251625

16261626
let header_str = String::from_utf8_lossy(&raw[..header_end]);
1627-
let mut lines = header_str.split("\r\n").collect::<Vec<_>>();
1628-
1629-
// Rewrite request line: METHOD absolute-uri HTTP/1.1 → METHOD path HTTP/1.1
1630-
if let Some(first_line) = lines.first_mut() {
1631-
let parts: Vec<&str> = first_line.splitn(3, ' ').collect();
1632-
if parts.len() == 3 {
1633-
let new_line = format!("{} {} {}", parts[0], path, parts[2]);
1634-
*first_line = Box::leak(new_line.into_boxed_str()); // safe: short-lived
1635-
}
1636-
}
1627+
let lines = header_str.split("\r\n").collect::<Vec<_>>();
16371628

16381629
// Rebuild headers, stripping hop-by-hop and adding proxy headers
16391630
let mut output = Vec::with_capacity(header_end + 128);
@@ -1642,8 +1633,17 @@ fn rewrite_forward_request(
16421633

16431634
for (i, line) in lines.iter().enumerate() {
16441635
if i == 0 {
1645-
// Request line — already rewritten
1646-
output.extend_from_slice(line.as_bytes());
1636+
// Rewrite request line: METHOD absolute-uri HTTP/1.1 → METHOD path HTTP/1.1
1637+
let parts: Vec<&str> = line.splitn(3, ' ').collect();
1638+
if parts.len() == 3 {
1639+
output.extend_from_slice(parts[0].as_bytes());
1640+
output.push(b' ');
1641+
output.extend_from_slice(path.as_bytes());
1642+
output.push(b' ');
1643+
output.extend_from_slice(parts[2].as_bytes());
1644+
} else {
1645+
output.extend_from_slice(line.as_bytes());
1646+
}
16471647
output.extend_from_slice(b"\r\n");
16481648
continue;
16491649
}

0 commit comments

Comments
 (0)