fix: harden the vault before release

Require a private passphrase and loopback-or-token admin auth. New vaults use Argon2id; existing PBKDF2 vaults still open, and new snapshot metadata is encrypted. Wormhole tokens survive restart, offset uploads seal into the vault, and client-certificate uploads are real. Reject the command provider and keep Postgres passwords off the process arguments. Keep the interruptible canvas motion.
This commit is contained in:
2026-11-10 09:53:06 +07:00
parent 60ba8fed88
commit 95778d54e4
46 changed files with 2311 additions and 415 deletions

View File

@@ -66,8 +66,22 @@ impl Db {
total INTEGER,
created_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS wormhole_tokens (
token TEXT PRIMARY KEY,
outpost_id TEXT NOT NULL,
code TEXT NOT NULL,
state TEXT NOT NULL,
expires_at TEXT NOT NULL,
outpost_name TEXT
);
CREATE TABLE IF NOT EXISTS upload_ranges (
session_id TEXT NOT NULL,
start_off INTEGER NOT NULL,
end_off INTEGER NOT NULL
);
"#,
)?;
ensure_column(&conn, "upload_sessions", "sealed", "INTEGER NOT NULL DEFAULT 0")?;
Ok(())
}
@@ -137,9 +151,156 @@ impl Db {
) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"INSERT INTO upload_sessions (id, orbit_id, offset, total, created_at) VALUES (?1, ?2, 0, ?3, datetime('now'))",
"INSERT INTO upload_sessions (id, orbit_id, offset, total, sealed, created_at) VALUES (?1, ?2, 0, ?3, 0, datetime('now'))",
params![id, orbit_id, total],
)?;
Ok(())
}
pub fn insert_wormhole_token(
&self,
token: &str,
outpost_id: &str,
code: &str,
expires_at: &str,
) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"INSERT INTO wormhole_tokens (token, outpost_id, code, state, expires_at) VALUES (?1, ?2, ?3, 'code_shown', ?4)",
params![token, outpost_id, code, expires_at],
)?;
Ok(())
}
pub fn claim_wormhole_code(
&self,
code: &str,
outpost_name: &str,
now: &str,
) -> Result<Option<(String, String)>> {
let conn = self.conn.lock().unwrap();
let changed = conn.execute(
"UPDATE wormhole_tokens SET state = 'active', outpost_name = ?2 WHERE code = ?1 AND state = 'code_shown' AND expires_at > ?3",
params![code, outpost_name, now],
)?;
if changed != 1 {
return Ok(None);
}
let mut stmt = conn.prepare(
"SELECT token, outpost_id FROM wormhole_tokens WHERE code = ?1 AND state = 'active'",
)?;
let mut rows = stmt.query(params![code])?;
let Some(row) = rows.next()? else {
return Ok(None);
};
Ok(Some((row.get(0)?, row.get(1)?)))
}
pub fn upload_token_active(&self, token: &str) -> Result<bool> {
let conn = self.conn.lock().unwrap();
let mut stmt = conn.prepare(
"SELECT 1 FROM wormhole_tokens WHERE token = ?1 AND state = 'active' LIMIT 1",
)?;
Ok(stmt.query_row(params![token], |_| Ok(())).is_ok())
}
pub fn upload_session(&self, id: &str) -> Result<Option<UploadSession>> {
let conn = self.conn.lock().unwrap();
let mut stmt = conn.prepare(
"SELECT orbit_id, total, sealed FROM upload_sessions WHERE id = ?1",
)?;
let mut rows = stmt.query(params![id])?;
let Some(row) = rows.next()? else {
return Ok(None);
};
let orbit_id: String = row.get(0)?;
let total: Option<i64> = row.get(1)?;
let sealed: i64 = row.get(2)?;
Ok(Some(UploadSession {
orbit_id: Uuid::parse_str(&orbit_id).map_err(|e| anyhow::anyhow!(e))?,
total: total.unwrap_or(0).max(0) as u64,
sealed: sealed != 0,
}))
}
/// Records a received range. Returns whether this call won the right to seal.
pub fn record_range(&self, session_id: &str, start: u64, end: u64, total: u64) -> Result<bool> {
let conn = self.conn.lock().unwrap();
conn.execute_batch("BEGIN IMMEDIATE")?;
let outcome = (|| -> Result<bool> {
let sealed: i64 = conn.query_row(
"SELECT sealed FROM upload_sessions WHERE id = ?1",
params![session_id],
|row| row.get(0),
)?;
if sealed != 0 {
return Ok(false);
}
conn.execute(
"INSERT INTO upload_ranges (session_id, start_off, end_off) VALUES (?1, ?2, ?3)",
params![session_id, start as i64, end as i64],
)?;
let mut stmt =
conn.prepare("SELECT start_off, end_off FROM upload_ranges WHERE session_id = ?1")?;
let ranges = stmt
.query_map(params![session_id], |row| {
Ok(crate::upload::ByteRange {
start: row.get::<_, i64>(0)? as u64,
end: row.get::<_, i64>(1)? as u64,
})
})?
.collect::<Result<Vec<_>, _>>()?;
let covered = crate::upload::contiguous_from_zero(&ranges);
conn.execute(
"UPDATE upload_sessions SET offset = ?2 WHERE id = ?1",
params![session_id, covered as i64],
)?;
if covered >= total {
conn.execute(
"UPDATE upload_sessions SET sealed = 1 WHERE id = ?1 AND sealed = 0",
params![session_id],
)?;
return Ok(true);
}
Ok(false)
})();
match outcome {
Ok(value) => {
conn.execute_batch("COMMIT")?;
Ok(value)
}
Err(error) => {
let _ = conn.execute_batch("ROLLBACK");
Err(error)
}
}
}
pub fn mark_upload_unsealed(&self, session_id: &str) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"UPDATE upload_sessions SET sealed = 0 WHERE id = ?1",
params![session_id],
)?;
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct UploadSession {
pub orbit_id: Uuid,
pub total: u64,
pub sealed: bool,
}
fn ensure_column(conn: &Connection, table: &str, column: &str, decl: &str) -> Result<()> {
let mut stmt = conn.prepare(&format!("PRAGMA table_info({table})"))?;
let names = stmt.query_map([], |row| row.get::<_, String>(1))?;
for name in names {
if name? == column {
return Ok(());
}
}
conn.execute(&format!("ALTER TABLE {table} ADD COLUMN {column} {decl}"), [])?;
Ok(())
}