fix(installer): make GUI bridge approvals single-use and bounded
Consume the approval before destructive work and refuse a second start_install from a retry, a second frontend or a reconnect. Bound request lines and secret values; reject deeply nested JSON. Add regression tests for replay, oversized input and nesting.
This commit is contained in:
parent
5ae166e827
commit
d395fd3464
1 changed files with 134 additions and 9 deletions
|
|
@ -30,6 +30,11 @@ use serde_json::{json, Value};
|
||||||
pub const PROTOCOL_VERSION: u32 = 1;
|
pub const PROTOCOL_VERSION: u32 = 1;
|
||||||
|
|
||||||
const MAX_LOG_TAIL: usize = 200;
|
const MAX_LOG_TAIL: usize = 200;
|
||||||
|
/// Upper bound on one request line. The frontend sends small, typed requests;
|
||||||
|
/// anything larger is refused before parsing rather than buffered blindly.
|
||||||
|
const MAX_LINE_BYTES: usize = 256 * 1024;
|
||||||
|
/// Upper bound on a single secret value (passwords/passphrases/API keys).
|
||||||
|
const MAX_SECRET_BYTES: usize = 16 * 1024;
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Protocol types
|
// Protocol types
|
||||||
|
|
@ -161,6 +166,11 @@ pub struct Bridge {
|
||||||
api_key: Option<SecretString>,
|
api_key: Option<SecretString>,
|
||||||
approved_digest: Option<String>,
|
approved_digest: Option<String>,
|
||||||
phase: std::rc::Rc<std::cell::Cell<DiskPhase>>,
|
phase: std::rc::Rc<std::cell::Cell<DiskPhase>>,
|
||||||
|
/// One destructive transaction at a time. An approval is single-use: it is
|
||||||
|
/// consumed when the install starts so a replay, a second frontend or a
|
||||||
|
/// reconnected client cannot start a second overlapping installation.
|
||||||
|
installing: bool,
|
||||||
|
installed: bool,
|
||||||
quit: bool,
|
quit: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -175,6 +185,8 @@ impl Bridge {
|
||||||
api_key: None,
|
api_key: None,
|
||||||
approved_digest: None,
|
approved_digest: None,
|
||||||
phase: std::rc::Rc::new(std::cell::Cell::new(DiskPhase::BeforeStorage)),
|
phase: std::rc::Rc::new(std::cell::Cell::new(DiskPhase::BeforeStorage)),
|
||||||
|
installing: false,
|
||||||
|
installed: false,
|
||||||
quit: false,
|
quit: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -189,6 +201,8 @@ impl Bridge {
|
||||||
api_key: None,
|
api_key: None,
|
||||||
approved_digest: None,
|
approved_digest: None,
|
||||||
phase: std::rc::Rc::new(std::cell::Cell::new(DiskPhase::BeforeStorage)),
|
phase: std::rc::Rc::new(std::cell::Cell::new(DiskPhase::BeforeStorage)),
|
||||||
|
installing: false,
|
||||||
|
installed: false,
|
||||||
quit: false,
|
quit: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -204,6 +218,15 @@ impl Bridge {
|
||||||
if line.is_empty() {
|
if line.is_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
if line.len() > MAX_LINE_BYTES {
|
||||||
|
return write_json(
|
||||||
|
out,
|
||||||
|
&json!({
|
||||||
|
"ok": false,
|
||||||
|
"error": format!("request too large ({} bytes)", line.len()),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
let request: Request = match serde_json::from_str(line) {
|
let request: Request = match serde_json::from_str(line) {
|
||||||
Ok(request) => request,
|
Ok(request) => request,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
@ -399,6 +422,19 @@ impl Bridge {
|
||||||
.secrets
|
.secrets
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or_else(|| "set_secrets requires a `secrets` object".to_string())?;
|
.ok_or_else(|| "set_secrets requires a `secrets` object".to_string())?;
|
||||||
|
for value in [
|
||||||
|
&patch.luks,
|
||||||
|
&patch.user_password,
|
||||||
|
&patch.root_password,
|
||||||
|
&patch.ai_api_key,
|
||||||
|
]
|
||||||
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
|
{
|
||||||
|
if value.len() > MAX_SECRET_BYTES {
|
||||||
|
return Err("secret value is too large".into());
|
||||||
|
}
|
||||||
|
}
|
||||||
if let Some(v) = &patch.luks {
|
if let Some(v) = &patch.luks {
|
||||||
let s = SecretString::new(v.clone());
|
let s = SecretString::new(v.clone());
|
||||||
self.installer.exec.register_secret(&s);
|
self.installer.exec.register_secret(&s);
|
||||||
|
|
@ -497,6 +533,16 @@ impl Bridge {
|
||||||
request: &Request,
|
request: &Request,
|
||||||
out: &mut dyn Write,
|
out: &mut dyn Write,
|
||||||
) -> Result<Value, String> {
|
) -> Result<Value, String> {
|
||||||
|
if self.installing {
|
||||||
|
return Err("an installation is already in progress".into());
|
||||||
|
}
|
||||||
|
if self.installed {
|
||||||
|
return Err(
|
||||||
|
"this session has already installed Phoebus; start a new session to \
|
||||||
|
install again"
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
}
|
||||||
// The plan may not have changed since the user approved it.
|
// The plan may not have changed since the user approved it.
|
||||||
let current = self.plan_digest();
|
let current = self.plan_digest();
|
||||||
match (&self.approved_digest, &request.plan_digest) {
|
match (&self.approved_digest, &request.plan_digest) {
|
||||||
|
|
@ -553,6 +599,10 @@ impl Bridge {
|
||||||
phase: self.phase.clone(),
|
phase: self.phase.clone(),
|
||||||
};
|
};
|
||||||
self.phase.set(DiskPhase::BeforeStorage);
|
self.phase.set(DiskPhase::BeforeStorage);
|
||||||
|
// Consume the approval and mark the session busy before any destructive
|
||||||
|
// work: a replay or a second frontend can never reach storage_prepare.
|
||||||
|
self.approved_digest = None;
|
||||||
|
self.installing = true;
|
||||||
let outcome = self.installer.install_observed(
|
let outcome = self.installer.install_observed(
|
||||||
&auth,
|
&auth,
|
||||||
self.luks.as_ref(),
|
self.luks.as_ref(),
|
||||||
|
|
@ -560,17 +610,21 @@ impl Bridge {
|
||||||
self.root_password.as_ref(),
|
self.root_password.as_ref(),
|
||||||
&mut observer,
|
&mut observer,
|
||||||
);
|
);
|
||||||
|
self.installing = false;
|
||||||
|
|
||||||
match outcome {
|
match outcome {
|
||||||
Ok(()) => Ok(json!({
|
Ok(()) => {
|
||||||
"complete": true,
|
self.installed = true;
|
||||||
"dry_run": dry_run,
|
Ok(json!({
|
||||||
"log_path": self.installer.exec.log_path().display().to_string(),
|
"complete": true,
|
||||||
"next_actions": [
|
"dry_run": dry_run,
|
||||||
"Remove the installation medium.",
|
"log_path": self.installer.exec.log_path().display().to_string(),
|
||||||
"Restart the computer and boot Phoebus OS.",
|
"next_actions": [
|
||||||
],
|
"Remove the installation medium.",
|
||||||
})),
|
"Restart the computer and boot Phoebus OS.",
|
||||||
|
],
|
||||||
|
}))
|
||||||
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let message = self.installer.exec.redactor().redact(&e.to_string());
|
let message = self.installer.exec.redactor().redact(&e.to_string());
|
||||||
let (disk_changed, next_actions) = disk_change_report(self.phase.get());
|
let (disk_changed, next_actions) = disk_change_report(self.phase.get());
|
||||||
|
|
@ -1150,4 +1204,75 @@ mod tests {
|
||||||
assert_eq!(response(&lines)["data"]["bye"], json!(true));
|
assert_eq!(response(&lines)["data"]["bye"], json!(true));
|
||||||
assert!(bridge.should_quit());
|
assert!(bridge.should_quit());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn start_install_cannot_be_replayed_after_success() {
|
||||||
|
let mut bridge = test_bridge();
|
||||||
|
set_valid_config(&mut bridge);
|
||||||
|
let (digest, _) = approve(&mut bridge);
|
||||||
|
let first = call(
|
||||||
|
&mut bridge,
|
||||||
|
&json!({
|
||||||
|
"id": 20, "op": "start_install", "erase": "ERASE",
|
||||||
|
"final_confirm": true, "plan_digest": digest
|
||||||
|
})
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
assert_eq!(response(&first)["ok"], json!(true));
|
||||||
|
assert!(bridge.installed);
|
||||||
|
|
||||||
|
// A second frontend (or a retry) re-approves and tries again: refused.
|
||||||
|
let (digest2, _) = approve(&mut bridge);
|
||||||
|
let second = call(
|
||||||
|
&mut bridge,
|
||||||
|
&json!({
|
||||||
|
"id": 21, "op": "start_install", "erase": "ERASE",
|
||||||
|
"final_confirm": true, "plan_digest": digest2
|
||||||
|
})
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
assert_eq!(response(&second)["ok"], json!(false));
|
||||||
|
assert!(
|
||||||
|
response(&second)["error"]
|
||||||
|
.as_str()
|
||||||
|
.unwrap()
|
||||||
|
.contains("already installed"),
|
||||||
|
"unexpected error: {}",
|
||||||
|
response(&second)["error"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oversized_request_line_is_refused() {
|
||||||
|
let mut bridge = test_bridge();
|
||||||
|
let huge = format!(
|
||||||
|
r#"{{"id":1,"op":"handshake","pad":"{}"}}"#,
|
||||||
|
"x".repeat(MAX_LINE_BYTES)
|
||||||
|
);
|
||||||
|
let lines = call(&mut bridge, &huge);
|
||||||
|
let resp = response(&lines);
|
||||||
|
assert_eq!(resp["ok"], json!(false));
|
||||||
|
assert!(resp["error"].as_str().unwrap().contains("too large"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oversized_secret_is_refused() {
|
||||||
|
let mut bridge = test_bridge();
|
||||||
|
let secret = "x".repeat(MAX_SECRET_BYTES + 1);
|
||||||
|
let lines = call(
|
||||||
|
&mut bridge,
|
||||||
|
&json!({ "id": 22, "op": "set_secrets", "secrets": { "luks": secret } }).to_string(),
|
||||||
|
);
|
||||||
|
assert_eq!(response(&lines)["ok"], json!(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deeply_nested_json_is_rejected_without_panicking() {
|
||||||
|
let mut bridge = test_bridge();
|
||||||
|
let depth = 300;
|
||||||
|
let nested = format!("{}{}{}", "[".repeat(depth), "0", "]".repeat(depth));
|
||||||
|
let line = format!(r#"{{"id":23,"op":"handshake","config":{nested}}}"#);
|
||||||
|
let lines = call(&mut bridge, &line);
|
||||||
|
assert_eq!(response(&lines)["ok"], json!(false));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue