orbital-navigation: close cross-user data leaks
This commit is contained in:
1 parent
4f70df55dd
commit
f3b2cb9c22
2 files changed
+88
-15
No files matched your search
@@ -7,7 +7,7 @@ use std::net::{TcpListener, TcpStream, ToSocketAddrs};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
type Answer = Result<Value, String>;
|
||||
type Headers = HashMap<String, String>;
|
||||
@@ -130,7 +130,17 @@ fn canonical(s: &str) -> String {
|
||||
}
|
||||
|
||||
fn cache_key(f: &Flight) -> String {
|
||||
format!("{}\0{}\0{}", f.geometry, f.map_version, f.options)
|
||||
format!(
|
||||
"{}\0{}\0{}\0{}",
|
||||
f.owner, f.geometry, f.map_version, f.options
|
||||
)
|
||||
}
|
||||
|
||||
fn unix_time() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs()
|
||||
}
|
||||
|
||||
const STATE_FILE: &str = "state.json";
|
||||
@@ -208,7 +218,22 @@ impl Server {
|
||||
|
||||
fs::create_dir_all(root.join("work"))?;
|
||||
|
||||
let state = load_state(&root)?;
|
||||
let mut state = load_state(&root)?;
|
||||
let old_cache = std::mem::take(&mut state.geometry_cache);
|
||||
for flight_id in old_cache.values() {
|
||||
if let Some(flight) = state.flights.get(flight_id) {
|
||||
state
|
||||
.geometry_cache
|
||||
.entry(cache_key(flight))
|
||||
.or_insert_with(|| flight_id.clone());
|
||||
}
|
||||
}
|
||||
for (flight_id, flight) in &state.flights {
|
||||
state
|
||||
.geometry_cache
|
||||
.entry(cache_key(flight))
|
||||
.or_insert_with(|| flight_id.clone());
|
||||
}
|
||||
cleanup_storage(&root, &state)?;
|
||||
|
||||
Ok(Self {
|
||||
@@ -287,9 +312,8 @@ impl Server {
|
||||
if s.sources.contains_key(&source) {
|
||||
return Err("source exists".into());
|
||||
}
|
||||
if !s.ships.contains_key(&canonical(&source)) {
|
||||
return Err("unknown ship".into());
|
||||
}
|
||||
let ship = canonical(&source);
|
||||
check_owner(s.ships.get(&ship).ok_or("unknown ship")?, &user)?;
|
||||
s.sources.insert(source, user);
|
||||
Ok(json!({"ok":true}))
|
||||
}
|
||||
@@ -336,9 +360,7 @@ impl Server {
|
||||
let ship = owned(&v, "ship_id")?;
|
||||
let mut s = self.state.lock().unwrap();
|
||||
|
||||
if !s.ships.contains_key(&ship) {
|
||||
return Err("unknown ship".into());
|
||||
}
|
||||
check_owner(s.ships.get(&ship).ok_or("unknown ship")?, &user)?;
|
||||
let p = s.profiles.get_mut(id).ok_or("unknown profile")?;
|
||||
check_owner(&p.owner, &user)?;
|
||||
p.ship = ship;
|
||||
@@ -361,6 +383,7 @@ impl Server {
|
||||
.ok_or("unknown profile")?
|
||||
.ship
|
||||
.clone();
|
||||
check_owner(s.ships.get(&ship).ok_or("unknown ship")?, &user)?;
|
||||
|
||||
let result = s.messages.get(&ship).cloned().unwrap_or_default();
|
||||
|
||||
@@ -482,7 +505,13 @@ impl Server {
|
||||
if p.subject != "public" && p.subject != user {
|
||||
return Err("forbidden".into());
|
||||
}
|
||||
if !p.rights.iter().any(|x| x == "delegate") || expires > p.expires {
|
||||
let now = unix_time();
|
||||
if !p.rights.iter().any(|x| x == "delegate")
|
||||
|| p.expires < now
|
||||
|| expires < now
|
||||
|| expires > p.expires
|
||||
|| !rights.iter().all(|right| p.rights.contains(right))
|
||||
{
|
||||
return Err("invalid delegation".into());
|
||||
}
|
||||
let flight = p.flight.clone();
|
||||
@@ -514,6 +543,7 @@ impl Server {
|
||||
|
||||
if !(g.flight == flight
|
||||
&& g.subject == user
|
||||
&& g.expires >= unix_time()
|
||||
&& g.rights.iter().any(|x| x == "manifest.read"))
|
||||
{
|
||||
return Err("forbidden".into());
|
||||
@@ -656,15 +686,14 @@ impl Server {
|
||||
fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
|
||||
|
||||
let report = (|| {
|
||||
validate_safe_tar(&bytes)?;
|
||||
unpack_tar(&bytes, &dir)?;
|
||||
|
||||
let target = fs::canonicalize(dir.join(attachment))
|
||||
.map_err(|e| format!("resolve report attachment: {e}"))?;
|
||||
let work_root = fs::canonicalize(&dir)
|
||||
.map_err(|e| format!("resolve report workspace: {e}"))?;
|
||||
let navigation_root = fs::canonicalize(self.root.join("navigation"))
|
||||
.map_err(|e| format!("resolve navigation workspace: {e}"))?;
|
||||
if !target.starts_with(&work_root) && !target.starts_with(&navigation_root) {
|
||||
if !target.starts_with(&work_root) {
|
||||
return Err("report attachment resolves outside allowed storage".into());
|
||||
}
|
||||
|
||||
@@ -694,6 +723,7 @@ impl Server {
|
||||
}
|
||||
|
||||
fn handle(&self, mut stream: TcpStream) -> std::io::Result<()> {
|
||||
let peer = stream.peer_addr()?;
|
||||
stream.set_read_timeout(Some(Duration::from_secs(5)))?;
|
||||
let (method, path, headers, body) = read_request(&mut stream)?;
|
||||
let result: Result<(u16, Vec<u8>), String> = if method == "POST" && path == "/rpc" {
|
||||
@@ -727,6 +757,7 @@ impl Server {
|
||||
let p = s.packages.get_mut(id).ok_or("unknown package")?;
|
||||
check_owner(&p.owner, &u)?;
|
||||
p.bytes = body;
|
||||
p.approved = false;
|
||||
Ok(())
|
||||
})
|
||||
};
|
||||
@@ -747,7 +778,9 @@ impl Server {
|
||||
} else if method == "GET" && path.starts_with("/internal/summaries/") {
|
||||
let id = path.trim_start_matches("/internal/summaries/");
|
||||
|
||||
if headers.get("x-importer-key").map(String::as_str) != Some(&self.importer_key) {
|
||||
if !peer.ip().is_loopback()
|
||||
|| headers.get("x-importer-key").map(String::as_str) != Some(&self.importer_key)
|
||||
{
|
||||
Ok((403, b"forbidden".to_vec()))
|
||||
} else {
|
||||
let s = self.state.lock().unwrap();
|
||||
@@ -786,6 +819,9 @@ impl Server {
|
||||
.map_err(|e| format!("resolve {host}:{port}: {e}"))?
|
||||
.next()
|
||||
.ok_or("no address")?;
|
||||
if addr.ip().is_loopback() {
|
||||
return Err("internal source forbidden".into());
|
||||
}
|
||||
|
||||
let mut stream = TcpStream::connect_timeout(&addr, Duration::from_secs(2))
|
||||
.map_err(|e| format!("connect {addr}: {e}"))?;
|
||||
@@ -1047,4 +1083,4 @@ fn read_request(stream: &mut TcpStream) -> std::io::Result<HttpRequest> {
|
||||
}
|
||||
|
||||
Ok((method, path, headers, data[start..start + len].to_vec()))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user