feat(phase15): release and security hardening, DPAPI key store, probe & protocol hardening, secret redaction, audit and readiness report

This commit is contained in:
Дмитрий committed 2026-10-09 21:59:44 +05:00
1 parent b69fa6b867
commit 51e0f136d6
207 files changed
+38235 -7770

No files matched your search

+113 -26
View File
@@ -11,7 +11,10 @@ use coa_control_proto::app;
use uuid::Uuid;
use super::service::{AccountRow, CharInfo, ClaimExport, PreflightVerdict, Presence, RealmBackend, RealmInfo, RemoteImportOutcome, RemoteImportParams};
use super::service::{
AccountRow, CharInfo, ClaimExport, PreflightVerdict, Presence, RealmBackend, RealmInfo,
RemoteImportOutcome, RemoteImportParams,
};
use crate::portable::realm;
use crate::portable::service::access::RealmAccess;
use crate::portable::service::runtime::PortableRuntime;
@@ -46,20 +49,36 @@ fn is_internal(upper: &str) -> bool {
}
impl LocalBackend {
pub fn new(portable: Arc<PortableRuntime>, settings_dir: impl Into<PathBuf>, descriptors: impl Into<PathBuf>, installs: Installs) -> Self {
Self { portable, settings_dir: settings_dir.into(), descriptors: descriptors.into(), installs }
pub fn new(
portable: Arc<PortableRuntime>,
settings_dir: impl Into<PathBuf>,
descriptors: impl Into<PathBuf>,
installs: Installs,
) -> Self {
Self {
portable,
settings_dir: settings_dir.into(),
descriptors: descriptors.into(),
installs,
}
}
fn access(&self, local_id: &str) -> Result<RealmAccess> {
find_access(&self.descriptors, &(self.installs)(), local_id).ok_or_else(|| Error::Invalid("That realm is not known to this Manager.".into()))
find_access(&self.descriptors, &(self.installs)(), local_id)
.ok_or_else(|| Error::Invalid("That realm is not known to this Manager.".into()))
}
fn account(&self, local_id: &str, filter: &str) -> Result<Option<AccountRow>> {
let db = self.access(local_id)?.db()?;
let out = db.query(&format!("SELECT id, username FROM acore_auth.account WHERE {filter} LIMIT 1;"))?;
let out = db.query(&format!(
"SELECT id, username FROM acore_auth.account WHERE {filter} LIMIT 1;"
))?;
Ok(out.lines().find(|l| !l.trim().is_empty()).and_then(|l| {
let mut c = l.split('\t');
Some(AccountRow { id: c.next()?.trim().parse().ok()?, username: c.next()?.trim().to_string() })
Some(AccountRow {
id: c.next()?.trim().parse().ok()?,
username: c.next()?.trim().to_string(),
})
}))
}
}
@@ -67,7 +86,10 @@ impl LocalBackend {
impl RealmBackend for LocalBackend {
fn info(&self, local_id: &str) -> Result<RealmInfo> {
let cfg = settings::load(&self.settings_dir)?.realms.remove(local_id);
Ok(RealmInfo { automatic: cfg.as_ref().is_none_or(|c| !c.existing_only), route: cfg.and_then(|c| c.route) })
Ok(RealmInfo {
automatic: cfg.as_ref().is_none_or(|c| !c.existing_only),
route: cfg.and_then(|c| c.route),
})
}
fn account_by_name(&self, local_id: &str, username: &str) -> Result<Option<AccountRow>> {
@@ -92,27 +114,50 @@ impl RealmBackend for LocalBackend {
if is_internal(&n) {
return Err(Error::Invalid("That name is reserved.".into()));
}
self.access(local_id)?.ra()?.set_account_password(&n, password)
self.access(local_id)?
.ra()?
.set_account_password(&n, password)
}
fn check_login(&self, local_id: &str, username: &str, password: &str) -> Result<Option<AccountRow>> {
fn check_login(
&self,
local_id: &str,
username: &str,
password: &str,
) -> Result<Option<AccountRow>> {
let n = name(username)?;
if is_internal(&n) || !app::valid_password_for_link(password) {
return Ok(None);
}
let db = self.access(local_id)?.db()?;
let out = db.query(&format!("SELECT id, username, HEX(salt), HEX(verifier) FROM acore_auth.account WHERE username = '{n}' LIMIT 1;"))?;
let Some(line) = out.lines().find(|l| !l.trim().is_empty()) else { return Ok(None) };
let Some(line) = out.lines().find(|l| !l.trim().is_empty()) else {
return Ok(None);
};
let c: Vec<&str> = line.split('\t').map(str::trim).collect();
if c.len() < 4 {
return Ok(None);
}
let (Ok(salt), Ok(stored)) = (hex::decode(c[2]), hex::decode(c[3])) else { return Ok(None) };
let Ok(salt): std::result::Result<[u8; 32], _> = salt.try_into() else { return Ok(None) };
let (Ok(salt), Ok(stored)) = (hex::decode(c[2]), hex::decode(c[3])) else {
return Ok(None);
};
let Ok(salt): std::result::Result<[u8; 32], _> = salt.try_into() else {
return Ok(None);
};
let computed = crate::srp6::verifier(&n, password, &salt);
// both are fixed-size secrets of equal length; compare without an early exit
let equal = stored.len() == 32 && computed.iter().zip(&stored).fold(0u8, |acc, (a, b)| acc | (a ^ b)) == 0;
Ok(equal.then(|| AccountRow { id: c[0].parse().unwrap_or(0), username: c[1].to_string() }).filter(|r| r.id != 0))
let equal = stored.len() == 32
&& computed
.iter()
.zip(&stored)
.fold(0u8, |acc, (a, b)| acc | (a ^ b))
== 0;
Ok(equal
.then(|| AccountRow {
id: c[0].parse().unwrap_or(0),
username: c[1].to_string(),
})
.filter(|r| r.id != 0))
}
fn presence(&self, local_id: &str, account_id: u32) -> Result<Presence> {
@@ -120,40 +165,82 @@ impl RealmBackend for LocalBackend {
let out = db.query(&format!("SELECT (SELECT online FROM acore_auth.account WHERE id = {account_id}), (SELECT COUNT(*) FROM acore_characters.characters WHERE account = {account_id} AND online = 1);"))?;
let line = out.lines().find(|l| !l.trim().is_empty()).unwrap_or("");
let mut c = line.split('\t').map(str::trim);
let account_online = c.next().is_some_and(|v| !v.is_empty() && v != "NULL" && v != "0");
let account_online = c
.next()
.is_some_and(|v| !v.is_empty() && v != "NULL" && v != "0");
let online_characters = c.next().and_then(|v| v.parse().ok()).unwrap_or(0);
Ok(Presence { account_online, online_characters })
Ok(Presence {
account_online,
online_characters,
})
}
fn characters(&self, local_id: &str, account_id: u32) -> Result<Vec<CharInfo>> {
let db = self.access(local_id)?.db()?;
let list = realm::inspect_characters(&db).map_err(|e| unavailable("reading characters", e))?;
let list =
realm::inspect_characters(&db).map_err(|e| unavailable("reading characters", e))?;
Ok(list
.into_iter()
.filter(|c| c.account == account_id)
.map(|c| CharInfo { guid: c.local_guid, name: c.name.clone(), class: c.class, race: c.race, level: c.level, eligible: c.eligible(), reasons: c.blockers.iter().map(|b| b.code().to_string()).collect() })
.map(|c| CharInfo {
guid: c.local_guid,
name: c.name.clone(),
class: c.class,
race: c.race,
level: c.level,
eligible: c.eligible(),
reasons: c.blockers.iter().map(|b| b.code().to_string()).collect(),
})
.collect())
}
fn is_portable(&self, local_id: &str, guid: u32) -> Result<bool> {
let id = local_id.to_string();
self.portable.call(move |s| s.is_portable_here(&id, guid)).map_err(|e| unavailable("portable engine", e))?.map_err(|e| unavailable("portable engine", e))
self.portable
.call(move |s| s.is_portable_here(&id, guid))
.map_err(|e| unavailable("portable engine", e))?
.map_err(|e| unavailable("portable engine", e))
}
fn export(&self, local_id: &str, _account_id: u32, guid: u32) -> Result<ClaimExport> {
let id = local_id.to_string();
let bundle = self.portable.call(move |s| s.export_for_claim(&id, guid)).map_err(|e| unavailable("portable engine", e))?.map_err(|e| unavailable("export", e))?;
Ok(ClaimExport { character_id: bundle.character_id.as_uuid(), payload: bundle.payload, sha256: bundle.content_hash, collections: bundle.collections })
let bundle = self
.portable
.call(move |s| s.export_for_claim(&id, guid))
.map_err(|e| unavailable("portable engine", e))?
.map_err(|e| unavailable("export", e))?;
Ok(ClaimExport {
character_id: bundle.character_id.as_uuid(),
payload: bundle.payload,
sha256: bundle.content_hash,
collections: bundle.collections,
})
}
fn check_transfer(&self, local_id: &str, character_id: Uuid, revision: u64) -> Result<PreflightVerdict> {
fn check_transfer(
&self,
local_id: &str,
character_id: Uuid,
revision: u64,
) -> Result<PreflightVerdict> {
let id = local_id.to_string();
let cid = crate::portable::CharacterId::from_uuid(character_id).map_err(|e| unavailable("character id", e))?;
self.portable.call(move |s| s.check_remote_transfer(&id, cid, revision)).map_err(|e| unavailable("portable engine", e))?.map_err(|e| unavailable("check transfer", e))
let cid = crate::portable::CharacterId::from_uuid(character_id)
.map_err(|e| unavailable("character id", e))?;
self.portable
.call(move |s| s.check_remote_transfer(&id, cid, revision))
.map_err(|e| unavailable("portable engine", e))?
.map_err(|e| unavailable("check transfer", e))
}
fn import_remote(&self, local_id: &str, params: RemoteImportParams) -> Result<RemoteImportOutcome> {
fn import_remote(
&self,
local_id: &str,
params: RemoteImportParams,
) -> Result<RemoteImportOutcome> {
let id = local_id.to_string();
self.portable.call(move |s| s.import_remote_character(&id, params)).map_err(|e| unavailable("portable engine", e))?.map_err(|e| unavailable("import remote", e))
self.portable
.call(move |s| s.import_remote_character(&id, params))
.map_err(|e| unavailable("portable engine", e))?
.map_err(|e| unavailable("import remote", e))
}
}