Skip to content

Commit b8f9011

Browse files
committed
Fix 'LocalRequest::clone()' soundness issue.
The existing implementation of 'LocalRequest::clone()' mistakenly copied the internal 'Request' pointer from the existing 'LocalRequest' to the cloned 'LocalRequest'. This resulted in an aliased '*mut Request' pointer, a clear soundness issue. The fix in this commit is to clone the internal 'Request', replacing the internal pointer with the newly cloned 'Request' when producing the cloned 'LocalRequest'. A fix that removes all 'unsafe' code should be explored. Fixes #1312.
1 parent ca4d157 commit b8f9011

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

core/lib/src/local/request.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,19 +476,35 @@ impl fmt::Debug for LocalResponse<'_> {
476476

477477
impl<'c> Clone for LocalRequest<'c> {
478478
fn clone(&self) -> LocalRequest<'c> {
479+
// Don't alias the existing `Request`. See #1312.
480+
let mut request = Rc::new(self.inner().clone());
481+
let ptr = Rc::get_mut(&mut request).unwrap() as *mut Request<'_>;
482+
479483
LocalRequest {
484+
ptr, request,
480485
client: self.client,
481-
ptr: self.ptr,
482-
request: self.request.clone(),
483486
data: self.data.clone(),
484487
uri: self.uri.clone()
485488
}
486489
}
487490
}
488491

489-
// #[cfg(test)]
492+
#[cfg(test)]
490493
mod tests {
491-
// Someday...
494+
use crate::Request;
495+
use crate::local::Client;
496+
497+
#[test]
498+
fn clone_unique_ptr() {
499+
let client = Client::new(crate::ignite()).unwrap();
500+
let r1 = client.get("/");
501+
let r2 = r1.clone();
502+
503+
assert_ne!(
504+
r1.inner() as *const Request<'_>,
505+
r2.inner() as *const Request<'_>
506+
);
507+
}
492508

493509
// #[test]
494510
// #[compile_fail]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use rocket::http::Header;
2+
use rocket::local::Client;
3+
4+
#[test]
5+
fn test_local_request_clone_soundness() {
6+
let client = Client::new(rocket::ignite()).unwrap();
7+
8+
// creates two LocalRequest instances that shouldn't share the same req
9+
let r1 = client.get("/").header(Header::new("key", "val1"));
10+
let mut r2 = r1.clone();
11+
12+
// save the iterator, which internally holds a slice
13+
let mut iter = r1.inner().headers().get("key");
14+
15+
// insert headers to force header map reallocation.
16+
for i in 0..100 {
17+
r2.add_header(Header::new(i.to_string(), i.to_string()));
18+
}
19+
20+
// Replace the original key/val.
21+
r2.add_header(Header::new("key", "val2"));
22+
23+
// Heap massage: so we've got crud to print.
24+
let _: Vec<usize> = vec![0, 0xcafebabe, 31337, 0];
25+
26+
// Ensure we're good.
27+
let s = iter.next().unwrap();
28+
println!("{}", s);
29+
30+
// And that we've got the right data.
31+
assert_eq!(r1.inner().headers().get("key").collect::<Vec<_>>(), vec!["val1"]);
32+
assert_eq!(r2.inner().headers().get("key").collect::<Vec<_>>(), vec!["val1", "val2"]);
33+
}

0 commit comments

Comments
 (0)