202 lines
9.5 KiB
Rust
202 lines
9.5 KiB
Rust
// file: crates/ksp-app-solprices-desk/tests/desktop_security.rs
|
|
// version: 8
|
|
|
|
//! Security, ownership and frontend instrumentation canaries for SOL Prices Desk.
|
|
|
|
fn app_root() -> std::path::PathBuf {
|
|
return std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
}
|
|
|
|
fn collect_files(directory: &std::path::Path, extension: &str, files: &mut std::vec::Vec<std::path::PathBuf>) {
|
|
let entries = std::fs::read_dir(directory);
|
|
let entries = match entries {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(_) => return,
|
|
};
|
|
for entry in entries {
|
|
let entry = match entry {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(_) => continue,
|
|
};
|
|
let path = entry.path();
|
|
if path.is_dir() {
|
|
collect_files(path.as_path(), extension, files);
|
|
continue;
|
|
}
|
|
if path.extension().and_then(std::ffi::OsStr::to_str) == std::option::Option::Some(extension) {
|
|
files.push(path);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn read_text(path: &std::path::Path) -> String {
|
|
let source = std::fs::read_to_string(path);
|
|
assert!(source.is_ok(), "unable to read {}", path.display());
|
|
return match source {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(_) => String::new(),
|
|
};
|
|
}
|
|
|
|
#[test]
|
|
fn pre_002_capability_surface_is_core_plus_tracing_only() {
|
|
let root = app_root();
|
|
let capability = read_text(root.join("capabilities/default.json").as_path());
|
|
assert!(capability.contains("\"core:default\""));
|
|
assert!(capability.contains("\"tracing:default\""));
|
|
for forbidden in ["dialog:", "fs:", "http:", "shell:"] {
|
|
assert!(!capability.contains(forbidden));
|
|
}
|
|
let manifest = read_text(root.join("Cargo.toml").as_path());
|
|
assert!(manifest.contains("tauri-plugin-tracing.workspace = true"));
|
|
assert!(!manifest.contains("tauri-plugin-dialog"));
|
|
assert!(manifest.contains("ksp-offchain-transport-lib"));
|
|
assert!(!manifest.contains("reqwest"));
|
|
}
|
|
|
|
#[test]
|
|
fn pre_002_frontend_has_no_network_persistence_or_native_dialog_surface() {
|
|
let root = app_root();
|
|
let mut typescript = std::vec::Vec::new();
|
|
collect_files(root.join("frontend/ts").as_path(), "ts", &mut typescript);
|
|
assert!(!typescript.is_empty());
|
|
for path in typescript {
|
|
let source = read_text(path.as_path());
|
|
for forbidden in ["fetch(", "XMLHttpRequest", "localStorage", "sessionStorage", "window.alert(", "window.confirm(", "window.prompt("] {
|
|
assert!(!source.contains(forbidden), "{} contains forbidden scaffold surface {forbidden}", path.display());
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pre_005_tauri_commands_remain_centralized_and_single_refresh_is_preserved() {
|
|
let root = app_root();
|
|
let mut rust_files = std::vec::Vec::new();
|
|
collect_files(root.join("src").as_path(), "rs", &mut rust_files);
|
|
let mut command_count = 0usize;
|
|
for path in rust_files {
|
|
let source = read_text(path.as_path());
|
|
let count = source.matches("#[tauri::command]").count();
|
|
if path.file_name().and_then(std::ffi::OsStr::to_str) == std::option::Option::Some("tauri.rs") {
|
|
command_count += count;
|
|
} else {
|
|
assert_eq!(count, 0, "{} declares a Tauri command outside tauri.rs", path.display());
|
|
}
|
|
if path.file_name().and_then(std::ffi::OsStr::to_str) != std::option::Option::Some("tauri.rs") {
|
|
assert!(!source.contains("tauri::generate_handler!"), "{} registers Tauri handlers outside tauri.rs", path.display());
|
|
}
|
|
}
|
|
assert!(command_count >= 5);
|
|
let tauri = read_text(root.join("src/tauri.rs").as_path());
|
|
assert!(tauri.contains("refresh_market_price"));
|
|
}
|
|
|
|
#[test]
|
|
fn pre_005_frontend_refresh_interactions_are_logged_without_business_values() {
|
|
let root = app_root();
|
|
let main = read_text(root.join("frontend/ts/main.ts").as_path());
|
|
let invoke = read_text(root.join("frontend/ts/invoke.ts").as_path());
|
|
assert!(main.contains(r#"button.addEventListener("click""#));
|
|
assert!(main.contains("SOL Prices Desk navigation control clicked"));
|
|
assert!(main.contains("frontendDebug"));
|
|
assert!(main.contains("SOL Prices Desk view activated"));
|
|
assert!(invoke.contains("Frontend IPC command requested"));
|
|
assert!(invoke.contains("Frontend IPC command completed"));
|
|
assert!(invoke.contains("frontendTrace"));
|
|
assert!(main.contains("SOL Prices Desk provider registry load started"));
|
|
assert!(main.contains("SOL Prices Desk market-price registry rows rendered"));
|
|
assert!(main.contains("SOL Prices Desk market-price refresh control clicked"));
|
|
assert!(main.contains("SOL Prices Desk market-price refresh row applied"));
|
|
assert!(main.contains("SOL Prices Desk market-price refresh failed"));
|
|
for forbidden in ["JSON.stringify(status)", "JSON.stringify(payload)", "apiKey", "authorization"] {
|
|
assert!(!main.contains(forbidden), "main frontend logging must not serialize business/secret values: {forbidden}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pre_006_selected_and_global_refresh_commands_are_centralized_and_bounded() {
|
|
let root = app_root();
|
|
let tauri = read_text(root.join("src/tauri.rs").as_path());
|
|
assert_eq!(tauri.matches("#[tauri::command]").count(), 7);
|
|
assert!(tauri.contains("refresh_market_price"));
|
|
assert!(tauri.contains("refresh_market_prices"));
|
|
assert!(tauri.contains("refresh_all_market_prices"));
|
|
let runtime = read_text(root.join("src/market_price_runtime.rs").as_path());
|
|
assert!(runtime.contains("ERROR_CODE_MARKET_PRICE_REFRESH_CONFLICT"));
|
|
assert!(runtime.contains("ERROR_CODE_MARKET_PRICE_SELECTION_INVALID"));
|
|
assert!(runtime.contains("refresh_many(provider_ids.as_slice()).await"));
|
|
assert!(runtime.contains("refresh_all().await"));
|
|
assert!(!runtime.contains("tokio::time::sleep"));
|
|
}
|
|
|
|
#[test]
|
|
fn pre_006_frontend_batch_controls_are_logged_without_price_or_provider_payloads() {
|
|
let root = app_root();
|
|
let main = read_text(root.join("frontend/ts/main.ts").as_path());
|
|
assert!(main.contains("SOL Prices Desk market-price selection control changed"));
|
|
assert!(main.contains("SOL Prices Desk selected market-price refresh control clicked"));
|
|
assert!(main.contains("SOL Prices Desk global market-price refresh control clicked"));
|
|
assert!(main.contains("requestedCount: result.requestedCount"));
|
|
assert!(main.contains("refreshedCount: result.refreshedCount"));
|
|
for forbidden in ["JSON.stringify(result)", "JSON.stringify(currentMarketPriceRows)", "price: provider.price", "apiKey", "authorization"] {
|
|
assert!(!main.contains(forbidden), "batch frontend logging must not serialize price/provider/secret values: {forbidden}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pre_007_all_frontend_controls_and_backend_command_failures_are_instrumented_safely() {
|
|
let root = app_root();
|
|
let main = read_text(root.join("frontend/ts/main.ts").as_path());
|
|
for required in [
|
|
"SOL Prices Desk navigation control clicked",
|
|
"SOL Prices Desk market-price selection control changed",
|
|
"SOL Prices Desk select-all providers control changed",
|
|
"SOL Prices Desk clear provider selection control clicked",
|
|
"SOL Prices Desk market-price refresh control clicked",
|
|
"SOL Prices Desk selected market-price refresh control clicked",
|
|
"SOL Prices Desk global market-price refresh control clicked",
|
|
] {
|
|
assert!(main.contains(required), "pre.007 frontend control instrumentation is missing {required}");
|
|
}
|
|
for forbidden in [
|
|
"JSON.stringify(currentMarketPriceRows)",
|
|
"JSON.stringify(result)",
|
|
"price: provider.price",
|
|
"providerTimestampUnixMillis:",
|
|
"receivedAtUnixMillis:",
|
|
"retryAtUnixMillis:",
|
|
"apiKey",
|
|
"authorization",
|
|
] {
|
|
assert!(!main.contains(forbidden), "pre.007 frontend instrumentation must not log business/secret values: {forbidden}");
|
|
}
|
|
let tauri = read_text(root.join("src/tauri.rs").as_path());
|
|
assert!(tauri.contains("fn project_command_error"));
|
|
assert!(tauri.contains("error_domain = error.code().domain()"));
|
|
assert!(tauri.contains("error_code = error.code().code()"));
|
|
assert!(tauri.contains("SOL Prices Desk Tauri command failed"));
|
|
assert!(!tauri.contains("error_message = error"));
|
|
}
|
|
|
|
#[test]
|
|
fn pre_007_remains_manual_without_browser_persistence_or_price_scheduler() {
|
|
let root = app_root();
|
|
let main = read_text(root.join("frontend/ts/main.ts").as_path());
|
|
for forbidden in ["setInterval(", "setTimeout(", "requestAnimationFrame(", "localStorage", "sessionStorage", "fetch(", "XMLHttpRequest"] {
|
|
assert!(!main.contains(forbidden), "pre.007 must remain manual and backend-owned: {forbidden}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pre_009_hardening_bounds_hostile_batches_and_preserves_manual_backend_owned_refresh() {
|
|
let root = app_root();
|
|
let runtime = read_text(root.join("src/market_price_runtime.rs").as_path());
|
|
assert!(runtime.contains("const MARKET_PRICE_REFRESH_MAX_PROVIDER_IDS: usize = 64;"));
|
|
assert!(runtime.contains("provider_ids.is_empty() || provider_ids.len() > MARKET_PRICE_REFRESH_MAX_PROVIDER_IDS"));
|
|
assert!(runtime.contains("ERROR_CODE_MARKET_PRICE_SELECTION_INVALID"));
|
|
let main = read_text(root.join("frontend/ts/main.ts").as_path());
|
|
for forbidden in ["setInterval(", "setTimeout(", "requestAnimationFrame(", "fetch(", "XMLHttpRequest", "localStorage", "sessionStorage"] {
|
|
assert!(!main.contains(forbidden), "release hardening must preserve manual backend-owned refresh: {forbidden}");
|
|
}
|
|
}
|